1if not modules then modules = { } end modules ['buff-imp-tex'] = {
2 version = 1.001,
3 comment = "companion to v-tex.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
9
10
11local P, S, V, patterns = lpeg.P, lpeg.S, lpeg.V, lpeg.patterns
12
13local context = context
14local verbatim = context.verbatim
15local makepattern = visualizers.makepattern
16local makenested = visualizers.makenested
17local getvisualizer = visualizers.getvisualizer
18
19local TexSnippet = context.TexSnippet
20local startTexSnippet = context.startTexSnippet
21local stopTexSnippet = context.stopTexSnippet
22
23local TexSnippetName = verbatim.TexSnippetName
24local TexSnippetGroup = verbatim.TexSnippetGroup
25local TexSnippetBoundary = verbatim.TexSnippetBoundary
26local TexSnippetSpecial = verbatim.TexSnippetSpecial
27local TexSnippetComment = verbatim.TexSnippetComment
28local TexSnippetCommentText = verbatim.TexSnippetCommentText
29
30local handler = visualizers.newhandler {
31 startinline = function() TexSnippet(false,"{") end,
32 stopinline = function() context("}") end,
33 startdisplay = function() startTexSnippet() end,
34 stopdisplay = function() stopTexSnippet() end ,
35 name = function(s) TexSnippetName(s) end,
36 group = function(s) TexSnippetGroup(s) end,
37 boundary = function(s) TexSnippetBoundary(s) end,
38 special = function(s) TexSnippetSpecial(s) end,
39 comment = function(s) TexSnippetComment(s) end,
40 commenttext = function(s) TexSnippetCommentText(s) end,
41}
42
43
44
45local comment = S("%")
46local name = P("\\") * (patterns.letter + S("@!?_") + patterns.utf8two + patterns.utf8three + patterns.utf8four)^1
47local escape = P("\\") * (patterns.anything - patterns.newline)^-1
48local group = S("${}")
49local boundary = S('[]()<>#="')
50local special = S("/^_-&+'`|")
51
52local p_comment = makepattern(handler,"comment",comment)
53 * makepattern(handler,"commenttext",(patterns.anything - patterns.newline)^0)
54local p_name = makepattern(handler,"name",name)
55local p_escape = makepattern(handler,"name",escape)
56local p_group = makepattern(handler,"group",group)
57local p_boundary = makepattern(handler,"boundary",boundary)
58local p_special = makepattern(handler,"special",special)
59local p_somespace = V("newline") * V("emptyline")^0 * V("beginline")
60 + V("space")
61
62
63
64local grammar = visualizers.newgrammar("default", { "visualizer",
65
66 comment = p_comment,
67 name = p_name,
68 escape = p_escape,
69 group = p_group,
70 boundary = p_boundary,
71 special = p_special,
72 somespace = p_somespace,
73
74 pattern = V("comment")
75 + V("name") + V("escape") + V("group") + V("boundary") + V("special")
76 + V("newline") * V("emptyline")^0 * V("beginline")
77 + V("space")
78 + V("default"),
79
80 visualizer = V("pattern")^1
81
82} )
83
84local parser = P(grammar)
85
86visualizers.register("tex", { parser = parser, handler = handler, grammar = grammar } )
87
88local function makecommand(handler,how,start,left,right)
89 local c, l, r, f = P(start), P(left), P(right), how
90 local n = ( P { l * ((1 - (l + r)) + V(1))^0 * r } + P(1-r) )^0
91 if type(how) == "string" then
92 f = function(s) getvisualizer(how,"direct")(s) end
93 end
94 return makepattern(handler,"name",c)
95 * V("somespace")^0
96 * makepattern(handler,"group",l)
97 * (n/f)
98 * makepattern(handler,"group",r)
99end
100
101local grammar = visualizers.newgrammar("default", { "visualizer",
102
103 comment = p_comment,
104 name = p_name,
105 escape = p_escape,
106 group = p_group,
107 boundary = p_boundary,
108 special = p_special,
109 somespace = p_somespace,
110
111 mpcode = makenested(handler,"mp","\\startMPcode","\\stopMPcode")
112 + makenested(handler,"mp","\\startMPgraphic","\\stopMPgraphic")
113 + makenested(handler,"mp","\\startuseMPgraphic","\\stopuseMPgraphic")
114 + makenested(handler,"mp","\\startreusableMPgraphic","\\stopreusableMPgraphic")
115 + makenested(handler,"mp","\\startuniqueMPgraphic","\\stopuniqueMPgraphic")
116 + makenested(handler,"mp","\\startMPpage","\\stopMPpage"),
117
118 luacode = makenested (handler,"lua","\\startluacode","\\stopluacode")
119 + makecommand(handler,"lua","\\ctxlua","{","}"),
120
121 pattern = V("comment")
122 + V("mpcode") + V("luacode")
123 + V("name") + V("escape") + V("group") + V("boundary") + V("special")
124 + V("newline") * V("emptyline")^0 * V("beginline")
125 + V("space")
126 + V("default"),
127
128 visualizer = V("pattern")^1
129
130} )
131
132local parser = P(grammar)
133
134visualizers.register("context", { parser = parser, handler = handler, grammar = grammar } )
135 |