buff-imp-tex.lua /size: 5237 b    last modification: 2020-07-01 14:35
1
if
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
-- needs an update, use mult-low
10 11
local
P
,
S
,
V
,
patterns
=
lpeg
.
P
,
lpeg
.
S
,
lpeg
.
V
,
lpeg
.
patterns
12 13
local
context
=
context
14
local
verbatim
=
context
.
verbatim
15
local
makepattern
=
visualizers
.
makepattern
16
local
makenested
=
visualizers
.
makenested
17
local
getvisualizer
=
visualizers
.
getvisualizer
18 19
local
TexSnippet
=
context
.
TexSnippet
20
local
startTexSnippet
=
context
.
startTexSnippet
21
local
stopTexSnippet
=
context
.
stopTexSnippet
22 23
local
TexSnippetName
=
verbatim
.
TexSnippetName
24
local
TexSnippetGroup
=
verbatim
.
TexSnippetGroup
25
local
TexSnippetBoundary
=
verbatim
.
TexSnippetBoundary
26
local
TexSnippetSpecial
=
verbatim
.
TexSnippetSpecial
27
local
TexSnippetComment
=
verbatim
.
TexSnippetComment
28
local
TexSnippetCommentText
=
verbatim
.
TexSnippetCommentText
29 30
local
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
-- todo: unicode letters in control sequences (slow as we need to test the nature)
44 45
local
comment
=
S
(
"
%
"
)
46
local
name
=
P
(
"
\\
"
)
*
(
patterns
.
letter
+
S
(
"
@!?_
"
)
+
patterns
.
utf8two
+
patterns
.
utf8three
+
patterns
.
utf8four
)
^
1
47
local
escape
=
P
(
"
\\
"
)
*
(
patterns
.
anything
-
patterns
.
newline
)
^
-1
-- else we get \n
48
local
group
=
S
(
"
${}
"
)
49
local
boundary
=
S
(
'
[]()<>#="
'
)
50
local
special
=
S
(
"
/^_-&+'`|
"
)
51 52
local
p_comment
=
makepattern
(
handler
,
"
comment
"
,
comment
)
53
*
makepattern
(
handler
,
"
commenttext
"
,
(
patterns
.
anything
-
patterns
.
newline
)
^
0
)
54
local
p_name
=
makepattern
(
handler
,
"
name
"
,
name
)
55
local
p_escape
=
makepattern
(
handler
,
"
name
"
,
escape
)
56
local
p_group
=
makepattern
(
handler
,
"
group
"
,
group
)
57
local
p_boundary
=
makepattern
(
handler
,
"
boundary
"
,
boundary
)
58
local
p_special
=
makepattern
(
handler
,
"
special
"
,
special
)
59
local
p_somespace
=
V
(
"
newline
"
)
*
V
(
"
emptyline
"
)
^
0
*
V
(
"
beginline
"
)
60
+
V
(
"
space
"
)
61 62
--~ local pattern = visualizers.pattern
63 64
local
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 84
local
parser
=
P
(
grammar
)
85 86
visualizers
.
register
(
"
tex
"
,
{
parser
=
parser
,
handler
=
handler
,
grammar
=
grammar
}
)
87 88
local
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
)
99
end
100 101
local
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 132
local
parser
=
P
(
grammar
)
133 134
visualizers
.
register
(
"
context
"
,
{
parser
=
parser
,
handler
=
handler
,
grammar
=
grammar
}
)
135