diff options
Diffstat (limited to 'tex/context/base/x-mathml.lua')
-rw-r--r-- | tex/context/base/x-mathml.lua | 131 |
1 files changed, 74 insertions, 57 deletions
diff --git a/tex/context/base/x-mathml.lua b/tex/context/base/x-mathml.lua index 813f6d78a..5a60fab52 100644 --- a/tex/context/base/x-mathml.lua +++ b/tex/context/base/x-mathml.lua @@ -9,22 +9,50 @@ if not modules then modules = { } end modules ['x-mathml'] = { -- This needs an upgrade to the latest greatest mechanisms. local type, next = type, next -local format, lower, find, gsub = string.format, string.lower, string.find, string.gsub +local formatters, lower, find, gsub, match = string.formatters, string.lower, string.find, string.gsub, string.match local strip = string.strip local xmlsprint, xmlcprint, xmltext, xmlcontent = xml.sprint, xml.cprint, xml.text, xml.content +local lxmlcollected = lxml.collected local getid = lxml.getid local utfchar, utfcharacters, utfvalues = utf.char, utf.characters, utf.values -local lpegmatch = lpeg.match +local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns local mathml = { } moduledata.mathml = mathml lxml.mathml = mathml -- for the moment -local context = context +local context = context -local ctx_enabledelimiter = context.enabledelimiter -local ctx_disabledelimiter = context.disabledelimiter -local ctx_xmlflush = context.xmlflush -- better xmlsprint +local ctx_enabledelimiter = context.enabledelimiter +local ctx_disabledelimiter = context.disabledelimiter +local ctx_xmlflush = context.xmlflush -- better xmlsprint + +local ctx_halign = context.halign +local ctx_noalign = context.noalign +local ctx_bgroup = context.bgroup +local ctx_egroup = context.egroup +local ctx_crcr = context.crcr + +local ctx_bTABLE = context.bTABLE +local ctx_eTABLE = context.eTABLE +local ctx_bTR = context.bTR +local ctx_eTR = context.eTR +local ctx_bTD = context.bTD +local ctx_eTD = context.eTD + +local ctx_mn = context.mn +local ctx_mi = context.mi +local ctx_mo = context.mo +local ctx_startimath = context.startimath +local ctx_ignorespaces = context.ignorespaces +local ctx_removeunwantedspaces = context.removeunwantedspaces +local ctx_stopimath = context.stopimath + +local ctx_mmlapplycsymbol = context.mmlapplycsymbol + +local ctx_mathopnolimits = context.mathopnolimits +local ctx_left = context.left +local ctx_right = context.right -- an alternative is to remap to private codes, where we can have -- different properties .. to be done; this will move and become @@ -68,6 +96,7 @@ local o_replacements = { -- in main table ["{"] = "\\mmlleftdelimiter \\lbrace", ["}"] = "\\mmlrightdelimiter\\rbrace", ["|"] = "\\mmlleftorrightdelimiter\\vert", + -- ["."] = "\\mmlleftorrightdelimiter.", ["/"] = "\\mmlleftorrightdelimiter\\solidus", [doublebar] = "\\mmlleftorrightdelimiter\\Vert", ["("] = "\\mmlleftdelimiter(", @@ -480,7 +509,7 @@ function mathml.mn(id,pattern) local rep = gsub(str,"&.-;","") local rep = gsub(rep,"(%s+)",utfchar(0x205F)) -- medspace e.g.: twenty one (nbsp is not seen) local rep = gsub(rep,".",n_replacements) - context.mn(rep) + ctx_mn(rep) end function mathml.mo(id) @@ -506,7 +535,7 @@ function mathml.mi(id) rep = gsub(str,".",i_replacements) end context(rep) - -- context.mi(rep) + -- ctx_mi(rep) else ctx_xmlflush(id) -- xmlsprint or so end @@ -567,25 +596,6 @@ function mathml.mfenced(id) -- multiple separators ctx_disabledelimiter() end ---~ local function flush(e,tag,toggle) ---~ if toggle then ---~ context("^{") ---~ else ---~ context("_{") ---~ end ---~ if tag == "none" then ---~ context("{}") ---~ else ---~ xmlsprint(e.dt) ---~ end ---~ if not toggle then ---~ context("}") ---~ else ---~ context("}{}") ---~ end ---~ return not toggle ---~ end - local function flush(e,tag,toggle) if tag == "none" then -- if not toggle then @@ -605,7 +615,7 @@ end function mathml.mmultiscripts(id) local done, toggle = false, false - for e in lxml.collected(id,"/*") do + for e in lxmlcollected(id,"/*") do local tag = e.tg if tag == "mprescripts" then context("{}") @@ -615,7 +625,7 @@ function mathml.mmultiscripts(id) end end local done, toggle = false, false - for e in lxml.collected(id,"/*") do + for e in lxmlcollected(id,"/*") do local tag = e.tg if tag == "mprescripts" then break @@ -676,20 +686,20 @@ function mathml.mcolumn(root) -- m[#m+1] = { tag, e } end end - for e in lxml.collected(root,"/*") do + for e in lxmlcollected(root,"/*") do local m = { } matrix[#matrix+1] = m if e.tg == "mrow" then -- only one level - for e in lxml.collected(e,"/*") do + for e in lxmlcollected(e,"/*") do collect(m,e) end else collect(m,e) end end - context.halign() - context.bgroup() + ctx_halign() + ctx_bgroup() context([[\hss\startimath\alignmark\stopimath\aligntab\startimath\alignmark\stopimath\cr]]) for i=1,#matrix do local m = matrix[i] @@ -701,7 +711,7 @@ function mathml.mcolumn(root) end end if mline then - context.noalign([[\obeydepth\nointerlineskip]]) + ctx_noalign([[\obeydepth\nointerlineskip]]) end for j=1,#m do local mm = m[j] @@ -744,9 +754,9 @@ function mathml.mcolumn(root) local nchr = n_replacements[chr] context(nchr or chr) end - context.crcr() + ctx_crcr() end - context.egroup() + ctx_egroup() end local spacesplitter = lpeg.tsplitat(" ") @@ -764,42 +774,47 @@ function mathml.mtable(root) local framespacing = at.framespacing or "0pt" local framespacing = at.framespacing or "-\\ruledlinewidth" -- make this an option - context.bTABLE { frame = frametypes[frame or "none"] or "off", offset = framespacing, background = "" } -- todo: use xtables and definextable - for e in lxml.collected(root,"/(mml:mtr|mml:mlabeledtr)") do - context.bTR() + ctx_bTABLE { frame = frametypes[frame or "none"] or "off", offset = framespacing, background = "" } -- todo: use xtables and definextable + for e in lxmlcollected(root,"/(mml:mtr|mml:mlabeledtr)") do + ctx_bTR() local at = e.at local col = 0 local rfr = at.frame or (frames and frames [#frames]) local rra = at.rowalign or (rowaligns and rowaligns [#rowaligns]) local rca = at.columnalign or (columnaligns and columnaligns[#columnaligns]) local ignorelabel = e.tg == "mlabeledtr" - for e in lxml.collected(e,"/mml:mtd") do -- nested we can use xml.collected + for e in lxmlcollected(e,"/mml:mtd") do -- nested we can use xml.collected col = col + 1 if ignorelabel and col == 1 then -- get rid of label, should happen at the document level else local at = e.at - local rowspan, columnspan = at.rowspan or 1, at.columnspan or 1 + local rowspan = at.rowspan or 1 + local columnspan = at.columnspan or 1 local cra = rowalignments [at.rowalign or (rowaligns and rowaligns [col]) or rra or "center"] or "lohi" local cca = columnalignments[at.columnalign or (columnaligns and columnaligns[col]) or rca or "center"] or "middle" local cfr = frametypes [at.frame or (frames and frames [col]) or rfr or "none" ] or "off" - context.bTD { align = format("{%s,%s}",cra,cca), frame = cfr, nx = columnspan, ny = rowspan } - context.startimath() - context.ignorespaces() - xmlcprint(e) - context.stopimath() - context.removeunwantedspaces() - context.eTD() + ctx_bTD { align = formatters["{%s,%s}"](cra,cca), frame = cfr, nx = columnspan, ny = rowspan } + if xml.empty(e,".") then + -- nothing, else hsize max + else + ctx_startimath() + -- ctx_ignorespaces() + xmlcprint(e) + -- ctx_removeunwantedspaces() + ctx_stopimath() + end + ctx_eTD() end end -- if e.tg == "mlabeledtr" then - -- context.bTD() + -- ctx_bTD() -- xmlcprint(xml.first(e,"/!mml:mtd")) - -- context.eTD() + -- ctx_eTD() -- end - context.eTR() + ctx_eTR() end - context.eTABLE() + ctx_eTABLE() end function mathml.csymbol(root) @@ -810,14 +825,16 @@ function mathml.csymbol(root) local full = hash.original or "" local base = hash.path or "" local text = strip(xmltext(root) or "") - context.mmlapplycsymbol(full,base,encoding,text) + ctx_mmlapplycsymbol(full,base,encoding,text) end +local p = lpeg.Cs(((1-lpegpatterns.whitespace)^1 / "mml:enclose:%0" + (lpegpatterns.whitespace^1)/",")^1) + function mathml.menclosepattern(root) root = getid(root) local a = root.at.notation if a and a ~= "" then - context("mml:enclose:",(gsub(a," +",",mml:enclose:"))) + context(lpegmatch(p,a)) end end @@ -828,8 +845,8 @@ end function mathml.cpolar_a(root) root = getid(root) local dt = root.dt - context.mathopnolimits("Polar") - context.left(false,"(") + ctx_mathopnolimits("Polar") + ctx_left(false,"(") for k=1,#dt do local dk = dt[k] if xml.is_element(dk,"sep") then @@ -838,7 +855,7 @@ function mathml.cpolar_a(root) xmlsprint(dk) end end - context.right(false,")") + ctx_right(false,")") end -- crap .. maybe in char-def a mathml overload |