buff-imp-xml.lua /size: 4955 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['buff-imp-xml'] = {
2    version   = 1.001,
3    comment   = "companion to v-xml.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local P, S, V, patterns = lpeg.P, lpeg.S, lpeg.V, lpeg.patterns
10
11local context            = context
12local verbatim           = context.verbatim
13local makepattern        = visualizers.makepattern
14
15local XmlSnippet         = context.XmlSnippet
16local startXmlSnippet    = context.startXmlSnippet
17local stopXmlSnippet     = context.stopXmlSnippet
18
19local XmlSnippetName     = verbatim.XmlSnippetName
20local XmlSnippetKey      = verbatim.XmlSnippetKey
21local XmlSnippetBoundary = verbatim.XmlSnippetBoundary
22local XmlSnippetString   = verbatim.XmlSnippetString
23local XmlSnippetEqual    = verbatim.XmlSnippetEqual
24local XmlSnippetEntity   = verbatim.XmlSnippetEntity
25local XmlSnippetComment  = verbatim.XmlSnippetComment
26local XmlSnippetCdata    = verbatim.XmlSnippetCdata
27
28local handler = visualizers.newhandler {
29    startinline  = function() XmlSnippet(false,"{") end,
30    stopinline   = function() context("}") end,
31    startdisplay = function() startXmlSnippet() end,
32    stopdisplay  = function() stopXmlSnippet () end,
33    name         = function(s) XmlSnippetName(s) end,
34    key          = function(s) XmlSnippetKey(s) end,
35    boundary     = function(s) XmlSnippetBoundary(s) end,
36    string       = function(s) XmlSnippetString(s) end,
37    equal        = function(s) XmlSnippetEqual(s) end,
38    entity       = function(s) XmlSnippetEntity(s) end,
39    comment      = function(s) XmlSnippetComment(s) end,
40    cdata        = function(s) XmlSnippetCdata(s) end,
41}
42
43local comment          = P("--")
44local alsoname         = patterns.utf8two + patterns.utf8three + patterns.utf8four
45----- alsoname         = R("\128\255") -- basically any encoding without checking (fast)
46local name             = (patterns.letter + patterns.digit + S('_-.') + alsoname)^1
47local entity           = P("&") * (1-P(";"))^1 * P(";")
48local openbegin        = P("<")
49local openend          = P("</")
50local closebegin       = P("/>") + P(">")
51local closeend         = P(">")
52local opencomment      = P("<!--")
53local closecomment     = P("-->")
54local openinstruction  = P("<?")
55local closeinstruction = P("?>")
56local opencdata        = P("<![CDATA[")
57local closecdata       = P("]]>")
58
59local grammar = visualizers.newgrammar("default", { "visualizer",
60    sstring =
61        makepattern(handler,"string",patterns.dquote)
62      * (V("whitespace") + makepattern(handler,"default",(1-patterns.dquote)^0))
63      * makepattern(handler,"string",patterns.dquote),
64    dstring =
65        makepattern(handler,"string",patterns.squote)
66      * (V("whitespace") + makepattern(handler,"default",(1-patterns.squote)^0))
67      * makepattern(handler,"string",patterns.squote),
68    entity =
69        makepattern(handler,"entity",entity),
70    name =
71        makepattern(handler,"name",name)
72      * (
73            makepattern(handler,"default",patterns.colon)
74          * makepattern(handler,"name",name)
75        )^0,
76    key =
77        makepattern(handler,"key",name)
78      * (
79            makepattern(handler,"default",patterns.colon)
80          * makepattern(handler,"key",name)
81        )^0,
82    attributes = (
83        V("optionalwhitespace")
84      * V("key")
85      * V("optionalwhitespace")
86      * makepattern(handler,"equal",patterns.equal)
87      * V("optionalwhitespace")
88      * (V("dstring") + V("sstring"))
89      * V("optionalwhitespace")
90    )^0,
91    open =
92        makepattern(handler,"boundary",openbegin)
93      * V("name")
94      * V("optionalwhitespace")
95      * V("attributes")
96      * makepattern(handler,"boundary",closebegin),
97    close =
98        makepattern(handler,"boundary",openend)
99      * V("name")
100      * V("optionalwhitespace")
101      * makepattern(handler,"boundary",closeend),
102    comment =
103        makepattern(handler,"boundary",opencomment)
104      * (V("whitespace") + makepattern(handler,"comment",(1-closecomment)^1))^0 -- slow
105      * makepattern(handler,"boundary",closecomment),
106    cdata =
107        makepattern(handler,"boundary",opencdata)
108      * (V("whitespace") + makepattern(handler,"comment",(1-closecdata)^1))^0 -- slow
109      * makepattern(handler,"boundary",closecdata),
110    instruction =
111        makepattern(handler,"boundary",openinstruction)
112      * V("name")
113      * V("optionalwhitespace")
114      * V("attributes")
115      * V("optionalwhitespace")
116      * makepattern(handler,"boundary",closeinstruction),
117
118    pattern =
119        V("comment")
120      + V("instruction")
121      + V("cdata")
122      + V("close")
123      + V("open")
124      + V("entity")
125      + V("space")
126      + V("line")
127      + V("default"),
128
129    visualizer =
130        V("pattern")^1
131} )
132
133local parser = P(grammar)
134
135visualizers.register("xml", { parser = parser, handler = handler, grammar = grammar } )
136