Software /
code /
prosody-modules
Comparison
mod_latex/mod_latex.lua @ 126:e190c1643a8f
mod_latex: New (well, kind of) module for rendering LaTeX in messages and replacing with a rendered embedded image
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 27 Jan 2010 20:50:52 +0000 |
child | 1343:7dbde05b48a9 |
comparison
equal
deleted
inserted
replaced
125:8111c8a9e1ad | 126:e190c1643a8f |
---|---|
1 local st = require "stanza"; | |
2 local urlencode = require "net.http".urlencode; | |
3 | |
4 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im"; | |
5 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; | |
6 | |
7 local function replace_latex(data) | |
8 module:log("debug", "Replacing latex..."); | |
9 local origin, stanza = data.origin, data.stanza; | |
10 local body = stanza:child_with_name("body"); | |
11 if not body then return; end | |
12 | |
13 body = body:get_text(); | |
14 if not body:match("%$%$") then | |
15 return; | |
16 end | |
17 | |
18 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }) | |
19 :tag("body", { xmlns = xmlns_xhtml }); | |
20 | |
21 local in_latex, last_char; | |
22 for snippet, up_to in body:gmatch("(.-)%$%$()") do | |
23 last_char = up_to; | |
24 if in_latex then | |
25 -- Render latex and add image, next snippet is text | |
26 in_latex = nil; | |
27 html:tag("img", { src = "http://www.mathtran.org/cgi-bin/mathtran?D=2;tex="..urlencode(snippet), alt = snippet }):up(); | |
28 else | |
29 -- Add text to HTML, next snippet is latex | |
30 in_latex = true; | |
31 html:tag("span"):text(snippet):up(); | |
32 | |
33 end | |
34 end | |
35 if last_char < #body then | |
36 html:tag("span"):text(body:sub(last_char, #body)):up(); | |
37 end | |
38 | |
39 for n, tag in ipairs(stanza.tags) do | |
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 | |
42 stanza.tags[n] = html; | |
43 for n, child in ipairs(stanza) do | |
44 if child == tag then | |
45 stanza[n] = html; | |
46 end | |
47 end | |
48 return; | |
49 end | |
50 end | |
51 | |
52 stanza[#stanza+1] = html; | |
53 stanza.tags[#stanza.tags+1] = html; | |
54 end | |
55 | |
56 module:hook("message/bare", replace_latex, 30); | |
57 module:hook("message/full", replace_latex, 30); |