summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/buff-imp-parsed-xml.lua
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2016-04-22 22:14:39 +0000
committerKarl Berry <karl@freefriends.org>2016-04-22 22:14:39 +0000
commitfc4466b32ed330a956ac603b00fd145524cff49a (patch)
tree2c50e2b8de13aa9233b2c76dffe201558f169e86 /Master/texmf-dist/tex/context/base/mkiv/buff-imp-parsed-xml.lua
parent50e2368597d5f6fe2057195d0ae6a9f2044923e4 (diff)
context (22apr16)
git-svn-id: svn://tug.org/texlive/trunk@40691 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkiv/buff-imp-parsed-xml.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/mkiv/buff-imp-parsed-xml.lua101
1 files changed, 101 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkiv/buff-imp-parsed-xml.lua b/Master/texmf-dist/tex/context/base/mkiv/buff-imp-parsed-xml.lua
new file mode 100644
index 00000000000..22611ac8ab7
--- /dev/null
+++ b/Master/texmf-dist/tex/context/base/mkiv/buff-imp-parsed-xml.lua
@@ -0,0 +1,101 @@
+if not modules then modules = { } end modules ['buff-imp-parsed-xml'] = {
+ version = 1.001,
+ comment = "companion to buff-imp-parsed-xml.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local format = string.format
+
+local context = context
+local verbatim = context.verbatim
+
+local write = visualizers.write
+local writespace = visualizers.writespace
+local writeargument = visualizers.writeargument
+
+local ParsedXmlSnippetKey = context.ParsedXmlSnippetKey
+local ParsedXmlSnippetValue = context.ParsedXmlSnippetValue
+
+local ParsedXmlSnippetElement = verbatim.ParsedXmlSnippetElement
+local ParsedXmlSnippetInstruction = verbatim.ParsedXmlSnippetInstruction
+local ParsedXmlSnippetComment = verbatim.ParsedXmlSnippetComment
+local ParsedXmlSnippetCdata = verbatim.ParsedXmlSnippetCdata
+local ParsedXmlSnippetDoctype = verbatim.ParsedXmlSnippetDoctype
+
+local startParsedXmlSnippet = context.startParsedXmlSnippet
+local stopParsedXmlSnippet = context.stopParsedXmlSnippet
+
+local parsedxmlhandler = xml.newhandlers { -- todo: treat spaces and tabs
+ name = "parsedxml",
+ handle = function(...)
+ print("error:",...) -- we need a handler as fallback, even if not used
+ end,
+ functions = {
+ ["@el@"] = function(e,handler)
+ local at = e.at
+ if at and next(at) then
+ ParsedXmlSnippetElement(format("<%s",e.tg))
+ for k, v in next, at do
+ writespace()
+ ParsedXmlSnippetKey()
+ writeargument(k)
+ verbatim("=")
+ ParsedXmlSnippetValue()
+ writeargument(format("%q",k))
+ end
+ ParsedXmlSnippetElement(">")
+ else
+ ParsedXmlSnippetElement(format("<%s>",e.tg))
+ end
+ handler.serialize(e.dt,handler)
+ ParsedXmlSnippetElement(format("</%s>",e.tg))
+ end,
+ ["@pi@"] = function(e,handler)
+ ParsedXmlSnippetInstruction("<?")
+ write(e.dt[1])
+ ParsedXmlSnippetInstruction("?>")
+ end ,
+ ["@cm@"] = function(e,handler)
+ ParsedXmlSnippetComment("<!--")
+ write(e.dt[1])
+ ParsedXmlSnippetComment("-->")
+ end,
+ ["@cd@"] = function(e,handler)
+ ParsedXmlSnippetCdata("<![CDATA[")
+ write(e.dt[1])
+ ParsedXmlSnippetCdata("]]>")
+ end,
+ ["@dt@"] = function(e,handler)
+ ParsedXmlSnippetDoctype("<!DOCTYPE")
+ write(e.dt[1])
+ ParsedXmlSnippetDoctype(">")
+ end,
+ ["@tx@"] = function(s,handler)
+ write(s)
+ end,
+ }
+}
+
+local function parsedxml(root,pattern)
+ if root then
+ if pattern then
+ root = xml.filter(root,pattern)
+ end
+ if root then
+ context.startParsedXmlSnippet()
+ xml.serialize(root,parsedxmlhandler)
+ context.stopParsedXmlSnippet()
+ end
+ end
+end
+
+local function parser(str,settings)
+ parsedxml(xml.convert(str),settings and settings.pattern)
+end
+
+visualizers.parsedxml = parsedxml -- for use at the lua end (maybe namespace needed)
+
+visualizers.register("parsed-xml", { parser = parser } )
+