mod_latex/mod_latex.lua
changeset 1343 7dbde05b48a9
parent 126 e190c1643a8f
child 1432 ee2302b78c74
equal deleted inserted replaced
1342:0ae065453dc9 1343:7dbde05b48a9
     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
    46 				end
    46 				end
    47 			end
    47 			end
    48 			return;
    48 			return;
    49 		end
    49 		end
    50 	end
    50 	end
    51 	
    51 
    52 	stanza[#stanza+1] = html;
    52 	stanza[#stanza+1] = html;
    53 	stanza.tags[#stanza.tags+1] = html;
    53 	stanza.tags[#stanza.tags+1] = html;
    54 end
    54 end
    55 
    55 
    56 module:hook("message/bare", replace_latex, 30);
    56 module:hook("message/bare", replace_latex, 30);