diff options
author | Taco Hoekwater <taco@elvenkind.com> | 2010-05-24 14:05:02 +0000 |
---|---|---|
committer | Taco Hoekwater <taco@elvenkind.com> | 2010-05-24 14:05:02 +0000 |
commit | 57ea7dad48fbf2541c04e434c31bde655ada3ac4 (patch) | |
tree | 1f8b43bc7cb92939271e1f5bec610710be69097f /Master/texmf-dist/tex/context/base/x-mathml.lua | |
parent | 6ee41e1f1822657f7f23231ec56c0272de3855e3 (diff) |
here is context 2010.05.24 13:05
git-svn-id: svn://tug.org/texlive/trunk@18445 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/x-mathml.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/x-mathml.lua | 331 |
1 files changed, 165 insertions, 166 deletions
diff --git a/Master/texmf-dist/tex/context/base/x-mathml.lua b/Master/texmf-dist/tex/context/base/x-mathml.lua index aa2e4e716f3..2ee3cd09093 100644 --- a/Master/texmf-dist/tex/context/base/x-mathml.lua +++ b/Master/texmf-dist/tex/context/base/x-mathml.lua @@ -1,26 +1,23 @@ if not modules then modules = { } end modules ['x-mathml'] = { version = 1.001, - comment = "companion to x-mathml.tex", + comment = "companion to x-mathml.mkiv", author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } +local type, next = type, next local utf = unicode.utf8 +local texsprint, ctxcatcodes = tex.sprint, tex.ctxcatcodes +local format, lower, find, gsub = string.format, string.lower, string.find, string.gsub +local utfchar, utffind, utfgmatch, utfgsub = utf.char, utf.find, utf.gmatch, utf.gsub +local xmlsprint, xmlcprint, xmltext, xmlcontent = xml.sprint, xml.cprint, xml.text, xml.content +local lxmltext, get_id = lxml.text, lxml.get_id +local utfcharacters, utfvalues = string.utfcharacters, string.utfvalues +local lpegmatch = lpeg.match -lxml = lxml or { } lxml.mml = lxml.mml or { } -local texsprint = tex.sprint -local format = string.format -local lower = string.lower -local utfchar = utf.char -local utffind = utf.find -local xmlsprint = xml.sprint -local xmlcprint = xml.cprint - -local utfcharacters, utfvalues = string.utfcharacters, string.utfvalues - -- an alternative is to remap to private codes, where we can have -- different properties .. to be done; this will move and become -- generic @@ -387,30 +384,29 @@ local csymbols = { }, } -function xml.functions.remapmmlcsymbol(r,d,k) - local dk = d[k] - local at = dk.at +function xml.functions.remapmmlcsymbol(e) + local at = e.at local cd = at.cd if cd then cd = csymbols[cd] if cd then - local tx = dk.dt[1] + local tx = e.dt[1] if tx and tx ~= "" then local tg = cd[tx] if tg then at.cd = nil at.cdbase = nil - dk.dt = { } + e.dt = { } if type(tg) == "table" then - for k, v in pairs(tg) do + for k, v in next, tg do if k == "tag" then - dk.tg = v + e.tg = v else at[k] = v end end else - dk.tg = tg + e.tg = tg end end end @@ -418,170 +414,167 @@ function xml.functions.remapmmlcsymbol(r,d,k) end end -function xml.functions.remapmmlbind(r,d,k) - d[k].tg = "apply" +function xml.functions.remapmmlbind(e) + e.tg = "apply" end -function xml.functions.remapopenmath(r,d,k) - local dk = d[k] - local tg = dk.tg +function xml.functions.remapopenmath(e) + local tg = e.tg if tg == "OMOBJ" then - dk.tg = "math" + e.tg = "math" elseif tg == "OMA" then - dk.tg = "apply" + e.tg = "apply" elseif tg == "OMB" then - dk.tg = "apply" + e.tg = "apply" elseif tg == "OMS" then - -- xml.functions.remapmmlcsymbol(r,d,k) - local at = dk.at - dk.tg = "csymbol" - dk.dt = { at.name or "unknown" } + local at = e.at + e.tg = "csymbol" + e.dt = { at.name or "unknown" } at.name = nil elseif tg == "OMV" then - local at = dk.at - dk.tg = "ci" - dk.dt = { at.name or "unknown" } + local at = e.at + e.tg = "ci" + e.dt = { at.name or "unknown" } at.name = nil elseif tg == "OMI" then - dk.tg = "ci" + e.tg = "ci" end - dk.rn = "mml" + e.rn = "mml" end function lxml.mml.checked_operator(str) - texsprint(tex.ctxcatcodes,(utf.gsub(str,".",o_replacements))) + texsprint(ctxcatcodes,(utfgsub(str,".",o_replacements))) end function lxml.mml.stripped(str) - tex.sprint(tex.ctxcatcodes,str:strip()) + tex.sprint(ctxcatcodes,str:strip()) end -function lxml.mml.mn(id,pattern) - -- maybe at some point we need to interpret the number, but - -- currently we assume an upright font - local str = xml.content(lxml.id(id),pattern) or "" - -- str = str:gsub("^%s*(.-)%s*$","%1") - str = str:gsub("(%s+)",utfchar(0x205F)) -- medspace e.g.: twenty one (nbsp is not seen) - texsprint(tex.ctxcatcodes,(str:gsub(".",n_replacements))) +function table.keys_as_string(t) + local k = { } + for k,_ in next, t do + k[#k+1] = k + end + return concat(k,"") end +--~ local leftdelimiters = "[" .. table.keys_as_string(l_replacements) .. "]" +--~ local rightdelimiters = "[" .. table.keys_as_string(r_replacements) .. "]" + function characters.remapentity(chr,slot) texsprint(format("{\\catcode%s=13\\xdef%s{\\string%s}}",slot,utfchar(slot),chr)) end -function lxml.mml.mo(id,pattern) - local str = xml.content(lxml.id(id),pattern) or "" - texsprint(tex.ctxcatcodes,(utf.gsub(str,".",o_replacements))) +function lxml.mml.mn(id,pattern) + -- maybe at some point we need to interpret the number, but + -- currently we assume an upright font + local str = xmlcontent(get_id(id)) or "" + str = gsub(str,"(%s+)",utfchar(0x205F)) -- medspace e.g.: twenty one (nbsp is not seen) + texsprint(ctxcatcodes,(gsub(str,".",n_replacements))) +end + +function lxml.mml.mo(id) + local str = xmlcontent(get_id(id)) or "" + texsprint(ctxcatcodes,(utfgsub(str,".",o_replacements))) end -function lxml.mml.mi(id,pattern) - local str = xml.content(lxml.id(id),pattern) or "" - -- str = str:gsub("^%s*(.-)%s*$","%1") +function lxml.mml.mi(id) + local str = xmlcontent(get_id(id)) or "" + -- str = gsub(str,"^%s*(.-)%s*$","%1") local rep = i_replacements[str] if rep then - texsprint(tex.ctxcatcodes,rep) + texsprint(ctxcatcodes,rep) else - texsprint(tex.ctxcatcodes,(str:gsub(".",i_replacements))) + texsprint(ctxcatcodes,(gsub(str,".",i_replacements))) end end -function table.keys_as_string(t) - local k = { } - for k,_ in pairs(t) do - k[#k+1] = k - end - return concat(k,"") -end - ---~ local leftdelimiters = "[" .. table.keys_as_string(l_replacements) .. "]" ---~ local rightdelimiters = "[" .. table.keys_as_string(r_replacements) .. "]" - -function lxml.mml.mfenced(id,pattern) -- multiple separators - id = lxml.id(id) +function lxml.mml.mfenced(id) -- multiple separators + id = get_id(id) local left, right, separators = id.at.open or "(", id.at.close or ")", id.at.separators or "," local l, r = l_replacements[left], r_replacements[right] - texsprint(tex.ctxcatcodes,"\\enabledelimiter") + texsprint(ctxcatcodes,"\\enabledelimiter") if l then - texsprint(tex.ctxcatcodes,l_replacements[left] or o_replacements[left] or "") + texsprint(ctxcatcodes,l_replacements[left] or o_replacements[left] or "") else - texsprint(tex.ctxcatcodes,o_replacements["@l"]) - texsprint(tex.ctxcatcodes,left) + texsprint(ctxcatcodes,o_replacements["@l"]) + texsprint(ctxcatcodes,left) end - texsprint(tex.ctxcatcodes,"\\disabledelimiter") - local n = xml.count(id,pattern) - if n == 0 then - -- skip - elseif n == 1 then - lxml.all(id,pattern) - else - local t = { } - for s in utf.gmatch(separators,"[^%s]") do - t[#t+1] = s - end - for i=1,n do - lxml.idx(id,pattern,i) -- kind of slow, some day ... - if i < n then - local m = t[i] or t[#t] or "" - if m == "|" then - m = "\\enabledelimiter\\middle|\\relax\\disabledelimiter" - elseif m == doublebar then - m = "\\enabledelimiter\\middle|\\relax\\disabledelimiter" - elseif m == "{" then - m = "\\{" - elseif m == "}" then - m = "\\}" + texsprint(ctxcatcodes,"\\disabledelimiter") + local collected = lxml.filter(id,"/*") -- check the * + if collected then + local n = #collected + if n == 0 then + -- skip + elseif n == 1 then + xmlsprint(collected[1]) -- to be checked +--~ lxml.all(id,"/*") + else + local t = { } + for s in utfgmatch(separators,"[^%s]") do + t[#t+1] = s + end + for i=1,n do + xmlsprint(collected[i]) -- to be checked + if i < n then + local m = t[i] or t[#t] or "" + if m == "|" then + m = "\\enabledelimiter\\middle|\\relax\\disabledelimiter" + elseif m == doublebar then + m = "\\enabledelimiter\\middle|\\relax\\disabledelimiter" + elseif m == "{" then + m = "\\{" + elseif m == "}" then + m = "\\}" + end + texsprint(ctxcatcodes,m) end - texsprint(tex.ctxcatcodes,m) end end end - texsprint(tex.ctxcatcodes,"\\enabledelimiter") + texsprint(ctxcatcodes,"\\enabledelimiter") if r then - texsprint(tex.ctxcatcodes,r_replacements[right] or o_replacements[right] or "") + texsprint(ctxcatcodes,r_replacements[right] or o_replacements[right] or "") else - texsprint(tex.ctxcatcodes,right) - texsprint(tex.ctxcatcodes,o_replacements["@r"]) + texsprint(ctxcatcodes,right) + texsprint(ctxcatcodes,o_replacements["@r"]) end - texsprint(tex.ctxcatcodes,"\\disabledelimiter") + texsprint(ctxcatcodes,"\\disabledelimiter") end local function flush(e,tag,toggle) - -- texsprint(tex.ctxcatcodes,(toggle and "^{") or "_{") + -- texsprint(ctxcatcodes,(toggle and "^{") or "_{") if toggle then - texsprint(tex.ctxcatcodes,"^{") + texsprint(ctxcatcodes,"^{") else - texsprint(tex.ctxcatcodes,"_{") + texsprint(ctxcatcodes,"_{") end if tag == "none" then - texsprint(tex.ctxcatcodes,"{}") + texsprint(ctxcatcodes,"{}") else xmlsprint(e.dt) end if not toggle then - texsprint(tex.ctxcatcodes,"}") + texsprint(ctxcatcodes,"}") else - texsprint(tex.ctxcatcodes,"}{}") + texsprint(ctxcatcodes,"}{}") end return not toggle end function lxml.mml.mmultiscripts(id) local done, toggle = false, false - id = lxml.id(id) - -- for i=1,#id.dt do local e = id.dt[i] if type(e) == table then ... - for r, d, k in xml.elements(id,"/*") do - local e = d[k] + for e in lxml.collected(id,"/*") do local tag = e.tg if tag == "mprescripts" then - texsprint(tex.ctxcatcodes,"{}") + texsprint(ctxcatcodes,"{}") done = true elseif done then toggle = flush(e,tag,toggle) end end local done, toggle = false, false - for r, d, k in xml.elements(id,"/*") do - local e = d[k] + for e in lxml.collected(id,"/*") do local tag = e.tg if tag == "mprescripts" then break @@ -617,12 +610,12 @@ local frametypes = { -- crazy element ... should be a proper structure instead of such a mess function lxml.mml.mcolumn(root) - root = lxml.id(root) + root = get_id(root) local matrix, numbers = { }, 0 - local function collect(m,dk) - local tag = dk.tg + local function collect(m,e) + local tag = e.tg if tag == "mi" or tag == "mn" or tag == "mo" or tag == "mtext" then - local str = xml.content(dk) + local str = xmltext(e) for s in utfcharacters(str) do -- utf.gmatch(str,".") btw, the gmatch was bugged m[#m+1] = { tag, s } end @@ -633,28 +626,27 @@ function lxml.mml.mcolumn(root) end end elseif tag == "mspace" or tag == "mline" then - local str = dk.at.spacing or "" + local str = e.at.spacing or "" for s in utfcharacters(str) do -- utf.gmatch(str,".") btw, the gmatch was bugged m[#m+1] = { tag, s } end elseif tag == "mline" then - m[#m+1] = { tag, dk } + m[#m+1] = { tag, e } end end - for r, d, k in xml.elements(root,"/*") do + for e in lxml.collected(root,"/*") do local m = { } matrix[#matrix+1] = m - local dk = d[k] - if dk.tg == "mrow" then + if e.tg == "mrow" then -- only one level - for r, d, k in xml.elements(dk,"/*") do - collect(m,d[k]) + for e in lxml.collected(e,"/*") do + collect(m,e) end else - collect(m,dk) + collect(m,e) end end - tex.sprint(tex.ctxcatcodes,"\\halign\\bgroup\\hss$#$&$#$\\cr") + tex.sprint(ctxcatcodes,"\\halign\\bgroup\\hss$#$&$#$\\cr") for i=1,#matrix do local m = matrix[i] local mline = true @@ -665,7 +657,7 @@ function lxml.mml.mcolumn(root) end end if mline then - tex.sprint(tex.ctxcatcodes,"\\noalign{\\obeydepth\\nointerlineskip}") + tex.sprint(ctxcatcodes,"\\noalign{\\obeydepth\\nointerlineskip}") end for j=1,#m do local mm = m[j] @@ -679,9 +671,9 @@ function lxml.mml.mcolumn(root) --~ if type(mc) ~= "string" then --~ n, p = false, false --~ break ---~ elseif mc:find("^[%d ]$") then -- rangecheck is faster +--~ elseif find(mc,"^[%d ]$") then -- rangecheck is faster --~ -- digit ---~ elseif not mc:find("^[%.%,]$") then -- rangecheck is faster +--~ elseif not find(mc,"^[%.%,]$") then -- rangecheck is faster --~ -- punctuation --~ else --~ n = false @@ -701,85 +693,92 @@ function lxml.mml.mcolumn(root) chr = "\\mmlmcolumndigitspace" -- utfchar(0x2007) end if j == numbers + 1 then - tex.sprint(tex.ctxcatcodes,"&") + tex.sprint(ctxcatcodes,"&") end local nchr = n_replacements[chr] - tex.sprint(tex.ctxcatcodes,nchr or chr) + tex.sprint(ctxcatcodes,nchr or chr) end - tex.sprint(tex.ctxcatcodes,"\\crcr") + tex.sprint(ctxcatcodes,"\\crcr") end - tex.sprint(tex.ctxcatcodes,"\\egroup") + tex.sprint(ctxcatcodes,"\\egroup") end local spacesplitter = lpeg.Ct(lpeg.splitat(" ")) function lxml.mml.mtable(root) - root = lxml.id(root) - -- todo: align, rowspacing, columnspacing, rowlines, columnlines - + root = get_id(root) local at = root.at local rowalign = at.rowalign local columnalign = at.columnalign local frame = at.frame - local rowaligns = rowalign and spacesplitter:match(rowalign) - local columnaligns = columnalign and spacesplitter:match(columnalign) - local frames = frame and spacesplitter:match(frame) + local rowaligns = rowalign and lpegmatch(spacesplitter,rowalign) + local columnaligns = columnalign and lpegmatch(spacesplitter,columnalign) + local frames = frame and lpegmatch(spacesplitter,frame) local framespacing = at.framespacing or "0pt" local framespacing = at.framespacing or "-\\ruledlinewidth" -- make this an option ---~ function texsprint(a,b) print(b) end - - texsprint(tex.ctxcatcodes, format("\\bTABLE[frame=%s,offset=%s]",frametypes[frame or "none"] or "off",framespacing)) - for r, d, k in xml.elements(root,"/(mml:mtr|mml:mlabeledtr)") do - texsprint(tex.ctxcatcodes,"\\bTR") - local dk = d[k] - local at = dk.at + texsprint(ctxcatcodes, format("\\bTABLE[frame=%s,offset=%s]",frametypes[frame or "none"] or "off",framespacing)) +--~ context.bTABLE { frame = frametypes[frame or "none"] or "off", offset = framespacing } + for e in lxml.collected(root,"/(mml:mtr|mml:mlabeledtr)") do + texsprint(ctxcatcodes,"\\bTR") +--~ context.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 = dk.tg == "mlabeledtr" - for rr, dd, kk in xml.elements(dk,"/mml:mtd") do + local ignorelabel = e.tg == "mlabeledtr" + for e in lxml.collected(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 dk = dd[kk] - local at = dk.at + local at = e.at local rowspan, columnspan = at.rowspan or 1, 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" - texsprint(tex.ctxcatcodes,format("\\bTD[align={%s,%s},frame=%s,nx=%s,ny=%s]$\\ignorespaces",cra,cca,cfr,columnspan,rowspan)) - xmlcprint(dk) - texsprint(tex.ctxcatcodes,"\\removeunwantedspaces$\\eTD") -- $ + texsprint(ctxcatcodes,format("\\bTD[align={%s,%s},frame=%s,nx=%s,ny=%s]$\\ignorespaces",cra,cca,cfr,columnspan,rowspan)) +--~ texfprint("\\bTD[align={%s,%s},frame=%s,nx=%s,ny=%s]$\\ignorespaces",cra,cca,cfr,columnspan,rowspan) +--~ context.bTD { align = format("{%s,%s}",cra,cca), frame = cfr, nx = columnspan, ny = rowspan } +--~ context.bmath() +--~ context.ignorespaces() + xmlcprint(e) + texsprint(ctxcatcodes,"\\removeunwantedspaces$\\eTD") -- $ +--~ context.emath() +--~ context.removeunwantedspaces() +--~ context.eTD() end end ---~ if dk.tg == "mlabeledtr" then ---~ texsprint(tex.ctxcatcodes,"\\bTD") ---~ xmlcprint(xml.first(dk,"/!mml:mtd")) ---~ texsprint(tex.ctxcatcodes,"\\eTD") +--~ if e.tg == "mlabeledtr" then +--~ texsprint(ctxcatcodes,"\\bTD") +--~ xmlcprint(xml.first(e,"/!mml:mtd")) +--~ texsprint(ctxcatcodes,"\\eTD") --~ end - texsprint(tex.ctxcatcodes,"\\eTR") + texsprint(ctxcatcodes,"\\eTR") +--~ context.eTR() end - texsprint(tex.ctxcatcodes, "\\eTABLE") + texsprint(ctxcatcodes, "\\eTABLE") +--~ context.eTABLE() end function lxml.mml.csymbol(root) - root = lxml.id(root) - local encoding = root.at.encoding or "" - local hash = url.hashed(lower(root.at.definitionUrl or "")) + root = get_id(root) + local at = root.at + local encoding = at.encoding or "" + local hash = url.hashed(lower(at.definitionUrl or "")) local full = hash.original or "" local base = hash.path or "" - local text = string.strip(xml.content(root) or "") - texsprint(tex.ctxcatcodes,format("\\mmlapplycsymbol{%s}{%s}{%s}{%s}",full,base,encoding,text)) + local text = string.strip(lxmltext(root)) +--~ texsprint(ctxcatcodes,format("\\mmlapplycsymbol{%s}{%s}{%s}{%s}",full,base,encoding,text)) + texsprint(ctxcatcodes,"\\mmlapplycsymbol{",full,"}{",base,"}{",encoding,"}{",text,"}") end function lxml.mml.menclosepattern(root) - root = lxml.id(root) + root = get_id(root) local a = root.at.notation if a and a ~= "" then - texsprint("mml:enclose:"..a:gsub(" +",",mml:enclose:")) + texsprint("mml:enclose:",gsub(a," +",",mml:enclose:")) end end |