summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/prettytok/prettytok.lua
blob: 0abb6595f0ee5c8f13fffa1069235311fd9647b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
do
local function looks_like_token(t)
	return type(t)=="userdata" or (type(t)=="table" and t.cmdname~=nil and t.tok~=nil)
end

-- public API :: once the package is loaded, this holds the .tok value of the frozen relax token
prettyprint_frozenrelaxtok=nil


local function prettyprint_one_arg(tokenlist)
	if looks_like_token(tokenlist) then
		return prettyprint_one_arg({tokenlist})
	end

	local s=""

	local function printstring(t)
		-- this is just for debugging/convenience purpose. If the item is a string instead of a token...
		t=tostring(t)
		for _, c in utf8.codes(t) do
			if c==32 then
				s=s.."token("..c..',"A"),'
			else
				s=s.."token("..c..',"C"),'
			end
		end
	end

	if type(tokenlist)~="table" then
		-- user give a string (or something similar). Print it as detokenized characters
		printstring(tokenlist)
	
	else

		-- normal print of tokenlist
		for i=1, #tokenlist do
			local t=tokenlist[i]
			if not looks_like_token(t) then
				printstring(t)
			elseif t.csname==nil then
				s=s.."token("..t.mode..',"'..(({left_brace=1, right_brace=2, math_shift=3, tab_mark=4, mac_param=6, sup_mark=7, sub_mark=8, spacer="A", letter="B", other_char="C"})[t.cmdname])..'"),'
			else
				if t.active then
					s=s.."token("..utf8.codepoint(t.csname)..',"D"),'
				elseif t.tok==prettyprint_frozenrelaxtok then
					s=s.."csfrozenrelax(),"
				else
					s=s.."cs("
					-- TODO
					for _, c in utf8.codes(t.csname) do
						s=s..c..","
					end
					s=s.."),"
				end
			end
		end
		
	end
	return s
end

local function get_output_format()
	return token.get_macro "_prettytok_mode"
end

local function get_file_number()
	return token.get_mode(token.create("_prettytok_file"))
end

function prettyprint(...)
	local output_format=get_output_format()
	local args={...}
	if output_format=="html" then
		local prettyfilenumber=get_file_number()

		local s="print_tl("
		for i=1, select("#", ...) do
			s=s..prettyprint_one_arg(args[i])
		end

		texio.write(prettyfilenumber, s..")//</script><script>\n")
	else
		assert(output_format=="term-8bit" or output_format=="term-shell")
		texio.write_nl("")

		local content=""
		local content_len=0  -- only count visible content

		local function typeout_content()
			print(content)
			content=""
			content_len=0
		end

		local function append_content(s)  -- only call this on content that takes visible width. For example don't use this to set color
			assert(type(s)=="string")
			content=content..s
			content_len=content_len+utf8.len(s)
		end

		append_content(token.get_macro "prettytermprefix")
		local wraplimit=tonumber(token.get_macro "prettytermwraplimit")

		---- color

		--[[
		local gray  ='\x1b[90m'
		local red   ='\x1b[31m'
		local green ='\x1b[38;5;121m'
		local yellow='\x1b[93m'
		local white ='\x1b[0m'
		]]

		local gray  =token.get_macro "_prettytok_gray"
		local red   =token.get_macro "_prettytok_red"
		local green =token.get_macro "_prettytok_green"
		local yellow=token.get_macro "_prettytok_yellow"
		local white =token.get_macro "_prettytok_white"

		local current_color=white
		local function setcolor(c)
			if current_color~=c then
				current_color=c
				content=content..c
			end
		end

		local prettytermprefixmore=token.get_macro "prettytermprefixmore"

		local function check_wrap()
			if content_len>wraplimit then
				if current_color~=white then
					content=content..white
				end
				print(content)
				
				content=""
				content_len=0
				append_content(prettytermprefixmore)
				if current_color~=white then
					content=content..current_color
				end
			end
		end

		local function prettyprint_term_one_arg(tokenlist)
			if looks_like_token(tokenlist) then
				return prettyprint_term_one_arg({tokenlist})
			end


			local function print_term_char(code)
				if code<27 then
					if code==9 then
						append_content("⇥")
					else
						append_content("^^"..string.char(code+64))
						if code==13 or code==10 then
							typeout_content()
							append_content(prettytermprefixmore)  -- color the prefix with the same color
						end
					end
				elseif code==32 then
					append_content("␣")
				else
					append_content(utf8.char(code))  -- unlike TeX implementation currently this does not show ^^⟨hex digits⟩ representation
				end
			end


			local function printstring(t)
				-- this is just for debugging/convenience purpose. If the item is a string instead of a token...
				t=tostring(t)
				for _, c in utf8.codes(t) do
					if c==32 then
						setcolor(gray)
						append_content("␣")
					else
						setcolor(white)
						append_content(utf8.char(c))
					end
				end
			end

			if type(tokenlist)~="table" then
				-- user give a string (or something similar). Print it as detokenized characters
				printstring(tokenlist)
				check_wrap()
			
			else

				-- normal print of tokenlist
				for i=1, #tokenlist do
					local t=tokenlist[i]
					if not looks_like_token(t) then
						printstring(t)
						check_wrap()
					elseif t.csname==nil then
						setcolor(({left_brace=red, right_brace=red, math_shift=red, tab_mark=red, mac_param=red, sup_mark=red, sub_mark=red, spacer=gray, letter=green, other_char=white})[t.cmdname])
						print_term_char(t.mode)
						check_wrap()
					else
						if t.active then
							setcolor(yellow)
							print_term_char(utf8.codepoint(t.csname))
						elseif t.tok==prettyprint_frozenrelaxtok then
							setcolor(red)
							append_content("\\relax ")
						else
							setcolor(yellow)
							append_content("\\")
							for _, c in utf8.codes(t.csname) do
								print_term_char(c)
							end
							append_content(" ")
						end
						check_wrap()
					end
				end
				
			end
		end

		for i=1, select("#", ...) do
			prettyprint_term_one_arg(args[i])
		end

		setcolor(white)

		typeout_content()
	end
end

function prettyprintw()
	local callback=token.scan_toks()
	local extra=token.scan_toks()
	local s={}
	local last_token
	while true do
		last_token=token.get_next()
		if last_token.csname=="prettystop" then break end
		s[#s+1]=last_token
		callback[#callback+1]=last_token
	end

	prettyprint(extra, s)  -- exclude the \prettystop token
	callback[#callback+1]=last_token
	token.put_next(callback)
end
end