7 local function replace_latex(data) |
7 local function replace_latex(data) |
8 module:log("debug", "Replacing latex..."); |
8 module:log("debug", "Replacing latex..."); |
9 local origin, stanza = data.origin, data.stanza; |
9 local origin, stanza = data.origin, data.stanza; |
10 local body = stanza:child_with_name("body"); |
10 local body = stanza:child_with_name("body"); |
11 if not body then return; end |
11 if not body then return; end |
12 |
12 |
13 body = body:get_text(); |
13 body = body:get_text(); |
14 if not body:match("%$%$") then |
14 if not body:match("%$%$") then |
15 return; |
15 return; |
16 end |
16 end |
17 |
17 |
18 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }) |
18 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }) |
19 :tag("body", { xmlns = xmlns_xhtml }); |
19 :tag("body", { xmlns = xmlns_xhtml }); |
20 |
20 |
21 local in_latex, last_char; |
21 local in_latex, last_char; |
22 for snippet, up_to in body:gmatch("(.-)%$%$()") do |
22 for snippet, up_to in body:gmatch("(.-)%$%$()") do |
23 last_char = up_to; |
23 last_char = up_to; |
24 if in_latex then |
24 if in_latex then |
25 -- Render latex and add image, next snippet is text |
25 -- Render latex and add image, next snippet is text |
27 html:tag("img", { src = "http://www.mathtran.org/cgi-bin/mathtran?D=2;tex="..urlencode(snippet), alt = snippet }):up(); |
27 html:tag("img", { src = "http://www.mathtran.org/cgi-bin/mathtran?D=2;tex="..urlencode(snippet), alt = snippet }):up(); |
28 else |
28 else |
29 -- Add text to HTML, next snippet is latex |
29 -- Add text to HTML, next snippet is latex |
30 in_latex = true; |
30 in_latex = true; |
31 html:tag("span"):text(snippet):up(); |
31 html:tag("span"):text(snippet):up(); |
32 |
32 |
33 end |
33 end |
34 end |
34 end |
35 if last_char < #body then |
35 if last_char < #body then |
36 html:tag("span"):text(body:sub(last_char, #body)):up(); |
36 html:tag("span"):text(body:sub(last_char, #body)):up(); |
37 end |
37 end |
38 |
38 |
39 for n, tag in ipairs(stanza.tags) do |
39 for n, tag in ipairs(stanza.tags) do |
40 module:log("debug", "Tag: %s|%s", tag.attr.xmlns or "", tag.name or ""); |
40 module:log("debug", "Tag: %s|%s", tag.attr.xmlns or "", tag.name or ""); |
41 if tag.name == "html" and tag.attr.xmlns == xmlns_xhtmlim then |
41 if tag.name == "html" and tag.attr.xmlns == xmlns_xhtmlim then |
42 stanza.tags[n] = html; |
42 stanza.tags[n] = html; |
43 for n, child in ipairs(stanza) do |
43 for n, child in ipairs(stanza) do |