summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/buff-imp-xml.lua
diff options
context:
space:
mode:
authorContext Git Mirror Bot <phg42.2a@gmail.com>2016-01-12 17:15:07 +0100
committerContext Git Mirror Bot <phg42.2a@gmail.com>2016-01-12 17:15:07 +0100
commit8d8d528d2ad52599f11250cfc567fea4f37f2a8b (patch)
tree94286bc131ef7d994f9432febaf03fe23d10eef8 /tex/context/base/mkiv/buff-imp-xml.lua
parentf5aed2e51223c36c84c5f25a6cad238b2af59087 (diff)
downloadcontext-8d8d528d2ad52599f11250cfc567fea4f37f2a8b.tar.gz
2016-01-12 16:26:00
Diffstat (limited to 'tex/context/base/mkiv/buff-imp-xml.lua')
-rw-r--r--tex/context/base/mkiv/buff-imp-xml.lua133
1 files changed, 133 insertions, 0 deletions
diff --git a/tex/context/base/mkiv/buff-imp-xml.lua b/tex/context/base/mkiv/buff-imp-xml.lua
new file mode 100644
index 000000000..0c48ed3b0
--- /dev/null
+++ b/tex/context/base/mkiv/buff-imp-xml.lua
@@ -0,0 +1,133 @@
+if not modules then modules = { } end modules ['buff-imp-xml'] = {
+ version = 1.001,
+ comment = "companion to v-xml.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local P, S, V, patterns = lpeg.P, lpeg.S, lpeg.V, lpeg.patterns
+
+local context = context
+local verbatim = context.verbatim
+local makepattern = visualizers.makepattern
+
+local XmlSnippet = context.XmlSnippet
+local startXmlSnippet = context.startXmlSnippet
+local stopXmlSnippet = context.stopXmlSnippet
+
+local XmlSnippetName = verbatim.XmlSnippetName
+local XmlSnippetKey = verbatim.XmlSnippetKey
+local XmlSnippetBoundary = verbatim.XmlSnippetBoundary
+local XmlSnippetString = verbatim.XmlSnippetString
+local XmlSnippetEqual = verbatim.XmlSnippetEqual
+local XmlSnippetEntity = verbatim.XmlSnippetEntity
+local XmlSnippetComment = verbatim.XmlSnippetComment
+local XmlSnippetCdata = verbatim.XmlSnippetCdata
+
+local handler = visualizers.newhandler {
+ startinline = function() XmlSnippet(false,"{") end,
+ stopinline = function() context("}") end,
+ startdisplay = function() startXmlSnippet() end,
+ stopdisplay = function() stopXmlSnippet () end,
+ name = function(s) XmlSnippetName(s) end,
+ key = function(s) XmlSnippetKey(s) end,
+ boundary = function(s) XmlSnippetBoundary(s) end,
+ string = function(s) XmlSnippetString(s) end,
+ equal = function(s) XmlSnippetEqual(s) end,
+ entity = function(s) XmlSnippetEntity(s) end,
+ comment = function(s) XmlSnippetComment(s) end,
+ cdata = function(s) XmlSnippetCdata(s) end,
+}
+
+local comment = P("--")
+local name = (patterns.letter + patterns.digit + S('_-.'))^1
+local entity = P("&") * (1-P(";"))^1 * P(";")
+local openbegin = P("<")
+local openend = P("</")
+local closebegin = P("/>") + P(">")
+local closeend = P(">")
+local opencomment = P("<!--")
+local closecomment = P("-->")
+local openinstruction = P("<?")
+local closeinstruction = P("?>")
+local opencdata = P("<![CDATA[")
+local closecdata = P("]]>")
+
+local grammar = visualizers.newgrammar("default", { "visualizer",
+ sstring =
+ makepattern(handler,"string",patterns.dquote)
+ * (V("whitespace") + makepattern(handler,"default",1-patterns.dquote))^0
+ * makepattern(handler,"string",patterns.dquote),
+ dstring =
+ makepattern(handler,"string",patterns.squote)
+ * (V("whitespace") + makepattern(handler,"default",1-patterns.squote))^0
+ * makepattern(handler,"string",patterns.squote),
+ entity =
+ makepattern(handler,"entity",entity),
+ name =
+ makepattern(handler,"name",name)
+ * (
+ makepattern(handler,"default",patterns.colon)
+ * makepattern(handler,"name",name)
+ )^0,
+ key =
+ makepattern(handler,"key",name)
+ * (
+ makepattern(handler,"default",patterns.colon)
+ * makepattern(handler,"key",name)
+ )^0,
+ attributes = (
+ V("optionalwhitespace")
+ * V("key")
+ * V("optionalwhitespace")
+ * makepattern(handler,"equal",patterns.equal)
+ * V("optionalwhitespace")
+ * (V("dstring") + V("sstring"))
+ * V("optionalwhitespace")
+ )^0,
+ open =
+ makepattern(handler,"boundary",openbegin)
+ * V("name")
+ * V("optionalwhitespace")
+ * V("attributes")
+ * makepattern(handler,"boundary",closebegin),
+ close =
+ makepattern(handler,"boundary",openend)
+ * V("name")
+ * V("optionalwhitespace")
+ * makepattern(handler,"boundary",closeend),
+ comment =
+ makepattern(handler,"boundary",opencomment)
+ * (V("whitespace") + makepattern(handler,"comment",(1-closecomment)^1))^0 -- slow
+ * makepattern(handler,"boundary",closecomment),
+ cdata =
+ makepattern(handler,"boundary",opencdata)
+ * (V("whitespace") + makepattern(handler,"comment",(1-closecdata)^1))^0 -- slow
+ * makepattern(handler,"boundary",closecdata),
+ instruction =
+ makepattern(handler,"boundary",openinstruction)
+ * V("name")
+ * V("optionalwhitespace")
+ * V("attributes")
+ * V("optionalwhitespace")
+ * makepattern(handler,"boundary",closeinstruction),
+
+ pattern =
+ V("comment")
+ + V("instruction")
+ + V("cdata")
+ + V("close")
+ + V("open")
+ + V("entity")
+ + V("space")
+ + V("line")
+ + V("default"),
+
+ visualizer =
+ V("pattern")^1
+} )
+
+local parser = P(grammar)
+
+visualizers.register("xml", { parser = parser, handler = handler, grammar = grammar } )