diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/lxml-mis.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/lxml-mis.lua | 57 |
1 files changed, 25 insertions, 32 deletions
diff --git a/Master/texmf-dist/tex/context/base/lxml-mis.lua b/Master/texmf-dist/tex/context/base/lxml-mis.lua index a117b1af92a..9fd4270af4a 100644 --- a/Master/texmf-dist/tex/context/base/lxml-mis.lua +++ b/Master/texmf-dist/tex/context/base/lxml-mis.lua @@ -8,17 +8,18 @@ if not modules then modules = { } end modules ['lxml-mis'] = { local concat = table.concat local type, next, tonumber, tostring, setmetatable, loadstring = type, next, tonumber, tostring, setmetatable, loadstring -local format, gsub = string.format, string.gsub +local format, gsub, match = string.format, string.gsub, string.match +local lpegmatch = lpeg.match --[[ldx-- -<p>The following helper functions best belong to the <t>lmxl-ini</t> +<p>The following helper functions best belong to the <t>lxml-ini</t> module. Some are here because we need then in the <t>mk</t> document and other manuals, others came up when playing with this module. Since this module is also used in <l n='mtxrun'/> we've put them here instead of loading mode modules there then needed.</p> --ldx]]-- -function xml.gsub(t,old,new) +local function xmlgsub(t,old,new) -- will be replaced local dt = t.dt if dt then for k=1,#dt do @@ -26,28 +27,26 @@ function xml.gsub(t,old,new) if type(v) == "string" then dt[k] = gsub(v,old,new) else - xml.gsub(v,old,new) + xmlgsub(v,old,new) end end end end +--~ xml.gsub = xmlgsub + function xml.strip_leading_spaces(dk,d,k) -- cosmetic, for manual - if d and k and d[k-1] and type(d[k-1]) == "string" then - local s = d[k-1]:match("\n(%s+)") - xml.gsub(dk,"\n"..string.rep(" ",#s),"\n") + if d and k then + local dkm = d[k-1] + if dkm and type(dkm) == "string" then + local s = match(dkm,"\n(%s+)") + xmlgsub(dk,"\n"..rep(" ",#s),"\n") + end end end -function xml.serialize_path(root,lpath,handle) - local dk, r, d, k = xml.first(root,lpath) - dk = xml.copy(dk) - xml.strip_leading_spaces(dk,d,k) - xml.serialize(dk,handle) -end - --~ xml.escapes = { ['&'] = '&', ['<'] = '<', ['>'] = '>', ['"'] = '"' } ---~ xml.unescapes = { } for k,v in pairs(xml.escapes) do xml.unescapes[v] = k end +--~ xml.unescapes = { } for k,v in next, xml.escapes do xml.unescapes[v] = k end --~ function xml.escaped (str) return (gsub(str,"(.)" , xml.escapes )) end --~ function xml.unescaped(str) return (gsub(str,"(&.-;)", xml.unescapes)) end @@ -71,8 +70,6 @@ local escaped = Cs(normal * (special * normal)^0) -- 100 * 1000 * "oeps< oeps> oeps&" : gsub:lpeg == 0153:0280:0151:0080 (last one by roberto) --- unescaped = Cs((S("<")/"<" + S(">")/">" + S("&")/"&" + 1)^0) --- unescaped = Cs((((P("&")/"") * (P("lt")/"<" + P("gt")/">" + P("amp")/"&") * (P(";")/"")) + 1)^0) local normal = (1 - S"&")^0 local special = P("<")/"<" + P(">")/">" + P("&")/"&" local unescaped = Cs(normal * (special * normal)^0) @@ -85,22 +82,18 @@ xml.escaped_pattern = escaped xml.unescaped_pattern = unescaped xml.cleansed_pattern = cleansed -function xml.escaped (str) return escaped :match(str) end -function xml.unescaped(str) return unescaped:match(str) end -function xml.cleansed (str) return cleansed :match(str) end +function xml.escaped (str) return lpegmatch(escaped,str) end +function xml.unescaped(str) return lpegmatch(unescaped,str) end +function xml.cleansed (str) return lpegmatch(cleansed,str) end -function xml.join(t,separator,lastseparator) - if #t > 0 then - local result = { } - for k,v in pairs(t) do - result[k] = xml.tostring(v) - end - if lastseparator then - return concat(result,separator or "",1,#result-1) .. (lastseparator or "") .. result[#result] - else - return concat(result,separator) +-- this might move + +function xml.fillin(root,pattern,str,check) + local e = xml.first(root,pattern) + if e then + local n = #e.dt + if not check or n == 0 or (n == 1 and e.dt[1] == "") then + e.dt = { str } end - else - return "" end end |