Debian Codegraph @

texmf-dist/source/lualatex/lua-list-hyphen/lua-list-hyphen.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2026 Alan J. Cain
4 %
5 % This file may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version
6 % 1.3c of this license or (at your option) any later version. The latest version of this license is in:
7 %
8 % http://www.latex-project.org/lppl.txt
9 %
10 % and version 1.3c or later is part of all distributions of LaTeX version 2008-05-04 or later.
11 %
12 % \fi
13 %
14 % \iffalse
15 %<*driver>
16 \PassOptionsToPackage{inline}{enumitem}
17 \documentclass{l3doc}
18
19 \usepackage{polyglossia}
20 \setmainlanguage[variant=british]{english}
21
22
23 \makeatletter
24 \ExplSyntaxOn
25
26 \cs_gset:Npn \l@subsection { \@dottedtocline{2}{2.5em}{2.8em} } % #2 = 1.5em
27 \cs_gset:Npn \l@subsubsection { \@dottedtocline{3}{5.3em}{3.5em} } % #2 = 1.5em
28 \cs_gset:Npn \l@paragraph { \@dottedtocline{4}{8.8em}{3.2em} } % #2 = 1.5em
29
30 \ExplSyntaxOff
31 \makeatother
32
33
34 \usepackage{xcolor}
35
36 \definecolor{linkcolor}{rgb}{0.0,0.4,0.7}
37 \colorlet{citecolor}{linkcolor}
38 \colorlet{urlcolor}{linkcolor}
39
40 \hypersetup{
41 linkcolor=linkcolor,%
42 citecolor=citecolor,%
43 urlcolor=urlcolor,%
44 }
45 \usepackage{xurl}
46 \renewcommand*\UrlBigBreaks{}
47
48 \newcommand*\fullref[2]{%
49 \hyperref[#2]{#1\penalty 200\ \ref*{#2}}%
50 }
51 \newcommand*\fullpageref[1]{%
52 \hyperref[#1]{page\penalty 200\ \pageref*{#1}}%
53 }
54
55
56 \setcounter{tocdepth}{7}
57 \numberwithin{figure}{section}
58
59
60
61 \usepackage{lua-list-hyphen}
62
63 \usepackage{lipsum}
64 \usepackage{tikz}
65
66 \newcommand*\key[1]{\texttt{#1}}
67 \newcommand*\val[1]{\texttt{#1}}
68 \newcommand*\keyvalue[2]{\texttt{#1=#2}}
69
70 \newlist{vallist}{description}{1}
71 \setlist[vallist]{
72 leftmargin=3em,
73 style=unboxed,
74 labelsep=1em,
75 font=\descriptionitemcolon,
76 nosep,
77 }
78 \newcommand*{\descriptionitemcolon}[1]{\kern 1em #1:}
79
80 \NewDocumentCommand{\default}{ m }{(\textit{Default:}\nobreakspace #1)}
81
82
83 \newcommand*\luafunc[1]{\texttt{#1}}
84 \newcommand*\luavar[1]{\texttt{#1}}
85
86
87
88 \newcommand*\prefixedurl[1]{\textsc{url}:~\url{#1}}
89
90
91
92 \begin{document}
93
94 \DocInput{lua-list-hyphen.dtx}
95
96 \PrintIndex
97
98 \end{document}
99 %</driver>
100 % \fi
101 %
102 %
103 %
104 % \GetFileInfo{lua-list-hyphen.sty}
105 %
106 %
107 %
108 % \title{^^A
109 % \pkg{lua-list-hyphen} ^^A
110 % --- Per-language listing of hyphenated words for Lua\LaTeX^^A
111 % \footnote{This document describes \fileversion, last revised \filedate.}^^A
112 % }
113 %
114 % \author{^^A
115 % Alan J. Cain\footnote{\texttt{a.j.cain (AT) gmail.com}}^^A
116 % }
117 %
118 % \date{Released \filedate}
119 %
120 % \maketitle
121 %
122 %
123 %
124 % \begin{abstract}
125 % This Lua\LaTeX\ package writes each word that has been hyphenated across lines to a file, using a different file for
126 % each language, for subsequent external checking.
127 % \end{abstract}
128 %
129 %
130 %
131 % \tableofcontents
132 %
133 %
134 %
135 % \begin{documentation}
136 %
137 %
138 %
139 % \section{Introduction}
140 %
141 % \TeX's algorithm for finding points where a word can be hyphenated is good, but not perfect.\footnote{For a
142 % description of the algorithm and its limitations, see Knuth's account in Appendix~H of \textit{The \TeX book}
143 % (Addison-Wesley, 2021. ISBN:~\texttt{978-0-201-13447-6})} The present author writes in British English, where the
144 % valid division points can depend on both the pronunciation of a word and its internal structure (and hence its
145 % etymology). Currently, \TeX's pattern-based approach produces \textit{bio-lo-gic}, \textit{bio-logy},
146 % \textit{bio-lo-gist}, rather than the standard \textit{bio-logic}, \textit{biol-ogy},
147 % \textit{biolo-gist}.\footnote{See the \textit{New Oxford Spelling Dictionary}, which is the authority for word
148 % divisions in British English (Oxford University Press, 2005. ISBN:~\texttt{978-0-19-860881-3}).} To deal with such
149 % cases, at least a substantially larger number of patterns would be required than are available at present. There are
150 % also various words where the valid division points in British English cannot be deduced from their spelling alone: for
151 % instance, the verbs \textit{at-trib-ute}, \textit{pre-sent}, \textit{pro-duce}, \textit{re-cord} have different
152 % division points from the orthographically identical nouns \textit{at-tri-bute}, \textit{pres-ent}, \textit{prod-uce},
153 % \textit{rec-ord}. For another example, compare \textit{cur-ric-ulum vitae} and \textit{school cur-ricu-lum}.
154 %
155 % Easy checking of the chosen hyphenations is desirable. With Lua\TeX, it is possible to extract the hyphenated words.
156 % The Lua\LaTeX\ package \pkg{lua-check-hyphen} offers this facility. It checks hyphenated words against a whitelist,
157 % visually flags unknown hyphenations, and writes unknown hyphenations to a file. But it was first written in 2012, when
158 % Lua\TeX\ was at an earlier stage of development, and so it has certain problems, such as with words containing
159 % ligatures. It also lacks multi-language support.
160 %
161 % This Lua\LaTeX\ package, \pkg{lua-list-hyphen}, uses some ideas from \pkg{lua-check-hyphen} but was written from
162 % scratch to work with a modern Lua\TeX. It simply writes hyphenated words from each language to a separate file, so
163 % that they can be checked (manually or by an external program).
164 %
165 % [The author has written a simple Python application \texttt{hyphenassist}\footnote{\textsc{url}:
166 % \url{https://codeberg.org/ajcain/hyphenassist}.} that checks the listed hyphenations against a dictionary of valid
167 % divisions and allows the user to quickly choose to add entries to the division dictionary, add hyphenation exceptions,
168 % or ignore particular hyphenations. He has used this program in conjunction with code incorporated into this package to
169 % check hyphenations in his own books.\footnote{In particular, \textit{Form \& Number: A History of Mathematical
170 % Beauty}. \textsc{url}: \url{https://archive.org/details/cain_formandnumber_ebook_large}.}]
171 %
172 %
173 %
174 % \paragraph*{Licence.} \noindent\pkg{lua-list-hyphen} is released under the \LaTeX\ Project Public Licence v1.3c or
175 % later.\footnote{\textsc{url}: \url{https://www.latex-project.org/lppl.txt}}
176 %
177 %
178 %
179 % \paragraph*{Acknowledgements.} The author thanks Keno Wehr for corrections and comments on the documentation.
180 %
181 %
182 %
183 % \paragraph*{Feature requests and bug reports}
184 %
185 % The development code and issue tracker are hosted at Codeberg.\footnote{\textsc{url}:
186 % \url{https://codeberg.org/ajcain/lua-list-hyphen}}
187 %
188 %
189 %
190 % \section{Requirements}
191 %
192 % \pkg{lua-list-hyphen} requires
193 % \begin{enumerate}[label={(\arabic*)}]
194 % \item Lua\LaTeX,
195 % \item a recent \LaTeX\ kernel with \pkg{expl3} support (any kernel version since 2020-02-02 should suffice).
196 % \end{enumerate}
197 % It does not depend on any other packages, but will interface with \pkg{babel} or \pkg{polyglossia} (if one of them is
198 % loaded) to determine language names.
199 %
200 %
201 %
202 % \section{Installation}
203 %
204 % To install \pkg{lua-list-hyphen} manually, run \texttt{luatex lua-list-hyphen.ins} and copy
205 % \texttt{lua-list-hyphen.sty} and \texttt{lua-list-hyphen.lua} to somewhere Lua\LaTeX\ can find them.
206 %
207 %
208 %
209 % \section{Getting started}
210 %
211 % Simply load the package; the hyphenated words are by default written to the file
212 % \cs{jobname}\file{-}\meta{lang-id}\file{.hyph}, without being sorted or having duplicates removed. The \meta{lang-id}
213 % is either a Lua\TeX\ numerical language~ID, or a \pkg{babel} or \pkg{polyglossia} name of the language, if one of
214 % these packages is in use. The prefix \cs{jobname}\file{-} and the extension \file{.hyph} can be customized; see
215 % \fullref{Section}{sec:options}.
216 %
217 %
218 %
219 % \section{Package options}
220 % \label{sec:options}
221 %
222 % \DescribeOption{verbose} The boolean option \key{verbose} controls how much information is written to the file about
223 % each hyphenated word. When \val{true}, for each hyphenated word, both the undivided original and the divided word are
224 % written out, as well as the page number on which the hyphenated word appears (or, more precisely, begins) and the
225 % undivided word in context (as specified by the \key{context} keys; see below). When \val{false}, only the hyphenated
226 % word is written. \default{\val{false}}
227 %
228 % \DescribeOption{context}
229 % \DescribeOption{context-before}
230 % \DescribeOption{context-after}
231 % Integer options controlling how many words before (\key{context-before}) and after (\key{context-after}) the
232 % hyphenated word are written as context when \keyvalue{verbose}{true}. The key \key{context} is simply a shortcut for
233 % setting \key{context-before} and \key{context-after} to the same value. \default{\val{2}}
234 %
235 % \medskip
236 % \DescribeOption{unique}
237 % The option \key{unique} controls removal of duplicates from the list of hyphenated words written out. It can be be
238 % set to one of the following three values:
239 % \begin{vallist}
240 % \item[\val{none}] Duplicate hyphenations are not removed.
241 % \item[\val{case}] Hyphenations that are duplicate (case-sensitively) are removed. In this case, the hyphenations
242 % \texttt{geo-metry} and \texttt{Geo-metry} are considered to be distinct.
243 % \item[\val{nocase}] Hyphenations that are duplicate (case-insensitively) are removed. In this case, the hyphenations
244 % \texttt{geo-metry} and \texttt{Geo-metry} are considered to be duplicates. The case of each listed hyphenation
245 % will be that of the first appearance of that hyphenation.
246 % \end{vallist}
247 % Note that removal of duplicates is unaffected by the page number or context that is written out when
248 % \keyvalue{verbose}{true}. \default{\val{none}}
249 %
250 % \DescribeOption{sort}
251 % The option \key{sort} controls sorting of the list of hyphenated words. It can be be
252 % set to one of the following three values:
253 % \begin{vallist}
254 % \item[\val{none}] Hyphenations appear in the same order as the occur in the document, or, if duplicates are removed,
255 % in the order of first appearance in the document.
256 % \item[\val{case}] Hyphenations are sorted case-sensitively. In this case, \texttt{Geo-metry} precedes
257 % \texttt{geo-meter}.
258 % \item[\val{nocase}] Hyphenations are sorted case-insensitively. In this case, \texttt{geo-meter} precedes
259 % \texttt{Geo-metry}.
260 % \end{vallist}
261 % \default{\val{none}}
262 %
263 % \DescribeOption{include-non-output}
264 % Boolean option determining whether hyphenated words that are never written to the page are listed. (For instance,
265 % a hyphenated word might occur in text that a package temporarily typesets into a box, measures, and then discards.)
266 % \default{\val{false}}
267 %
268 % \medskip
269 % The two options \key{prefix} and \key{extension} specify the files to which hyphenations are written. Between the
270 % prefix and the extension is either a Lua\TeX\ numerical language~ID, or a \pkg{babel} or \pkg{polyglossia}
271 % name of the language, if one of these packages is in use.
272 %
273 % \DescribeOption{prefix}
274 % The \key{prefix} is the part of the file name to which the list of hyphenated words is written, before the
275 % language~ID.
276 % \default{\cs{jobname}\file{-} (note the hyphen).}
277 %
278 % \DescribeOption{extension}
279 % The extension of the file (including the \file{.}) to which the list of hyphenated words for each language is written.
280 % \default{\file{.hyph}}
281 %
282 % \medskip
283 % \DescribeOption{debug}
284 % The boolean option \key{debug} controls whether debugging information is written to the terminal.
285 % \default{\val{false}}
286 %
287 %
288 %
289 % \section{Output format}
290 %
291 % Each output file begins with a header (each line of which begins with a ‘comment’ symbol \texttt{\%}) that includes
292 % information about the language and the package options that were used. Each line of the remainder of the file
293 % describes one hyphenation.
294 %
295 % When \keyvalue{verbose}{false}, the line contains only the hyphenated word.
296 %
297 % ^^A Use \symbol{34} in this paragraph since " is an active character.
298 % When \keyvalue{verbose}{true}, the line contains the original undivided word, the hyphenated word, the page number
299 % where the hyphenated word appears (or, to be precise, begins), and the context in which the hyphenated word appears.
300 % Each part of the output is padded so that the various lines align. The original and undivided words are separated by
301 % the ASCII ‘arrow’ \texttt{->}; the page number is prefixed by \texttt{p.}; and the context is surrounded by (straight)
302 % quotation marks \texttt{\symbol{34}}\kern .5em\texttt{\symbol{34}}. If the hyphenation was never written to the page,
303 % \texttt{p.}\meta{page} is replaced by \texttt{<none>}. (This can only happen with
304 % \keyvalue{include-non-output}{true}.)
305 %
306 %
307 %
308 % \section{Usage notes}
309 %
310 % \subsection{Languages}
311 %
312 % To determine the language of a word, \pkg{lua-list-hyphen} looks at what language is applied at the first possible
313 % hyphenation point, first considering the part of the word before it, then the part after it. In the (presumably rare)
314 % case of a ‘mixed-language’ word like ‘near-Zugzwang’ being specified (using, for example, \pkg{babel}) with
315 % \texttt{near-\cs{foreignlanguage}\{german\}\{Zugzwang\}}, it would be assigned to the language in which \hbox{‘near-’}
316 % is set.
317 %
318 % Duplicates are removed within each language. If the same hyphenation occurs in two different languages, it will appear
319 % in both files, regardless of the value of the \key{unique} package option.
320 %
321 %
322 %
323 % \subsection{Limitations}
324 %
325 % \pkg{lua-list-hyphen} uses Lua\TeX's built-in Unicode functions for pattern matching and converting between upper and
326 % lower case. These function are based on the \texttt{slnunicode} library. This library has not been updated for some
327 % time and is based on an out-of-date version of the Unicode standard. Thus there may be problems with languages added
328 % to Unicode more recently. Hyphenated words from such languages should still be listed, but may contain extraneous
329 % characters (such as adjacent punctuation) and may not be sorted correctly. Users may prefer to leave sorting and
330 % removal of duplicates to an external program that adheres to the current Unicode standard.
331 %
332 %
333 %
334 % \end{documentation}
335 %
336 %
337 %
338 % \clearpage
339 % \begin{implementation}
340 %
341 %
342 %
343 % \section{Implementation (\LaTeX\ package)}
344 %
345 % \begin{macrocode}
346 %<*package>
347 %<@@=lualisthyphen>
348 % \end{macrocode}
349 %
350 %
351 %
352 % \subsection{Initial set-up}
353 %
354 % Package identification/version information.
355 % \begin{macrocode}
356 \NeedsTeXFormat{LaTeX2e}[2020-02-02]
357 \ProvidesExplPackage{lua-list-hyphen}{2026-05-27}{0.3.20}
358 {Listing hyphenated words for LuaLaTeX}
359 % \end{macrocode}
360 % Check that Lua\TeX\ is in use.
361 % \begin{macrocode}
362 \sys_if_engine_luatex:F
363 {
364 \msg_new:nnn{ lua-list-hyphen }{ lualatex_required }
365 { LuaLaTeX~required.~Package~loading~will~abort. }
366 \msg_critical:nn{ lua-list-hyphen }{ lualatex_required }
367 }
368 % \end{macrocode}
369 %
370 %
371 %
372 % \subsection{Options}
373 %
374 % \begin{macro}{\l_@@_verbose_bool}
375 % Boolean option to indicate whether lists of hyphenations should be written verbosely.
376 % \begin{macrocode}
377 \keys_define:nn { lua-list-hyphen }{
378 verbose .bool_set:N = \l_@@_verbose_bool,
379 }
380 % \end{macrocode}
381 % \end{macro}
382 %
383 %
384 %
385 % \begin{macro}{\l_@@_context_before_int,\l_@@_context_after_int}
386 % Integer options to determine the number of words before and after a hyphenation shown as context in verbose output.
387 % \begin{macrocode}
388 \keys_define:nn { lua-list-hyphen }{
389 context-before .int_set:N = \l_@@_context_before_int,
390 context-before .initial:n = { 2 },
391 context-after .int_set:N = \l_@@_context_after_int,
392 context-after .initial:n = { 2 },
393 context .code:n = {
394 \keys_set:nn{ lua-list-hyphen }{
395 context-before=#1,
396 context-after=#1,
397 }
398 },
399
400 }
401 % \end{macrocode}
402 % \end{macro}
403 %
404 %
405 %
406 % \begin{macro}{\l_@@_unique_int}
407 % Choice option to indicate whether lists of hyphenations should have duplicates removed, case-sensitively or
408 % case-insensitively.
409 % \begin{macrocode}
410 \int_new:N\l_@@_unique_int
411 \keys_define:nn { lua-list-hyphen }{
412 unique .choices:nn = { none, case, nocase }{
413 \int_set:Nn\l_@@_unique_int{ \l_keys_choice_int - 1 }
414 },
415 }
416 % \end{macrocode}
417 % \end{macro}
418 %
419 %
420 %
421 % \begin{macro}{\l_@@_sort_int}
422 % Choice option to indicate whether lists of hyphenations should be sorted, case-sensitively or case-insensitively.
423 % \begin{macrocode}
424 \int_new:N\l_@@_sort_int
425 \keys_define:nn { lua-list-hyphen }{
426 sort .choices:nn = { none, case, nocase }{
427 \int_set:Nn\l_@@_sort_int{ \l_keys_choice_int - 1 }
428 },
429 }
430 % \end{macrocode}
431 % \end{macro}
432 %
433 %
434 %
435 %
436 % \begin{macro}{\l_@@_include_non_output_bool}
437 % Boolean option to indicate whether lists of hyphenations should include those that are never output to the page.
438 % \begin{macrocode}
439 \keys_define:nn { lua-list-hyphen }{
440 include-non-output .bool_set:N = \l_@@_include_non_output_bool,
441 }
442 % \end{macrocode}
443 % \end{macro}
444 %
445 %
446 %
447 % \begin{macro}{\l_@@_file_prefix_str}
448 % String option for the prefix of files to which hyphenations are writtten.
449 % \begin{macrocode}
450 \keys_define:nn { lua-list-hyphen }{
451 prefix .str_set:N = \l_@@_file_prefix_str,
452 prefix .initial:e = { \c_sys_jobname_str- },
453 }
454 % \end{macrocode}
455 % \end{macro}
456 %
457 %
458 %
459 % \begin{macro}{\l_@@_file_extension_str}
460 % String option for the extension of files to which hyphenations are writtten.
461 % \begin{macrocode}
462 \keys_define:nn { lua-list-hyphen }{
463 extension .str_set:N = \l_@@_file_extension_str,
464 extension .initial:n = { .hyph },
465 }
466 % \end{macrocode}
467 % \end{macro}
468 %
469 %
470 %
471 % \begin{macro}{
472 % \l_@@_debug_int
473 % }
474 % Option to specify whether debug information is written to the terminal. Not intended for end users.
475 % \begin{macrocode}
476 \int_new:N\l_@@_debug_int
477 \keys_define:nn { lua-list-hyphen }{
478 debug .code:n = {\int_set_eq:NN\l_@@_debug_int\c_one_int}
479 }
480 % \end{macrocode}
481 % \end{macro}
482 %
483 %
484 %
485 % \subsection{Processing package options}
486 %
487 % Process package options.
488 % \begin{macrocode}
489 \ProcessKeyOptions [ lua-list-hyphen ]
490 % \end{macrocode}
491 %
492 %
493 %
494 % Convert boolean options to integers (which can be accessed from Lua).
495 % \begin{macrocode}
496 \int_new:N\l_@@_verbose_int
497 \bool_if:NT\l_@@_verbose_bool
498 { \int_set_eq:NN\l_@@_verbose_int\c_one_int }
499 \int_new:N\l_@@_include_non_output_int
500 \bool_if:NT\l_@@_include_non_output_bool
501 { \int_set_eq:NN\l_@@_include_non_output_int\c_one_int }
502 % \end{macrocode}
503 %
504 %
505 %
506 % \subsection{Lua backend}
507 %
508 % Load the Lua backend.
509 % \begin{macrocode}
510 \lua_now:n{
511 lualisthyphen = require('lua-list-hyphen')
512 }
513 % \end{macrocode}
514 %
515 %
516 %
517 % \subsection{Saving \pkg{babel} language names}
518 %
519 % At \texttt{enddocument/afterlastpage}, if possible save \pkg{babel}'s language names. (\pkg{polyglossia}'s names can
520 % be found directly from Lua.)
521 % \begin{macrocode}
522 \hook_gput_code:nnn{ enddocument/afterlastpage }{ lua-list-hyphen } {
523 \@@_babel_save_language_names:
524 }
525 % \end{macrocode}
526 %
527 % \begin{macro}{\@@_babel_save_language_names:}
528 % If \pkg{babel} is in use, get language names from \cs{bbl@languages}.
529 % \begin{macrocode}
530 \cs_new:Npn \@@_babel_save_language_names:
531 {
532 \cs_if_exist:NT\bbl@languages
533 {
534 % \end{macrocode}
535 % Iterate through \cs{bbl@languages} to get language names. Items stored in this macro are quadruples prefixed with
536 % \cs{bbl@elt}, so locally redefine this latter macro to an auxiliary function that passes language ID/name pairs to
537 % the Lua backend.
538 % \begin{macrocode}
539 \group_begin:
540 \cs_set_eq:NN
541 \bbl@elt
542 \@@_babel_save_language_names_elt:nnnn
543 \bbl@languages
544 \group_end:
545 }
546 }
547 % \end{macrocode}
548 % \end{macro}
549 %
550 % \begin{macro}{\@@_babel_save_language_names_elt:nnnn}
551 % Auxiliary function that takes a quadruple stored in \cs{bbl@languages} and passes language ID/name pairs to the Lua
552 % backend.
553 % \begin{macrocode}
554 \cs_new:Npn \@@_babel_save_language_names_elt:nnnn #1#2#3#4
555 {
556 \lua_now:n{
557 lualisthyphen.babel_save_language_name(#2,'#1')
558 }
559 }
560 % \end{macrocode}
561 % \end{macro}
562 %
563 %
564 %
565 % \subsection{Processing and writing hyphenation lists}
566 %
567 % At \texttt{enddocument/info}, process and output the hyphenations that have been found.
568 % \begin{macrocode}
569 \hook_gput_code:nnn{ enddocument/info }{ lua-list-hyphen } {
570 \@@_process_write_hyphenation_lists:ee
571 {\str_use:N\l_@@_file_prefix_str}
572 {\str_use:N\l_@@_file_extension_str}
573 }
574 % \end{macrocode}
575 %
576 %
577 %
578 % \begin{macro}{\@@_process_write_hyphenation_lists:nn}
579 % Sort the list of hyphenations into separate lists for each language, sort and deduplicate them as required, and
580 % write them to files with prefix given in the first parameter and suffix in the second.
581 % \begin{macrocode}
582 \cs_new:Npn \@@_process_write_hyphenation_lists:nn #1#2
583 {
584 \lua_now:e{
585 lualisthyphen.process_write_hyphenation_lists(
586 '\luaescapestring{#1}',
587 '\luaescapestring{#2}'
588 )
589 }
590 }
591 \cs_generate_variant:Nn
592 \@@_process_write_hyphenation_lists:nn
593 { ee }
594 % \end{macrocode}
595 % \end{macro}
596 %
597 %
598 %
599 % \begin{macrocode}
600 %</package>
601 % \end{macrocode}
602 %
603 %
604 %
605 % \section{Implementation (Lua backend)}
606 %
607 % \begin{macrocode}
608 %<*lua>
609 % \end{macrocode}
610 %
611 %
612 %
613 % \subsection{Debugging function}
614 %
615 % \begin{macro}[int]{debug}
616 % Debugging function. Defined according to the package option \key{debug} to either do nothing or write debugging
617 % information.
618 % \begin{macrocode}
619 local debug
620
621 if tex.count['l__lualisthyphen_debug_int'] == 0 then
622 debug = function(s)
623 end
624 else
625 debug = function(s)
626 print('lua-list-hyphen DEBUG: ' .. s)
627 end
628 end
629 % \end{macrocode}
630 % \end{macro}
631 %
632 %
633 %
634 % \subsection{Table key constants}
635 %
636 % Keys for tables containing hyphenatable/hyphenated word data.
637 % \begin{macrocode}
638 local KEY_TYPE = 'type'
639 local KEY_WORD = 'word'
640 local KEY_LANG = 'lang'
641 local KEY_DIVISION = 'division'
642 local KEY_INDEX = 'index'
643 local KEY_CONTEXT = 'context'
644 local KEY_PAGE = 'page'
645 % \end{macrocode}
646 %
647 %
648 %
649 % \subsection{Segment type}
650 %
651 % Constants for types of segments found while scanning hlist before linebreaking.
652 % \begin{macrocode}
653 local SEGMENT_WORD = 0
654 local SEGMENT_SPACE = 1
655 local SEGMENT_MATH = 2
656 % \end{macrocode}
657 %
658 %
659 %
660 % \subsection{Node ID and subtype constants}
661 %
662 % Define constants for the node IDs that need to be recognized.
663 % \begin{macrocode}
664 local NODE_ID_HLIST = node.id('hlist')
665 local NODE_ID_DISC = node.id('disc')
666 local NODE_ID_GLUE = node.id('glue')
667 local NODE_ID_KERN = node.id('kern')
668 local NODE_ID_MARGIN_KERN = node.id('margin_kern')
669 local NODE_ID_GLYPH = node.id('glyph')
670 local NODE_ID_MATH = node.id('math')
671 % \end{macrocode}
672 % Define constants for the kern node subtypes that have to be recognized. (There seems to be no automatic way to get
673 % the numerical value from the subtype text other than searching the \luavar{node.subtype(\meta{node type})} tables.)
674 % \begin{macrocode}
675 local NODE_KERN_SUBTYPE_FONTKERN
676 local NODE_KERN_SUBTYPE_USERKERN
677 for k,v in pairs(node.subtypes('kern')) do
678 if v == 'fontkern' then
679 NODE_KERN_SUBTYPE_FONTKERN = k
680 elseif v == 'userkern' then
681 NODE_KERN_SUBTYPE_USERKERN = k
682 end
683 end
684 % \end{macrocode}
685 % Define constants for the math node subtypes.
686 % \begin{macrocode}
687 local NODE_MATH_SUBTYPE_BEGIN
688 local NODE_MATH_SUBTYPE_END
689 for k,v in pairs(node.subtypes('math')) do
690 if v == 'beginmath' then
691 NODE_MATH_SUBTYPE_BEGIN = k
692 elseif v == 'endmath' then
693 NODE_MATH_SUBTYPE_END = k
694 end
695 end
696 % \end{macrocode}
697 %
698 %
699 %
700 % \subsection{Output constants}
701 %
702 % Constants for output.
703 % \begin{macrocode}
704 local STR_MATH = '[MATH]'
705 local STR_SPACE = ' '
706 local STR_SPACE_TWO = ' '
707 local STR_ARROW = ' -> '
708 local STR_PAGE_PREFIX = 'p.'
709 local STR_PAGE_NONE = '<none>'
710 local STR_QUOTE_OPEN = '"'
711 local STR_QUOTE_CLOSE = '"'
712 % \end{macrocode}
713 %
714 %
715 %
716 % \subsection{Utility functions}
717 %
718 % \begin{macro}[int]{list_filter}
719 % Take a list \luavar{t} and remove from it any elements for which the function
720 % \luavar{f} does not return true. (The index \luavar{j} is always the destination index to which a ‘keep’ element
721 % is moved.)\footnote{Code adapted from \url{https://stackoverflow.com/a/53038524}.}
722 % \begin{macrocode}
723 local function list_filter(t, f)
724 local j = 1
725 local n = #t
726
727 for i=1,n do
728 if (f(t[i])) then
729 if (i ~= j) then
730 t[j] = t[i]
731 t[i] = nil
732 end
733 j = j + 1
734 else
735 t[i] = nil
736 end
737 end
738
739 end
740 % \end{macrocode}
741 % \end{macro}
742 %
743 %
744 %
745 % \begin{macro}[int]{list_uniq}
746 % Take a list \luavar{t} and remove from it adjacent elements for which the function \luavar{f} returns true. (The
747 % index \luavar{j} is always the last ‘kept’ element.)
748 % \begin{macrocode}
749 local function list_uniq(t, f)
750 local j = 1
751 local n = #t
752
753 for i=2,n do
754 if (f(t[i],t[j])) then
755 t[i] = nil
756 else
757 j = i
758 end
759 end
760
761 list_filter(
762 t,
763 function(a) return a end
764 )
765 end
766 % \end{macrocode}
767 % \end{macro}
768 %
769 %
770 %
771 % \subsection{Getting text from nodes}
772 %
773 % Getting the components of the ligatures that have Unicode code points can be problematic, at least for some fonts,
774 % so define a lookup table for these cases.
775 % \begin{macrocode}
776 local LIGATURE_TEXT = {
777 [0xfb00] = 'ff',
778 [0xfb01] = 'fi',
779 [0xfb02] = 'fl',
780 [0xfb03] = 'ffi',
781 [0xfb04] = 'ffl',
782 }
783 % \end{macrocode}
784 %
785 %
786 %
787 % Cache to save table lookups when extracting text.
788 % \begin{macrocode}
789 local font_characters = {}
790 % \end{macrocode}
791 %
792 %
793 %
794 % Extracting text from nodes uses two functions that call each other, so the names have to be defined ahead of time.
795 % \begin{macrocode}
796 local get_node_text
797 local get_nodelist_text
798 % \end{macrocode}
799 %
800 %
801 %
802 % \begin{macro}[int]{get_node_text}
803 % Return the text content of a glyph node (which might be a normal glyph, a ligature, etc.).
804 % \begin{macrocode}
805 get_node_text = function(n)
806
807 if n.id == NODE_ID_GLYPH then
808
809 local ligature_text = LIGATURE_TEXT[n.char]
810 if ligature_text ~= nil then
811 return ligature_text
812 elseif n.components then
813 return get_nodelist_text(n.components)
814 else
815 -- See [https://tug.org/pipermail/luatex/2018-March/006786.html]
816 local characters = font_characters[n.font]
817 if not characters then
818 characters = fonts.hashes.identifiers[n.font].characters
819 font_characters[n.font] = characters
820 end
821 % \end{macrocode}
822 % In looking up the text, things can go wrong (e.g. the glyph might not be part of the version of unicode than is
823 % supported by the underlying \luavar{slnunicode} library). So use \luafunc{pcall} and return an empty string if
824 % there is an error.
825 % \begin{macrocode}
826 local ok,retval = pcall(
827 function ()
828 return utf8.char(tonumber(characters[n.char].tounicode,16))
829 end
830 )
831 if ok then
832 return retval
833 else
834 return ''
835 end
836 end
837
838 elseif n.id == NODE_ID_DISC then
839
840 if n.replace then
841 return get_nodelist_text(n.replace)
842 else
843 return ''
844 end
845
846 else
847 return ''
848 end
849
850 end
851 % \end{macrocode}
852 % \end{macro}
853 %
854 %
855 %
856 % \begin{macro}[int]{get_nodelist_text}
857 % Return the text content of the glyph nodes in the list starting at \luavar{head} up to and including the node
858 % \luavar{last}, or up to the end of the list if \luavar{last} is not specified.
859 % \begin{macrocode}
860 get_nodelist_text = function (head,last)
861
862 local text = ''
863
864 for item in node.traverse(head) do
865
866 text = text .. get_node_text(item)
867
868 if item == last then
869 break
870 end
871 end
872
873 return text
874
875 end
876 % \end{macrocode}
877 % \end{macro}
878 %
879 %
880 %
881 % \begin{macro}[int]{is_possible_word_node}
882 % Return boolean indicating if node \luavar{n} could be part of a word. Assume that \luavar{glyph}, \luavar{disc},
883 % and \luavar{margin_kern} nodes could be part of a word, as could a \luavar{kern} node with subtype
884 % \luavar{fontkern}.
885 % \begin{macrocode}
886 local function is_possible_word_node(n)
887
888 return (
889 n.id == NODE_ID_GLYPH
890 or
891 n.id == NODE_ID_DISC
892 or
893 (n.id == NODE_ID_KERN and n.subtype == NODE_KERN_SUBTYPE_FONTKERN)
894 or
895 n.id == NODE_ID_MARGIN_KERN
896 )
897
898 end
899 % \end{macrocode}
900 % \end{macro}
901 %
902 %
903 %
904 % \begin{macro}[int]{is_possible_space_node}
905 % Return boolean indicating if node \luavar{n} could be part of a space. Assume that \luavar{glue} nodes could be
906 % part of a space, as could a \luavar{kern} node with subtype \luavar{userkern}.
907 % \begin{macrocode}
908 local function is_possible_space_node(n)
909
910 return (
911 n.id == NODE_ID_GLUE
912 or
913 (n.id == NODE_ID_KERN and n.subtype == NODE_KERN_SUBTYPE_USERKERN)
914 )
915
916 end
917 % \end{macrocode}
918 % \end{macro}
919 %
920 %
921 %
922 % \subsection{String manipulation}
923 %
924 % \begin{macro}[int]{trim_nonlettershyphens_both}
925 % Remove characters other than letters and hyphens from both the start and end of a string.
926 % \begin{macrocode}
927 local function trim_nonlettershyphens_both(s)
928
929 return unicode.utf8.match(s,'^[^%a-]*(.-)[^%a-]*$')
930
931 end
932 % \end{macrocode}
933 % \end{macro}
934 %
935 %
936 %
937 % \begin{macro}[int]{trim_nonlettershyphens_start}
938 % Remove characters other than letters and hyphens from the start of a string.
939 % \begin{macrocode}
940 local function trim_nonlettershyphens_start(s)
941
942 return unicode.utf8.match(s,'^[^%a-]*(.-)$')
943
944 end
945 % \end{macrocode}
946 % \end{macro}
947 %
948 %
949 %
950 % \begin{macro}[int]{trim_nonlettershyphens_end}
951 % Remove characters other than letters and hyphens from the end of a string.
952 % \begin{macrocode}
953 local function trim_nonlettershyphens_end(s)
954
955 return unicode.utf8.match(s,'^(.-)[^%a-]*$')
956
957 end
958 % \end{macrocode}
959 % \end{macro}
960 %
961 %
962 %
963 % \begin{macro}[int]{rpad}
964 % Return string \luavar{s} padded on the right with spaces to length \luavar{n}.
965 % \begin{macrocode}
966 local function rpad(s,n)
967
968 return s .. unicode.utf8.rep(STR_SPACE,n - unicode.utf8.len(s))
969
970 end
971 % \end{macrocode}
972 % \end{macro}
973 %
974 % \begin{macro}[int]{lpad}
975 % Return string \luavar{s} padded on the left with spaces to length \luavar{n}.
976 % \begin{macrocode}
977 local function lpad(s,n)
978
979 return unicode.utf8.rep(STR_SPACE,n - unicode.utf8.len(s)) .. s
980
981 end
982 % \end{macrocode}
983 % \end{macro}
984 %
985 %
986 %
987 % \subsection{Pre-linebreak processing}
988 %
989 % Before each line has been broken, find all potential division points and store the words in which they occur,
990 % linking each potential break point to the corresponding word.
991 %
992 % Declare a new attribute, which will be used to store in each disc node the index of the corresponding word in the
993 % table \luavar{hlist_segment_list}.
994 % \begin{macrocode}
995 local hyphen_attr = luatexbase.new_attribute('hyphen_attr')
996 % \end{macrocode}
997 %
998 %
999 %
1000 % Table to hold segments (word/space/math) in the hlist that will be broken. This table will be cleared after the
1001 % post-linebreak processing.
1002 % \begin{macrocode}
1003 local hlist_segment_list = {}
1004 % \end{macrocode}
1005 %
1006 %
1007 %
1008 % \begin{macro}[int]{get_first_glyph_lang}
1009 % Return the lang attribute of the first glyph in the the part of the list starting n that could be part of a word.
1010 % (Currently unused; see the documentation of \luafunc{get_disc_lang}.)
1011 % \begin{macrocode}
1012 -- local function get_first_glyph_lang(n)
1013
1014 -- local item = n
1015 -- while item and is_possible_word_node(item) do
1016 -- if item.id == NODE_ID_GLYPH then
1017 -- return item.lang
1018 -- end
1019 -- item = item.next
1020 -- end
1021
1022 -- return nil
1023
1024 -- end
1025 % \end{macrocode}
1026 % \end{macro}
1027 %
1028 %
1029 %
1030 % \begin{macro}[int]{get_disc_lang}
1031 % Try to find the language ID in force at a given disc node by looking at (1)~the last glyph in the word
1032 % before the disc node; (2)~the first glyph in the word after the disc node. Default to language ID \luavar{0}.
1033 %
1034 % (Looking at \luavar{replace}, \luavar{pre}, \luavar{post} is possible, but is unreliable and so disabled for the
1035 % present. The author has encountered the situation where an explicit hyphen results in the hyphen characters in
1036 % \luavar{replace} and \luavar{pre} having different language IDs. He has not had time to investigate how this
1037 % arises from the interaction of \pkg{babel}/\pkg{polyglossia} and Lua\LaTeX.)
1038 % \begin{macrocode}
1039 local function get_disc_lang(n)
1040
1041 -- lang = get_first_glyph_lang(n.replace)
1042 -- if lang then
1043 -- print(lang)
1044 -- return lang
1045 -- end
1046
1047 -- lang = get_first_glyph_lang(n.pre)
1048 -- if lang then
1049 -- print(lang)
1050 -- return lang
1051 -- end
1052
1053 -- lang = get_first_glyph_lang(n.post)
1054 -- if lang then
1055 -- return lang
1056 -- end
1057
1058 local item
1059 % \end{macrocode}
1060 % Before the disc node.
1061 % \begin{macrocode}
1062 item = n
1063 while item and is_possible_word_node(item) do
1064 if item.id == NODE_ID_GLYPH then
1065 return item.lang
1066 end
1067 item = item.prev
1068 end
1069 % \end{macrocode}
1070 % After the disc node.
1071 % \begin{macrocode}
1072 item = n
1073 while item and is_possible_word_node(item) do
1074 if item.id == NODE_ID_GLYPH then
1075 return item.lang
1076 end
1077 item = item.next
1078 end
1079
1080 return 0
1081
1082 end
1083 % \end{macrocode}
1084 % \end{macro}
1085 %
1086 %
1087 %
1088 % \begin{macro}[int]{pre_linebreak}
1089 % Extract segments (word/space/math) from the hlist at \luavar{hlist_head} and store appropriate data in
1090 % \luavar{hlist_segment_list}. For spaces and math, this is just the existence of a segment. For a word, store its
1091 % text and its language ID (as determined by \luafunc{get_disc_lang}). Also, for each disc node, assign the index
1092 % of the word in \luavar{hlist_segment_list} to its \luavar{hyphen_attr} attribute (declared above).
1093 % \begin{macrocode}
1094 local function pre_linebreak(hlist_head,groupcode)
1095
1096 local word_start_node = nil
1097 local segment_count = 0
1098 local lang = nil
1099
1100 debug('Pre-linebreak processing start')
1101
1102 % \end{macrocode}
1103 % Per \S~8.2 of the Lua\TeX\ manual, there can be cases where \luavar{.prev} is invalid, so run
1104 % \luafunc{node.slide()} to set them correctly.
1105 % \begin{macrocode}
1106 node.slide(hlist_head)
1107
1108 local item = hlist_head
1109 while item do
1110 % \end{macrocode}
1111 % If \luavar{item} is a math node (which must have subtype beginmath, unless something has changed the node list),
1112 % skip the math and add "[MATH]" to \luavar{hlist_segment_list}.
1113 % \begin{macrocode}
1114 if item.id == NODE_ID_MATH then
1115 assert(item.subtype == NODE_MATH_SUBTYPE_BEGIN)
1116 while not (
1117 item.id == NODE_ID_MATH and item.subtype == NODE_MATH_SUBTYPE_END
1118 ) do
1119 item = item.next
1120 end
1121 item = item.next
1122
1123 segment_count = segment_count + 1
1124 hlist_segment_list[segment_count] = {
1125 [KEY_TYPE] = SEGMENT_MATH
1126 }
1127
1128 goto continue
1129 end
1130 % \end{macrocode}
1131 % If \luavar{item} is a possible word node, read the whole word, setting the \luavar{hyphen_attr} of any disc nodes
1132 % to \luavar{segment_count}, and adding the word to \luavar{hlist_segment_list}.
1133 % \begin{macrocode}
1134 if is_possible_word_node(item) then
1135 word_start_node = item
1136 segment_count = segment_count + 1
1137 while item and is_possible_word_node(item) do
1138
1139 % \end{macrocode}
1140 % When the first disc node is found, find the language of the word.
1141 % \begin{macrocode}
1142 if item.id == NODE_ID_DISC then
1143 if not lang then
1144 lang = get_disc_lang(item)
1145 end
1146 node.set_attribute(item,hyphen_attr,segment_count)
1147 end
1148
1149 item = item.next
1150 end
1151 % \end{macrocode}
1152 % \luavar{item} should be a node, because even after the last word node, the hlist will contain something. But just
1153 % in case, check and find the last node using \luafunc{node.tail} if necessary. This latter case should be very
1154 % rare, so it is more efficient to recalculate here if necessary rather than having an extra assignment to store the
1155 % previous node in the while loop.
1156 % \begin{macrocode}
1157 local word_end_node
1158 if item then
1159 word_end_node = item.prev
1160 else
1161 word_end_node = node.tail(word_start_node)
1162 end
1163
1164 local word = get_nodelist_text(word_start_node,word_end_node)
1165 hlist_segment_list[segment_count] = {
1166 [KEY_TYPE] = SEGMENT_WORD,
1167 [KEY_WORD] = word,
1168 [KEY_LANG] = lang,
1169 }
1170
1171 word_start_node = nil
1172 lang = nil
1173
1174 goto continue
1175 end
1176 % \end{macrocode}
1177 % If \luavar{item} is a node that could be part of a space, add a space to the segment list.
1178 % \begin{macrocode}
1179 if is_possible_space_node(item) then
1180 segment_count = segment_count + 1
1181
1182 while item and is_possible_space_node(item) do
1183 item = item.next
1184 end
1185
1186 hlist_segment_list[segment_count] = {
1187 [KEY_TYPE] = SEGMENT_SPACE
1188 }
1189
1190 goto continue
1191 end
1192 % \end{macrocode}
1193 % If \luavar{item} is anything else, just move on.
1194 % \begin{macrocode}
1195 item = item.next
1196
1197 ::continue::
1198 end
1199
1200 debug('Pre-linebreak processing finish')
1201
1202 return true
1203 end
1204 % \end{macrocode}
1205 % \end{macro}
1206 %
1207 %
1208 %
1209 % \subsection{Post-linebeak processing}
1210 %
1211 % After linebreaking, look for a discretionary node at the end of each line, which indicates that a word has been
1212 % divided between the end of that line and the start of the next. Extract the two word-pieces from the lines and store
1213 % them, together with the undivided word and its context in the appropriate language table. Also insert a whatsit to
1214 % that will set the page number when the hyphenation is written out.
1215 %
1216 % \begin{macro}[int]{get_used_disc}
1217 % If at the tail of the hlist at \luavar{hlist_head} (which will be a line) there is a disc node not followed by a
1218 % glyph node, return that disc node. Otherwise return \luavar{nil}. Only search up to at most
1219 % \luavar{USED_DISC_SEARCH_LIMIT} nodes from the tail.
1220 % \begin{macrocode}
1221 local USED_DISC_SEARCH_LIMIT = 20
1222
1223 local function get_used_disc(hlist_head)
1224
1225 debug(' Looking for disc node at end of line')
1226
1227 local item = node.tail(hlist_head)
1228
1229 local count = 0
1230 while item and item.id ~= NODE_ID_GLYPH and count < USED_DISC_SEARCH_LIMIT do
1231 if item.id == NODE_ID_DISC then
1232 return item
1233 end
1234 item = item.prev
1235 count = count + 1
1236 end
1237
1238 return nil
1239
1240 end
1241 % \end{macrocode}
1242 % \end{macro}
1243 %
1244 %
1245 %
1246 % \begin{macro}[int]{get_disc_word_start}
1247 % Return the node starting the word that includes a given disc node \luavar{n}, or \luavar{nil} if there is no such
1248 % node.
1249 % \begin{macrocode}
1250 local function get_disc_word_start(hlist_head,n)
1251
1252 local item = n
1253
1254 while item do
1255 local prev = item.prev
1256
1257 if not (prev and is_possible_word_node(prev)) then
1258 return item
1259 end
1260
1261 item = prev
1262 end
1263
1264 return nil
1265 end
1266 % \end{macrocode}
1267 % \end{macro}
1268 %
1269 %
1270 %
1271 % \begin{macro}[int]{get_next_hlist}
1272 % Return the next hlist in the list containing the given node \luavar{n}, or \luavar{nil} if there is no such hlist
1273 % node.
1274 % \begin{macrocode}
1275 local function get_next_hlist(n)
1276
1277 local item = n.next
1278
1279 while item do
1280 if item.id == NODE_ID_HLIST then
1281 return item
1282 end
1283 item = item.next
1284 end
1285
1286 return nil
1287
1288 end
1289 % \end{macrocode}
1290 % \end{macro}
1291 %
1292 %
1293 %
1294 % \begin{macro}[int]{get_line_first_word}
1295 % Return the first word in the hlist at \luavar{hlist_head}, or \luavar{nil} if there is no such word.
1296 % \begin{macrocode}
1297 local function get_line_first_word(hlist_head)
1298 % \end{macrocode}
1299 % \luavar{word_start_node} is either \luavar{nil} or the (glyph) node that starts the word.
1300 % \begin{macrocode}
1301 local word_start_node = nil
1302
1303 for item in node.traverse(hlist_head) do
1304
1305 if item.id == NODE_ID_GLYPH then
1306 if not word_start_node then
1307 word_start_node = item
1308 end
1309 end
1310
1311 if not is_possible_word_node(item) then
1312 if word_start_node then
1313 return get_nodelist_text(word_start_node,item.prev)
1314 end
1315 end
1316
1317 end
1318 % \end{macrocode}
1319 % It is possible that the word ends at the end of the hlist, so check if a word has been started.
1320 % \begin{macrocode}
1321 if word_start_node then
1322 return get_nodelist_text(word_start_node,node.tail(hlist_head))
1323 else
1324 return nil
1325 end
1326 end
1327 % \end{macrocode}
1328 % \end{macro}
1329 %
1330 %
1331 %
1332 % \begin{macro}[int]{get_context}
1333 % Return a string assembled from the part of \luavar{hlist_segment_list} before or after \luavar{index} according to
1334 % \luavar{incr} (which must be \(\pm 1\)) up a maximum of \luavar{target_word_count} words.
1335 % \begin{macrocode}
1336 local function get_context(index,incr,target_word_count)
1337
1338 local result = ''
1339 local word_count = 0
1340
1341 local i = index + incr
1342 while (
1343 i > 0 and i <= #hlist_segment_list and word_count < target_word_count
1344 ) do
1345 local t = hlist_segment_list[i]
1346
1347 local item
1348
1349 if t[KEY_TYPE] == SEGMENT_WORD then
1350 item = t[KEY_WORD]
1351 word_count = word_count + 1
1352 elseif t[KEY_TYPE] == SEGMENT_SPACE then
1353 item = STR_SPACE
1354 elseif t[KEY_TYPE] == SEGMENT_MATH then
1355 item = STR_MATH
1356 end
1357
1358 if incr > 0 then
1359 result = result .. item
1360 else
1361 result = item .. result
1362 end
1363
1364 i = i + incr
1365 end
1366
1367 return result
1368
1369 end
1370 % \end{macrocode}
1371 % \end{macro}
1372 %
1373 %
1374 %
1375 % Count and list for hyphenated words. Each entry in the list will be a table containing the original word, the
1376 % hyphenation, the language, the index of the table in the list (which is needed later for stable sorting and sorting
1377 % into the original order), and the context.
1378 % \begin{macrocode}
1379 local hyphenation_list = {}
1380 local hyphenation_count = 0
1381 % \end{macrocode}
1382 %
1383 %
1384 %
1385 % \begin{macro}[int]{check_line_hyphenation}
1386 % Check whether there is a hyphenated word at the end of the given hlist; if so, save the word to
1387 % \luavar{hyphenation_list}.
1388 % \begin{macrocode}
1389 local function check_line_hyphenation(hlist)
1390 % \end{macrocode}
1391 % First, is there a disc node not followed by a glyph node at the end of the list?
1392 % \begin{macrocode}
1393 local last_disc = get_used_disc(hlist.head)
1394 if not last_disc then
1395 debug(' No disc node found at end of line')
1396 return
1397 end
1398 debug(' Disc node found at end of line')
1399 % \end{macrocode}
1400 % Get the undivided word and its language from \luavar{hlist_segment_list}.
1401 % \begin{macrocode}
1402 local hyphenation_index = node.has_attribute(last_disc,hyphen_attr)
1403 local t = hlist_segment_list[hyphenation_index]
1404 assert(t)
1405 assert(t[KEY_TYPE] == SEGMENT_WORD)
1406 local word = t[KEY_WORD]
1407 local lang = t[KEY_LANG]
1408 % \end{macrocode}
1409 % \luavar{word} might be something other than a genuine word, such as an ISBN (with hyphen separators). So only
1410 % proceed if it contains at least one letter.
1411 % \begin{macrocode}
1412 if not unicode.utf8.match(word,'%a') then
1413 debug(' Divided "word" contains no letters')
1414 return
1415 end
1416 % \end{macrocode}
1417 % There should always be a next line, since there is a disc node at the end of \luavar{hlist}, but check anyway.
1418 % \begin{macrocode}
1419 local next_line = get_next_hlist(hlist)
1420
1421 if not next_line then
1422 debug(' No following line found (which should not happen)')
1423 return
1424 end
1425 % \end{macrocode}
1426 % For the pre-linebreak part of the word, get the word that ends the line, and trim any leading non-letters. This
1427 % could leave an empty word; for example, if \(n\)-dimensional is broken at the hyphen, the word ending the line is
1428 % just the hyphen. If an empty word is left, just use the non-trimmed result.
1429 % \begin{macrocode}
1430 local pre = get_nodelist_text(get_disc_word_start(hlist.head,last_disc))
1431 local pre_temp = trim_nonlettershyphens_start(pre)
1432 if pre_temp ~= '' then
1433 pre = pre_temp
1434 end
1435 % \end{macrocode}
1436 % For the post-linebreak part, just get the word at the start of the next line, and trim and trailing non-letters.
1437 % \begin{macrocode}
1438 local post = trim_nonlettershyphens_end(get_line_first_word(next_line.head))
1439 % \end{macrocode}
1440 % Compute the context and then trim any unwanted symbols from the word itself.
1441 % \begin{macrocode}
1442 local context =
1443 get_context(
1444 hyphenation_index,-1,tex.count['l__lualisthyphen_context_before_int']
1445 )
1446 .. word ..
1447 get_context(
1448 hyphenation_index,1,tex.count['l__lualisthyphen_context_after_int']
1449 )
1450
1451 word = trim_nonlettershyphens_both(word)
1452
1453 debug(
1454 ' Hyphenated word found: "' .. word .. '" -> "' .. pre .. '<>' .. post .. '"'
1455 )
1456 % \end{macrocode}
1457 % Store everything (except the page number on which the hyphenated word appears, which is not yet known) in the
1458 % hyphenation list.
1459 % \begin{macrocode}
1460 hyphenation_count = hyphenation_count + 1
1461 hyphenation_list[hyphenation_count] = {
1462 [KEY_LANG] = lang,
1463 [KEY_WORD] = word,
1464 [KEY_DIVISION] = pre .. post,
1465 [KEY_INDEX] = hyphenation_count,
1466 [KEY_CONTEXT] = context,
1467 }
1468 % \end{macrocode}
1469 % Add a whatsit to record the page number when the page with the hyphenation is shipped out. This information also
1470 % serves to distinguish hyphenations that are written to the page from those that occur in (e.g.) boxes that are
1471 % discarded without being written to the page.
1472 % \begin{macrocode}
1473 late_lua_n = node.new('whatsit','late_lua')
1474 late_lua_n.data =
1475 'lualisthyphen.set_hyphenation_page(' .. hyphenation_count .. ',tex.count["c@page"])'
1476
1477 node.insert_after(hlist.head,last_disc,late_lua_n)
1478
1479 end
1480 % \end{macrocode}
1481 % \end{macro}
1482 %
1483 %
1484 %
1485 % \begin{macro}[int]{set_hyphenation_page}
1486 % Set the page on which the hyphenation with the given index appears.
1487 % \begin{macrocode}
1488 local function set_hyphenation_page(index,page)
1489
1490 hyphenation_list[index][KEY_PAGE] = page
1491
1492 end
1493 % \end{macrocode}
1494 % \end{macro}
1495 %
1496 %
1497 %
1498 % \begin{macro}[int]{post_linebreak}
1499 % For every line in the vlist at \luavar{vlist_head}, check whether there is a hyphenated word at the end.
1500 % \begin{macrocode}
1501 local function post_linebreak(vlist_head,groupcode)
1502
1503 debug('Post-linebreak processing start')
1504 % \end{macrocode}
1505 % Per \S~8.2 of the Lua\TeX\ manual, there can be cases where \luavar{.prev} is invalid, so run
1506 % \luafunc{node.slide()} on each line to set them correctly.
1507 % \begin{macrocode}
1508 for item in node.traverse(vlist_head) do
1509 if item.id == NODE_ID_HLIST then
1510 node.slide(item.head)
1511 end
1512 end
1513 % \end{macrocode}
1514 % Now actually look for hyphenations.
1515 % \begin{macrocode}
1516 local line_no = 0
1517
1518 for item in node.traverse(vlist_head) do
1519
1520 if item.id == NODE_ID_HLIST then
1521 line_no = line_no + 1
1522 debug(' Line no.' .. line_no)
1523 check_line_hyphenation(item)
1524 end
1525
1526 end
1527
1528 hlist_segment_list = {}
1529
1530 debug('Post-linebreak processing end')
1531
1532 return true
1533
1534 end
1535 % \end{macrocode}
1536 % \end{macro}
1537 %
1538 %
1539 %
1540 % \subsection{Callbacks}
1541 %
1542 % Add \luafunc{pre_linebreak} and \luafunc{post_linebreak} to the relevant callbacks.
1543 % \begin{macrocode}
1544 local LUA_LIST_HYPHEN_PRE_LINEBREAK = 'LUA_LIST_HYPHEN_PRE_LINEBREAK'
1545 luatexbase.add_to_callback(
1546 'pre_linebreak_filter',
1547 pre_linebreak,
1548 LUA_LIST_HYPHEN_PRE_LINEBREAK
1549 )
1550
1551 local LUA_LIST_HYPHEN_POST_LINEBREAK = 'LUA_LIST_HYPHEN_POST_LINEBREAK'
1552 luatexbase.add_to_callback(
1553 'post_linebreak_filter',
1554 post_linebreak,
1555 LUA_LIST_HYPHEN_POST_LINEBREAK
1556 )
1557 % \end{macrocode}
1558 %
1559 %
1560 %
1561 % \subsection{Language settings}
1562 %
1563 % Table mapping language IDs to textual names.
1564 % \begin{macrocode}
1565 local language_table = {}
1566 % \end{macrocode}
1567 %
1568 % Populating \luavar{language_table} is done differently for \pkg{babel} and \pkg{polyglossia}. If \pkg{babel} is in
1569 % use, the \LaTeX\ frontend iterates through \cs{bbl@languages} and calls \luafunc{babel_save_language_name}. If
1570 % \pkg{polyglossia} is in use, \luavar{language_table} is populated by \luafunc{polyglossia_get_language_names}, which
1571 % is called just before the hyphenation lists are written.
1572 %
1573 % \begin{macro}[int]{babel_save_language_name}
1574 % Store the association of a language ID to \pkg{babel}'s texual name, if no name has been assigned to that ID
1575 % already.
1576 % \begin{macrocode}
1577 local function babel_save_language_name(lang_id,name)
1578
1579 if not language_table[lang_id] then
1580 language_table[lang_id] = name
1581 end
1582
1583 end
1584 % \end{macrocode}
1585 % \end{macro}
1586 %
1587 %
1588 %
1589 % \begin{macro}[int]{polyglossia_get_language_names}
1590 % If polyglossia has been loaded, use it to build the table mapping language IDs to textual names.
1591 % \begin{macrocode}
1592 local function polyglossia_get_language_names()
1593
1594 if not polyglossia then
1595 return
1596 end
1597
1598 for name,language in pairs(polyglossia.newloader_loaded_languages) do
1599 language_table[lang.id(language)] = name
1600 end
1601
1602 end
1603 % \end{macrocode}
1604 % \end{macro}
1605 %
1606 %
1607 %
1608 % \subsection{Processing hyphenation lists}
1609 %
1610 % Before writing out hyphenation lists, remove duplicates and/or perform sorting, in accordance with the set options.
1611 %
1612 %
1613 %
1614 % \subsubsection{Comparisons and equality checks}
1615 %
1616 % \begin{macro}[int]{equal_hyphenation_case_sensitive}
1617 % Equality check for deduplicating the list of hyphenations case-sensitively.
1618 % \begin{macrocode}
1619 local function equal_hyphenation_case_sensitive(a,b)
1620 return (
1621 a[KEY_WORD] == b[KEY_WORD]
1622 and
1623 a[KEY_DIVISION] == b[KEY_DIVISION]
1624 )
1625 end
1626 % \end{macrocode}
1627 % \end{macro}
1628 %
1629 %
1630 %
1631 % \begin{macro}[int]{equal_hyphenation_case_insensitive}
1632 % Equality check for deduplicating the list of hyphenations case-insensitively.
1633 % \begin{macrocode}
1634 local function equal_hyphenation_case_insensitive(a,b)
1635 return (
1636 unicode.utf8.lower(a[KEY_WORD]) == unicode.utf8.lower(b[KEY_WORD])
1637 and
1638 unicode.utf8.lower(a[KEY_DIVISION]) == unicode.utf8.lower(b[KEY_DIVISION])
1639 )
1640 end
1641 % \end{macrocode}
1642 % \end{macro}
1643 %
1644 %
1645 %
1646 % \begin{macro}[int]{lessthan_hyphenation_case_sensitive}
1647 % Comparison for sorting the list of hyphenations case-sensitively.
1648 %
1649 % The comparison of index keys ensures that the sorting is stable.
1650 % \begin{macrocode}
1651 local function lessthan_hyphenation_case_sensitive(a,b)
1652 return (
1653 a[KEY_WORD] < b[KEY_WORD]
1654 or
1655 (
1656 a[KEY_WORD] == b[KEY_WORD]
1657 and
1658 a[KEY_DIVISION] < b[KEY_DIVISION]
1659 )
1660 or
1661 (
1662 a[KEY_WORD] == b[KEY_WORD]
1663 and
1664 a[KEY_DIVISION] == b[KEY_DIVISION]
1665 and
1666 a[KEY_INDEX] < b[KEY_INDEX]
1667 )
1668 )
1669 end
1670 % \end{macrocode}
1671 % \end{macro}
1672 %
1673 %
1674 %
1675 % \begin{macro}[int]{lessthan_hyphenation_case_insensitive}
1676 % Comparison for sorting the list of hyphenations case-insensitively.
1677 %
1678 % The comparison of index keys ensures that the sorting is stable.
1679 % \begin{macrocode}
1680 local function lessthan_hyphenation_case_insensitive(a,b)
1681 return (
1682 unicode.utf8.lower(a[KEY_WORD]) < unicode.utf8.lower(b[KEY_WORD])
1683 or
1684 (
1685 unicode.utf8.lower(a[KEY_WORD]) == unicode.utf8.lower(b[KEY_WORD])
1686 and
1687 unicode.utf8.lower(a[KEY_DIVISION]) < unicode.utf8.lower(b[KEY_DIVISION])
1688 )
1689 or
1690 (
1691 unicode.utf8.lower(a[KEY_WORD]) == unicode.utf8.lower(b[KEY_WORD])
1692 and
1693 unicode.utf8.lower(a[KEY_DIVISION]) < unicode.utf8.lower(b[KEY_DIVISION])
1694 and
1695 a[KEY_INDEX] < b[KEY_INDEX]
1696 )
1697 )
1698 end
1699 % \end{macrocode}
1700 % \end{macro}
1701 %
1702 %
1703 %
1704 % \subsubsection{Sorting}
1705 %
1706 % \begin{macro}[int]{sort_hyphenation_list_none}
1707 % Sort \luavar{hyphenation_list} into its original order of appearance.
1708 % \begin{macrocode}
1709 local function sort_hyphenation_list_none(hyphenation_list)
1710 table.sort(
1711 hyphenation_list,
1712 function(a,b)
1713 return a[KEY_INDEX] < b[KEY_INDEX]
1714 end
1715 )
1716 end
1717 % \end{macrocode}
1718 % \end{macro}
1719 %
1720 %
1721 %
1722 % \begin{macro}[int]{sort_hyphenation_list_case}
1723 % Sort \luavar{hyphenation_list} case-sensitively.
1724 % \begin{macrocode}
1725 local function sort_hyphenation_list_case(hyphenation_list)
1726 table.sort(
1727 hyphenation_list,
1728 lessthan_hyphenation_case_sensitive
1729 )
1730 end
1731 % \end{macrocode}
1732 % \end{macro}
1733 %
1734 %
1735 %
1736 % \begin{macro}[int]{sort_hyphenation_list_nocase}
1737 % Sort \luavar{hyphenation_list} case-insensitively.
1738 % \begin{macrocode}
1739 local function sort_hyphenation_list_nocase(hyphenation_list)
1740 table.sort(
1741 hyphenation_list,
1742 lessthan_hyphenation_case_insensitive
1743 )
1744 end
1745 % \end{macrocode}
1746 % \end{macro}
1747 %
1748 %
1749 %
1750 % \begin{macro}[int]{process_lang_hyphenation_list_sort}
1751 % Select the appropriate function for sorting.
1752 % \begin{macrocode}
1753 local sort_hyphenation_list
1754 if tex.count['l__lualisthyphen_sort_int'] == 1 then
1755 sort_hyphenation_list = sort_hyphenation_list_case
1756 elseif tex.count['l__lualisthyphen_sort_int'] == 2 then
1757 sort_hyphenation_list = sort_hyphenation_list_nocase
1758 else
1759 sort_hyphenation_list = sort_hyphenation_list_none
1760 end
1761 % \end{macrocode}
1762 % \end{macro}
1763 %
1764 %
1765 %
1766 % \subsubsection{Deduplication}
1767 %
1768 % \begin{macro}[int]{deduplicate_hyphenation_list_none}
1769 % Dummy function; does not deduplicate \luavar{hyphenation_list}.
1770 % \begin{macrocode}
1771 local function deduplicate_hyphenation_list_none(hyphenation_list)
1772 end
1773 % \end{macrocode}
1774 % \end{macro}
1775 %
1776 %
1777 %
1778 % \begin{macro}[int]{deduplicate_hyphenation_list_case}
1779 % Remove duplicates from \luavar{hyphenation_list} case-sensitively.
1780 % \begin{macrocode}
1781 local function deduplicate_hyphenation_list_case(hyphenation_list)
1782 table.sort(
1783 hyphenation_list,
1784 lessthan_hyphenation_case_sensitive
1785 )
1786 list_uniq(
1787 hyphenation_list,
1788 equal_hyphenation_case_sensitive
1789 )
1790 end
1791 % \end{macrocode}
1792 % \end{macro}
1793 %
1794 %
1795 %
1796 % \begin{macro}[int]{deduplicate_hyphenation_list_nocase}
1797 % Remove duplicates from \luavar{hyphenation_list} case-insensitively.
1798 % \begin{macrocode}
1799 local function deduplicate_hyphenation_list_nocase(hyphenation_list)
1800 table.sort(
1801 hyphenation_list,
1802 lessthan_hyphenation_case_insensitive
1803 )
1804 list_uniq(
1805 hyphenation_list,
1806 equal_hyphenation_case_insensitive
1807 )
1808 end
1809 % \end{macrocode}
1810 % \end{macro}
1811 %
1812 %
1813 %
1814 % \begin{macro}[int]{deduplicate_hyphenation_list}
1815 % Select the appropriate function for whether duplicates whould be removed.
1816 % \begin{macrocode}
1817 local deduplicate_hyphenation_list
1818 if tex.count['l__lualisthyphen_unique_int'] == 1 then
1819 deduplicate_hyphenation_list = deduplicate_hyphenation_list_case
1820 elseif tex.count['l__lualisthyphen_unique_int'] == 2 then
1821 deduplicate_hyphenation_list = deduplicate_hyphenation_list_nocase
1822 else
1823 deduplicate_hyphenation_list = deduplicate_hyphenation_list_none
1824 end
1825 % \end{macrocode}
1826 % \end{macro}
1827 %
1828 %
1829 %
1830 % \subsubsection{Combined processing}
1831 %
1832 % \begin{macro}[int]{process_lang_hyphenation_list}
1833 % Remove duplicates and sort \luavar{hyphenation_list}.
1834 % \begin{macrocode}
1835 local function process_lang_hyphenation_list(hyphenation_list)
1836 deduplicate_hyphenation_list(hyphenation_list)
1837 sort_hyphenation_list(hyphenation_list)
1838 end
1839 % \end{macrocode}
1840 % \end{macro}
1841 %
1842 %
1843 %
1844 % \subsection{Writing}
1845 %
1846 % \begin{macro}[int]{write_lang_hyphenation_list_standard}
1847 % Write out just the hyphenated words in \luavar{hyphenation_list} to file handle \luavar{f}.
1848 % \begin{macrocode}
1849 local function write_lang_hyphenation_list_standard(f,hyphenation_list,widths)
1850
1851 for i,v in ipairs(hyphenation_list) do
1852
1853 if v then
1854 f:write(v[KEY_DIVISION] .. '\n')
1855 end
1856
1857 end
1858
1859 end
1860 % \end{macrocode}
1861 % \end{macro}
1862 %
1863 %
1864 %
1865 % \begin{macro}[int]{write_lang_hyphenation_list_verbose}
1866 % Write out all hyphenation information in \luavar{hyphenation_list} to file handle \luavar{f}, in columns as
1867 % specified in \luavar{widths}.
1868 % \begin{macrocode}
1869 local function write_lang_hyphenation_list_verbose(f,hyphenation_list,widths)
1870
1871 local cols_word = widths[KEY_WORD]
1872 local cols_division = widths[KEY_DIVISION]
1873 local cols_page = widths[KEY_PAGE]
1874
1875 for i,v in ipairs(hyphenation_list) do
1876
1877 if v then
1878 % \end{macrocode}
1879 % It is possible for KEY_PAGE not to have been set, for instance if the hyphenation occured in a box that was never
1880 % output.
1881 % \begin{macrocode}
1882 local page = v[KEY_PAGE]
1883 if page then
1884 page = STR_PAGE_PREFIX .. page
1885 else
1886 page = STR_PAGE_NONE
1887 end
1888
1889 f:write(
1890 rpad(v[KEY_WORD],cols_word)
1891 .. STR_ARROW
1892 .. rpad(v[KEY_DIVISION],cols_division)
1893 .. STR_SPACE_TWO
1894 .. lpad(page,cols_page)
1895 .. STR_SPACE
1896 .. STR_QUOTE_OPEN
1897 .. v[KEY_CONTEXT]
1898 .. STR_QUOTE_CLOSE
1899 .. '\n'
1900 )
1901 end
1902
1903 end
1904
1905 end
1906 % \end{macrocode}
1907 % \end{macro}
1908 %
1909 %
1910 %
1911 % \begin{macro}[int]{write_lang_hyphenation_list}
1912 % Set \luafunc{write_lang_hyphenation_list} to be either \luafunc{write_lang_hyphenation_list_standard} or
1913 % \luafunc{write_lang_hyphenation_list_verbose}, depending on the
1914 % package options.
1915 % \begin{macrocode}
1916 local write_lang_hyphenation_list
1917 if tex.count['l__lualisthyphen_verbose_int'] == 0 then
1918 write_lang_hyphenation_list = write_lang_hyphenation_list_standard
1919 else
1920 write_lang_hyphenation_list = write_lang_hyphenation_list_verbose
1921 end
1922 % \end{macrocode}
1923 % \end{macro}
1924 %
1925 %
1926 %
1927 % Compute a settings description to insert into file headers.
1928 % \begin{macrocode}
1929 local settings_desc
1930 if tex.count['l__lualisthyphen_verbose_int'] == 0 then
1931 settings_desc = 'verbose=false'
1932 else
1933 settings_desc = 'verbose=true'
1934 .. ',context-before=' .. tex.count['l__lualisthyphen_context_before_int']
1935 .. ',context-after=' .. tex.count['l__lualisthyphen_context_after_int']
1936 end
1937 if tex.count['l__lualisthyphen_include_non_output_int'] == 0 then
1938 settings_desc = settings_desc .. ',include-non-output=false'
1939 else
1940 settings_desc = settings_desc .. ',include-non-output=true'
1941 end
1942 local NONE_CASE_NOCASE = {
1943 [0] = 'none',
1944 [1] = 'case',
1945 [2] = 'nocase'
1946 }
1947 settings_desc = settings_desc
1948 .. ',sort=' .. NONE_CASE_NOCASE[tex.count['l__lualisthyphen_sort_int']]
1949 .. ',unique=' .. NONE_CASE_NOCASE[tex.count['l__lualisthyphen_unique_int']]
1950 % \end{macrocode}
1951 %
1952 %
1953 %
1954 % \begin{macro}[int]{get_hyphenation_file_path}
1955 % Get the file to which the list of hyphenated words will be written, based on the given \luavar{prefix},
1956 % \luavar{extension}, \luavar{lang_name}, and taking into account any specified output directory for
1957 % Lua\TeX, and with a file header.
1958 % \begin{macrocode}
1959 local function get_hyphenation_file_path(prefix,extension,lang_name)
1960
1961 local hyphenation_file_path = prefix .. tostring(lang_name) .. extension
1962
1963 if not status.output_directory then
1964 return hyphenation_file_path
1965 end
1966
1967 if string.sub(status.output_directory,-1,-1) == '/' then
1968 hyphenation_file_path = status.output_directory
1969 .. hyphenation_file_path
1970 else
1971 hyphenation_file_path = status.output_directory
1972 .. '/' .. hyphenation_file_path
1973 end
1974
1975 return hyphenation_file_path
1976
1977 end
1978 % \end{macrocode}
1979 % \end{macro}
1980 %
1981 %
1982 %
1983 % \begin{macro}[int]{process_write_lang_hyphenation_list}
1984 % Process and write out the \luavar{hyphenation_list} (which will be for the language with the numerical
1985 % \luavar{lang_id}) to a file with the given \luavar{prefix} and \luavar{extension}, using \luavar{widths} for the
1986 % ‘columns’ in verbose mode.
1987 % \begin{macrocode}
1988 local function process_write_lang_hyphenation_list(
1989 prefix,extension,lang_id,hyphenation_list,widths
1990 )
1991
1992 process_lang_hyphenation_list(hyphenation_list)
1993
1994 local lang_name = language_table[lang_id]
1995 local lang_desc
1996 if not lang_name then
1997 lang_name = lang_id
1998 lang_desc = 'language with ID ' .. lang_id
1999 else
2000 lang_desc = 'language "' .. lang_name .. '" (ID ' .. lang_id .. ')'
2001 end
2002
2003 local f = io.open(get_hyphenation_file_path(prefix,extension,lang_name),'w')
2004
2005 f:write('% Chosen hyphenations for ' .. lang_desc .. '\n')
2006 f:write('% Generated by lua-list-hyphen (' .. settings_desc .. ')\n')
2007
2008 write_lang_hyphenation_list(f,hyphenation_list,widths)
2009 f:close()
2010
2011 end
2012 % \end{macrocode}
2013 % \end{macro}
2014 %
2015 %
2016 %
2017 % \begin{macro}[int]{process_write_hyphenation_lists}
2018 % Sort \luavar{hyphenation_list} into per-language lists and write them out to separate files.
2019 % \begin{macrocode}
2020 local function process_write_hyphenation_lists(prefix,extension)
2021
2022 local lang_hyphenation_table = {}
2023 local lang_widths_table = {}
2024 % \end{macrocode}
2025 % Iterate through all the stored hyphenations. Sort them into per-language lists (creating the list the first time
2026 % each language is encountered) and also storing the maximum width of values, for output alignment.
2027 % \begin{macrocode}
2028 for _,h in pairs(hyphenation_list) do
2029
2030 if h[KEY_PAGE] or tex.count['l__lualisthyphen_include_non_output_int'] == 1 then
2031
2032 local lang = h[KEY_LANG]
2033
2034 local t = lang_hyphenation_table[lang]
2035 if not t then
2036 lang_hyphenation_table[lang] = {}
2037 t = lang_hyphenation_table[lang]
2038 end
2039
2040 local widths = lang_widths_table[lang]
2041 if not widths then
2042 lang_widths_table[lang] = {
2043 [KEY_WORD] = 0,
2044 [KEY_DIVISION] = 0,
2045 [KEY_PAGE] = 0
2046 }
2047 widths = lang_widths_table[lang]
2048 end
2049
2050 widths[KEY_WORD] = math.max(
2051 widths[KEY_WORD],
2052 unicode.utf8.len(h[KEY_WORD])
2053 )
2054 widths[KEY_DIVISION] = math.max(
2055 widths[KEY_DIVISION],
2056 unicode.utf8.len(h[KEY_DIVISION])
2057 )
2058 widths[KEY_PAGE] = math.max(
2059 widths[KEY_PAGE],
2060 unicode.utf8.len(tostring(h[KEY_PAGE]))
2061 )
2062
2063 table.insert(t,h)
2064
2065 end
2066
2067 end
2068 % \end{macrocode}
2069 % Adjust the maximum width for the page output, since there is a prefix and a ‘no page’ indicator to consider.
2070 % \begin{macrocode}
2071 for _,widths in pairs(lang_widths_table) do
2072 widths[KEY_PAGE] = math.max(
2073 widths[KEY_PAGE] + unicode.utf8.len(STR_PAGE_PREFIX),
2074 unicode.utf8.len(STR_PAGE_NONE)
2075 )
2076 end
2077 % \end{macrocode}
2078 % If polyglossia is in use, populate \luavar{language_table}.
2079 % \begin{macrocode}
2080 polyglossia_get_language_names()
2081 % \end{macrocode}
2082 % For each language, process and write out its hyphenations to a file.
2083 % \begin{macrocode}
2084 for k,v in pairs(lang_hyphenation_table) do
2085 process_write_lang_hyphenation_list(prefix,extension,k,v,lang_widths_table[k])
2086 end
2087
2088 end
2089 % \end{macrocode}
2090 % \end{macro}
2091 %
2092 %
2093 %
2094 % \subsection{Export public functions}
2095 %
2096 % Finally, make available the functions that will be called from the \LaTeX\ frontend using \cs{lua_now:n}.
2097 % \begin{macrocode}
2098 return {
2099 process_write_hyphenation_lists = process_write_hyphenation_lists,
2100 set_hyphenation_page = set_hyphenation_page,
2101 babel_save_language_name = babel_save_language_name,
2102 }
2103 % \end{macrocode}
2104 %
2105 %
2106 %
2107 % \begin{macrocode}
2108 %</lua>
2109 % \end{macrocode}
2110
2111 %
2112 %
2113 %
2114 % \clearpage
2115 % \end{implementation}
2116 %
2117 %
2118 %
2119 % \iffalse
2120 %<*metadriver>
2121 \input{lua-list-hyphen.dtx}
2122 %</metadriver>
2123 % \fi