| 1 |
--- This file (`nodetree.lua`) is part of the LuaTeX package |
| 2 |
--- 'nodetree'. |
| 3 |
--- |
| 4 |
---`nodetree` uses the annotation system of the |
| 5 |
---[lua-language-server](https://github.com/LuaLS/lua-language-server/wiki/Annotations). |
| 6 |
---Install the [type definitions for LuaTeX](https://github.com/Josef-Friedrich/LuaTeX_Lua-API) |
| 7 |
---or the [Visual Studio Code Extension](https://marketplace.visualstudio.com/items?itemName=JosefFriedrich.luatex). |
| 8 |
--- |
| 9 |
---The LDoc support is deprecated. |
| 10 |
--- |
| 11 |
---Nodes in LuaTeX are connected. The nodetree view distinguishes |
| 12 |
---between *list* and *field* connections. |
| 13 |
--- |
| 14 |
---* list: Nodes that are doubly connected by `next` and `previous` |
| 15 |
--- fields. |
| 16 |
---* field: Connections to nodes by other fields than `next` and |
| 17 |
--- `previous` fields, e.g., `head`, `pre`. |
| 18 |
---@module nodetree |
| 19 |
|
| 20 |
---luacheck: globals node lang tex luatexbase lfs |
| 21 |
---luacheck: globals callback os unicode status modules |
| 22 |
|
| 23 |
--- |
| 24 |
---@alias ColorName 'black'|'red' |'green'|'yellow'|'blue'|'magenta'|'cyan'|'white'|'reset' |
| 25 |
---@alias ColorMode 'bright'|'dim'|'' |
| 26 |
--- |
| 27 |
---@alias ConnectionType 'list'|'field' # A string literal, |
| 28 |
--- which can be either 'list' or 'field'. |
| 29 |
---@alias ConnectionState 'stop'|'continue' # A literal, which can |
| 30 |
--- be either `continue` or `stop`. |
| 31 |
|
| 32 |
if not modules then modules = {} end modules ['nodetree'] = { |
| 33 |
version = '2.4.1', |
| 34 |
comment = 'nodetree', |
| 35 |
author = 'Josef Friedrich', |
| 36 |
copyright = 'Josef Friedrich', |
| 37 |
license = 'The LaTeX Project Public License Version 1.3c 2008-05-04' |
| 38 |
} |
| 39 |
|
| 40 |
local direct = node.direct |
| 41 |
local todirect = direct.todirect |
| 42 |
local getchar = direct.getchar |
| 43 |
---Lua 5.1 does not have the utf8 library (Lua 5.1 is the default |
| 44 |
---version in LuajitTeX). LuaJitTeX does include the slnunicode library. |
| 45 |
local utf8 = utf8 or unicode.utf8 |
| 46 |
local utfchar = utf8.char |
| 47 |
local properties = direct.get_properties_table() |
| 48 |
|
| 49 |
---A counter for the compiled TeX examples. Some TeX code snippets |
| 50 |
---a written into files, wrapped with some TeX boilerplate code. |
| 51 |
---These written files are compiled later on. |
| 52 |
local example_counter = 0 |
| 53 |
|
| 54 |
---A flag to indicate that something has been emitted by nodetree. |
| 55 |
local have_output = false |
| 56 |
|
| 57 |
--- The default options. |
| 58 |
local default_options = { |
| 59 |
callback = 'post_linebreak_filter', |
| 60 |
channel = 'term', |
| 61 |
color = 'colored', |
| 62 |
decimalplaces = 2, |
| 63 |
unit = 'pt', |
| 64 |
verbosity = 0, |
| 65 |
firstline = 1, |
| 66 |
lastline = -1, |
| 67 |
} |
| 68 |
|
| 69 |
--- The current options. |
| 70 |
local options = {} |
| 71 |
for key, value in pairs(default_options) do |
| 72 |
options[key] = value |
| 73 |
end |
| 74 |
|
| 75 |
--- The previous options. |
| 76 |
---We need this for functions `push_options` and `pop_options` so that |
| 77 |
---the effects of the `\setkeys` commands in `nodetree-embed.sty` |
| 78 |
---(which directly manipulates the `options` table) stay local. |
| 79 |
local prev_options = {} |
| 80 |
local option_level = 0 |
| 81 |
|
| 82 |
---File descriptor. |
| 83 |
local output_file |
| 84 |
|
| 85 |
--- The state values of the current tree item. |
| 86 |
--- |
| 87 |
---`tree_state`: |
| 88 |
--- |
| 89 |
---* `1` (level): |
| 90 |
--- * `list`: `continue` |
| 91 |
--- * `field`: `stop` |
| 92 |
---* `2`: |
| 93 |
--- * `list`: `continue` |
| 94 |
--- * `field`: `stop` |
| 95 |
--- |
| 96 |
---... |
| 97 |
local tree_state = {} |
| 98 |
|
| 99 |
--- Format functions. |
| 100 |
--- |
| 101 |
---Low-level template functions. |
| 102 |
--- |
| 103 |
---@section format |
| 104 |
|
| 105 |
local format = { |
| 106 |
---@function format.underscore |
| 107 |
--- |
| 108 |
---@param input string |
| 109 |
--- |
| 110 |
---@return string |
| 111 |
underscore = function(input) |
| 112 |
if options.channel == 'tex' then |
| 113 |
local result = input.gsub(input, '_', '\\_') |
| 114 |
return result |
| 115 |
else |
| 116 |
return input |
| 117 |
end |
| 118 |
end, |
| 119 |
|
| 120 |
---@function format.escape |
| 121 |
--- |
| 122 |
---@param input string |
| 123 |
--- |
| 124 |
---@return string |
| 125 |
escape = function(input) |
| 126 |
if options.channel == 'tex' then |
| 127 |
local result = input.gsub(input, [[\]], [[\string\]]) |
| 128 |
return result |
| 129 |
else |
| 130 |
return input |
| 131 |
end |
| 132 |
end, |
| 133 |
|
| 134 |
---@function format.function |
| 135 |
--- |
| 136 |
---@param input number |
| 137 |
--- |
| 138 |
---@return number |
| 139 |
number = function(input) |
| 140 |
local mult = 10^(options.decimalplaces or 0) |
| 141 |
return math.floor(input * mult + 0.5) / mult |
| 142 |
end, |
| 143 |
|
| 144 |
---@function format.whitespace |
| 145 |
--- |
| 146 |
---@param count? number # How many spaces should be output. |
| 147 |
--- |
| 148 |
---@return string |
| 149 |
whitespace = function(count) |
| 150 |
local whitespace |
| 151 |
local output = '' |
| 152 |
if options.channel == 'tex' then |
| 153 |
whitespace = '\\hspace{0.5em}' |
| 154 |
else |
| 155 |
whitespace = ' ' |
| 156 |
end |
| 157 |
if not count then |
| 158 |
count = 1 |
| 159 |
end |
| 160 |
for _ = 1, count do |
| 161 |
output = output .. whitespace |
| 162 |
end |
| 163 |
return output |
| 164 |
end, |
| 165 |
|
| 166 |
---@function format.color_code |
| 167 |
--- |
| 168 |
---@param code number |
| 169 |
--- |
| 170 |
---@return string |
| 171 |
color_code = function(code) |
| 172 |
return string.char(27) .. '[' .. tostring(code) .. 'm' |
| 173 |
end, |
| 174 |
|
| 175 |
---@function format.color_tex |
| 176 |
--- |
| 177 |
---@param color string |
| 178 |
---@param mode? string |
| 179 |
--- |
| 180 |
---@return string |
| 181 |
color_tex = function(color, mode) |
| 182 |
if not mode then mode = '' end |
| 183 |
return 'NTE' .. color .. mode |
| 184 |
end, |
| 185 |
|
| 186 |
---@function format.node_begin |
| 187 |
--- |
| 188 |
---@return string |
| 189 |
node_begin = function() |
| 190 |
if options.channel == 'tex' then |
| 191 |
return '\\mbox{' |
| 192 |
else |
| 193 |
return '' |
| 194 |
end |
| 195 |
end, |
| 196 |
|
| 197 |
---@function format.node_end |
| 198 |
--- |
| 199 |
---@return string |
| 200 |
node_end = function() |
| 201 |
if options.channel == 'tex' then |
| 202 |
return '}' |
| 203 |
else |
| 204 |
return '' |
| 205 |
end |
| 206 |
end, |
| 207 |
|
| 208 |
---@function format.new_line |
| 209 |
--- |
| 210 |
---@param count? number # How many new lines should be output. |
| 211 |
--- |
| 212 |
---@return string |
| 213 |
new_line = function(count) |
| 214 |
local output = '' |
| 215 |
if not count then |
| 216 |
count = 1 |
| 217 |
end |
| 218 |
local new_line |
| 219 |
if options.channel == 'tex' then |
| 220 |
new_line = '\\par\n' |
| 221 |
else |
| 222 |
new_line = '\n' |
| 223 |
end |
| 224 |
|
| 225 |
for _ = 1, count do |
| 226 |
output = output .. new_line |
| 227 |
end |
| 228 |
return output |
| 229 |
end, |
| 230 |
|
| 231 |
---@function format.type_id |
| 232 |
--- |
| 233 |
---@param id number |
| 234 |
--- |
| 235 |
---@return string |
| 236 |
type_id = function(id) |
| 237 |
return '[' .. tostring(id) .. ']' |
| 238 |
end |
| 239 |
} |
| 240 |
|
| 241 |
--- Print the output to stdout or write it into a file (`output_file`). |
| 242 |
---New text is appended. |
| 243 |
--- |
| 244 |
---@param text string # A text string. |
| 245 |
local function nodetree_print(text) |
| 246 |
if options.channel == 'log' or options.channel == 'tex' then |
| 247 |
output_file:write(text) |
| 248 |
else |
| 249 |
io.write(text) |
| 250 |
end |
| 251 |
end |
| 252 |
|
| 253 |
--- Template functions. |
| 254 |
--- |
| 255 |
---@section template |
| 256 |
|
| 257 |
local template = { |
| 258 |
node_colors = { |
| 259 |
hlist = {'red', 'bright'}, |
| 260 |
vlist = {'green', 'bright'}, |
| 261 |
rule = {'blue', 'bright'}, |
| 262 |
ins = {'blue'}, |
| 263 |
mark = {'magenta'}, |
| 264 |
adjust = {'cyan'}, |
| 265 |
boundary = {'red', 'bright'}, |
| 266 |
disc = {'green', 'bright'}, |
| 267 |
whatsit = {'yellow', 'bright'}, |
| 268 |
local_par = {'blue', 'bright'}, |
| 269 |
dir = {'magenta', 'bright'}, |
| 270 |
math = {'cyan', 'bright'}, |
| 271 |
glue = {'magenta', 'bright'}, |
| 272 |
kern = {'green', 'bright'}, |
| 273 |
penalty = {'yellow', 'bright'}, |
| 274 |
unset = {'blue'}, |
| 275 |
style = {'magenta'}, |
| 276 |
choice = {'cyan'}, |
| 277 |
noad = {'red'}, |
| 278 |
radical = {'green'}, |
| 279 |
fraction = {'yellow'}, |
| 280 |
accent = {'blue'}, |
| 281 |
fence = {'magenta'}, |
| 282 |
math_char = {'cyan'}, |
| 283 |
sub_box = {'red', 'bright'}, |
| 284 |
sub_mlist = {'green', 'bright'}, |
| 285 |
math_text_char = {'yellow', 'bright'}, |
| 286 |
delim = {'blue', 'bright'}, |
| 287 |
margin_kern = {'magenta', 'bright'}, |
| 288 |
glyph = {'cyan', 'bright'}, |
| 289 |
align_record = {'red'}, |
| 290 |
pseudo_file = {'green'}, |
| 291 |
pseudo_line = {'yellow'}, |
| 292 |
page_insert = {'blue'}, |
| 293 |
split_insert = {'magenta'}, |
| 294 |
expr_stack = {'cyan'}, |
| 295 |
nested_list = {'red'}, |
| 296 |
span = {'green'}, |
| 297 |
attribute = {'yellow'}, |
| 298 |
glue_spec = {'magenta'}, |
| 299 |
attribute_list = {'cyan'}, |
| 300 |
temp = {'magenta'}, |
| 301 |
align_stack = {'red', 'bright'}, |
| 302 |
movement_stack = {'green', 'bright'}, |
| 303 |
if_stack = {'yellow', 'bright'}, |
| 304 |
unhyphenated = {'magenta', 'bright'}, |
| 305 |
hyphenated = {'cyan', 'bright'}, |
| 306 |
delta = {'red'}, |
| 307 |
passive = {'green'}, |
| 308 |
shape = {'yellow'}, |
| 309 |
}, |
| 310 |
|
| 311 |
-- Field name abbreviations for verbosity level 0. A second field |
| 312 |
-- limits the abbreviation to this node type. |
| 313 |
-- |
| 314 |
-- Entry '' means to omit the key, printing only the value. Entry |
| 315 |
-- '()' means the same, but the value gets printed in parentheses. |
| 316 |
field_abbrevs = { |
| 317 |
char = {''}, |
| 318 |
depth = {'dp'}, |
| 319 |
dir = {'()', 'dir'}, |
| 320 |
height = {'ht'}, |
| 321 |
kern = {''}, |
| 322 |
mark = {''}, |
| 323 |
penalty = {'', 'penalty'}, |
| 324 |
shrink = {'minus'}, |
| 325 |
stretch = {'plus'}, |
| 326 |
style = {''}, |
| 327 |
subtype = {'()'}, |
| 328 |
width = {'wd'}, |
| 329 |
}, |
| 330 |
|
| 331 |
--- [SGR (Select Graphic Rendition) |
| 332 |
-- parameters](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters). |
| 333 |
-- |
| 334 |
-- __attributes__ |
| 335 |
-- |
| 336 |
-- | color |code| |
| 337 |
-- |------------|----| |
| 338 |
-- | reset | 0 | |
| 339 |
-- | clear | 0 | |
| 340 |
-- | bright | 1 | |
| 341 |
-- | dim | 2 | |
| 342 |
-- | underscore | 4 | |
| 343 |
-- | blink | 5 | |
| 344 |
-- | reverse | 7 | |
| 345 |
-- | hidden | 8 | |
| 346 |
-- |
| 347 |
-- __foreground__ |
| 348 |
-- |
| 349 |
-- | color |code| |
| 350 |
-- |------------|----| |
| 351 |
-- | black | 30 | |
| 352 |
-- | red | 31 | |
| 353 |
-- | green | 32 | |
| 354 |
-- | yellow | 33 | |
| 355 |
-- | blue | 34 | |
| 356 |
-- | magenta | 35 | |
| 357 |
-- | cyan | 36 | |
| 358 |
-- | white | 37 | |
| 359 |
-- |
| 360 |
-- __background__ |
| 361 |
-- |
| 362 |
-- | color |code| |
| 363 |
-- |------------|----| |
| 364 |
-- | onblack | 40 | |
| 365 |
-- | onred | 41 | |
| 366 |
-- | ongreen | 42 | |
| 367 |
-- | onyellow | 43 | |
| 368 |
-- | onblue | 44 | |
| 369 |
-- | onmagenta | 45 | |
| 370 |
-- | oncyan | 46 | |
| 371 |
-- | onwhite | 47 | |
| 372 |
-- |
| 373 |
---@function template.color |
| 374 |
--- |
| 375 |
---@param color ColorName # A color name. |
| 376 |
---@param mode? ColorMode |
| 377 |
---@param background? boolean # If set, colorize the background instead of the text. |
| 378 |
--- |
| 379 |
---@return string |
| 380 |
color = function(color, mode, background) |
| 381 |
if options.color ~= 'colored' then |
| 382 |
return '' |
| 383 |
end |
| 384 |
|
| 385 |
local output = '' |
| 386 |
local code |
| 387 |
|
| 388 |
if mode == 'bright' then |
| 389 |
output = format.color_code(1) |
| 390 |
elseif mode == 'dim' then |
| 391 |
output = format.color_code(2) |
| 392 |
end |
| 393 |
|
| 394 |
if not background then |
| 395 |
if color == 'reset' then code = 0 |
| 396 |
elseif color == 'red' then code = 31 |
| 397 |
elseif color == 'green' then code = 32 |
| 398 |
elseif color == 'yellow' then code = 33 |
| 399 |
elseif color == 'blue' then code = 34 |
| 400 |
elseif color == 'magenta' then code = 35 |
| 401 |
elseif color == 'cyan' then code = 36 |
| 402 |
else code = 37 end |
| 403 |
else |
| 404 |
if color == 'black' then code = 40 |
| 405 |
elseif color == 'red' then code = 41 |
| 406 |
elseif color == 'green' then code = 42 |
| 407 |
elseif color == 'yellow' then code = 43 |
| 408 |
elseif color == 'blue' then code = 44 |
| 409 |
elseif color == 'magenta' then code = 45 |
| 410 |
elseif color == 'cyan' then code = 46 |
| 411 |
elseif color == 'white' then code = 47 |
| 412 |
else code = 40 end |
| 413 |
end |
| 414 |
return output .. format.color_code(code) |
| 415 |
end, |
| 416 |
|
| 417 |
--- Format the char field of a node. Try to find a textual representation that |
| 418 |
-- corresponds with the number stored in the `char` field. |
| 419 |
-- |
| 420 |
-- LuaTeX’s `node.char` values are not really characters; they are font glyph indices |
| 421 |
-- which sometimes happen to match valid Unicode characters. HarfBuzz shapers |
| 422 |
-- differentiate between glyph IDs and characters by adding to 0x120000 to |
| 423 |
-- glyph IDs. |
| 424 |
-- |
| 425 |
-- The code of this function is borrowed from the [function |
| 426 |
-- `get_glyph_info(n)`](https://github.com/latex3/luaotfload/blob/4c09fe264c1644792d95182280be259449e7da02/src/luaotfload-harf-plug.lua#L1018-L1031) |
| 427 |
-- of the luaotfload package. The harfbuzz mode in luaotfload uses this |
| 428 |
-- function to embed text in a PDF file and for messages that show textual |
| 429 |
-- representation of the nodes like over/underfull messages. It will not |
| 430 |
-- result in an error in other modes, but it might not give proper text |
| 431 |
-- representation, but that is a limitation of these modes. |
| 432 |
-- |
| 433 |
-- It should be understood what the glyph nodes represent. Before |
| 434 |
-- processing by luaotfload they represent one-to-one mapping of the |
| 435 |
-- input characters. After processing, they represent font glyphs with |
| 436 |
-- potentially complicated relationship with input characters. |
| 437 |
-- |
| 438 |
-- Relation between input characters and output glyphs are many-to-many. |
| 439 |
-- An input character may be represented by one or more glyphs, and |
| 440 |
-- output glyph might represent one or more input characters, and in |
| 441 |
-- some cases (e.g. when there is reordering) a group of input |
| 442 |
-- characters are represented by a group of output glyphs. In the 2nd |
| 443 |
-- and 3rd cases, the first glyph node will have a `glyph_info` property |
| 444 |
-- with all the characters of the group, and subsequent glyph nodes in |
| 445 |
-- the group will have empty `glyph_info` properties. |
| 446 |
-- |
| 447 |
-- It should also noted that this mapping is not unique, the same glyph |
| 448 |
-- can represent different characters in different context. |
| 449 |
-- |
| 450 |
---@function template.char |
| 451 |
-- |
| 452 |
---@param head Node # The head node of a node list. |
| 453 |
--- |
| 454 |
---@return string # A textual representation of the `char` number. |
| 455 |
char = function(head) |
| 456 |
local node_id = todirect(head) -- Convert to node id. |
| 457 |
local props = properties[node_id] |
| 458 |
local info = props and props.glyph_info |
| 459 |
local textual |
| 460 |
local character_index = getchar(node_id) |
| 461 |
|
| 462 |
if info then |
| 463 |
textual = info |
| 464 |
elseif character_index == 0 then |
| 465 |
textual = '^^@' |
| 466 |
elseif character_index <= 31 or (character_index >= 127 and character_index <= 159) then |
| 467 |
-- The C0 range [c-zero] contains characters from U+0000 to U+001F |
| 468 |
-- (decimal 0-31) and U+007F (decimal 127), the C1 range covers |
| 469 |
-- characters from U+0080 to U+009F (decimal 128-159). |
| 470 |
textual = '???' |
| 471 |
elseif character_index ~= nil and character_index < 0x110000 then |
| 472 |
textual = utfchar(character_index) |
| 473 |
else |
| 474 |
textual = string.format('^^^^^^%06X', character_index) |
| 475 |
end |
| 476 |
|
| 477 |
if options.verbosity == 0 then |
| 478 |
if textual == '???' then |
| 479 |
return character_index |
| 480 |
else |
| 481 |
return "'" .. textual .. "'" |
| 482 |
end |
| 483 |
elseif options.verbosity <= 2 then |
| 484 |
return character_index .. " ('" .. textual .. "')" |
| 485 |
else |
| 486 |
return character_index |
| 487 |
.. ' (' |
| 488 |
.. string.format('0x%x', character_index) |
| 489 |
.. ", '" |
| 490 |
.. textual |
| 491 |
.. "')" |
| 492 |
end |
| 493 |
end, |
| 494 |
|
| 495 |
---@function template.line |
| 496 |
--- |
| 497 |
---@param length string # If `long`, emit a longer line. |
| 498 |
--- |
| 499 |
---@return string |
| 500 |
line = function(length) |
| 501 |
local output |
| 502 |
if length == 'long' then |
| 503 |
output = '------------------------------------------' |
| 504 |
else |
| 505 |
output = '-----------------------' |
| 506 |
end |
| 507 |
return output .. format.new_line() |
| 508 |
end, |
| 509 |
|
| 510 |
---@function template.branch |
| 511 |
--- |
| 512 |
---@param connection_type ConnectionType |
| 513 |
---@param connection_state ConnectionState |
| 514 |
---@param last boolean |
| 515 |
--- |
| 516 |
---@return string |
| 517 |
branch = function(connection_type, connection_state, last) |
| 518 |
local c = connection_type |
| 519 |
local s = connection_state |
| 520 |
local l = last |
| 521 |
if c == 'list' and s == 'stop' and l == false then |
| 522 |
return format.whitespace(2) |
| 523 |
elseif c == 'field' and s == 'stop' and l == false then |
| 524 |
return format.whitespace(2) |
| 525 |
elseif c == 'list' and s == 'continue' and l == false then |
| 526 |
return '│' .. format.whitespace() |
| 527 |
elseif c == 'field' and s == 'continue' and l == false then |
| 528 |
return '║' .. format.whitespace() |
| 529 |
elseif c == 'list' and s == 'continue' and l == true then |
| 530 |
return '├─' |
| 531 |
elseif c == 'field' and s == 'continue' and l == true then |
| 532 |
return '╠═' |
| 533 |
elseif c == 'list' and s == 'stop' and l == true then |
| 534 |
return '└─' |
| 535 |
elseif c == 'field' and s == 'stop' and l == true then |
| 536 |
return '╚═' |
| 537 |
end |
| 538 |
return '' |
| 539 |
end, |
| 540 |
} |
| 541 |
|
| 542 |
---@param number number |
| 543 |
---@param order number |
| 544 |
---@param field string |
| 545 |
--- |
| 546 |
---@return string |
| 547 |
function template.fill(number, order, field) |
| 548 |
local output |
| 549 |
if order ~= nil and order ~= 0 then |
| 550 |
if field == 'stretch' then |
| 551 |
output = '+' |
| 552 |
else |
| 553 |
output = '-' |
| 554 |
end |
| 555 |
return output .. string.format( |
| 556 |
'%g%s', number / 2^16, |
| 557 |
template.colored_string( |
| 558 |
'fi' .. string.rep('l', order - 1), |
| 559 |
'white', |
| 560 |
'dim' |
| 561 |
) |
| 562 |
) |
| 563 |
else |
| 564 |
return template.length(number) |
| 565 |
end |
| 566 |
end |
| 567 |
|
| 568 |
--- Colorize a text string. |
| 569 |
--- |
| 570 |
---@param text string # A text string. |
| 571 |
---@param color ColorName # A color name. |
| 572 |
---@param mode? ColorMode |
| 573 |
---@param background? boolean # If set, colorize the background instead of the text. |
| 574 |
--- |
| 575 |
---@return string |
| 576 |
function template.colored_string(text, color, mode, background) |
| 577 |
if options.channel == 'tex' then |
| 578 |
if mode == 'dim' then |
| 579 |
mode = '' |
| 580 |
end |
| 581 |
return '\\textcolor{' .. |
| 582 |
format.color_tex(color, mode) .. |
| 583 |
'}{' .. |
| 584 |
text .. |
| 585 |
'}' |
| 586 |
else |
| 587 |
return template.color(color, mode, background) .. text .. template.color('reset') |
| 588 |
end |
| 589 |
end |
| 590 |
|
| 591 |
--- Format a scaled point input value into dimension string (`12pt`, |
| 592 |
--- `1cm`) |
| 593 |
--- |
| 594 |
---@param input number |
| 595 |
--- |
| 596 |
---@return string |
| 597 |
function template.length(input) |
| 598 |
local i = tonumber(input) |
| 599 |
if i ~= nil then |
| 600 |
input = i / tex.sp('1' .. options.unit) |
| 601 |
end |
| 602 |
return string.format( |
| 603 |
'%g%s', |
| 604 |
format.number(input), |
| 605 |
template.colored_string(options.unit, 'white', 'dim') |
| 606 |
) |
| 607 |
end |
| 608 |
|
| 609 |
--- Get all data from a table including metatables. |
| 610 |
--- |
| 611 |
---Properties will reside in a metatable if nodes were copied using an |
| 612 |
---operation like box copy: (\copy). The LuaTeX manual states this: “If |
| 613 |
---the second argument of `set_properties_mode` is true, then a |
| 614 |
---metatable approach is chosen: the copy gets its own table with the |
| 615 |
---original table as metatable.” |
| 616 |
--- |
| 617 |
---Source: [StackOverflow](https://stackoverflow.com/a/5639667) – this |
| 618 |
---works if `__index` returns a table, which it should as per LuaTeX |
| 619 |
---manual. |
| 620 |
--- |
| 621 |
---@param data table # A Lua table. |
| 622 |
---@param previous_data? table # The data of a Lua table of a previous recursive call. |
| 623 |
--- |
| 624 |
---@return table # A merged table. |
| 625 |
local function get_all_table_data(data, previous_data) |
| 626 |
-- If previous_data is nil, start empty, otherwise start with previous_data. |
| 627 |
local output = previous_data or {} |
| 628 |
|
| 629 |
-- Copy all the attributes from the table. |
| 630 |
for key, value in pairs(data) do |
| 631 |
output[key] = output[key] or value |
| 632 |
end |
| 633 |
|
| 634 |
-- Get table’s metatable, or exit if not existing. |
| 635 |
local metatable = getmetatable(data) |
| 636 |
if type(metatable) ~= 'table' then |
| 637 |
return output |
| 638 |
end |
| 639 |
|
| 640 |
-- Get the `__index` from metatable, or exit if not table. |
| 641 |
local index = metatable.__index |
| 642 |
if type(index) ~= 'table' then |
| 643 |
return output |
| 644 |
end |
| 645 |
|
| 646 |
-- Include the data from index into data recursively and return. |
| 647 |
return get_all_table_data(index, output) |
| 648 |
end |
| 649 |
|
| 650 |
--- Convert a Lua table into a format string. |
| 651 |
--- |
| 652 |
---@param tbl table # A table to generate an inline view of. |
| 653 |
--- |
| 654 |
---@return string |
| 655 |
function template.table_inline(tbl) |
| 656 |
local tex_escape = '' |
| 657 |
if options.channel == 'tex' then |
| 658 |
tex_escape = '\\' |
| 659 |
end |
| 660 |
if type(tbl) == 'table' then |
| 661 |
tbl = get_all_table_data(tbl) |
| 662 |
local output = tex_escape .. '{' |
| 663 |
local kv_list = '' |
| 664 |
local keys = {} |
| 665 |
for key in pairs(tbl) do |
| 666 |
keys[#keys + 1] = key |
| 667 |
end |
| 668 |
table.sort(keys) |
| 669 |
for i = 1, #keys do |
| 670 |
local key = keys[i] |
| 671 |
local value = tbl[key] |
| 672 |
if type(key) ~= 'numbers' then |
| 673 |
key = '\'' .. |
| 674 |
template.colored_string(key, 'cyan', 'dim') .. '\'' |
| 675 |
end |
| 676 |
kv_list = kv_list .. '[' .. key .. '] = ' .. |
| 677 |
template.table_inline(value) .. ', ' |
| 678 |
end |
| 679 |
output = output .. kv_list:gsub(', $', '') |
| 680 |
return output .. tex_escape .. '}' |
| 681 |
else |
| 682 |
return tostring(tbl) |
| 683 |
end |
| 684 |
end |
| 685 |
|
| 686 |
--- Format a key-value pair (`key: value, `). |
| 687 |
--- |
| 688 |
---@param key string # A key. |
| 689 |
---@param value string|number # A value. |
| 690 |
---@param typ? string # A node type. Had to be named typ to avoid conflict with the type() function. |
| 691 |
---@param color? ColorName # A color name. |
| 692 |
--- |
| 693 |
---@return string |
| 694 |
function template.key_value(key, value, typ, color) |
| 695 |
if type(color) ~= 'string' then |
| 696 |
color = 'yellow' |
| 697 |
end |
| 698 |
key = format.underscore(key) |
| 699 |
|
| 700 |
local output = '' |
| 701 |
local abbrev = nil |
| 702 |
local separator = ':' |
| 703 |
|
| 704 |
if options.verbosity == 0 then |
| 705 |
if template.field_abbrevs[key] then |
| 706 |
local only_this_type = template.field_abbrevs[key][2] |
| 707 |
if not only_this_type or not typ or only_this_type == typ then |
| 708 |
abbrev = template.field_abbrevs[key][1] |
| 709 |
end |
| 710 |
end |
| 711 |
separator = '' |
| 712 |
end |
| 713 |
|
| 714 |
if abbrev == nil then |
| 715 |
output = template.colored_string(key .. separator, color) |
| 716 |
elseif abbrev ~= '' and abbrev ~= '()' then |
| 717 |
output = template.colored_string(abbrev, color) |
| 718 |
end |
| 719 |
|
| 720 |
if value then |
| 721 |
if abbrev == '()' then |
| 722 |
-- Printing '(unused)' is confusing. |
| 723 |
if value ~= 'unused' then |
| 724 |
output = output .. '(' .. value .. ') ' |
| 725 |
end |
| 726 |
elseif abbrev == '' then |
| 727 |
output = output .. value .. ', ' |
| 728 |
else |
| 729 |
output = output .. ' ' .. value .. ', ' |
| 730 |
end |
| 731 |
end |
| 732 |
return output |
| 733 |
end |
| 734 |
|
| 735 |
---@param type string |
| 736 |
---@param id number |
| 737 |
--- |
| 738 |
---@return string |
| 739 |
function template.type(type, id) |
| 740 |
local output |
| 741 |
output = format.underscore(type) |
| 742 |
output = string.upper(output) |
| 743 |
if options.verbosity > 1 then |
| 744 |
output = output .. format.type_id(id) |
| 745 |
end |
| 746 |
return template.colored_string( |
| 747 |
output, |
| 748 |
template.node_colors[type][1], |
| 749 |
template.node_colors[type][2] |
| 750 |
) |
| 751 |
end |
| 752 |
|
| 753 |
---@param callback_name string |
| 754 |
---@param variables table|nil |
| 755 |
---@param where 'before'|'after' # `'before'` or `'after'` |
| 756 |
function template.callback(callback_name, variables, where) |
| 757 |
if options.channel == 'term' or have_output == true then |
| 758 |
nodetree_print(format.new_line(2)) |
| 759 |
end |
| 760 |
|
| 761 |
have_output = true |
| 762 |
|
| 763 |
nodetree_print( |
| 764 |
where .. ' callback ' .. |
| 765 |
template.colored_string(format.underscore(callback_name), 'red', '', true) .. |
| 766 |
format.new_line() |
| 767 |
) |
| 768 |
if variables then |
| 769 |
for name, value in pairs(variables) do |
| 770 |
if value ~= nil and value ~= '' then |
| 771 |
nodetree_print( |
| 772 |
'- ' .. |
| 773 |
format.underscore(name) .. |
| 774 |
': ' .. |
| 775 |
format.underscore(tostring(value)) .. |
| 776 |
format.new_line() |
| 777 |
) |
| 778 |
end |
| 779 |
end |
| 780 |
end |
| 781 |
nodetree_print(template.line('long')) |
| 782 |
end |
| 783 |
|
| 784 |
--- Format the branching tree for one output line. |
| 785 |
--- |
| 786 |
---@param level number |
| 787 |
---@param connection_type ConnectionType |
| 788 |
--- |
| 789 |
---@return string |
| 790 |
function template.branches(level, connection_type) |
| 791 |
local output = '' |
| 792 |
for i = 1, level - 1 do |
| 793 |
output = output .. template.branch('list', tree_state[i]['list'], false) |
| 794 |
output = output .. template.branch('field', tree_state[i]['field'], false) |
| 795 |
end |
| 796 |
---Format the last branches |
| 797 |
if connection_type == 'list' then |
| 798 |
output = output .. template.branch('list', tree_state[level]['list'], true) |
| 799 |
else |
| 800 |
output = output .. template.branch('list', tree_state[level]['list'], false) |
| 801 |
output = output .. template.branch('field', tree_state[level]['field'], true) |
| 802 |
end |
| 803 |
return output |
| 804 |
end |
| 805 |
|
| 806 |
--- Node library extensions. |
| 807 |
--- |
| 808 |
---@section node_extended |
| 809 |
|
| 810 |
local node_extended = {} |
| 811 |
|
| 812 |
--- Get the ID of a node. |
| 813 |
--- |
| 814 |
---We have to convert the node into a string and then to extract |
| 815 |
---the ID from this string using a regular expression. If you convert a |
| 816 |
---node into a string it looks like: `<node nil < 172 > nil : |
| 817 |
---hlist 2>`. |
| 818 |
--- |
| 819 |
---@param n Node # A node. |
| 820 |
--- |
| 821 |
---@return string |
| 822 |
function node_extended.node_id(n) |
| 823 |
local result = string.gsub(tostring(n), '^<node%s+%S+%s+<%s+(%d+).*', '%1') |
| 824 |
return result |
| 825 |
end |
| 826 |
|
| 827 |
--- A table of all node subtype names. |
| 828 |
--- |
| 829 |
---__Nodes without subtypes:__ |
| 830 |
--- |
| 831 |
---* `ins` (3) |
| 832 |
---* `local_par` (9) |
| 833 |
---* `penalty` (14) |
| 834 |
---* `unset` (15) |
| 835 |
---* `style` (16) |
| 836 |
---* `choice` (17) |
| 837 |
---* `fraction` (20) |
| 838 |
---* `math_char` (23) |
| 839 |
---* `sub_box` (24) |
| 840 |
---* `sub_mlist` (25) |
| 841 |
---* `math_text_char` (26) |
| 842 |
---* `delim` (27) |
| 843 |
---* `margin_kern` (28) |
| 844 |
---* `align_record` (30) |
| 845 |
---* `pseudo_file` (31) |
| 846 |
---* `pseudo_line` (32) |
| 847 |
---* `page_insert` (33) |
| 848 |
---* `split_insert` (34) |
| 849 |
---* `expr_stack` (35) |
| 850 |
---* `nested_list` (36) |
| 851 |
---* `span` (37) |
| 852 |
---* `attribute` (38) |
| 853 |
---* `glue_spec` (39) |
| 854 |
---* `attribute_list` (40) |
| 855 |
---* `temp` (41) |
| 856 |
---* `align_stack` (42) |
| 857 |
---* `movement_stack` (43) |
| 858 |
---* `if_stack` (44) |
| 859 |
---* `unhyphenated` (45) |
| 860 |
---* `hyphenated` (46) |
| 861 |
---* `delta` (47) |
| 862 |
---* `passive` (48) |
| 863 |
---* `shape` (49) |
| 864 |
--- |
| 865 |
---@return table |
| 866 |
local function get_node_subtypes() |
| 867 |
local subtypes = { |
| 868 |
-- hlist (0) |
| 869 |
hlist = { |
| 870 |
[0] = 'unknown', |
| 871 |
[1] = 'line', |
| 872 |
[2] = 'box', |
| 873 |
[3] = 'indent', |
| 874 |
[4] = 'alignment', |
| 875 |
[5] = 'cell', |
| 876 |
[6] = 'equation', |
| 877 |
[7] = 'equationnumber', |
| 878 |
[8] = 'math', |
| 879 |
[9] = 'mathchar', |
| 880 |
[10] = 'hextensible', |
| 881 |
[11] = 'vextensible', |
| 882 |
[12] = 'hdelimiter', |
| 883 |
[13] = 'vdelimiter', |
| 884 |
[14] = 'overdelimiter', |
| 885 |
[15] = 'underdelimiter', |
| 886 |
[16] = 'numerator', |
| 887 |
[17] = 'denominator', |
| 888 |
[18] = 'limits', |
| 889 |
[19] = 'fraction', |
| 890 |
[20] = 'nucleus', |
| 891 |
[21] = 'sup', |
| 892 |
[22] = 'sub', |
| 893 |
[23] = 'degree', |
| 894 |
[24] = 'scripts', |
| 895 |
[25] = 'over', |
| 896 |
[26] = 'under', |
| 897 |
[27] = 'accent', |
| 898 |
[28] = 'radical', |
| 899 |
}, |
| 900 |
-- vlist (1) |
| 901 |
vlist = { |
| 902 |
[0] = 'unknown', |
| 903 |
[4] = 'alignment', |
| 904 |
[5] = 'cell', |
| 905 |
}, |
| 906 |
-- rule (2) |
| 907 |
rule = { |
| 908 |
[0] = 'normal', |
| 909 |
[1] = 'box', |
| 910 |
[2] = 'image', |
| 911 |
[3] = 'empty', |
| 912 |
[4] = 'user', |
| 913 |
[5] = 'over', |
| 914 |
[6] = 'under', |
| 915 |
[7] = 'fraction', |
| 916 |
[8] = 'radical', |
| 917 |
[9] = 'outline', |
| 918 |
}, |
| 919 |
-- mark (4) |
| 920 |
-- The subtype is not used. |
| 921 |
mark = { |
| 922 |
[0] = 'unused', |
| 923 |
}, |
| 924 |
-- adjust (5) |
| 925 |
adjust = { |
| 926 |
[0] = 'normal', |
| 927 |
[1] = 'pre', |
| 928 |
}, |
| 929 |
-- boundary (6) |
| 930 |
boundary = { |
| 931 |
[0] = 'cancel', |
| 932 |
[1] = 'user', |
| 933 |
[2] = 'protrusion', |
| 934 |
[3] = 'word', |
| 935 |
}, |
| 936 |
-- disc (7) |
| 937 |
disc = { |
| 938 |
[0] = 'discretionary', |
| 939 |
[1] = 'explicit', |
| 940 |
[2] = 'automatic', |
| 941 |
[3] = 'regular', |
| 942 |
[4] = 'first', |
| 943 |
[5] = 'second', |
| 944 |
}, |
| 945 |
-- dir (10) |
| 946 |
-- This is an internal detail, see luatex source code file |
| 947 |
-- `texnodes.h`. |
| 948 |
-- dir = { |
| 949 |
-- [0] = 'normal_dir', |
| 950 |
-- [1] = 'cancel_dir', |
| 951 |
-- }, |
| 952 |
-- math (11) |
| 953 |
math = { |
| 954 |
[0] = 'beginmath', |
| 955 |
[1] = 'endmath', |
| 956 |
}, |
| 957 |
-- glue (12) |
| 958 |
glue = { |
| 959 |
[0] = 'userskip', |
| 960 |
[1] = 'lineskip', |
| 961 |
[2] = 'baselineskip', |
| 962 |
[3] = 'parskip', |
| 963 |
[4] = 'abovedisplayskip', |
| 964 |
[5] = 'belowdisplayskip', |
| 965 |
[6] = 'abovedisplayshortskip', |
| 966 |
[7] = 'belowdisplayshortskip', |
| 967 |
[8] = 'leftskip', |
| 968 |
[9] = 'rightskip', |
| 969 |
[10] = 'topskip', |
| 970 |
[11] = 'splittopskip', |
| 971 |
[12] = 'tabskip', |
| 972 |
[13] = 'spaceskip', |
| 973 |
[14] = 'xspaceskip', |
| 974 |
[15] = 'parfillskip', |
| 975 |
[16] = 'mathskip', |
| 976 |
[17] = 'thinmuskip', |
| 977 |
[18] = 'medmuskip', |
| 978 |
[19] = 'thickmuskip', |
| 979 |
[98] = 'conditionalmathskip', |
| 980 |
[99] = 'muglue', |
| 981 |
[100] = 'leaders', |
| 982 |
[101] = 'cleaders', |
| 983 |
[102] = 'xleaders', |
| 984 |
[103] = 'gleaders', |
| 985 |
}, |
| 986 |
-- kern (13) |
| 987 |
kern = { |
| 988 |
[0] = 'fontkern', |
| 989 |
[1] = 'userkern', |
| 990 |
[2] = 'accentkern', |
| 991 |
[3] = 'italiccorrection', |
| 992 |
}, |
| 993 |
-- penalty (14) |
| 994 |
penalty = { |
| 995 |
[0] = 'userpenalty', |
| 996 |
[1] = 'linebreakpenalty', |
| 997 |
[2] = 'linepenalty', |
| 998 |
[3] = 'wordpenalty', |
| 999 |
[4] = 'finalpenalty', |
| 1000 |
[5] = 'noadpenalty', |
| 1001 |
[6] = 'beforedisplaypenalty', |
| 1002 |
[7] = 'afterdisplaypenalty', |
| 1003 |
[8] = 'equationnumberpenalty', |
| 1004 |
}, |
| 1005 |
noad = { |
| 1006 |
[0] = 'ord', |
| 1007 |
[1] = 'opdisplaylimits', |
| 1008 |
[2] = 'oplimits', |
| 1009 |
[3] = 'opnolimits', |
| 1010 |
[4] = 'bin', |
| 1011 |
[5] = 'rel', |
| 1012 |
[6] = 'open', |
| 1013 |
[7] = 'close', |
| 1014 |
[8] = 'punct', |
| 1015 |
[9] = 'inner', |
| 1016 |
[10] = 'under', |
| 1017 |
[11] = 'over', |
| 1018 |
[12] = 'vcenter', |
| 1019 |
}, |
| 1020 |
-- radical (19) |
| 1021 |
radical = { |
| 1022 |
[0] = 'radical', |
| 1023 |
[1] = 'uradical', |
| 1024 |
[2] = 'uroot', |
| 1025 |
[3] = 'uunderdelimiter', |
| 1026 |
[4] = 'uoverdelimiter', |
| 1027 |
[5] = 'udelimiterunder', |
| 1028 |
[6] = 'udelimiterover', |
| 1029 |
}, |
| 1030 |
-- accent (21) |
| 1031 |
accent = { |
| 1032 |
[0] = 'bothflexible', |
| 1033 |
[1] = 'fixedtop', |
| 1034 |
[2] = 'fixedbottom', |
| 1035 |
[3] = 'fixedboth', |
| 1036 |
}, |
| 1037 |
-- fence (22) |
| 1038 |
fence = { |
| 1039 |
[0] = 'unset', |
| 1040 |
[1] = 'left', |
| 1041 |
[2] = 'middle', |
| 1042 |
[3] = 'right', |
| 1043 |
[4] = 'no', |
| 1044 |
}, |
| 1045 |
-- margin_kern (28) |
| 1046 |
margin_kern = { |
| 1047 |
[0] = 'left', |
| 1048 |
[1] = 'right', |
| 1049 |
}, |
| 1050 |
-- glyph (29) |
| 1051 |
-- The subtype for this node is a bit field, not an enumeration; |
| 1052 |
-- bit 0 gets handled separately. |
| 1053 |
glyph = { |
| 1054 |
[1] = 'ligature', |
| 1055 |
[2] = 'ghost', |
| 1056 |
[3] = 'left', |
| 1057 |
[4] = 'right', |
| 1058 |
}, |
| 1059 |
} |
| 1060 |
subtypes.whatsit = node.whatsits() |
| 1061 |
return subtypes |
| 1062 |
end |
| 1063 |
|
| 1064 |
---@param n Node |
| 1065 |
--- |
| 1066 |
---@return string |
| 1067 |
function node_extended.subtype(n) |
| 1068 |
local typ = node.type(n.id) |
| 1069 |
local subtypes = get_node_subtypes() |
| 1070 |
local output = '' |
| 1071 |
if subtypes[typ] then |
| 1072 |
if typ == 'glyph' then |
| 1073 |
-- Only handle the lowest five bits. |
| 1074 |
if n.subtype & 1 == 0 then |
| 1075 |
output = output .. 'glyph' |
| 1076 |
else |
| 1077 |
output = output .. 'character' |
| 1078 |
end |
| 1079 |
local mask = 2 |
| 1080 |
for i = 1,4,1 do |
| 1081 |
if n.subtype & mask ~= 0 then |
| 1082 |
output = output .. ' ' .. subtypes[typ][i] |
| 1083 |
end |
| 1084 |
mask = mask << 1 |
| 1085 |
end |
| 1086 |
else |
| 1087 |
if subtypes[typ][n.subtype] then |
| 1088 |
output = subtypes[typ][n.subtype] |
| 1089 |
else |
| 1090 |
return tostring(n.subtype) |
| 1091 |
end |
| 1092 |
end |
| 1093 |
|
| 1094 |
if options.verbosity > 1 then |
| 1095 |
output = output .. format.type_id(n.subtype) |
| 1096 |
end |
| 1097 |
return output |
| 1098 |
else |
| 1099 |
return tostring(n.subtype) |
| 1100 |
end |
| 1101 |
end |
| 1102 |
|
| 1103 |
--- Node tree building functions. |
| 1104 |
--- |
| 1105 |
---@section tree |
| 1106 |
|
| 1107 |
local tree = {} |
| 1108 |
|
| 1109 |
--- |
| 1110 |
---@param head Node # The head node of a node list. |
| 1111 |
---@param field string |
| 1112 |
--- |
| 1113 |
---@return string |
| 1114 |
function tree.format_field(head, field) |
| 1115 |
local output |
| 1116 |
local typ = node.type(head.id) |
| 1117 |
|
| 1118 |
-- Print subtypes also for nodes with ID=0. However, suppress the |
| 1119 |
-- internal 'subtype' field for 'dir' nodes. |
| 1120 |
if field == 'subtype' then |
| 1121 |
if typ == 'dir' then |
| 1122 |
return '' |
| 1123 |
elseif head[field] ~= nil then |
| 1124 |
return template.key_value(field, |
| 1125 |
format.underscore(node_extended.subtype(head))) |
| 1126 |
end |
| 1127 |
end |
| 1128 |
|
| 1129 |
-- Character 0 should be printed in a tree because the corresponding slot |
| 1130 |
-- zero in a TeX font usually contains a symbol. |
| 1131 |
if head[field] == nil or (head[field] == 0 and field ~= 'char') then |
| 1132 |
return '' |
| 1133 |
end |
| 1134 |
|
| 1135 |
if options.verbosity < 2 and |
| 1136 |
-- glyph |
| 1137 |
field == 'left' or |
| 1138 |
field == 'right' or |
| 1139 |
field == 'uchyph' or |
| 1140 |
-- hlist |
| 1141 |
-- Don't drop the 'dir' field of the 'dir' node. |
| 1142 |
(field == 'dir' and typ ~= 'dir') or |
| 1143 |
field == 'glue_order' or |
| 1144 |
field == 'glue_sign' or |
| 1145 |
field == 'glue_set' or |
| 1146 |
-- glue |
| 1147 |
field == 'stretch_order' then |
| 1148 |
return '' |
| 1149 |
elseif options.verbosity < 3 and |
| 1150 |
field == 'prev' or |
| 1151 |
field == 'next' or |
| 1152 |
field == 'id' then |
| 1153 |
return '' |
| 1154 |
end |
| 1155 |
|
| 1156 |
if field == 'prev' or field == 'next' then |
| 1157 |
output = node_extended.node_id(head[field]) |
| 1158 |
elseif |
| 1159 |
field == 'width' or |
| 1160 |
field == 'height' or |
| 1161 |
field == 'depth' or |
| 1162 |
field == 'kern' or |
| 1163 |
field == 'shift' then |
| 1164 |
output = template.length(head[field]) |
| 1165 |
elseif field == 'char' then |
| 1166 |
output = template.char(head) |
| 1167 |
elseif field == 'glue_set' then |
| 1168 |
output = format.number(head[field]) |
| 1169 |
elseif field == 'stretch' or field == 'shrink' then |
| 1170 |
output = template.fill(head[field], head[field .. '_order'], field) |
| 1171 |
else |
| 1172 |
-- Surround strings with single quotes except values of fields |
| 1173 |
-- that get potentially abbreviated (and thus don't really need |
| 1174 |
-- quotes). |
| 1175 |
if type(head[field]) == 'string' and not template.field_abbrevs[field] then |
| 1176 |
output = template.colored_string("'", 'yellow') .. |
| 1177 |
head[field] .. |
| 1178 |
template.colored_string("'", 'yellow') |
| 1179 |
elseif type(head[field]) == 'table' then |
| 1180 |
output = '<table>' |
| 1181 |
else |
| 1182 |
output = tostring(head[field]) |
| 1183 |
end |
| 1184 |
end |
| 1185 |
|
| 1186 |
return template.key_value(field, output, node.type(head.id)) |
| 1187 |
end |
| 1188 |
|
| 1189 |
--- |
| 1190 |
---Attributes are key-value number pairs. They are printed as an inline |
| 1191 |
---list. The attribute `0` with the value `0` is skipped because this |
| 1192 |
---attribute is in every node by default. |
| 1193 |
--- |
| 1194 |
---@param head Node # The head node of a node list. |
| 1195 |
--- |
| 1196 |
---@return string |
| 1197 |
function tree.format_attributes(head) |
| 1198 |
if not head then |
| 1199 |
return '' |
| 1200 |
end |
| 1201 |
local space = '' |
| 1202 |
local output = '' |
| 1203 |
local attr = head.next --[[@as AttributeNode]] |
| 1204 |
while attr do |
| 1205 |
if attr.number ~= 0 or (attr.number == 0 and attr.value ~= 0) then |
| 1206 |
output = output .. space .. tostring(attr.number) .. '=' .. tostring(attr.value) |
| 1207 |
space = ' ' |
| 1208 |
end |
| 1209 |
attr = attr.next --[[@as AttributeNode]] |
| 1210 |
end |
| 1211 |
return output |
| 1212 |
end |
| 1213 |
|
| 1214 |
--- |
| 1215 |
---@param level number # `level` is an integer beginning with 1. |
| 1216 |
---@param connection_type ConnectionType |
| 1217 |
---@param connection_state ConnectionState |
| 1218 |
function tree.set_state(level, connection_type, connection_state) |
| 1219 |
if not tree_state[level] then |
| 1220 |
tree_state[level] = {} |
| 1221 |
end |
| 1222 |
tree_state[level][connection_type] = connection_state |
| 1223 |
end |
| 1224 |
|
| 1225 |
--- |
| 1226 |
---@param fields table |
| 1227 |
---@param level number # The current recursion level. |
| 1228 |
function tree.analyze_fields(fields, level) |
| 1229 |
local max = 0 |
| 1230 |
local connection_state |
| 1231 |
for _ in pairs(fields) do |
| 1232 |
max = max + 1 |
| 1233 |
end |
| 1234 |
local count = 0 |
| 1235 |
for field_name, recursion_node in pairs(fields) do |
| 1236 |
count = count + 1 |
| 1237 |
if count == max then |
| 1238 |
connection_state = 'stop' |
| 1239 |
else |
| 1240 |
connection_state = 'continue' |
| 1241 |
end |
| 1242 |
tree.set_state(level, 'field', connection_state) |
| 1243 |
nodetree_print( |
| 1244 |
format.node_begin() .. |
| 1245 |
template.branches(level, 'field') .. |
| 1246 |
template.key_value(field_name) .. |
| 1247 |
format.node_end() .. |
| 1248 |
format.new_line() |
| 1249 |
) |
| 1250 |
tree.analyze_list(recursion_node, level + 1) |
| 1251 |
end |
| 1252 |
end |
| 1253 |
|
| 1254 |
--- |
| 1255 |
---@param head Node # The head node of a node list. |
| 1256 |
---@param level number # The current recursion level. |
| 1257 |
function tree.analyze_node(head, level) |
| 1258 |
local connection_state |
| 1259 |
local output |
| 1260 |
local need_whitespace = true |
| 1261 |
if head.next then |
| 1262 |
connection_state = 'continue' |
| 1263 |
else |
| 1264 |
connection_state = 'stop' |
| 1265 |
end |
| 1266 |
tree.set_state(level, 'list', connection_state) |
| 1267 |
output = template.branches(level, 'list') |
| 1268 |
.. template.type(node.type(head.id), head.id) |
| 1269 |
if options.verbosity > 1 then |
| 1270 |
output = output .. |
| 1271 |
format.whitespace() .. |
| 1272 |
template.key_value('no', node_extended.node_id(head)) |
| 1273 |
need_whitespace = false |
| 1274 |
end |
| 1275 |
|
| 1276 |
-- We store the attributes output so that we can append it to the field |
| 1277 |
-- list later on. |
| 1278 |
local attributes |
| 1279 |
|
| 1280 |
-- We store fields which are nodes for later treatment. |
| 1281 |
local fields = {} |
| 1282 |
|
| 1283 |
-- Inline fields, for example: char: 'm', width: 25pt, height: 13.33pt. |
| 1284 |
local output_fields = '' |
| 1285 |
for _, field_name in pairs(node.fields(head.id, head.subtype)) do |
| 1286 |
if field_name == 'attr' then |
| 1287 |
attributes = tree.format_attributes(head.attr) |
| 1288 |
elseif node.type(head.id) == 'whatsit' and field_name == 'data' and head.subtype == 1 then |
| 1289 |
-- Quick fix #46 |
| 1290 |
-- Unable to access head.data or head['data'] |
| 1291 |
-- A simple assignment causes a crash: local tmp = head.data |
| 1292 |
-- ! Incomplete \iffalse; all text was ignored after line 23. |
| 1293 |
-- <inserted text> |
| 1294 |
-- \fi |
| 1295 |
-- l.23 \end{figure} |
| 1296 |
-- local tmp = head.data |
| 1297 |
elseif field_name ~= 'next' and field_name ~= 'prev' and |
| 1298 |
node.is_node(head[field_name]) then |
| 1299 |
fields[field_name] = head[field_name] |
| 1300 |
else |
| 1301 |
output_fields = output_fields .. tree.format_field(head, field_name) |
| 1302 |
end |
| 1303 |
end |
| 1304 |
if output_fields ~= '' then |
| 1305 |
if need_whitespace == true then |
| 1306 |
output = output .. format.whitespace() |
| 1307 |
need_whitespace = false |
| 1308 |
end |
| 1309 |
output = output .. output_fields |
| 1310 |
end |
| 1311 |
|
| 1312 |
-- Append the attributes output if available. |
| 1313 |
if attributes and attributes ~= '' then |
| 1314 |
if need_whitespace == true then |
| 1315 |
output = output .. format.whitespace() |
| 1316 |
end |
| 1317 |
output = output .. template.key_value('attr', attributes, nil, 'blue') |
| 1318 |
end |
| 1319 |
|
| 1320 |
output = output:gsub(', $', '') |
| 1321 |
|
| 1322 |
nodetree_print( |
| 1323 |
format.node_begin() .. |
| 1324 |
output .. |
| 1325 |
format.node_end() .. |
| 1326 |
format.new_line() |
| 1327 |
) |
| 1328 |
|
| 1329 |
local property = node.getproperty(head) |
| 1330 |
if property then |
| 1331 |
local props |
| 1332 |
if options.verbosity == 0 then |
| 1333 |
props = 'props' |
| 1334 |
else |
| 1335 |
props = 'properties:' |
| 1336 |
end |
| 1337 |
|
| 1338 |
-- Print attributes in a separate line. |
| 1339 |
nodetree_print( |
| 1340 |
format.node_begin() .. |
| 1341 |
template.branches(level, 'field') .. |
| 1342 |
' ' .. |
| 1343 |
template.colored_string(props, 'blue') .. ' ' .. |
| 1344 |
template.table_inline(property) .. |
| 1345 |
format.node_end() .. |
| 1346 |
format.new_line() |
| 1347 |
) |
| 1348 |
end |
| 1349 |
|
| 1350 |
tree.analyze_fields(fields, level) |
| 1351 |
end |
| 1352 |
|
| 1353 |
--- Recurse over the current node list. |
| 1354 |
--- |
| 1355 |
---@param head Node # The head node of a node list. |
| 1356 |
---@param level number # The current recursion level. |
| 1357 |
function tree.analyze_list(head, level) |
| 1358 |
while head do |
| 1359 |
tree.analyze_node(head, level) |
| 1360 |
head = head.next |
| 1361 |
end |
| 1362 |
end |
| 1363 |
|
| 1364 |
--- The top-level internal entry point. |
| 1365 |
--- |
| 1366 |
---@param head Node # The head node of a node list. |
| 1367 |
function tree.analyze_callback(head) |
| 1368 |
tree.analyze_list(head, 1) |
| 1369 |
nodetree_print(template.line('short')) |
| 1370 |
end |
| 1371 |
|
| 1372 |
local orig_callbacks = {} |
| 1373 |
local orig_descriptions = {} |
| 1374 |
|
| 1375 |
local print_positions = {} |
| 1376 |
local callback_has_default_action = { |
| 1377 |
hyphenate = true, |
| 1378 |
ligaturing = true, |
| 1379 |
kerning = true, |
| 1380 |
mlist_to_hlist = true |
| 1381 |
} |
| 1382 |
|
| 1383 |
--- Callback wrappers. |
| 1384 |
--- |
| 1385 |
---Nodetree uses luatexbase's functions to manage callbacks if |
| 1386 |
---available. Otherwise a simplistic approach is taken by prepending |
| 1387 |
---or appending nodetree's diagnostic callbacks to the existing ones |
| 1388 |
---(and also removing them again if requested). |
| 1389 |
--- |
| 1390 |
---Each function in the `callback_wrappers` table consists of three |
| 1391 |
---parts: |
| 1392 |
--- |
| 1393 |
---* before-callback inspection |
| 1394 |
---* original callback or default function call |
| 1395 |
---* after-callback inspection |
| 1396 |
--- |
| 1397 |
---The actual callback functions are stored in the `callbacks` table. |
| 1398 |
--- |
| 1399 |
---@section callbacks |
| 1400 |
|
| 1401 |
local callback_wrappers = { |
| 1402 |
---@function callbacks.contribute_filter |
| 1403 |
--- |
| 1404 |
---@param extrainfo string |
| 1405 |
---@param where string |
| 1406 |
contribute_filter = function(extrainfo, where) |
| 1407 |
local cb = 'contribute_filter' |
| 1408 |
local before, after = template.get_print_position(where) |
| 1409 |
|
| 1410 |
if before then |
| 1411 |
template.callback(cb, {extrainfo = extrainfo}, before) |
| 1412 |
end |
| 1413 |
if orig_callbacks[cb] then |
| 1414 |
if orig_callbacks[cb] ~= '' then |
| 1415 |
orig_callbacks[cb](extrainfo) |
| 1416 |
end |
| 1417 |
else |
| 1418 |
template.no_callback(cb) |
| 1419 |
end |
| 1420 |
if after then |
| 1421 |
template.callback(cb, {extrainfo = extrainfo}, after) |
| 1422 |
end |
| 1423 |
end, |
| 1424 |
|
| 1425 |
---@function callbacks.buildpage_filter |
| 1426 |
--- |
| 1427 |
---@param extrainfo string |
| 1428 |
---@param where string |
| 1429 |
buildpage_filter = function(extrainfo, where) |
| 1430 |
local cb = 'buildpage_filter' |
| 1431 |
local before, after = template.get_print_position(where) |
| 1432 |
|
| 1433 |
if before then |
| 1434 |
template.callback(cb, {extrainfo = extrainfo}, before) |
| 1435 |
end |
| 1436 |
if orig_callbacks[cb] then |
| 1437 |
if orig_callbacks[cb] ~= '' then |
| 1438 |
orig_callbacks[cb](extrainfo) |
| 1439 |
end |
| 1440 |
else |
| 1441 |
template.no_callback(cb) |
| 1442 |
end |
| 1443 |
if after then |
| 1444 |
template.callback(cb, {extrainfo = extrainfo}, after) |
| 1445 |
end |
| 1446 |
end, |
| 1447 |
|
| 1448 |
---@function callbacks.build_page_insert |
| 1449 |
--- |
| 1450 |
---@param n string |
| 1451 |
---@param i string |
| 1452 |
--- |
| 1453 |
---@return number |
| 1454 |
build_page_insert = function(n, i) |
| 1455 |
local cb = 'build_page_insert' |
| 1456 |
local before, after = template.get_print_position(cb) |
| 1457 |
local retval = 0 |
| 1458 |
|
| 1459 |
if before then |
| 1460 |
template.callback(cb, {n = n, i = i}, before) |
| 1461 |
end |
| 1462 |
if orig_callbacks[cb] then |
| 1463 |
if orig_callbacks[cb] ~= '' then |
| 1464 |
retval = orig_callbacks[cb](n, i) |
| 1465 |
end |
| 1466 |
else |
| 1467 |
template.no_callback(cb) |
| 1468 |
end |
| 1469 |
if after then |
| 1470 |
template.callback(cb, {n = n, i = i}, after) |
| 1471 |
end |
| 1472 |
|
| 1473 |
return retval |
| 1474 |
end, |
| 1475 |
|
| 1476 |
---@function callbacks.pre_linebreak_filter |
| 1477 |
--- |
| 1478 |
---@param head Node # The head node of a node list. |
| 1479 |
---@param groupcode string |
| 1480 |
---@param where string |
| 1481 |
--- |
| 1482 |
---@return boolean |
| 1483 |
pre_linebreak_filter = function(head, groupcode, where) |
| 1484 |
local cb = 'pre_linebreak_filter' |
| 1485 |
local before, after = template.get_print_position(where) |
| 1486 |
local retval = true |
| 1487 |
|
| 1488 |
if before then |
| 1489 |
template.callback(cb, {groupcode = groupcode}, before) |
| 1490 |
tree.analyze_callback(head) |
| 1491 |
end |
| 1492 |
if orig_callbacks[cb] then |
| 1493 |
if orig_callbacks[cb] ~= '' then |
| 1494 |
retval = orig_callbacks[cb](head, groupcode) |
| 1495 |
end |
| 1496 |
else |
| 1497 |
template.no_callback(cb) |
| 1498 |
end |
| 1499 |
if after then |
| 1500 |
template.callback(cb, {groupcode = groupcode}, after) |
| 1501 |
tree.analyze_callback(head) |
| 1502 |
end |
| 1503 |
|
| 1504 |
return retval |
| 1505 |
end, |
| 1506 |
|
| 1507 |
---@function callbacks.linebreak_filter |
| 1508 |
--- |
| 1509 |
---@param head Node # The head node of a node list. |
| 1510 |
---@param is_display boolean |
| 1511 |
--- |
| 1512 |
---@return boolean |
| 1513 |
linebreak_filter = function(head, is_display) |
| 1514 |
local cb = 'linebreak_filter' |
| 1515 |
local before, after = template.get_print_position(cb) |
| 1516 |
local retval = true |
| 1517 |
|
| 1518 |
if before then |
| 1519 |
template.callback(cb, {is_display = is_display}, before) |
| 1520 |
tree.analyze_callback(head) |
| 1521 |
end |
| 1522 |
if orig_callbacks[cb] then |
| 1523 |
if orig_callbacks[cb] ~= '' then |
| 1524 |
retval = orig_callbacks[cb](head, is_display) |
| 1525 |
end |
| 1526 |
else |
| 1527 |
template.no_callback(cb) |
| 1528 |
end |
| 1529 |
if after then |
| 1530 |
template.callback(cb, {is_display = is_display}, after) |
| 1531 |
tree.analyze_callback(head) |
| 1532 |
end |
| 1533 |
|
| 1534 |
return retval |
| 1535 |
end, |
| 1536 |
|
| 1537 |
---@function callbacks.append_to_vlist_filter |
| 1538 |
--- |
| 1539 |
---@param box Node |
| 1540 |
---@param locationcode string |
| 1541 |
---@param prevdepth number |
| 1542 |
---@param mirrored boolean |
| 1543 |
--- |
| 1544 |
---@return Node |
| 1545 |
---@return number |
| 1546 |
append_to_vlist_filter = function(box, locationcode, prevdepth, mirrored) |
| 1547 |
local cb = 'append_to_vlist_filter' |
| 1548 |
local before, after = template.get_print_position(cb) |
| 1549 |
|
| 1550 |
if before then |
| 1551 |
template.callback(cb, {locationcode = locationcode, |
| 1552 |
prevdepth = prevdepth, |
| 1553 |
mirrored = mirrored}, before) |
| 1554 |
tree.analyze_callback(box) |
| 1555 |
end |
| 1556 |
if orig_callbacks[cb] then |
| 1557 |
if orig_callbacks[cb] ~= '' then |
| 1558 |
box, prevdepth = orig_callbacks[cb](box, locationcode, |
| 1559 |
prevdepth, mirrored) |
| 1560 |
end |
| 1561 |
else |
| 1562 |
template.no_callback(cb) |
| 1563 |
end |
| 1564 |
if after then |
| 1565 |
template.callback(cb, {locationcode = locationcode, |
| 1566 |
prevdepth = prevdepth, |
| 1567 |
mirrored = mirrored}, after) |
| 1568 |
tree.analyze_callback(box) |
| 1569 |
end |
| 1570 |
|
| 1571 |
return box, prevdepth |
| 1572 |
end, |
| 1573 |
|
| 1574 |
---@function callbacks.post_linebreak_filter |
| 1575 |
--- |
| 1576 |
---@param head Node # The head node of a node list. |
| 1577 |
---@param groupcode string |
| 1578 |
---@param where string |
| 1579 |
--- |
| 1580 |
---@return boolean |
| 1581 |
post_linebreak_filter = function(head, groupcode, where) |
| 1582 |
local cb = 'post_linebreak_filter' |
| 1583 |
local before, after = template.get_print_position(where) |
| 1584 |
local retval = true |
| 1585 |
|
| 1586 |
if before then |
| 1587 |
template.callback(cb, {groupcode = groupcode}, before) |
| 1588 |
tree.analyze_callback(head) |
| 1589 |
end |
| 1590 |
if orig_callbacks[cb] then |
| 1591 |
if orig_callbacks[cb] ~= '' then |
| 1592 |
retval = orig_callbacks[cb](head, groupcode) |
| 1593 |
end |
| 1594 |
else |
| 1595 |
template.no_callback(cb) |
| 1596 |
end |
| 1597 |
if after then |
| 1598 |
template.callback(cb, {groupcode = groupcode}, after) |
| 1599 |
tree.analyze_callback(head) |
| 1600 |
end |
| 1601 |
|
| 1602 |
return retval |
| 1603 |
end, |
| 1604 |
|
| 1605 |
---@function callbacks.hpack_filter |
| 1606 |
--- |
| 1607 |
---@param head Node # The head node of a node list. |
| 1608 |
---@param groupcode string |
| 1609 |
---@param size number |
| 1610 |
---@param packtype string |
| 1611 |
---@param direction string |
| 1612 |
---@param attributelist Node |
| 1613 |
---@param where string |
| 1614 |
--- |
| 1615 |
---@return boolean |
| 1616 |
hpack_filter = function(head, groupcode, size, packtype, |
| 1617 |
direction, attributelist, where) |
| 1618 |
local cb = 'hpack_filter' |
| 1619 |
local before, after = template.get_print_position(where) |
| 1620 |
local retval = true |
| 1621 |
|
| 1622 |
if before then |
| 1623 |
template.callback(cb, {groupcode = groupcode, |
| 1624 |
size = size, |
| 1625 |
packtype = packtype, |
| 1626 |
direction = direction, |
| 1627 |
attributelist = attributelist}, before) |
| 1628 |
tree.analyze_callback(head) |
| 1629 |
end |
| 1630 |
if orig_callbacks[cb] then |
| 1631 |
if orig_callbacks[cb] ~= '' then |
| 1632 |
retval = orig_callbacks[cb](head, groupcode, size, |
| 1633 |
packtype, direction, |
| 1634 |
attributelist) |
| 1635 |
end |
| 1636 |
else |
| 1637 |
template.no_callback(cb) |
| 1638 |
end |
| 1639 |
if after then |
| 1640 |
template.callback(cb, {groupcode = groupcode, |
| 1641 |
size = size, |
| 1642 |
packtype = packtype, |
| 1643 |
direction = direction, |
| 1644 |
attributelist = attributelist}, after) |
| 1645 |
tree.analyze_callback(head) |
| 1646 |
end |
| 1647 |
|
| 1648 |
return retval |
| 1649 |
end, |
| 1650 |
|
| 1651 |
---@function callbacks.vpack_filter |
| 1652 |
--- |
| 1653 |
---@param head Node # The head node of a node list. |
| 1654 |
---@param groupcode string |
| 1655 |
---@param size number |
| 1656 |
---@param packtype string |
| 1657 |
---@param maxdepth number |
| 1658 |
---@param direction string |
| 1659 |
---@param attributelist Node |
| 1660 |
---@param where string |
| 1661 |
--- |
| 1662 |
---@return boolean |
| 1663 |
vpack_filter = function(head, groupcode, size, packtype, |
| 1664 |
maxdepth, direction, attributelist, where) |
| 1665 |
local cb = 'vpack_filter' |
| 1666 |
local before, after = template.get_print_position(where) |
| 1667 |
local retval = true |
| 1668 |
|
| 1669 |
if before then |
| 1670 |
template.callback(cb, {groupcode = groupcode, |
| 1671 |
size = size, |
| 1672 |
packtype = packtype, |
| 1673 |
maxdepth = template.length(maxdepth), |
| 1674 |
direction = direction, |
| 1675 |
attributelist = attributelist}, before) |
| 1676 |
tree.analyze_callback(head) |
| 1677 |
tree.analyze_callback(attributelist) |
| 1678 |
end |
| 1679 |
if orig_callbacks[cb] then |
| 1680 |
if orig_callbacks[cb] ~= '' then |
| 1681 |
retval = orig_callbacks[cb](head, groupcode, size, packtype, |
| 1682 |
maxdepth, direction, |
| 1683 |
attributelist) |
| 1684 |
end |
| 1685 |
else |
| 1686 |
template.no_callback(cb) |
| 1687 |
end |
| 1688 |
if after then |
| 1689 |
template.callback(cb, {groupcode = groupcode, |
| 1690 |
size = size, |
| 1691 |
packtype = packtype, |
| 1692 |
maxdepth = template.length(maxdepth), |
| 1693 |
direction = direction, |
| 1694 |
attributelist = attributelist}, after) |
| 1695 |
tree.analyze_callback(head) |
| 1696 |
tree.analyze_callback(attributelist) |
| 1697 |
end |
| 1698 |
|
| 1699 |
return retval |
| 1700 |
end, |
| 1701 |
|
| 1702 |
---@function callbacks.hpack_quality |
| 1703 |
--- |
| 1704 |
---@param incident string |
| 1705 |
---@param detail number |
| 1706 |
---@param head Node # The head node of a node list. |
| 1707 |
---@param first number |
| 1708 |
---@param last number |
| 1709 |
--- |
| 1710 |
---@return Node |
| 1711 |
hpack_quality = function(incident, detail, head, first, last) |
| 1712 |
local cb = 'hpack_quality' |
| 1713 |
local before, after = template.get_print_position(cb) |
| 1714 |
local retval = nil |
| 1715 |
|
| 1716 |
if before then |
| 1717 |
template.callback(cb, {incident = incident, |
| 1718 |
detail = detail, |
| 1719 |
first = first, |
| 1720 |
last = last}, before) |
| 1721 |
tree.analyze_callback(head) |
| 1722 |
end |
| 1723 |
if orig_callbacks[cb] then |
| 1724 |
if orig_callbacks[cb] ~= '' then |
| 1725 |
retval = orig_callbacks[cb](incident, detail, head, first, last) |
| 1726 |
end |
| 1727 |
else |
| 1728 |
template.no_callback(cb) |
| 1729 |
end |
| 1730 |
if after then |
| 1731 |
template.callback(cb, {incident = incident, |
| 1732 |
detail = detail, |
| 1733 |
first = first, |
| 1734 |
last = last}, after) |
| 1735 |
tree.analyze_callback(head) |
| 1736 |
end |
| 1737 |
|
| 1738 |
return retval |
| 1739 |
end, |
| 1740 |
|
| 1741 |
---@function callbacks.vpack_quality |
| 1742 |
--- |
| 1743 |
---@param incident string |
| 1744 |
---@param detail number |
| 1745 |
---@param head Node # The head node of a node list. |
| 1746 |
---@param first number |
| 1747 |
---@param last number |
| 1748 |
vpack_quality = function(incident, detail, head, first, last) |
| 1749 |
local cb = 'vpack_quality' |
| 1750 |
local before, after = template.get_print_position(cb) |
| 1751 |
|
| 1752 |
if before then |
| 1753 |
template.callback(cb, {incident = incident, |
| 1754 |
detail = detail, |
| 1755 |
first = first, |
| 1756 |
last = last}, before) |
| 1757 |
tree.analyze_callback(head) |
| 1758 |
end |
| 1759 |
if orig_callbacks[cb] then |
| 1760 |
if orig_callbacks[cb] ~= '' then |
| 1761 |
orig_callbacks[cb](incident, detail, head, first, last) |
| 1762 |
end |
| 1763 |
else |
| 1764 |
template.no_callback(cb) |
| 1765 |
end |
| 1766 |
if after then |
| 1767 |
template.callback(cb, {incident = incident, |
| 1768 |
detail = detail, |
| 1769 |
first = first, |
| 1770 |
last = last}, after) |
| 1771 |
tree.analyze_callback(head) |
| 1772 |
end |
| 1773 |
end, |
| 1774 |
|
| 1775 |
---@function callbacks.process_rule |
| 1776 |
--- |
| 1777 |
---@param head Node # The head node of a node list. |
| 1778 |
---@param width number |
| 1779 |
---@param height number |
| 1780 |
process_rule = function(head, width, height) |
| 1781 |
local cb = 'process_rule' |
| 1782 |
local before, after = template.get_print_position(cb) |
| 1783 |
|
| 1784 |
if before then |
| 1785 |
template.callback(cb, {width = width, height = height}, before) |
| 1786 |
tree.analyze_callback(head) |
| 1787 |
end |
| 1788 |
if orig_callbacks[cb] then |
| 1789 |
if orig_callbacks[cb] ~= '' then |
| 1790 |
orig_callbacks[cb](head, width, height) |
| 1791 |
end |
| 1792 |
else |
| 1793 |
template.no_callback(cb) |
| 1794 |
end |
| 1795 |
if after then |
| 1796 |
template.callback(cb, {width = width, height = height}, after) |
| 1797 |
tree.analyze_callback(head) |
| 1798 |
end |
| 1799 |
end, |
| 1800 |
|
| 1801 |
---@function callbacks.pre_output_filter |
| 1802 |
--- |
| 1803 |
---@param head Node # The head node of a node list. |
| 1804 |
---@param groupcode string |
| 1805 |
---@param size number |
| 1806 |
---@param packtype string |
| 1807 |
---@param maxdepth number |
| 1808 |
---@param direction string |
| 1809 |
---@param where string |
| 1810 |
--- |
| 1811 |
---@return boolean |
| 1812 |
pre_output_filter = function(head, groupcode, size, packtype, |
| 1813 |
maxdepth, direction, where) |
| 1814 |
local cb = 'pre_output_filter' |
| 1815 |
local before, after = template.get_print_position(where) |
| 1816 |
local retval = true |
| 1817 |
|
| 1818 |
if before then |
| 1819 |
template.callback(cb, {groupcode = groupcode, |
| 1820 |
size = size, |
| 1821 |
packtype = packtype, |
| 1822 |
maxdepth = maxdepth, |
| 1823 |
direction = direction}, before) |
| 1824 |
tree.analyze_callback(head) |
| 1825 |
end |
| 1826 |
if orig_callbacks[cb] then |
| 1827 |
if orig_callbacks[cb] ~= '' then |
| 1828 |
retval = orig_callbacks[cb](head, groupcode, size, |
| 1829 |
packtype, maxdepth, |
| 1830 |
direction) |
| 1831 |
end |
| 1832 |
else |
| 1833 |
template.no_callback(cb) |
| 1834 |
end |
| 1835 |
if after then |
| 1836 |
template.callback(cb, {groupcode = groupcode, |
| 1837 |
size = size, |
| 1838 |
packtype = packtype, |
| 1839 |
maxdepth = maxdepth, |
| 1840 |
direction = direction}, after) |
| 1841 |
tree.analyze_callback(head) |
| 1842 |
end |
| 1843 |
|
| 1844 |
return retval |
| 1845 |
end, |
| 1846 |
|
| 1847 |
---@function callbacks.hyphenate |
| 1848 |
--- |
| 1849 |
---@param head Node # The head node of a node list. |
| 1850 |
---@param tail Node |
| 1851 |
---@param where string |
| 1852 |
hyphenate = function(head, tail, where) |
| 1853 |
local cb = 'hyphenate' |
| 1854 |
local before, after = template.get_print_position(where) |
| 1855 |
|
| 1856 |
if before then |
| 1857 |
template.callback(cb, nil, before) |
| 1858 |
nodetree_print('head:' .. format.new_line()) |
| 1859 |
tree.analyze_callback(head) |
| 1860 |
end |
| 1861 |
if orig_callbacks[cb] then |
| 1862 |
if orig_callbacks[cb] ~= '' then |
| 1863 |
orig_callbacks[cb](head, tail) |
| 1864 |
end |
| 1865 |
else |
| 1866 |
template.no_callback(cb, true) |
| 1867 |
lang.hyphenate(head, tail) |
| 1868 |
end |
| 1869 |
if after then |
| 1870 |
template.callback(cb, nil, after) |
| 1871 |
nodetree_print('head:' .. format.new_line()) |
| 1872 |
tree.analyze_callback(head) |
| 1873 |
end |
| 1874 |
end, |
| 1875 |
|
| 1876 |
---@function callbacks.ligaturing |
| 1877 |
--- |
| 1878 |
---@param head Node # The head node of a node list. |
| 1879 |
---@param tail Node |
| 1880 |
---@param where string |
| 1881 |
ligaturing = function(head, tail, where) |
| 1882 |
local cb = 'ligaturing' |
| 1883 |
local before, after = template.get_print_position(where) |
| 1884 |
|
| 1885 |
if before then |
| 1886 |
template.callback(cb, nil, before) |
| 1887 |
nodetree_print('head:' .. format.new_line()) |
| 1888 |
tree.analyze_callback(head) |
| 1889 |
end |
| 1890 |
if orig_callbacks[cb] then |
| 1891 |
if orig_callbacks[cb] ~= '' then |
| 1892 |
orig_callbacks[cb](head, tail) |
| 1893 |
end |
| 1894 |
else |
| 1895 |
template.no_callback(cb, true) |
| 1896 |
node.ligaturing(head, tail) |
| 1897 |
end |
| 1898 |
if after then |
| 1899 |
template.callback(cb, nil, after) |
| 1900 |
nodetree_print('head:' .. format.new_line()) |
| 1901 |
tree.analyze_callback(head) |
| 1902 |
end |
| 1903 |
end, |
| 1904 |
|
| 1905 |
---@function callbacks.kerning |
| 1906 |
--- |
| 1907 |
---@param head Node # The head node of a node list. |
| 1908 |
---@param tail Node |
| 1909 |
---@param where string |
| 1910 |
kerning = function(head, tail, where) |
| 1911 |
local cb = 'kerning' |
| 1912 |
local before, after = template.get_print_position(where) |
| 1913 |
|
| 1914 |
if before then |
| 1915 |
template.callback(cb, nil, before) |
| 1916 |
nodetree_print('head:' .. format.new_line()) |
| 1917 |
tree.analyze_callback(head) |
| 1918 |
end |
| 1919 |
if orig_callbacks[cb] then |
| 1920 |
if orig_callbacks[cb] ~= '' then |
| 1921 |
orig_callbacks[cb](head, tail) |
| 1922 |
end |
| 1923 |
else |
| 1924 |
template.no_callback(cb, true) |
| 1925 |
node.kerning(head, tail) |
| 1926 |
end |
| 1927 |
if after then |
| 1928 |
template.callback(cb, nil, after) |
| 1929 |
nodetree_print('head:' .. format.new_line()) |
| 1930 |
tree.analyze_callback(head) |
| 1931 |
end |
| 1932 |
end, |
| 1933 |
|
| 1934 |
---@function callbacks.insert_local_par |
| 1935 |
--- |
| 1936 |
---@param local_par Node |
| 1937 |
---@param location string |
| 1938 |
---@param where string |
| 1939 |
insert_local_par = function(local_par, location, where) |
| 1940 |
local cb = 'insert_local_par' |
| 1941 |
local before, after = template.get_print_position(where) |
| 1942 |
|
| 1943 |
if before then |
| 1944 |
template.callback(cb, {location = location}, before) |
| 1945 |
tree.analyze_callback(local_par) |
| 1946 |
end |
| 1947 |
if orig_callbacks[cb] then |
| 1948 |
if orig_callbacks[cb] ~= '' then |
| 1949 |
orig_callbacks[cb](local_par, location) |
| 1950 |
end |
| 1951 |
else |
| 1952 |
template.no_callback(cb) |
| 1953 |
end |
| 1954 |
if after then |
| 1955 |
template.callback(cb, {location = location}, after) |
| 1956 |
tree.analyze_callback(local_par) |
| 1957 |
end |
| 1958 |
end, |
| 1959 |
|
| 1960 |
---@function callbacks.mlist_to_hlist |
| 1961 |
--- |
| 1962 |
---@param head Node # The head node of a node list. |
| 1963 |
---@param display_type string |
| 1964 |
---@param need_penalties boolean |
| 1965 |
--- |
| 1966 |
---@return Node |
| 1967 |
mlist_to_hlist = function(head, display_type, need_penalties) |
| 1968 |
local cb = 'mlist_to_hlist' |
| 1969 |
local before, after = template.get_print_position(cb) |
| 1970 |
local retval |
| 1971 |
|
| 1972 |
if before then |
| 1973 |
template.callback(cb, {display_type = display_type, |
| 1974 |
need_penalties = need_penalties}, before) |
| 1975 |
tree.analyze_callback(head) |
| 1976 |
end |
| 1977 |
if orig_callbacks[cb] then |
| 1978 |
if orig_callbacks[cb] ~= '' then |
| 1979 |
retval = orig_callbacks[cb](head, display_type, need_penalties) |
| 1980 |
end |
| 1981 |
else |
| 1982 |
template.no_callback(cb, true) |
| 1983 |
retval = node.mlist_to_hlist(head, display_type, need_penalties) |
| 1984 |
end |
| 1985 |
if after then |
| 1986 |
template.callback(cb, {display_type = display_type, |
| 1987 |
need_penalties = need_penalties}, after) |
| 1988 |
tree.analyze_callback(head) |
| 1989 |
end |
| 1990 |
|
| 1991 |
return retval |
| 1992 |
end |
| 1993 |
} |
| 1994 |
|
| 1995 |
-- The actual callback functions. The `*_before` and `*_after` |
| 1996 |
-- variants are needed for luatexbase. For 'exclusive' callbacks we |
| 1997 |
-- directly use the corresponding functions from the |
| 1998 |
-- `callback_wrappers` table. |
| 1999 |
|
| 2000 |
local callbacks = { |
| 2001 |
contribute_filter = function(extrainfo) |
| 2002 |
callback_wrappers.contribute_filter(extrainfo, 'contribute_filter') |
| 2003 |
end, |
| 2004 |
contribute_filter_before = function(extrainfo) |
| 2005 |
callback_wrappers.contribute_filter(extrainfo, 'before') |
| 2006 |
end, |
| 2007 |
contribute_filter_after = function(extrainfo) |
| 2008 |
callback_wrappers.contribute_filter(extrainfo, 'after') |
| 2009 |
end, |
| 2010 |
|
| 2011 |
buildpage_filter = function(extrainfo) |
| 2012 |
callback_wrappers.buildpage_filter(extrainfo, 'buildpage_filter') |
| 2013 |
end, |
| 2014 |
buildpage_filter_before = function(extrainfo) |
| 2015 |
callback_wrappers.buildpage_filter(extrainfo, 'before') |
| 2016 |
end, |
| 2017 |
buildpage_filter_after = function(extrainfo) |
| 2018 |
callback_wrappers.buildpage_filter(extrainfo, 'after') |
| 2019 |
end, |
| 2020 |
|
| 2021 |
build_page_insert = callback_wrappers.build_page_insert, |
| 2022 |
|
| 2023 |
pre_linebreak_filter = function(head, groupcode) |
| 2024 |
return callback_wrappers.pre_linebreak_filter(head, groupcode, |
| 2025 |
'pre_linebreak_filter') |
| 2026 |
end, |
| 2027 |
pre_linebreak_filter_before = function(head, groupcode) |
| 2028 |
return callback_wrappers.pre_linebreak_filter(head, groupcode, 'before') |
| 2029 |
end, |
| 2030 |
pre_linebreak_filter_after = function(head, groupcode) |
| 2031 |
return callback_wrappers.pre_linebreak_filter(head, groupcode, 'after') |
| 2032 |
end, |
| 2033 |
|
| 2034 |
linebreak_filter = callback_wrappers.linebreak_filter, |
| 2035 |
append_to_vlist_filter = callback_wrappers.append_to_vlist_filter, |
| 2036 |
|
| 2037 |
post_linebreak_filter = function(head, groupcode) |
| 2038 |
return callback_wrappers.post_linebreak_filter(head, groupcode, |
| 2039 |
'post_linebreak_filter') |
| 2040 |
end, |
| 2041 |
post_linebreak_filter_before = function(head, groupcode) |
| 2042 |
return callback_wrappers.post_linebreak_filter(head, groupcode, 'before') |
| 2043 |
end, |
| 2044 |
post_linebreak_filter_after = function(head, groupcode) |
| 2045 |
return callback_wrappers.post_linebreak_filter(head, groupcode, 'after') |
| 2046 |
end, |
| 2047 |
|
| 2048 |
hpack_filter = function(head, groupcode, size, packtype, |
| 2049 |
direction, attributelist) |
| 2050 |
return callback_wrappers.hpack_filter(head, groupcode, size, packtype, |
| 2051 |
direction, attributelist, |
| 2052 |
'hpack_filter') |
| 2053 |
end, |
| 2054 |
hpack_filter_before = function(head, groupcode, size, packtype, |
| 2055 |
direction, attributelist) |
| 2056 |
return callback_wrappers.hpack_filter(head, groupcode, size, packtype, |
| 2057 |
direction, attributelist, 'before') |
| 2058 |
end, |
| 2059 |
hpack_filter_after = function(head, groupcode, size, packtype, |
| 2060 |
direction, attributelist) |
| 2061 |
return callback_wrappers.hpack_filter(head, groupcode, size, packtype, |
| 2062 |
direction, attributelist, 'after') |
| 2063 |
end, |
| 2064 |
|
| 2065 |
vpack_filter = function(head, groupcode, size, packtype, |
| 2066 |
maxdepth, direction, attributelist) |
| 2067 |
return callback_wrappers.vpack_filter(head, groupcode, size, packtype, |
| 2068 |
maxdepth, direction, attributelist, |
| 2069 |
'vpack_filter') |
| 2070 |
end, |
| 2071 |
vpack_filter_before = function(head, groupcode, size, packtype, |
| 2072 |
maxdepth, direction, attributelist) |
| 2073 |
return callback_wrappers.vpack_filter(head, groupcode, size, packtype, |
| 2074 |
maxdepth, direction, attributelist, |
| 2075 |
'before') |
| 2076 |
end, |
| 2077 |
vpack_filter_after = function(head, groupcode, size, packtype, |
| 2078 |
maxdepth, direction, attributelist) |
| 2079 |
callback_wrappers.vpack_filter(head, groupcode, size, packtype, |
| 2080 |
maxdepth, direction, attributelist, |
| 2081 |
'after') |
| 2082 |
end, |
| 2083 |
|
| 2084 |
hpack_quality = callback_wrappers.hpack_quality, |
| 2085 |
vpack_quality = callback_wrappers.vpack_quality, |
| 2086 |
process_rule = callback_wrappers.process_rule, |
| 2087 |
|
| 2088 |
pre_output_filter = function(head, groupcode, size, packtype, |
| 2089 |
maxdepth, direction) |
| 2090 |
return callback_wrappers.pre_output_filter(head, groupcode, size, |
| 2091 |
packtype, maxdepth, direction, |
| 2092 |
'pre_output_filter') |
| 2093 |
end, |
| 2094 |
pre_output_filter_before = function(head, groupcode, size, packtype, |
| 2095 |
maxdepth, direction) |
| 2096 |
return callback_wrappers.pre_output_filter(head, groupcode, size, |
| 2097 |
packtype, maxdepth, direction, |
| 2098 |
'before') |
| 2099 |
end, |
| 2100 |
pre_output_filter_after = function(head, groupcode, size, packtype, |
| 2101 |
maxdepth, direction) |
| 2102 |
return callback_wrappers.pre_output_filter(head, groupcode, size, |
| 2103 |
packtype, maxdepth, direction, |
| 2104 |
'after') |
| 2105 |
end, |
| 2106 |
|
| 2107 |
hyphenate = function(head, tail) |
| 2108 |
callback_wrappers.hyphenate(head, tail, 'hyphenate') |
| 2109 |
end, |
| 2110 |
hyphenate_before = function(head, tail) |
| 2111 |
callback_wrappers.hyphenate(head, tail, 'before') |
| 2112 |
end, |
| 2113 |
hyphenate_after = function(head, tail) |
| 2114 |
callback_wrappers.hyphenate(head, tail, 'after') |
| 2115 |
end, |
| 2116 |
|
| 2117 |
ligaturing = function(head, tail) |
| 2118 |
callback_wrappers.ligaturing(head, tail, 'ligaturing') |
| 2119 |
end, |
| 2120 |
ligaturing_before = function(head, tail) |
| 2121 |
callback_wrappers.ligaturing(head, tail, 'before') |
| 2122 |
end, |
| 2123 |
ligaturing_after = function(head, tail) |
| 2124 |
callback_wrappers.ligaturing(head, tail, 'after') |
| 2125 |
end, |
| 2126 |
|
| 2127 |
kerning = function(head, tail) |
| 2128 |
callback_wrappers.kerning(head, tail, 'kerning') |
| 2129 |
end, |
| 2130 |
kerning_before = function(head, tail) |
| 2131 |
callback_wrappers.kerning(head, tail, 'before') |
| 2132 |
end, |
| 2133 |
kerning_after = function(head, tail) |
| 2134 |
callback_wrappers.kerning(head, tail, 'after') |
| 2135 |
end, |
| 2136 |
|
| 2137 |
insert_local_par = function(head, tail) |
| 2138 |
callback_wrappers.insert_local_par(head, tail, 'insert_local_par') |
| 2139 |
end, |
| 2140 |
insert_local_par_before = function(head, tail) |
| 2141 |
callback_wrappers.insert_local_par(head, tail, 'before') |
| 2142 |
end, |
| 2143 |
insert_local_par_after = function(head, tail) |
| 2144 |
callback_wrappers.insert_local_par(head, tail, 'after') |
| 2145 |
end, |
| 2146 |
|
| 2147 |
mlist_to_hlist = callback_wrappers.mlist_to_hlist |
| 2148 |
} |
| 2149 |
|
| 2150 |
--- Messages, options |
| 2151 |
--- |
| 2152 |
---@section messages |
| 2153 |
|
| 2154 |
--- Emit a warning or error message. |
| 2155 |
--- |
| 2156 |
---This works for plain TeX, Texinfo, and LaTeX (for plain TeX and |
| 2157 |
---Texinfo we make the message look identical to the LaTeX case). |
| 2158 |
---Note that a full stop gets appended to `what`. |
| 2159 |
--- |
| 2160 |
---@param why string # `'error'` or `'warning'`. |
| 2161 |
---@param where string # In which package the error happened. |
| 2162 |
---@param what string # The warning message to emit. |
| 2163 |
---@param help? string # Additional help text for errors. |
| 2164 |
local function message(why, where, what, help) |
| 2165 |
local msg |
| 2166 |
|
| 2167 |
what = string.gsub(what, '\n', '\\MessageBreak ') |
| 2168 |
|
| 2169 |
if why == 'error' then |
| 2170 |
if not help then |
| 2171 |
help = '' |
| 2172 |
end |
| 2173 |
|
| 2174 |
msg = { |
| 2175 |
'\\ifx\\mbox\\undefined', |
| 2176 |
' \\errhelp{' .. help .. '}%', |
| 2177 |
' \\begingroup', |
| 2178 |
' \\newlinechar`\\^^J%', |
| 2179 |
' \\def\\MessageBreak{^^J(' .. where .. ')' .. string.rep('\\space', 16) .. '}%', |
| 2180 |
' \\errmessage{Package ' .. where .. ' Error: ' .. what .. '}%', |
| 2181 |
' \\endgroup', |
| 2182 |
'\\else', |
| 2183 |
' \\PackageError{' .. where .. '}{' .. what .. '}{' .. help .. '}%', |
| 2184 |
'\\fi' |
| 2185 |
} |
| 2186 |
else |
| 2187 |
msg = { |
| 2188 |
'\\ifx\\mbox\\undefined', |
| 2189 |
' \\begingroup', |
| 2190 |
' \\newlinechar`\\^^J%', |
| 2191 |
' \\def\\MessageBreak{^^J(' .. where .. ')' .. string.rep('\\space', 16) .. '}%', |
| 2192 |
' \\message{Package ' .. where .. ' Warning: ' .. what .. '}%', |
| 2193 |
' \\endgroup', |
| 2194 |
'\\else', |
| 2195 |
' \\PackageWarning{' .. where .. '}{' .. what .. '}%', |
| 2196 |
'\\fi' |
| 2197 |
} |
| 2198 |
end |
| 2199 |
|
| 2200 |
if tex.escapechar == utf8.codepoint('@') then |
| 2201 |
table.insert(msg, 1, '@tex') |
| 2202 |
table.insert(msg, '@end tex') |
| 2203 |
end |
| 2204 |
|
| 2205 |
tex.print(msg) |
| 2206 |
end |
| 2207 |
|
| 2208 |
--- Set a single-option key-value pair. |
| 2209 |
--- |
| 2210 |
---@param key string # The key of the option pair. |
| 2211 |
---@param value number|string # The value of the option pair. |
| 2212 |
local function set_option(key, value) |
| 2213 |
if not default_options[key] then |
| 2214 |
message( |
| 2215 |
'warning', |
| 2216 |
'nodetree', |
| 2217 |
"Ignoring unknown option '" .. key .. "'" |
| 2218 |
) |
| 2219 |
return |
| 2220 |
end |
| 2221 |
if not options then |
| 2222 |
options = {} |
| 2223 |
end |
| 2224 |
if key == 'verbosity' then |
| 2225 |
options[key] = tonumber(value) or default_options.verbosity |
| 2226 |
elseif key == 'decimalplaces' then |
| 2227 |
options[key] = tonumber(value) or default_options.decimalplaces |
| 2228 |
elseif key == 'firstline' then |
| 2229 |
options[key] = tonumber(value) or default_options.firstline |
| 2230 |
elseif key == 'lastline' then |
| 2231 |
options[key] = tonumber(value) or default_options.lastline |
| 2232 |
else |
| 2233 |
options[key] = value |
| 2234 |
end |
| 2235 |
end |
| 2236 |
|
| 2237 |
--- Set multiple key-value option pairs using a table. |
| 2238 |
--- |
| 2239 |
---@param opts table # Options. |
| 2240 |
local function set_options(opts) |
| 2241 |
if not options then |
| 2242 |
options = {} |
| 2243 |
end |
| 2244 |
for key, value in pairs(opts) do |
| 2245 |
set_option(key, value) |
| 2246 |
end |
| 2247 |
end |
| 2248 |
|
| 2249 |
--- Callback management |
| 2250 |
--- |
| 2251 |
---@section callback_management |
| 2252 |
|
| 2253 |
--- |
| 2254 |
---@param what? string|'before'|'after' # The name of a callback, or either the string `before` or `after`. |
| 2255 |
--- |
| 2256 |
---@return 'before'|nil # 'before'` or `nil`. |
| 2257 |
---@return 'after'|nil # `'after'` or `nil`. |
| 2258 |
function template.get_print_position(what) |
| 2259 |
local before, after |
| 2260 |
|
| 2261 |
if what == 'before' then |
| 2262 |
before = what |
| 2263 |
after = nil |
| 2264 |
elseif what == 'after' then |
| 2265 |
before = nil |
| 2266 |
after = what |
| 2267 |
else |
| 2268 |
before = print_positions[what][1] |
| 2269 |
after = print_positions[what][2] |
| 2270 |
end |
| 2271 |
|
| 2272 |
return before, after |
| 2273 |
end |
| 2274 |
|
| 2275 |
--- |
| 2276 |
---@param name string |
| 2277 |
---@param internal? string|boolean |
| 2278 |
function template.no_callback(name, internal) |
| 2279 |
local more = '' |
| 2280 |
if internal == true then |
| 2281 |
more = ',' .. format.new_line() .. |
| 2282 |
' LuaTeX uses internal function instead' |
| 2283 |
end |
| 2284 |
nodetree_print( |
| 2285 |
format.new_line() .. |
| 2286 |
"<no registered function for '" .. |
| 2287 |
format.underscore(name) .. "' callback" .. more .. ">") |
| 2288 |
end |
| 2289 |
|
| 2290 |
--- Check whether the given callback name exists. |
| 2291 |
--- |
| 2292 |
---Throw an error if it doesn’t. |
| 2293 |
--- |
| 2294 |
---@param callback_name string # The name of a callback to check. |
| 2295 |
--- |
| 2296 |
---@return string # The unchanged input of the function. |
| 2297 |
local function check_callback_name(callback_name) |
| 2298 |
local info = callback.list() |
| 2299 |
if info[callback_name] == nil then |
| 2300 |
message( |
| 2301 |
'error', |
| 2302 |
'nodetree', |
| 2303 |
'Unknown callback name or callback alias\n' |
| 2304 |
.. "'" .. callback_name .. "'" |
| 2305 |
) |
| 2306 |
end |
| 2307 |
return callback_name |
| 2308 |
end |
| 2309 |
|
| 2310 |
--- Get the real callback name from an alias string. |
| 2311 |
--- |
| 2312 |
---@param alias string # The alias of a callback name or the callback name itself. |
| 2313 |
--- |
| 2314 |
---@return string # The real callback name. |
| 2315 |
local function get_callback_name(alias) |
| 2316 |
local callback_name |
| 2317 |
if alias == 'contribute' or alias == 'contributefilter' then |
| 2318 |
callback_name = 'contribute_filter' |
| 2319 |
|
| 2320 |
-- Formerly called buildpage, now there is a build_page_insert. |
| 2321 |
elseif alias == 'buildfilter' or alias == 'buildpagefilter' then |
| 2322 |
callback_name = 'buildpage_filter' |
| 2323 |
|
| 2324 |
-- Untested: I don’t know how to invoke this filter. |
| 2325 |
elseif alias == 'buildinsert' or alias == 'buildpageinsert' then |
| 2326 |
callback_name = 'build_page_insert' |
| 2327 |
|
| 2328 |
elseif alias == 'preline' or alias == 'prelinebreakfilter' then |
| 2329 |
callback_name = 'pre_linebreak_filter' |
| 2330 |
|
| 2331 |
elseif alias == 'line' or alias == 'linebreakfilter' then |
| 2332 |
callback_name = 'linebreak_filter' |
| 2333 |
|
| 2334 |
elseif alias == 'append' or alias == 'appendtovlistfilter' then |
| 2335 |
callback_name = 'append_to_vlist_filter' |
| 2336 |
|
| 2337 |
elseif alias == 'postline' or alias == 'postlinebreak' or alias == 'postlinebreakfilter' then |
| 2338 |
callback_name = 'post_linebreak_filter' |
| 2339 |
|
| 2340 |
elseif alias == 'hpack' or alias == 'hpackfilter' then |
| 2341 |
callback_name = 'hpack_filter' |
| 2342 |
|
| 2343 |
elseif alias == 'vpack' or alias == 'vpackfilter' then |
| 2344 |
callback_name = 'vpack_filter' |
| 2345 |
|
| 2346 |
elseif alias == 'hpackq' or alias == 'hpackquality' then |
| 2347 |
callback_name = 'hpack_quality' |
| 2348 |
|
| 2349 |
elseif alias == 'vpackq' or alias == 'vpackquality' then |
| 2350 |
callback_name = 'vpack_quality' |
| 2351 |
|
| 2352 |
elseif alias == 'process' or alias == 'processrule' then |
| 2353 |
callback_name = 'process_rule' |
| 2354 |
|
| 2355 |
elseif alias == 'preout' or alias == 'preoutputfilter' then |
| 2356 |
callback_name = 'pre_output_filter' |
| 2357 |
|
| 2358 |
elseif alias == 'hyph' or alias == 'hyphenate' then |
| 2359 |
callback_name = 'hyphenate' |
| 2360 |
|
| 2361 |
elseif alias == 'liga' or alias == 'ligaturing' then |
| 2362 |
callback_name = 'ligaturing' |
| 2363 |
|
| 2364 |
elseif alias == 'kern' or alias == 'kerning' then |
| 2365 |
callback_name = 'kerning' |
| 2366 |
|
| 2367 |
elseif alias == 'insert' or alias == 'insertlocalpar' then |
| 2368 |
callback_name = 'insert_local_par' |
| 2369 |
|
| 2370 |
elseif alias == 'mhlist' or alias == 'mlisttohlist' then |
| 2371 |
callback_name = 'mlist_to_hlist' |
| 2372 |
|
| 2373 |
else |
| 2374 |
callback_name = alias |
| 2375 |
end |
| 2376 |
return check_callback_name(callback_name) |
| 2377 |
end |
| 2378 |
|
| 2379 |
--- Register a callback. |
| 2380 |
--- |
| 2381 |
--- Doing this for plain TeX is simple; we have access to LuaTeX's |
| 2382 |
--- base function `callback.register` and thus can easily add our |
| 2383 |
--- callback wrapper, which in turn calls nodetree's functions at the |
| 2384 |
--- very beginning and/or at the very end. |
| 2385 |
--- |
| 2386 |
--- Enter LaTeX. It comes with its own callback management that can |
| 2387 |
--- register multiple callbacks, also taking care of the calling |
| 2388 |
--- order. Unfortunately, however, it is also more restrictive: for |
| 2389 |
--- example, some callbacks like `linebreak_filter` are tagged as |
| 2390 |
--- 'exclusive', only accepting a single callback. While this makes |
| 2391 |
--- sense for the end user, it complicates the situation for nodetree |
| 2392 |
--- to install its non-intrusive, observing-only callbacks. |
| 2393 |
--- |
| 2394 |
--- We thus take the following route. |
| 2395 |
--- |
| 2396 |
--- * If there is no function for callback `<foo>` installed, register |
| 2397 |
--- `callbacks.<foo>`. |
| 2398 |
--- |
| 2399 |
--- * If there is a (single) function for callback `<foo>` of type |
| 2400 |
--- three ('exclusive'), remove it, wrap it into `callbacks.<foo>` |
| 2401 |
--- (via `orig_callbacks`) and install `callbacks.<foo>`. |
| 2402 |
--- |
| 2403 |
--- * Otherwise register `callbacks.<foo>_{before,after}` as |
| 2404 |
--- necessary. |
| 2405 |
--- |
| 2406 |
---@param cb string # The name of a callback. |
| 2407 |
local function register_callback(cb) |
| 2408 |
if luatexbase then |
| 2409 |
local descriptions = luatexbase.callback_descriptions(cb) |
| 2410 |
|
| 2411 |
if #descriptions == 0 then |
| 2412 |
-- No callback installed. If there is no default action |
| 2413 |
-- (according to the LuaTeX manual), use only `before`, ignoring |
| 2414 |
-- the position requested by the user. |
| 2415 |
if not callback_has_default_action[cb] then |
| 2416 |
print_positions[cb] = { 'before', nil } |
| 2417 |
end |
| 2418 |
luatexbase.add_to_callback(cb, callbacks[cb], 'nodetree') |
| 2419 |
elseif luatexbase.callbacktypes[cb] == 3 then |
| 2420 |
-- A single, 'exclusive' callback. |
| 2421 |
orig_callbacks[cb], orig_descriptions[cb] = |
| 2422 |
luatexbase.remove_from_callback(cb, descriptions[1]) |
| 2423 |
luatexbase.add_to_callback(cb, callbacks[cb], 'nodetree') |
| 2424 |
else |
| 2425 |
-- All other callback types. |
| 2426 |
local funcs = {} |
| 2427 |
local descr = {} |
| 2428 |
local before, after = template.get_print_position(cb) |
| 2429 |
|
| 2430 |
-- XXX Is this correct for 'reverselist' callback type? |
| 2431 |
|
| 2432 |
-- This makes the callback wrapper call neither the old nor the |
| 2433 |
-- new function. |
| 2434 |
orig_callbacks[cb] = '' |
| 2435 |
|
| 2436 |
for i, description in ipairs(descriptions) do |
| 2437 |
funcs[i], descr[i] = luatexbase.remove_from_callback(cb, description) |
| 2438 |
end |
| 2439 |
|
| 2440 |
if before then |
| 2441 |
luatexbase.add_to_callback(cb, callbacks[cb .. '_before'], |
| 2442 |
'nodetree before') |
| 2443 |
end |
| 2444 |
for i, _ in ipairs(funcs) do |
| 2445 |
luatexbase.add_to_callback(cb, funcs[i], descr[i]) |
| 2446 |
end |
| 2447 |
if after then |
| 2448 |
luatexbase.add_to_callback(cb, callbacks[cb .. '_after'], |
| 2449 |
'nodetree after') |
| 2450 |
end |
| 2451 |
end |
| 2452 |
else |
| 2453 |
orig_callbacks[cb] = callback.find(cb) |
| 2454 |
callback.register(cb, callbacks[cb]) |
| 2455 |
end |
| 2456 |
end |
| 2457 |
|
| 2458 |
--- Unregister a callback. |
| 2459 |
--- |
| 2460 |
--- We follow the same logic as with `register_callback`. |
| 2461 |
--- |
| 2462 |
---@param cb string # The name of a callback. |
| 2463 |
local function unregister_callback(cb) |
| 2464 |
if luatexbase then |
| 2465 |
local descriptions = luatexbase.callback_descriptions(cb) |
| 2466 |
|
| 2467 |
if #descriptions == 0 then |
| 2468 |
return |
| 2469 |
elseif #descriptions == 1 then |
| 2470 |
luatexbase.remove_from_callback(cb, 'nodetree') |
| 2471 |
if orig_callbacks[cb] then |
| 2472 |
luatexbase.add_to_callback(cb, |
| 2473 |
orig_callbacks[cb], |
| 2474 |
orig_descriptions[cb]) |
| 2475 |
end |
| 2476 |
orig_callbacks[cb] = nil |
| 2477 |
orig_descriptions[cb] = nil |
| 2478 |
else |
| 2479 |
local funcs = {} |
| 2480 |
local descr = {} |
| 2481 |
|
| 2482 |
local i = 1 |
| 2483 |
for _, description in ipairs(descriptions) do |
| 2484 |
if description == 'nodetree before' or |
| 2485 |
description == 'nodetree after' then |
| 2486 |
luatexbase.remove_from_callback(cb, description) |
| 2487 |
else |
| 2488 |
funcs[i], descr[i] = luatexbase.remove_from_callback(cb, |
| 2489 |
description) |
| 2490 |
i = i + 1 |
| 2491 |
end |
| 2492 |
end |
| 2493 |
|
| 2494 |
for n, _ in ipairs(funcs) do |
| 2495 |
luatexbase.add_to_callback(cb, funcs[n], descr[n]) |
| 2496 |
end |
| 2497 |
end |
| 2498 |
else |
| 2499 |
callback.register(cb, nil) |
| 2500 |
callback.register(cb, orig_callbacks[cb]) |
| 2501 |
end |
| 2502 |
end |
| 2503 |
|
| 2504 |
--- Exported functions. |
| 2505 |
--- |
| 2506 |
---@section export |
| 2507 |
|
| 2508 |
local export = { |
| 2509 |
set_option = set_option, |
| 2510 |
set_options = set_options, |
| 2511 |
|
| 2512 |
---@function export.register_callbacks |
| 2513 |
register_callbacks = function() |
| 2514 |
if options.channel == 'log' or options.channel == 'tex' then |
| 2515 |
-- nt = nodetree |
| 2516 |
-- jobname.nttex |
| 2517 |
-- jobname.ntlog |
| 2518 |
local file_name = tex.jobname .. '.nt' .. options.channel |
| 2519 |
io.open(file_name, 'w'):close() -- Clear former content. |
| 2520 |
output_file = io.open(file_name, 'a') |
| 2521 |
end |
| 2522 |
|
| 2523 |
-- Split string at ','. |
| 2524 |
for alias in string.gmatch(options.callback, '([^,]+)') do |
| 2525 |
-- Trim whitespace. |
| 2526 |
alias = string.gsub(alias, '^%s*(.-)%s*$', '%1') |
| 2527 |
|
| 2528 |
-- Check where to position nodetree's inspection callback(s). |
| 2529 |
local before, after |
| 2530 |
if string.sub(alias, 1, 1) == ':' then |
| 2531 |
before = 'before' |
| 2532 |
alias = string.sub(alias, 2, -1) |
| 2533 |
end |
| 2534 |
if string.sub(alias, -1, -1) == ':' then |
| 2535 |
after = 'after' |
| 2536 |
alias = string.sub(alias, 1, -2) |
| 2537 |
end |
| 2538 |
if not before and not after then |
| 2539 |
before = 'before' |
| 2540 |
end |
| 2541 |
local name = get_callback_name(alias) |
| 2542 |
print_positions[name] = {before, after} |
| 2543 |
register_callback(name) |
| 2544 |
end |
| 2545 |
end, |
| 2546 |
|
| 2547 |
---@function export.unregister_callbacks |
| 2548 |
unregister_callbacks = function() |
| 2549 |
for alias in string.gmatch(options.callback, '([^,]+)') do |
| 2550 |
-- Split string at ',', then trim whitespace. For symmetry with |
| 2551 |
-- `register_callbacks`, also remove a leading and/or trailing |
| 2552 |
-- ':' character. |
| 2553 |
unregister_callback( |
| 2554 |
get_callback_name(string.gsub(alias, '^%s*:?(.-):?%s*$', '%1')) |
| 2555 |
) |
| 2556 |
end |
| 2557 |
end, |
| 2558 |
|
| 2559 |
--- Compile a TeX snippet. |
| 2560 |
-- |
| 2561 |
-- Write some TeX snippets into a temporary LaTeX file, compile this |
| 2562 |
-- file using `latexmk`, read the generated `*.nttex` file, and |
| 2563 |
-- return its content. |
| 2564 |
-- |
| 2565 |
---@function export.compile_include |
| 2566 |
-- |
| 2567 |
---@param tex_markup string |
| 2568 |
compile_include = function(tex_markup) |
| 2569 |
-- Generate a subfolder for all tempory files: _nodetree-jobname. |
| 2570 |
local parent_path = lfs.currentdir() .. '/' .. '_nodetree-' .. tex.jobname |
| 2571 |
lfs.mkdir(parent_path) |
| 2572 |
|
| 2573 |
-- Generate the temporary LuaTeX or LuaLaTeX file. |
| 2574 |
example_counter = example_counter + 1 |
| 2575 |
local filename_tex = example_counter .. '.tex' |
| 2576 |
local absolute_path_tex = parent_path .. '/' .. filename_tex |
| 2577 |
output_file = io.open(absolute_path_tex, 'w') |
| 2578 |
|
| 2579 |
local format_option = function(key, value) |
| 2580 |
return '\\NodetreeSetOption[' .. key .. ']{' .. value .. '}' .. '\n' |
| 2581 |
end |
| 2582 |
|
| 2583 |
-- Process the options. |
| 2584 |
local option_lines = |
| 2585 |
format_option('channel', 'tex') .. |
| 2586 |
format_option('verbosity', options.verbosity) .. |
| 2587 |
format_option('unit', options.unit) .. |
| 2588 |
format_option('decimalplaces', options.decimalplaces) .. |
| 2589 |
'\\NodetreeUnregisterCallback{post_linebreak_filter}' .. '\n' .. |
| 2590 |
'\\NodetreeRegisterCallback{' .. options.callback .. '}' |
| 2591 |
|
| 2592 |
local prefix = '%!TEX program = lualatex\n' .. |
| 2593 |
'\\documentclass{article}\n' .. |
| 2594 |
'\\usepackage{nodetree}\n' .. |
| 2595 |
option_lines .. '\n' .. |
| 2596 |
'\\begin{document}\n' |
| 2597 |
local suffix = '\n\\end{document}' |
| 2598 |
if output_file ~= nil then |
| 2599 |
output_file:write(prefix .. tex_markup .. suffix) |
| 2600 |
output_file:close() |
| 2601 |
end |
| 2602 |
|
| 2603 |
-- Compile the temporary LuaTeX or LuaLaTeX file. |
| 2604 |
os.spawn({ 'latexmk', '-cd', '-pdflua', absolute_path_tex }) |
| 2605 |
local include_file = assert(io.open(parent_path .. '/' .. example_counter .. '.nttex', 'r')) |
| 2606 |
local include_content = include_file:read('*all') |
| 2607 |
include_file:close() |
| 2608 |
-- To make the newline character be handled properly by the TeX engine |
| 2609 |
-- it would be necessary to set up its correct catcode. However, it is |
| 2610 |
-- simpler to just replace all newlines with '{}'. |
| 2611 |
include_content = include_content:gsub('[\r\n]', '{}') |
| 2612 |
tex.print(include_content) |
| 2613 |
end, |
| 2614 |
|
| 2615 |
--- Check for `\--shell-escape` within a command or environment. |
| 2616 |
--- |
| 2617 |
---@function export.check_shell_escape |
| 2618 |
--- |
| 2619 |
---@param what string # The name of the command or environment. |
| 2620 |
---@param is_command boolean # Set if `what` is a command. |
| 2621 |
check_shell_escape = function(what, is_command) |
| 2622 |
local info = status.list() |
| 2623 |
if info ~= nil and info.shell_escape ~= 1 then |
| 2624 |
local typ, stuff |
| 2625 |
if is_command == true then |
| 2626 |
typ = 'command' |
| 2627 |
stuff = 'argument' |
| 2628 |
else |
| 2629 |
typ = 'environment' |
| 2630 |
stuff = 'contents' |
| 2631 |
end |
| 2632 |
message( |
| 2633 |
'error', |
| 2634 |
'nodetree-embed', |
| 2635 |
what .. ' needs option --shell-escape', |
| 2636 |
"You must process this document with 'lualatex --shell-escape ...'\n" |
| 2637 |
.. "so that 'latexmk' can be executed to generate the nodetree view\n" |
| 2638 |
.. 'for the ' .. stuff .. ' of this ' .. typ .. '.' |
| 2639 |
) |
| 2640 |
end |
| 2641 |
end, |
| 2642 |
|
| 2643 |
--- Print a node tree. |
| 2644 |
--- |
| 2645 |
---@function export.print |
| 2646 |
--- |
| 2647 |
---@param head Node # The head node of a node list. |
| 2648 |
---@param opts table # Options as a table. |
| 2649 |
print = function(head, opts) |
| 2650 |
if opts and type(opts) == 'table' then |
| 2651 |
set_options(opts) |
| 2652 |
end |
| 2653 |
nodetree_print(format.new_line()) |
| 2654 |
tree.analyze_list(head, 1) |
| 2655 |
end, |
| 2656 |
|
| 2657 |
--- Format a scaled point value as a formatted string. |
| 2658 |
-- |
| 2659 |
---@function export.format_dim |
| 2660 |
--- |
| 2661 |
---@param sp number # A scaled point value. |
| 2662 |
-- |
| 2663 |
---@return string |
| 2664 |
format_dim = function(sp) |
| 2665 |
return template.length(sp) |
| 2666 |
end, |
| 2667 |
|
| 2668 |
--- Get a default option that is not changed. |
| 2669 |
--- |
| 2670 |
---@function export.get_default_option |
| 2671 |
--- |
| 2672 |
---@param key string # The key of the option. |
| 2673 |
-- |
| 2674 |
---@return string|number|boolean |
| 2675 |
get_default_option = function(key) |
| 2676 |
return default_options[key] |
| 2677 |
end, |
| 2678 |
|
| 2679 |
--- Push current options. |
| 2680 |
--- |
| 2681 |
---@function export.push_options |
| 2682 |
push_options = function() |
| 2683 |
prev_options[option_level] = {} |
| 2684 |
for k, v in pairs(options) do |
| 2685 |
prev_options[option_level][k] = v |
| 2686 |
end |
| 2687 |
option_level = option_level + 1 |
| 2688 |
end, |
| 2689 |
|
| 2690 |
--- Pop previous options. |
| 2691 |
--- |
| 2692 |
---@function export.pop_options |
| 2693 |
pop_options = function() |
| 2694 |
if option_level > 0 then |
| 2695 |
prev_options[option_level] = nil |
| 2696 |
option_level = option_level - 1 |
| 2697 |
for k, v in pairs(prev_options[option_level]) do |
| 2698 |
options[k] = v |
| 2699 |
end |
| 2700 |
end |
| 2701 |
end, |
| 2702 |
|
| 2703 |
--- Read a LaTeX input file and emit it immediately, obeying options |
| 2704 |
--- `firstline` and `lastline`. |
| 2705 |
--- |
| 2706 |
---@function export.input |
| 2707 |
--- |
| 2708 |
---@param filename string |
| 2709 |
input = function(filename) |
| 2710 |
local file = assert(io.open(filename, 'r')) |
| 2711 |
local lines_in = {} |
| 2712 |
for line in file:lines() do |
| 2713 |
table.insert(lines_in, line) |
| 2714 |
end |
| 2715 |
|
| 2716 |
local firstline = options.firstline |
| 2717 |
local lastline = options.lastline |
| 2718 |
|
| 2719 |
-- Handle negative line numbers. |
| 2720 |
if firstline < 0 then |
| 2721 |
firstline = #lines_in + 1 + firstline |
| 2722 |
elseif firstline == 0 then |
| 2723 |
firstline = 1 |
| 2724 |
end |
| 2725 |
if lastline < 0 then |
| 2726 |
lastline = #lines_in + 1 + lastline |
| 2727 |
elseif lastline == 0 then |
| 2728 |
lastline = 1 |
| 2729 |
end |
| 2730 |
|
| 2731 |
-- Clamp values. |
| 2732 |
if firstline < 1 then |
| 2733 |
firstline = 1 |
| 2734 |
elseif firstline > #lines_in then |
| 2735 |
firstline = #lines_in |
| 2736 |
end |
| 2737 |
if lastline < 1 then |
| 2738 |
lastline = 1 |
| 2739 |
elseif lastline > #lines_in then |
| 2740 |
lastline = #lines_in |
| 2741 |
end |
| 2742 |
|
| 2743 |
if firstline > lastline then |
| 2744 |
local tmp = firstline |
| 2745 |
firstline = lastline |
| 2746 |
lastline = tmp |
| 2747 |
end |
| 2748 |
|
| 2749 |
local lines_out = {} |
| 2750 |
for i, line in ipairs(lines_in) do |
| 2751 |
if firstline <= i and i <= lastline then |
| 2752 |
table.insert(lines_out, line) |
| 2753 |
end |
| 2754 |
end |
| 2755 |
|
| 2756 |
tex.print(lines_out) |
| 2757 |
end |
| 2758 |
} |
| 2759 |
|
| 2760 |
--- Set to `export.print`. |
| 2761 |
export.analyze = export.print |
| 2762 |
|
| 2763 |
return export |