Debian Codegraph @

texmf-dist/doc/lualatex/longmath/longmath-doc.lua

raw
1 --
2 -- longmath-doc.lua is part of longmath version 1.0.
3 --
4 -- (c) 2024 Hans-Jürgen Matschull
5 --
6 -- This work may be distributed and/or modified under the
7 -- conditions of the LaTeX Project Public License, either version 1.3
8 -- of this license or (at your option) any later version.
9 -- The latest version of this license is in
10 -- http://www.latex-project.org/lppl.txt
11 -- and version 1.3 or later is part of all distributions of LaTeX
12 -- version 2005/12/01 or later.
13 --
14 -- This work has the LPPL maintenance status 'maintained'.
15 --
16 -- The Current Maintainer of this work is Hans-Jürgen Matschull
17 --
18 -- see README for a list of files belonging to longmath.
19 --
20
21 local match = unicode.utf8.match
22 local gsub = unicode.utf8.gsub
23 local find = unicode.utf8.find
24
25 local nixpatt = {
26 { n = 'newl', p = '\n' },
27 { n = 'text', p = '[^\n]*' },
28 }
29
30 local texpatt = {
31 { n = 'group', p = '%b{}' },
32 { n = 'math', p = '%(%)' },
33 { n = 'defn', p = '\\newcount\\[%w@]+' },
34 { n = 'defn', p = '\\newdimen\\[%w@]+' },
35 { n = 'defn', p = '\\def\\[%w@]+' },
36 { n = 'expl', p = '%%%%%%[^\n]*' },
37 { n = 'expl', p = '%&%&%&[^\n]*' },
38 { n = 'comm', p = '%%[^\n]*' },
39 { n = 'oper', p = '[#]+%d' },
40 { n = 'macro', p = '\\[%w@]+' },
41 { n = 'punc', p = '\\%W' },
42 { n = 'newl', p = '\n' },
43 { n = 'space', p = '[ ]+' },
44 { n = 'inter', p = "»[^«]*«" },
45 { n = 'text', p = '[%w@_….,;:~?!\'€∞]+' },
46 { n = 'math', p = '[-+/^=≠<>≤≥·×÷≈"]+' },
47 { n = 'open', p = '[%[%(%{]' },
48 { n = 'close', p = '[%]%)%}]' },
49 { n = 'punc', p = '[%%&$#*|`\']' },
50 }
51
52
53 local function write( item, d )
54 if item.n == 'open' then item.n = 'group' if d < 9 then d = d + 1 end end
55 if item.n == 'close' then if d > 0 then d = d - 1 end item.n = 'group' end
56 if item.n == 'newl' then
57 tex.sprint( '\\\\\\strut' )
58 elseif item.n == 'expl' then
59 local t = item.p
60 t = gsub( t, '%%%%%%','' )
61 t = gsub( t, '%-%-%-', '' )
62 t = gsub( t, '%&%&%&', '' )
63 tex.sprint( '\\rlap{\\textrm{'..t..'}}' )
64 elseif item.n == 'inter' then
65 tex.sprint( ( item.p:gsub( '«', '' ):gsub( '»', '' ) ) )
66 else
67 local ex = string.explode( item.p, ' ' )
68 tex.sprint( '\\textcolor{code' .. item.n .. '}{' )
69 for i, t in ipairs( ex ) do
70 if i > 1 then tex.sprint( '{ }' ) end
71 t = t:gsub( '\\%<', '\\{' ):gsub( '\\%>', '\\}' ):gsub( '«', '' ):gsub( '»', '' )
72 tex.write( t )
73 end
74 tex.sprint( '}' )
75 end
76 return d
77 end
78
79 local function nsplit( code )
80 local list1 = {}
81 while code ~= '' do
82 local a, b
83 for _, p in ipairs( nixpatt ) do
84 a, b = match( code, '^(' .. p.p .. ')(.*)$' )
85 if a and b then table.insert( list1, { n = p.n, p = a } ); code = b; break end
86 end
87 if not a then table.insert( list1, { n = 'rest', p = code } ); code = '' end
88 end
89 return list1
90 end
91
92 local function tsplit( code )
93 local list1 = {}
94 while code ~= '' do
95 local a, b
96 for _, p in ipairs( texpatt ) do
97 a, b = match( code, '^(' .. p.p .. ')(.*)$' )
98 if a and b then table.insert( list1, { n = p.n, p = a } ); code = b; break end
99 end
100 if not a then table.insert( list1, { n = 'rest', p = code } ); code = '' end
101 end
102 local list2 = {}
103 local lgrp = 0
104 local isub
105 for _, item in ipairs( list1 ) do
106 if item.n == 'luap' then
107 table.insert( list2, { n = 'macro', p = '\\begin' } )
108 table.insert( list2, { n = 'open', p = '{' } )
109 table.insert( list2, { n = 'luac', p = 'luap' } )
110 table.insert( list2, { n = 'close', p = '}' } )
111 isub = lsplit( item.p:gsub( '\\begin{luap}', '' ):gsub( '\\end{luap}', '' ) )
112 for _, i in ipairs( isub ) do table.insert( list2, i ) end
113 table.insert( list2, { n = 'macro', p = '\\end' } )
114 table.insert( list2, { n = 'open', p = '{' } )
115 table.insert( list2, { n = 'luac', p = 'luap' } )
116 table.insert( list2, { n = 'close', p = '}' } )
117 elseif item.n == 'group' then
118 local a, b = item.p:sub( 1, 1 ), item.p:sub( -1, -1 )
119 if lgrp > 0 then
120 item = lsplit( item.p:sub( 2, -2 ) )
121 lgrp = lgrp - 1
122 else
123 item = tsplit( item.p:sub( 2, -2 ) )
124 end
125 table.insert( list2, { n = 'open', p = a } )
126 for _, i in ipairs( item ) do table.insert( list2, i ) end
127 table.insert( list2, { n = 'close', p = b } )
128 elseif item.n == 'macro' then
129 local lc = item.p:sub( 2 )
130 table.insert( list2, item )
131 elseif item.n == 'space' then
132 table.insert( list2, item )
133 else
134 table.insert( list2, item )
135 end
136 end
137 return list2
138 end
139
140 local codelist = {}
141
142 local function readcode( env )
143 local name = string.format( '\\end{%s}', env )
144 local patt = name:gsub( '(%W)', '%%%1' )
145 local typ, line
146 local lines, len, spc = {}, 0, 1000
147 while true do
148 line = token.scan_word()
149 if type( line ) == 'string' then
150 local a, b = line:find( patt )
151 if a and b then break end
152 line = line:gsub( '\\%.[%[%s%w%]]*$', '' ):gsub( '\\%.', '\\\\' )
153 line = line:gsub( '%s*$', '' )
154 local stub = line:gsub( '%%%%%%.*$', '' ):gsub( '%-%-%-.*$', '' )
155 if not typ and stub:match( '[^\\]\\%w' ) then typ = tsplit end
156 len = math.max( len, unicode.utf8.len( stub:gsub( '»[^«]*«', '' ) ) )
157 spc = math.min( spc, line:find( '%S' ) or 1000 )
158 table.insert( lines, line )
159 else
160 token.get_next()
161 table.insert( lines, '' )
162 end
163 end
164 local code = ''
165 for _, line in ipairs( lines ) do code = code .. line:sub( spc ) .. '\n' end
166 code = code:gsub( '\\(%W)', '°%1' ):gsub( '°}', '°>' ):gsub( '°{', '°<' ):gsub( '°', '\\' ):gsub( '\n$', '' )
167 tex.setdimen( 'global', 'codesize', ( len - spc + 1.5 ) * tex.dimen.ttwidth )
168 codelist = ( typ or lsplit )( code )
169 tex.sprint( tex.count['longmathdoc@ccnum@'], '\\endgroup' )
170 tex.sprint( name )
171 end
172
173 local function writecode()
174 local dep = 0
175 tex.sprint( '\\strut' )
176 for _, item in ipairs( codelist ) do dep = write( item, dep ) end
177 end
178
179 local dummy
180 local function readstring()
181 dummy = string.explode( token.scan_string() )
182 end
183 local function writestring()
184 for i, c in ipairs( dummy ) do
185 if i > 1 then tex.sprint( '~' ) end
186 tex.sprint( '\\verbC{' ) tex.write( c ) tex.sprint( '}' )
187 end
188 end
189
190 return { readcode = readcode, writecode = writecode,
191 readstring = readstring, writestring = writestring }
192