| 1 |
-- |
| 2 |
-- This is file `lua-list-hyphen.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- lua-list-hyphen.dtx (with options: `lua') |
| 8 |
-- |
| 9 |
-- This is a generated file. |
| 10 |
-- |
| 11 |
-- Copyright (C) 2026 Alan J. Cain |
| 12 |
-- |
| 13 |
-- This file may be distributed and/or modified under the |
| 14 |
-- conditions of the LaTeX Project Public License, either |
| 15 |
-- version 1.3c of this license or (at your option) any later |
| 16 |
-- version. The latest version of this license is in: |
| 17 |
-- |
| 18 |
-- http://www.latex-project.org/lppl.txt |
| 19 |
-- |
| 20 |
-- and version 1.3c or later is part of all distributions of |
| 21 |
-- LaTeX version 2008-05-04 or later. |
| 22 |
-- |
| 23 |
local debug |
| 24 |
|
| 25 |
if tex.count['l__lualisthyphen_debug_int'] == 0 then |
| 26 |
debug = function(s) |
| 27 |
end |
| 28 |
else |
| 29 |
debug = function(s) |
| 30 |
print('lua-list-hyphen DEBUG: ' .. s) |
| 31 |
end |
| 32 |
end |
| 33 |
local KEY_TYPE = 'type' |
| 34 |
local KEY_WORD = 'word' |
| 35 |
local KEY_LANG = 'lang' |
| 36 |
local KEY_DIVISION = 'division' |
| 37 |
local KEY_INDEX = 'index' |
| 38 |
local KEY_CONTEXT = 'context' |
| 39 |
local KEY_PAGE = 'page' |
| 40 |
local SEGMENT_WORD = 0 |
| 41 |
local SEGMENT_SPACE = 1 |
| 42 |
local SEGMENT_MATH = 2 |
| 43 |
local NODE_ID_HLIST = node.id('hlist') |
| 44 |
local NODE_ID_DISC = node.id('disc') |
| 45 |
local NODE_ID_GLUE = node.id('glue') |
| 46 |
local NODE_ID_KERN = node.id('kern') |
| 47 |
local NODE_ID_MARGIN_KERN = node.id('margin_kern') |
| 48 |
local NODE_ID_GLYPH = node.id('glyph') |
| 49 |
local NODE_ID_MATH = node.id('math') |
| 50 |
local NODE_KERN_SUBTYPE_FONTKERN |
| 51 |
local NODE_KERN_SUBTYPE_USERKERN |
| 52 |
for k,v in pairs(node.subtypes('kern')) do |
| 53 |
if v == 'fontkern' then |
| 54 |
NODE_KERN_SUBTYPE_FONTKERN = k |
| 55 |
elseif v == 'userkern' then |
| 56 |
NODE_KERN_SUBTYPE_USERKERN = k |
| 57 |
end |
| 58 |
end |
| 59 |
local NODE_MATH_SUBTYPE_BEGIN |
| 60 |
local NODE_MATH_SUBTYPE_END |
| 61 |
for k,v in pairs(node.subtypes('math')) do |
| 62 |
if v == 'beginmath' then |
| 63 |
NODE_MATH_SUBTYPE_BEGIN = k |
| 64 |
elseif v == 'endmath' then |
| 65 |
NODE_MATH_SUBTYPE_END = k |
| 66 |
end |
| 67 |
end |
| 68 |
local STR_MATH = '[MATH]' |
| 69 |
local STR_SPACE = ' ' |
| 70 |
local STR_SPACE_TWO = ' ' |
| 71 |
local STR_ARROW = ' -> ' |
| 72 |
local STR_PAGE_PREFIX = 'p.' |
| 73 |
local STR_PAGE_NONE = '<none>' |
| 74 |
local STR_QUOTE_OPEN = '"' |
| 75 |
local STR_QUOTE_CLOSE = '"' |
| 76 |
local function list_filter(t, f) |
| 77 |
local j = 1 |
| 78 |
local n = #t |
| 79 |
|
| 80 |
for i=1,n do |
| 81 |
if (f(t[i])) then |
| 82 |
if (i ~= j) then |
| 83 |
t[j] = t[i] |
| 84 |
t[i] = nil |
| 85 |
end |
| 86 |
j = j + 1 |
| 87 |
else |
| 88 |
t[i] = nil |
| 89 |
end |
| 90 |
end |
| 91 |
|
| 92 |
end |
| 93 |
local function list_uniq(t, f) |
| 94 |
local j = 1 |
| 95 |
local n = #t |
| 96 |
|
| 97 |
for i=2,n do |
| 98 |
if (f(t[i],t[j])) then |
| 99 |
t[i] = nil |
| 100 |
else |
| 101 |
j = i |
| 102 |
end |
| 103 |
end |
| 104 |
|
| 105 |
list_filter( |
| 106 |
t, |
| 107 |
function(a) return a end |
| 108 |
) |
| 109 |
end |
| 110 |
local LIGATURE_TEXT = { |
| 111 |
[0xfb00] = 'ff', |
| 112 |
[0xfb01] = 'fi', |
| 113 |
[0xfb02] = 'fl', |
| 114 |
[0xfb03] = 'ffi', |
| 115 |
[0xfb04] = 'ffl', |
| 116 |
} |
| 117 |
local font_characters = {} |
| 118 |
local get_node_text |
| 119 |
local get_nodelist_text |
| 120 |
get_node_text = function(n) |
| 121 |
|
| 122 |
if n.id == NODE_ID_GLYPH then |
| 123 |
|
| 124 |
local ligature_text = LIGATURE_TEXT[n.char] |
| 125 |
if ligature_text ~= nil then |
| 126 |
return ligature_text |
| 127 |
elseif n.components then |
| 128 |
return get_nodelist_text(n.components) |
| 129 |
else |
| 130 |
-- See [https://tug.org/pipermail/luatex/2018-March/006786.html] |
| 131 |
local characters = font_characters[n.font] |
| 132 |
if not characters then |
| 133 |
characters = fonts.hashes.identifiers[n.font].characters |
| 134 |
font_characters[n.font] = characters |
| 135 |
end |
| 136 |
local ok,retval = pcall( |
| 137 |
function () |
| 138 |
return utf8.char(tonumber(characters[n.char].tounicode,16)) |
| 139 |
end |
| 140 |
) |
| 141 |
if ok then |
| 142 |
return retval |
| 143 |
else |
| 144 |
return '' |
| 145 |
end |
| 146 |
end |
| 147 |
|
| 148 |
elseif n.id == NODE_ID_DISC then |
| 149 |
|
| 150 |
if n.replace then |
| 151 |
return get_nodelist_text(n.replace) |
| 152 |
else |
| 153 |
return '' |
| 154 |
end |
| 155 |
|
| 156 |
else |
| 157 |
return '' |
| 158 |
end |
| 159 |
|
| 160 |
end |
| 161 |
get_nodelist_text = function (head,last) |
| 162 |
|
| 163 |
local text = '' |
| 164 |
|
| 165 |
for item in node.traverse(head) do |
| 166 |
|
| 167 |
text = text .. get_node_text(item) |
| 168 |
|
| 169 |
if item == last then |
| 170 |
break |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
return text |
| 175 |
|
| 176 |
end |
| 177 |
local function is_possible_word_node(n) |
| 178 |
|
| 179 |
return ( |
| 180 |
n.id == NODE_ID_GLYPH |
| 181 |
or |
| 182 |
n.id == NODE_ID_DISC |
| 183 |
or |
| 184 |
(n.id == NODE_ID_KERN and n.subtype == NODE_KERN_SUBTYPE_FONTKERN) |
| 185 |
or |
| 186 |
n.id == NODE_ID_MARGIN_KERN |
| 187 |
) |
| 188 |
|
| 189 |
end |
| 190 |
local function is_possible_space_node(n) |
| 191 |
|
| 192 |
return ( |
| 193 |
n.id == NODE_ID_GLUE |
| 194 |
or |
| 195 |
(n.id == NODE_ID_KERN and n.subtype == NODE_KERN_SUBTYPE_USERKERN) |
| 196 |
) |
| 197 |
|
| 198 |
end |
| 199 |
local function trim_nonlettershyphens_both(s) |
| 200 |
|
| 201 |
return unicode.utf8.match(s,'^[^%a-]*(.-)[^%a-]*$') |
| 202 |
|
| 203 |
end |
| 204 |
local function trim_nonlettershyphens_start(s) |
| 205 |
|
| 206 |
return unicode.utf8.match(s,'^[^%a-]*(.-)$') |
| 207 |
|
| 208 |
end |
| 209 |
local function trim_nonlettershyphens_end(s) |
| 210 |
|
| 211 |
return unicode.utf8.match(s,'^(.-)[^%a-]*$') |
| 212 |
|
| 213 |
end |
| 214 |
local function rpad(s,n) |
| 215 |
|
| 216 |
return s .. unicode.utf8.rep(STR_SPACE,n - unicode.utf8.len(s)) |
| 217 |
|
| 218 |
end |
| 219 |
local function lpad(s,n) |
| 220 |
|
| 221 |
return unicode.utf8.rep(STR_SPACE,n - unicode.utf8.len(s)) .. s |
| 222 |
|
| 223 |
end |
| 224 |
local hyphen_attr = luatexbase.new_attribute('hyphen_attr') |
| 225 |
local hlist_segment_list = {} |
| 226 |
-- local function get_first_glyph_lang(n) |
| 227 |
|
| 228 |
-- local item = n |
| 229 |
-- while item and is_possible_word_node(item) do |
| 230 |
-- if item.id == NODE_ID_GLYPH then |
| 231 |
-- return item.lang |
| 232 |
-- end |
| 233 |
-- item = item.next |
| 234 |
-- end |
| 235 |
|
| 236 |
-- return nil |
| 237 |
|
| 238 |
-- end |
| 239 |
local function get_disc_lang(n) |
| 240 |
|
| 241 |
-- lang = get_first_glyph_lang(n.replace) |
| 242 |
-- if lang then |
| 243 |
-- print(lang) |
| 244 |
-- return lang |
| 245 |
-- end |
| 246 |
|
| 247 |
-- lang = get_first_glyph_lang(n.pre) |
| 248 |
-- if lang then |
| 249 |
-- print(lang) |
| 250 |
-- return lang |
| 251 |
-- end |
| 252 |
|
| 253 |
-- lang = get_first_glyph_lang(n.post) |
| 254 |
-- if lang then |
| 255 |
-- return lang |
| 256 |
-- end |
| 257 |
|
| 258 |
local item |
| 259 |
item = n |
| 260 |
while item and is_possible_word_node(item) do |
| 261 |
if item.id == NODE_ID_GLYPH then |
| 262 |
return item.lang |
| 263 |
end |
| 264 |
item = item.prev |
| 265 |
end |
| 266 |
item = n |
| 267 |
while item and is_possible_word_node(item) do |
| 268 |
if item.id == NODE_ID_GLYPH then |
| 269 |
return item.lang |
| 270 |
end |
| 271 |
item = item.next |
| 272 |
end |
| 273 |
|
| 274 |
return 0 |
| 275 |
|
| 276 |
end |
| 277 |
local function pre_linebreak(hlist_head,groupcode) |
| 278 |
|
| 279 |
local word_start_node = nil |
| 280 |
local segment_count = 0 |
| 281 |
local lang = nil |
| 282 |
|
| 283 |
debug('Pre-linebreak processing start') |
| 284 |
|
| 285 |
node.slide(hlist_head) |
| 286 |
|
| 287 |
local item = hlist_head |
| 288 |
while item do |
| 289 |
if item.id == NODE_ID_MATH then |
| 290 |
assert(item.subtype == NODE_MATH_SUBTYPE_BEGIN) |
| 291 |
while not ( |
| 292 |
item.id == NODE_ID_MATH and item.subtype == NODE_MATH_SUBTYPE_END |
| 293 |
) do |
| 294 |
item = item.next |
| 295 |
end |
| 296 |
item = item.next |
| 297 |
|
| 298 |
segment_count = segment_count + 1 |
| 299 |
hlist_segment_list[segment_count] = { |
| 300 |
[KEY_TYPE] = SEGMENT_MATH |
| 301 |
} |
| 302 |
|
| 303 |
goto continue |
| 304 |
end |
| 305 |
if is_possible_word_node(item) then |
| 306 |
word_start_node = item |
| 307 |
segment_count = segment_count + 1 |
| 308 |
while item and is_possible_word_node(item) do |
| 309 |
|
| 310 |
if item.id == NODE_ID_DISC then |
| 311 |
if not lang then |
| 312 |
lang = get_disc_lang(item) |
| 313 |
end |
| 314 |
node.set_attribute(item,hyphen_attr,segment_count) |
| 315 |
end |
| 316 |
|
| 317 |
item = item.next |
| 318 |
end |
| 319 |
local word_end_node |
| 320 |
if item then |
| 321 |
word_end_node = item.prev |
| 322 |
else |
| 323 |
word_end_node = node.tail(word_start_node) |
| 324 |
end |
| 325 |
|
| 326 |
local word = get_nodelist_text(word_start_node,word_end_node) |
| 327 |
hlist_segment_list[segment_count] = { |
| 328 |
[KEY_TYPE] = SEGMENT_WORD, |
| 329 |
[KEY_WORD] = word, |
| 330 |
[KEY_LANG] = lang, |
| 331 |
} |
| 332 |
|
| 333 |
word_start_node = nil |
| 334 |
lang = nil |
| 335 |
|
| 336 |
goto continue |
| 337 |
end |
| 338 |
if is_possible_space_node(item) then |
| 339 |
segment_count = segment_count + 1 |
| 340 |
|
| 341 |
while item and is_possible_space_node(item) do |
| 342 |
item = item.next |
| 343 |
end |
| 344 |
|
| 345 |
hlist_segment_list[segment_count] = { |
| 346 |
[KEY_TYPE] = SEGMENT_SPACE |
| 347 |
} |
| 348 |
|
| 349 |
goto continue |
| 350 |
end |
| 351 |
item = item.next |
| 352 |
|
| 353 |
::continue:: |
| 354 |
end |
| 355 |
|
| 356 |
debug('Pre-linebreak processing finish') |
| 357 |
|
| 358 |
return true |
| 359 |
end |
| 360 |
local USED_DISC_SEARCH_LIMIT = 20 |
| 361 |
|
| 362 |
local function get_used_disc(hlist_head) |
| 363 |
|
| 364 |
debug(' Looking for disc node at end of line') |
| 365 |
|
| 366 |
local item = node.tail(hlist_head) |
| 367 |
|
| 368 |
local count = 0 |
| 369 |
while item and item.id ~= NODE_ID_GLYPH and count < USED_DISC_SEARCH_LIMIT do |
| 370 |
if item.id == NODE_ID_DISC then |
| 371 |
return item |
| 372 |
end |
| 373 |
item = item.prev |
| 374 |
count = count + 1 |
| 375 |
end |
| 376 |
|
| 377 |
return nil |
| 378 |
|
| 379 |
end |
| 380 |
local function get_disc_word_start(hlist_head,n) |
| 381 |
|
| 382 |
local item = n |
| 383 |
|
| 384 |
while item do |
| 385 |
local prev = item.prev |
| 386 |
|
| 387 |
if not (prev and is_possible_word_node(prev)) then |
| 388 |
return item |
| 389 |
end |
| 390 |
|
| 391 |
item = prev |
| 392 |
end |
| 393 |
|
| 394 |
return nil |
| 395 |
end |
| 396 |
local function get_next_hlist(n) |
| 397 |
|
| 398 |
local item = n.next |
| 399 |
|
| 400 |
while item do |
| 401 |
if item.id == NODE_ID_HLIST then |
| 402 |
return item |
| 403 |
end |
| 404 |
item = item.next |
| 405 |
end |
| 406 |
|
| 407 |
return nil |
| 408 |
|
| 409 |
end |
| 410 |
local function get_line_first_word(hlist_head) |
| 411 |
local word_start_node = nil |
| 412 |
|
| 413 |
for item in node.traverse(hlist_head) do |
| 414 |
|
| 415 |
if item.id == NODE_ID_GLYPH then |
| 416 |
if not word_start_node then |
| 417 |
word_start_node = item |
| 418 |
end |
| 419 |
end |
| 420 |
|
| 421 |
if not is_possible_word_node(item) then |
| 422 |
if word_start_node then |
| 423 |
return get_nodelist_text(word_start_node,item.prev) |
| 424 |
end |
| 425 |
end |
| 426 |
|
| 427 |
end |
| 428 |
if word_start_node then |
| 429 |
return get_nodelist_text(word_start_node,node.tail(hlist_head)) |
| 430 |
else |
| 431 |
return nil |
| 432 |
end |
| 433 |
end |
| 434 |
local function get_context(index,incr,target_word_count) |
| 435 |
|
| 436 |
local result = '' |
| 437 |
local word_count = 0 |
| 438 |
|
| 439 |
local i = index + incr |
| 440 |
while ( |
| 441 |
i > 0 and i <= #hlist_segment_list and word_count < target_word_count |
| 442 |
) do |
| 443 |
local t = hlist_segment_list[i] |
| 444 |
|
| 445 |
local item |
| 446 |
|
| 447 |
if t[KEY_TYPE] == SEGMENT_WORD then |
| 448 |
item = t[KEY_WORD] |
| 449 |
word_count = word_count + 1 |
| 450 |
elseif t[KEY_TYPE] == SEGMENT_SPACE then |
| 451 |
item = STR_SPACE |
| 452 |
elseif t[KEY_TYPE] == SEGMENT_MATH then |
| 453 |
item = STR_MATH |
| 454 |
end |
| 455 |
|
| 456 |
if incr > 0 then |
| 457 |
result = result .. item |
| 458 |
else |
| 459 |
result = item .. result |
| 460 |
end |
| 461 |
|
| 462 |
i = i + incr |
| 463 |
end |
| 464 |
|
| 465 |
return result |
| 466 |
|
| 467 |
end |
| 468 |
local hyphenation_list = {} |
| 469 |
local hyphenation_count = 0 |
| 470 |
local function check_line_hyphenation(hlist) |
| 471 |
local last_disc = get_used_disc(hlist.head) |
| 472 |
if not last_disc then |
| 473 |
debug(' No disc node found at end of line') |
| 474 |
return |
| 475 |
end |
| 476 |
debug(' Disc node found at end of line') |
| 477 |
local hyphenation_index = node.has_attribute(last_disc,hyphen_attr) |
| 478 |
local t = hlist_segment_list[hyphenation_index] |
| 479 |
assert(t) |
| 480 |
assert(t[KEY_TYPE] == SEGMENT_WORD) |
| 481 |
local word = t[KEY_WORD] |
| 482 |
local lang = t[KEY_LANG] |
| 483 |
if not unicode.utf8.match(word,'%a') then |
| 484 |
debug(' Divided "word" contains no letters') |
| 485 |
return |
| 486 |
end |
| 487 |
local next_line = get_next_hlist(hlist) |
| 488 |
|
| 489 |
if not next_line then |
| 490 |
debug(' No following line found (which should not happen)') |
| 491 |
return |
| 492 |
end |
| 493 |
local pre = get_nodelist_text(get_disc_word_start(hlist.head,last_disc)) |
| 494 |
local pre_temp = trim_nonlettershyphens_start(pre) |
| 495 |
if pre_temp ~= '' then |
| 496 |
pre = pre_temp |
| 497 |
end |
| 498 |
local post = trim_nonlettershyphens_end(get_line_first_word(next_line.head)) |
| 499 |
local context = |
| 500 |
get_context( |
| 501 |
hyphenation_index,-1,tex.count['l__lualisthyphen_context_before_int'] |
| 502 |
) |
| 503 |
.. word .. |
| 504 |
get_context( |
| 505 |
hyphenation_index,1,tex.count['l__lualisthyphen_context_after_int'] |
| 506 |
) |
| 507 |
|
| 508 |
word = trim_nonlettershyphens_both(word) |
| 509 |
|
| 510 |
debug( |
| 511 |
' Hyphenated word found: "' .. word .. '" -> "' .. pre .. '<>' .. post .. '"' |
| 512 |
) |
| 513 |
hyphenation_count = hyphenation_count + 1 |
| 514 |
hyphenation_list[hyphenation_count] = { |
| 515 |
[KEY_LANG] = lang, |
| 516 |
[KEY_WORD] = word, |
| 517 |
[KEY_DIVISION] = pre .. post, |
| 518 |
[KEY_INDEX] = hyphenation_count, |
| 519 |
[KEY_CONTEXT] = context, |
| 520 |
} |
| 521 |
late_lua_n = node.new('whatsit','late_lua') |
| 522 |
late_lua_n.data = |
| 523 |
'lualisthyphen.set_hyphenation_page(' .. hyphenation_count .. ',tex.count["c@page"])' |
| 524 |
|
| 525 |
node.insert_after(hlist.head,last_disc,late_lua_n) |
| 526 |
|
| 527 |
end |
| 528 |
local function set_hyphenation_page(index,page) |
| 529 |
|
| 530 |
hyphenation_list[index][KEY_PAGE] = page |
| 531 |
|
| 532 |
end |
| 533 |
local function post_linebreak(vlist_head,groupcode) |
| 534 |
|
| 535 |
debug('Post-linebreak processing start') |
| 536 |
for item in node.traverse(vlist_head) do |
| 537 |
if item.id == NODE_ID_HLIST then |
| 538 |
node.slide(item.head) |
| 539 |
end |
| 540 |
end |
| 541 |
local line_no = 0 |
| 542 |
|
| 543 |
for item in node.traverse(vlist_head) do |
| 544 |
|
| 545 |
if item.id == NODE_ID_HLIST then |
| 546 |
line_no = line_no + 1 |
| 547 |
debug(' Line no.' .. line_no) |
| 548 |
check_line_hyphenation(item) |
| 549 |
end |
| 550 |
|
| 551 |
end |
| 552 |
|
| 553 |
hlist_segment_list = {} |
| 554 |
|
| 555 |
debug('Post-linebreak processing end') |
| 556 |
|
| 557 |
return true |
| 558 |
|
| 559 |
end |
| 560 |
local LUA_LIST_HYPHEN_PRE_LINEBREAK = 'LUA_LIST_HYPHEN_PRE_LINEBREAK' |
| 561 |
luatexbase.add_to_callback( |
| 562 |
'pre_linebreak_filter', |
| 563 |
pre_linebreak, |
| 564 |
LUA_LIST_HYPHEN_PRE_LINEBREAK |
| 565 |
) |
| 566 |
|
| 567 |
local LUA_LIST_HYPHEN_POST_LINEBREAK = 'LUA_LIST_HYPHEN_POST_LINEBREAK' |
| 568 |
luatexbase.add_to_callback( |
| 569 |
'post_linebreak_filter', |
| 570 |
post_linebreak, |
| 571 |
LUA_LIST_HYPHEN_POST_LINEBREAK |
| 572 |
) |
| 573 |
local language_table = {} |
| 574 |
local function babel_save_language_name(lang_id,name) |
| 575 |
|
| 576 |
if not language_table[lang_id] then |
| 577 |
language_table[lang_id] = name |
| 578 |
end |
| 579 |
|
| 580 |
end |
| 581 |
local function polyglossia_get_language_names() |
| 582 |
|
| 583 |
if not polyglossia then |
| 584 |
return |
| 585 |
end |
| 586 |
|
| 587 |
for name,language in pairs(polyglossia.newloader_loaded_languages) do |
| 588 |
language_table[lang.id(language)] = name |
| 589 |
end |
| 590 |
|
| 591 |
end |
| 592 |
local function equal_hyphenation_case_sensitive(a,b) |
| 593 |
return ( |
| 594 |
a[KEY_WORD] == b[KEY_WORD] |
| 595 |
and |
| 596 |
a[KEY_DIVISION] == b[KEY_DIVISION] |
| 597 |
) |
| 598 |
end |
| 599 |
local function equal_hyphenation_case_insensitive(a,b) |
| 600 |
return ( |
| 601 |
unicode.utf8.lower(a[KEY_WORD]) == unicode.utf8.lower(b[KEY_WORD]) |
| 602 |
and |
| 603 |
unicode.utf8.lower(a[KEY_DIVISION]) == unicode.utf8.lower(b[KEY_DIVISION]) |
| 604 |
) |
| 605 |
end |
| 606 |
local function lessthan_hyphenation_case_sensitive(a,b) |
| 607 |
return ( |
| 608 |
a[KEY_WORD] < b[KEY_WORD] |
| 609 |
or |
| 610 |
( |
| 611 |
a[KEY_WORD] == b[KEY_WORD] |
| 612 |
and |
| 613 |
a[KEY_DIVISION] < b[KEY_DIVISION] |
| 614 |
) |
| 615 |
or |
| 616 |
( |
| 617 |
a[KEY_WORD] == b[KEY_WORD] |
| 618 |
and |
| 619 |
a[KEY_DIVISION] == b[KEY_DIVISION] |
| 620 |
and |
| 621 |
a[KEY_INDEX] < b[KEY_INDEX] |
| 622 |
) |
| 623 |
) |
| 624 |
end |
| 625 |
local function lessthan_hyphenation_case_insensitive(a,b) |
| 626 |
return ( |
| 627 |
unicode.utf8.lower(a[KEY_WORD]) < unicode.utf8.lower(b[KEY_WORD]) |
| 628 |
or |
| 629 |
( |
| 630 |
unicode.utf8.lower(a[KEY_WORD]) == unicode.utf8.lower(b[KEY_WORD]) |
| 631 |
and |
| 632 |
unicode.utf8.lower(a[KEY_DIVISION]) < unicode.utf8.lower(b[KEY_DIVISION]) |
| 633 |
) |
| 634 |
or |
| 635 |
( |
| 636 |
unicode.utf8.lower(a[KEY_WORD]) == unicode.utf8.lower(b[KEY_WORD]) |
| 637 |
and |
| 638 |
unicode.utf8.lower(a[KEY_DIVISION]) < unicode.utf8.lower(b[KEY_DIVISION]) |
| 639 |
and |
| 640 |
a[KEY_INDEX] < b[KEY_INDEX] |
| 641 |
) |
| 642 |
) |
| 643 |
end |
| 644 |
local function sort_hyphenation_list_none(hyphenation_list) |
| 645 |
table.sort( |
| 646 |
hyphenation_list, |
| 647 |
function(a,b) |
| 648 |
return a[KEY_INDEX] < b[KEY_INDEX] |
| 649 |
end |
| 650 |
) |
| 651 |
end |
| 652 |
local function sort_hyphenation_list_case(hyphenation_list) |
| 653 |
table.sort( |
| 654 |
hyphenation_list, |
| 655 |
lessthan_hyphenation_case_sensitive |
| 656 |
) |
| 657 |
end |
| 658 |
local function sort_hyphenation_list_nocase(hyphenation_list) |
| 659 |
table.sort( |
| 660 |
hyphenation_list, |
| 661 |
lessthan_hyphenation_case_insensitive |
| 662 |
) |
| 663 |
end |
| 664 |
local sort_hyphenation_list |
| 665 |
if tex.count['l__lualisthyphen_sort_int'] == 1 then |
| 666 |
sort_hyphenation_list = sort_hyphenation_list_case |
| 667 |
elseif tex.count['l__lualisthyphen_sort_int'] == 2 then |
| 668 |
sort_hyphenation_list = sort_hyphenation_list_nocase |
| 669 |
else |
| 670 |
sort_hyphenation_list = sort_hyphenation_list_none |
| 671 |
end |
| 672 |
local function deduplicate_hyphenation_list_none(hyphenation_list) |
| 673 |
end |
| 674 |
local function deduplicate_hyphenation_list_case(hyphenation_list) |
| 675 |
table.sort( |
| 676 |
hyphenation_list, |
| 677 |
lessthan_hyphenation_case_sensitive |
| 678 |
) |
| 679 |
list_uniq( |
| 680 |
hyphenation_list, |
| 681 |
equal_hyphenation_case_sensitive |
| 682 |
) |
| 683 |
end |
| 684 |
local function deduplicate_hyphenation_list_nocase(hyphenation_list) |
| 685 |
table.sort( |
| 686 |
hyphenation_list, |
| 687 |
lessthan_hyphenation_case_insensitive |
| 688 |
) |
| 689 |
list_uniq( |
| 690 |
hyphenation_list, |
| 691 |
equal_hyphenation_case_insensitive |
| 692 |
) |
| 693 |
end |
| 694 |
local deduplicate_hyphenation_list |
| 695 |
if tex.count['l__lualisthyphen_unique_int'] == 1 then |
| 696 |
deduplicate_hyphenation_list = deduplicate_hyphenation_list_case |
| 697 |
elseif tex.count['l__lualisthyphen_unique_int'] == 2 then |
| 698 |
deduplicate_hyphenation_list = deduplicate_hyphenation_list_nocase |
| 699 |
else |
| 700 |
deduplicate_hyphenation_list = deduplicate_hyphenation_list_none |
| 701 |
end |
| 702 |
local function process_lang_hyphenation_list(hyphenation_list) |
| 703 |
deduplicate_hyphenation_list(hyphenation_list) |
| 704 |
sort_hyphenation_list(hyphenation_list) |
| 705 |
end |
| 706 |
local function write_lang_hyphenation_list_standard(f,hyphenation_list,widths) |
| 707 |
|
| 708 |
for i,v in ipairs(hyphenation_list) do |
| 709 |
|
| 710 |
if v then |
| 711 |
f:write(v[KEY_DIVISION] .. '\n') |
| 712 |
end |
| 713 |
|
| 714 |
end |
| 715 |
|
| 716 |
end |
| 717 |
local function write_lang_hyphenation_list_verbose(f,hyphenation_list,widths) |
| 718 |
|
| 719 |
local cols_word = widths[KEY_WORD] |
| 720 |
local cols_division = widths[KEY_DIVISION] |
| 721 |
local cols_page = widths[KEY_PAGE] |
| 722 |
|
| 723 |
for i,v in ipairs(hyphenation_list) do |
| 724 |
|
| 725 |
if v then |
| 726 |
local page = v[KEY_PAGE] |
| 727 |
if page then |
| 728 |
page = STR_PAGE_PREFIX .. page |
| 729 |
else |
| 730 |
page = STR_PAGE_NONE |
| 731 |
end |
| 732 |
|
| 733 |
f:write( |
| 734 |
rpad(v[KEY_WORD],cols_word) |
| 735 |
.. STR_ARROW |
| 736 |
.. rpad(v[KEY_DIVISION],cols_division) |
| 737 |
.. STR_SPACE_TWO |
| 738 |
.. lpad(page,cols_page) |
| 739 |
.. STR_SPACE |
| 740 |
.. STR_QUOTE_OPEN |
| 741 |
.. v[KEY_CONTEXT] |
| 742 |
.. STR_QUOTE_CLOSE |
| 743 |
.. '\n' |
| 744 |
) |
| 745 |
end |
| 746 |
|
| 747 |
end |
| 748 |
|
| 749 |
end |
| 750 |
local write_lang_hyphenation_list |
| 751 |
if tex.count['l__lualisthyphen_verbose_int'] == 0 then |
| 752 |
write_lang_hyphenation_list = write_lang_hyphenation_list_standard |
| 753 |
else |
| 754 |
write_lang_hyphenation_list = write_lang_hyphenation_list_verbose |
| 755 |
end |
| 756 |
local settings_desc |
| 757 |
if tex.count['l__lualisthyphen_verbose_int'] == 0 then |
| 758 |
settings_desc = 'verbose=false' |
| 759 |
else |
| 760 |
settings_desc = 'verbose=true' |
| 761 |
.. ',context-before=' .. tex.count['l__lualisthyphen_context_before_int'] |
| 762 |
.. ',context-after=' .. tex.count['l__lualisthyphen_context_after_int'] |
| 763 |
end |
| 764 |
if tex.count['l__lualisthyphen_include_non_output_int'] == 0 then |
| 765 |
settings_desc = settings_desc .. ',include-non-output=false' |
| 766 |
else |
| 767 |
settings_desc = settings_desc .. ',include-non-output=true' |
| 768 |
end |
| 769 |
local NONE_CASE_NOCASE = { |
| 770 |
[0] = 'none', |
| 771 |
[1] = 'case', |
| 772 |
[2] = 'nocase' |
| 773 |
} |
| 774 |
settings_desc = settings_desc |
| 775 |
.. ',sort=' .. NONE_CASE_NOCASE[tex.count['l__lualisthyphen_sort_int']] |
| 776 |
.. ',unique=' .. NONE_CASE_NOCASE[tex.count['l__lualisthyphen_unique_int']] |
| 777 |
local function get_hyphenation_file_path(prefix,extension,lang_name) |
| 778 |
|
| 779 |
local hyphenation_file_path = prefix .. tostring(lang_name) .. extension |
| 780 |
|
| 781 |
if not status.output_directory then |
| 782 |
return hyphenation_file_path |
| 783 |
end |
| 784 |
|
| 785 |
if string.sub(status.output_directory,-1,-1) == '/' then |
| 786 |
hyphenation_file_path = status.output_directory |
| 787 |
.. hyphenation_file_path |
| 788 |
else |
| 789 |
hyphenation_file_path = status.output_directory |
| 790 |
.. '/' .. hyphenation_file_path |
| 791 |
end |
| 792 |
|
| 793 |
return hyphenation_file_path |
| 794 |
|
| 795 |
end |
| 796 |
local function process_write_lang_hyphenation_list( |
| 797 |
prefix,extension,lang_id,hyphenation_list,widths |
| 798 |
) |
| 799 |
|
| 800 |
process_lang_hyphenation_list(hyphenation_list) |
| 801 |
|
| 802 |
local lang_name = language_table[lang_id] |
| 803 |
local lang_desc |
| 804 |
if not lang_name then |
| 805 |
lang_name = lang_id |
| 806 |
lang_desc = 'language with ID ' .. lang_id |
| 807 |
else |
| 808 |
lang_desc = 'language "' .. lang_name .. '" (ID ' .. lang_id .. ')' |
| 809 |
end |
| 810 |
|
| 811 |
local f = io.open(get_hyphenation_file_path(prefix,extension,lang_name),'w') |
| 812 |
|
| 813 |
f:write('% Chosen hyphenations for ' .. lang_desc .. '\n') |
| 814 |
f:write('% Generated by lua-list-hyphen (' .. settings_desc .. ')\n') |
| 815 |
|
| 816 |
write_lang_hyphenation_list(f,hyphenation_list,widths) |
| 817 |
f:close() |
| 818 |
|
| 819 |
end |
| 820 |
local function process_write_hyphenation_lists(prefix,extension) |
| 821 |
|
| 822 |
local lang_hyphenation_table = {} |
| 823 |
local lang_widths_table = {} |
| 824 |
for _,h in pairs(hyphenation_list) do |
| 825 |
|
| 826 |
if h[KEY_PAGE] or tex.count['l__lualisthyphen_include_non_output_int'] == 1 then |
| 827 |
|
| 828 |
local lang = h[KEY_LANG] |
| 829 |
|
| 830 |
local t = lang_hyphenation_table[lang] |
| 831 |
if not t then |
| 832 |
lang_hyphenation_table[lang] = {} |
| 833 |
t = lang_hyphenation_table[lang] |
| 834 |
end |
| 835 |
|
| 836 |
local widths = lang_widths_table[lang] |
| 837 |
if not widths then |
| 838 |
lang_widths_table[lang] = { |
| 839 |
[KEY_WORD] = 0, |
| 840 |
[KEY_DIVISION] = 0, |
| 841 |
[KEY_PAGE] = 0 |
| 842 |
} |
| 843 |
widths = lang_widths_table[lang] |
| 844 |
end |
| 845 |
|
| 846 |
widths[KEY_WORD] = math.max( |
| 847 |
widths[KEY_WORD], |
| 848 |
unicode.utf8.len(h[KEY_WORD]) |
| 849 |
) |
| 850 |
widths[KEY_DIVISION] = math.max( |
| 851 |
widths[KEY_DIVISION], |
| 852 |
unicode.utf8.len(h[KEY_DIVISION]) |
| 853 |
) |
| 854 |
widths[KEY_PAGE] = math.max( |
| 855 |
widths[KEY_PAGE], |
| 856 |
unicode.utf8.len(tostring(h[KEY_PAGE])) |
| 857 |
) |
| 858 |
|
| 859 |
table.insert(t,h) |
| 860 |
|
| 861 |
end |
| 862 |
|
| 863 |
end |
| 864 |
for _,widths in pairs(lang_widths_table) do |
| 865 |
widths[KEY_PAGE] = math.max( |
| 866 |
widths[KEY_PAGE] + unicode.utf8.len(STR_PAGE_PREFIX), |
| 867 |
unicode.utf8.len(STR_PAGE_NONE) |
| 868 |
) |
| 869 |
end |
| 870 |
polyglossia_get_language_names() |
| 871 |
for k,v in pairs(lang_hyphenation_table) do |
| 872 |
process_write_lang_hyphenation_list(prefix,extension,k,v,lang_widths_table[k]) |
| 873 |
end |
| 874 |
|
| 875 |
end |
| 876 |
return { |
| 877 |
process_write_hyphenation_lists = process_write_hyphenation_lists, |
| 878 |
set_hyphenation_page = set_hyphenation_page, |
| 879 |
babel_save_language_name = babel_save_language_name, |
| 880 |
} |
| 881 |
|