| 1 |
-- |
| 2 |
-- This is file `babel-transforms.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- babel.dtx (with options: `transforms') |
| 8 |
-- |
| 9 |
-- |
| 10 |
-- Copyright (C) 2012-2026 Javier Bezos and Johannes L. Braams. |
| 11 |
-- Copyright (C) 1989-2012 Johannes L. Braams and |
| 12 |
-- any individual authors listed elsewhere in this file. |
| 13 |
-- All rights reserved. |
| 14 |
-- |
| 15 |
-- |
| 16 |
-- This file is part of the Babel system. |
| 17 |
-- -------------------------------------- |
| 18 |
-- |
| 19 |
-- It may be distributed and/or modified under the |
| 20 |
-- conditions of the LaTeX Project Public License, either version 1.3 |
| 21 |
-- of this license or (at your option) any later version. |
| 22 |
-- The latest version of this license is in |
| 23 |
-- http://www.latex-project.org/lppl.txt |
| 24 |
-- and version 1.3 or later is part of all distributions of LaTeX |
| 25 |
-- version 2003/12/01 or later. |
| 26 |
-- |
| 27 |
-- This work has the LPPL maintenance status "maintained". |
| 28 |
-- |
| 29 |
-- The Current Maintainer of this work is Javier Bezos. |
| 30 |
-- |
| 31 |
-- The list of derived (unpacked) files belonging to the distribution |
| 32 |
-- and covered by LPPL is defined by the unpacking scripts (with |
| 33 |
-- extension |.ins|) which are part of the distribution. |
| 34 |
-- |
| 35 |
Babel.linebreaking.replacements = {} |
| 36 |
Babel.linebreaking.replacements[0] = {} -- pre |
| 37 |
Babel.linebreaking.replacements[1] = {} -- post |
| 38 |
|
| 39 |
function Babel.tovalue(v) |
| 40 |
if type(v) == 'table' then |
| 41 |
return Babel.locale_props[v[1]].vars[v[2]] or v[3] |
| 42 |
else |
| 43 |
return v |
| 44 |
end |
| 45 |
end |
| 46 |
|
| 47 |
Babel.attr_hboxed = luatexbase.registernumber'bbl@attr@hboxed' |
| 48 |
|
| 49 |
function Babel.set_hboxed(head, gc) |
| 50 |
for item in node.traverse(head) do |
| 51 |
node.set_attribute(item, Babel.attr_hboxed, 1) |
| 52 |
end |
| 53 |
return head |
| 54 |
end |
| 55 |
|
| 56 |
Babel.fetch_subtext = {} |
| 57 |
|
| 58 |
Babel.ignore_pre_char = function(node) |
| 59 |
return (node.lang == Babel.nohyphenation) |
| 60 |
end |
| 61 |
|
| 62 |
Babel.show_transforms = false |
| 63 |
|
| 64 |
-- Merging both functions doesn't seen feasible, because there are too |
| 65 |
-- many differences. |
| 66 |
Babel.fetch_subtext[0] = function(head) |
| 67 |
local word_string = '' |
| 68 |
local word_nodes = {} |
| 69 |
local lang |
| 70 |
local item = head |
| 71 |
local inmath = false |
| 72 |
|
| 73 |
while item do |
| 74 |
|
| 75 |
if item.id == 11 then |
| 76 |
inmath = (item.subtype == 0) |
| 77 |
end |
| 78 |
|
| 79 |
if inmath then |
| 80 |
-- pass |
| 81 |
|
| 82 |
elseif item.id == 29 then |
| 83 |
local locale = node.get_attribute(item, Babel.attr_locale) |
| 84 |
|
| 85 |
if lang == locale or lang == nil then |
| 86 |
lang = lang or locale |
| 87 |
if Babel.ignore_pre_char(item) then |
| 88 |
word_string = word_string .. Babel.us_char |
| 89 |
else |
| 90 |
if node.has_attribute(item, Babel.attr_hboxed) then |
| 91 |
word_string = word_string .. Babel.us_char |
| 92 |
else |
| 93 |
word_string = word_string .. unicode.utf8.char(item.char) |
| 94 |
end |
| 95 |
end |
| 96 |
word_nodes[#word_nodes+1] = item |
| 97 |
else |
| 98 |
break |
| 99 |
end |
| 100 |
|
| 101 |
elseif item.id == 12 and item.subtype == 13 then |
| 102 |
if node.has_attribute(item, Babel.attr_hboxed) then |
| 103 |
word_string = word_string .. Babel.us_char |
| 104 |
else |
| 105 |
word_string = word_string .. ' ' |
| 106 |
end |
| 107 |
word_nodes[#word_nodes+1] = item |
| 108 |
|
| 109 |
-- Ignore leading unrecognized nodes, too. |
| 110 |
elseif word_string ~= '' then |
| 111 |
word_string = word_string .. Babel.us_char |
| 112 |
word_nodes[#word_nodes+1] = item -- Will be ignored |
| 113 |
end |
| 114 |
|
| 115 |
item = item.next |
| 116 |
end |
| 117 |
|
| 118 |
-- Here and above we remove some trailing chars but not the |
| 119 |
-- corresponding nodes. But they aren't accessed. |
| 120 |
if word_string:sub(-1) == ' ' then |
| 121 |
word_string = word_string:sub(1,-2) |
| 122 |
end |
| 123 |
if Babel.show_transforms then texio.write_nl(word_string) end |
| 124 |
word_string = unicode.utf8.gsub(word_string, Babel.us_char .. '+$', '') |
| 125 |
return word_string, word_nodes, item, lang |
| 126 |
end |
| 127 |
|
| 128 |
Babel.fetch_subtext[1] = function(head) |
| 129 |
local word_string = '' |
| 130 |
local word_nodes = {} |
| 131 |
local lang |
| 132 |
local item = head |
| 133 |
local inmath = false |
| 134 |
|
| 135 |
while item do |
| 136 |
|
| 137 |
if item.id == 11 then |
| 138 |
inmath = (item.subtype == 0) |
| 139 |
end |
| 140 |
|
| 141 |
if inmath then |
| 142 |
-- pass |
| 143 |
|
| 144 |
elseif item.id == 29 then |
| 145 |
if item.lang == lang or lang == nil then |
| 146 |
lang = lang or item.lang |
| 147 |
if node.has_attribute(item, Babel.attr_hboxed) then |
| 148 |
word_string = word_string .. Babel.us_char |
| 149 |
elseif (item.char == 124) or (item.char == 61) then -- not =, not | |
| 150 |
word_string = word_string .. Babel.us_char |
| 151 |
else |
| 152 |
word_string = word_string .. unicode.utf8.char(item.char) |
| 153 |
end |
| 154 |
word_nodes[#word_nodes+1] = item |
| 155 |
else |
| 156 |
break |
| 157 |
end |
| 158 |
|
| 159 |
elseif item.id == 7 and item.subtype == 2 then |
| 160 |
if node.has_attribute(item, Babel.attr_hboxed) then |
| 161 |
word_string = word_string .. Babel.us_char |
| 162 |
else |
| 163 |
word_string = word_string .. '=' |
| 164 |
end |
| 165 |
word_nodes[#word_nodes+1] = item |
| 166 |
|
| 167 |
elseif item.id == 7 and item.subtype == 3 then |
| 168 |
if node.has_attribute(item, Babel.attr_hboxed) then |
| 169 |
word_string = word_string .. Babel.us_char |
| 170 |
else |
| 171 |
word_string = word_string .. '|' |
| 172 |
end |
| 173 |
word_nodes[#word_nodes+1] = item |
| 174 |
|
| 175 |
-- (1) Go to next word if nothing was found, and (2) implicitly |
| 176 |
-- remove leading USs. |
| 177 |
elseif word_string == '' then |
| 178 |
-- pass |
| 179 |
|
| 180 |
-- This is the responsible for splitting by words. |
| 181 |
elseif (item.id == 12 and item.subtype == 13) then |
| 182 |
break |
| 183 |
|
| 184 |
else |
| 185 |
word_string = word_string .. Babel.us_char |
| 186 |
word_nodes[#word_nodes+1] = item -- Will be ignored |
| 187 |
end |
| 188 |
|
| 189 |
item = item.next |
| 190 |
end |
| 191 |
if Babel.show_transforms then texio.write_nl(word_string) end |
| 192 |
word_string = unicode.utf8.gsub(word_string, Babel.us_char .. '+$', '') |
| 193 |
return word_string, word_nodes, item, lang |
| 194 |
end |
| 195 |
|
| 196 |
function Babel.pre_hyphenate_replace(head) |
| 197 |
Babel.hyphenate_replace(head, 0) |
| 198 |
end |
| 199 |
|
| 200 |
function Babel.post_hyphenate_replace(head) |
| 201 |
Babel.hyphenate_replace(head, 1) |
| 202 |
end |
| 203 |
|
| 204 |
Babel.us_char = string.char(31) |
| 205 |
|
| 206 |
function Babel.hyphenate_replace(head, mode) |
| 207 |
local u = unicode.utf8 |
| 208 |
local lbkr = Babel.linebreaking.replacements[mode] |
| 209 |
local tovalue = Babel.tovalue |
| 210 |
|
| 211 |
local word_head = head |
| 212 |
|
| 213 |
if Babel.show_transforms then |
| 214 |
texio.write_nl('\n==== Showing ' .. (mode == 0 and 'pre' or 'post') .. 'hyphenation ====') |
| 215 |
end |
| 216 |
|
| 217 |
while true do -- for each subtext block |
| 218 |
|
| 219 |
local w, w_nodes, nw, lang = Babel.fetch_subtext[mode](word_head) |
| 220 |
|
| 221 |
if Babel.debug then |
| 222 |
print() |
| 223 |
print((mode == 0) and '@@@@<' or '@@@@>', w) |
| 224 |
end |
| 225 |
|
| 226 |
if nw == nil and w == '' then break end |
| 227 |
|
| 228 |
if not lang then goto next end |
| 229 |
if not lbkr[lang] then goto next end |
| 230 |
|
| 231 |
-- For each saved (pre|post)hyphenation. TODO. Reconsider how |
| 232 |
-- loops are nested. |
| 233 |
for k=1, #lbkr[lang] do |
| 234 |
local p = lbkr[lang][k].pattern |
| 235 |
local r = lbkr[lang][k].replace |
| 236 |
local attr = lbkr[lang][k].attr or -1 |
| 237 |
|
| 238 |
if Babel.debug then |
| 239 |
print('*****', p, mode) |
| 240 |
end |
| 241 |
|
| 242 |
-- This variable is set in some cases below to the first *byte* |
| 243 |
-- after the match, either as found by u.match (faster) or the |
| 244 |
-- computed position based on sc if w has changed. |
| 245 |
local last_match = 0 |
| 246 |
local step = 0 |
| 247 |
|
| 248 |
-- For every match. |
| 249 |
while true do |
| 250 |
if Babel.debug then |
| 251 |
print('=====') |
| 252 |
end |
| 253 |
local new -- used when inserting and removing nodes |
| 254 |
local dummy_node -- used by after |
| 255 |
|
| 256 |
local matches = { u.match(w, p, last_match) } |
| 257 |
|
| 258 |
if #matches < 2 then break end |
| 259 |
|
| 260 |
-- Get and remove empty captures (with ()'s, which return a |
| 261 |
-- number with the position), and keep actual captures |
| 262 |
-- (from (...)), if any, in matches. |
| 263 |
local first = table.remove(matches, 1) |
| 264 |
local last = table.remove(matches, #matches) |
| 265 |
-- Non re-fetched substrings may contain \31, which separates |
| 266 |
-- subsubstrings. |
| 267 |
if string.find(w:sub(first, last-1), Babel.us_char) then break end |
| 268 |
|
| 269 |
local save_last = last -- with A()BC()D, points to D |
| 270 |
|
| 271 |
-- Fix offsets, from bytes to unicode. Explained above. |
| 272 |
first = u.len(w:sub(1, first-1)) + 1 |
| 273 |
last = u.len(w:sub(1, last-1)) -- now last points to C |
| 274 |
|
| 275 |
-- This loop stores in a small table the nodes |
| 276 |
-- corresponding to the pattern. Used by 'data' to provide a |
| 277 |
-- predictable behavior with 'insert' (w_nodes is modified on |
| 278 |
-- the fly), and also access to 'remove'd nodes. |
| 279 |
local sc = first-1 -- Used below, too |
| 280 |
local data_nodes = {} |
| 281 |
|
| 282 |
local enabled = true |
| 283 |
for q = 1, last-first+1 do |
| 284 |
data_nodes[q] = w_nodes[sc+q] |
| 285 |
if enabled |
| 286 |
and attr > -1 |
| 287 |
and not node.has_attribute(data_nodes[q], attr) |
| 288 |
then |
| 289 |
enabled = false |
| 290 |
end |
| 291 |
end |
| 292 |
|
| 293 |
-- This loop traverses the matched substring and takes the |
| 294 |
-- corresponding action stored in the replacement list. |
| 295 |
-- sc = the position in substr nodes / string |
| 296 |
-- rc = the replacement table index |
| 297 |
local rc = 0 |
| 298 |
|
| 299 |
------- TODO. dummy_node? |
| 300 |
while rc < last-first+1 or dummy_node do -- for each replacement |
| 301 |
if Babel.debug then |
| 302 |
print('.....', rc + 1) |
| 303 |
end |
| 304 |
sc = sc + 1 |
| 305 |
rc = rc + 1 |
| 306 |
|
| 307 |
if Babel.debug then |
| 308 |
Babel.debug_hyph(w, w_nodes, sc, first, last, last_match) |
| 309 |
local ss = '' |
| 310 |
for itt in node.traverse(head) do |
| 311 |
if itt.id == 29 then |
| 312 |
ss = ss .. unicode.utf8.char(itt.char) |
| 313 |
else |
| 314 |
ss = ss .. '{' .. itt.id .. '}' |
| 315 |
end |
| 316 |
end |
| 317 |
print('*****************', ss) |
| 318 |
|
| 319 |
end |
| 320 |
|
| 321 |
local crep = r[rc] |
| 322 |
local item = w_nodes[sc] |
| 323 |
local item_base = item |
| 324 |
local placeholder = Babel.us_char |
| 325 |
local d |
| 326 |
|
| 327 |
if crep and crep.data then |
| 328 |
item_base = data_nodes[crep.data] |
| 329 |
end |
| 330 |
|
| 331 |
if crep then |
| 332 |
step = crep.step or step |
| 333 |
end |
| 334 |
|
| 335 |
if crep and crep.after then |
| 336 |
crep.insert = true |
| 337 |
if dummy_node then |
| 338 |
item = dummy_node |
| 339 |
else -- TODO. if there is a node after? |
| 340 |
d = node.copy(item_base) |
| 341 |
head, item = node.insert_after(head, item, d) |
| 342 |
dummy_node = item |
| 343 |
end |
| 344 |
end |
| 345 |
|
| 346 |
if crep and not crep.after and dummy_node then |
| 347 |
node.remove(head, dummy_node) |
| 348 |
dummy_node = nil |
| 349 |
end |
| 350 |
|
| 351 |
if not enabled then |
| 352 |
last_match = save_last |
| 353 |
goto next |
| 354 |
|
| 355 |
elseif crep and next(crep) == nil then -- = {} |
| 356 |
if step == 0 then |
| 357 |
last_match = save_last -- Optimization |
| 358 |
else |
| 359 |
last_match = utf8.offset(w, sc+step) |
| 360 |
end |
| 361 |
goto next |
| 362 |
|
| 363 |
elseif crep == nil or crep.remove then |
| 364 |
node.remove(head, item) |
| 365 |
table.remove(w_nodes, sc) |
| 366 |
w = u.sub(w, 1, sc-1) .. u.sub(w, sc+1) |
| 367 |
sc = sc - 1 -- Nothing has been inserted. |
| 368 |
last_match = utf8.offset(w, sc+1+step) |
| 369 |
goto next |
| 370 |
|
| 371 |
elseif crep and crep.kashida then |
| 372 |
node.set_attribute(item, |
| 373 |
Babel.attr_kashida, |
| 374 |
crep.kashida) |
| 375 |
last_match = utf8.offset(w, sc+1+step) |
| 376 |
goto next |
| 377 |
|
| 378 |
elseif crep and crep.string then |
| 379 |
local str = crep.string(matches) |
| 380 |
if str == '' then -- Gather with nil |
| 381 |
node.remove(head, item) |
| 382 |
table.remove(w_nodes, sc) |
| 383 |
w = u.sub(w, 1, sc-1) .. u.sub(w, sc+1) |
| 384 |
sc = sc - 1 -- Nothing has been inserted. |
| 385 |
else |
| 386 |
local loop_first = true |
| 387 |
for s in string.utfvalues(str) do |
| 388 |
d = node.copy(item_base) |
| 389 |
d.char = s |
| 390 |
if loop_first then |
| 391 |
loop_first = false |
| 392 |
head, new = node.insert_before(head, item, d) |
| 393 |
if sc == 1 then |
| 394 |
word_head = head |
| 395 |
end |
| 396 |
w_nodes[sc] = d |
| 397 |
w = u.sub(w, 1, sc-1) .. u.char(s) .. u.sub(w, sc+1) |
| 398 |
else |
| 399 |
sc = sc + 1 |
| 400 |
head, new = node.insert_before(head, item, d) |
| 401 |
table.insert(w_nodes, sc, new) |
| 402 |
w = u.sub(w, 1, sc-1) .. u.char(s) .. u.sub(w, sc) |
| 403 |
end |
| 404 |
if Babel.debug then |
| 405 |
print('.....', 'str') |
| 406 |
Babel.debug_hyph(w, w_nodes, sc, first, last, last_match) |
| 407 |
end |
| 408 |
end -- for |
| 409 |
node.remove(head, item) |
| 410 |
end -- if '' |
| 411 |
last_match = utf8.offset(w, sc+1+step) |
| 412 |
goto next |
| 413 |
|
| 414 |
elseif mode == 1 and crep and (crep.pre or crep.no or crep.post) then |
| 415 |
d = node.new(7, 3) -- (disc, regular) |
| 416 |
d.pre = Babel.str_to_nodes(crep.pre, matches, item_base) |
| 417 |
d.post = Babel.str_to_nodes(crep.post, matches, item_base) |
| 418 |
d.replace = Babel.str_to_nodes(crep.no, matches, item_base) |
| 419 |
d.attr = item_base.attr |
| 420 |
if crep.pre == nil then -- TeXbook p96 |
| 421 |
d.penalty = tovalue(crep.penalty) or tex.hyphenpenalty |
| 422 |
else |
| 423 |
d.penalty = tovalue(crep.penalty) or tex.exhyphenpenalty |
| 424 |
end |
| 425 |
placeholder = '|' |
| 426 |
head, new = node.insert_before(head, item, d) |
| 427 |
|
| 428 |
elseif mode == 0 and crep and (crep.pre or crep.no or crep.post) then |
| 429 |
-- ERROR |
| 430 |
|
| 431 |
elseif crep and crep.penalty then |
| 432 |
d = node.new(14, 0) -- (penalty, userpenalty) |
| 433 |
d.attr = item_base.attr |
| 434 |
d.penalty = tovalue(crep.penalty) |
| 435 |
head, new = node.insert_before(head, item, d) |
| 436 |
|
| 437 |
elseif crep and crep.space then |
| 438 |
-- 655360 = 10 pt = 10 * 65536 sp |
| 439 |
d = node.new(12, 13) -- (glue, spaceskip) |
| 440 |
local quad = font.getfont(item_base.font).size or 655360 |
| 441 |
node.setglue(d, tovalue(crep.space[1]) * quad, |
| 442 |
tovalue(crep.space[2]) * quad, |
| 443 |
tovalue(crep.space[3]) * quad) |
| 444 |
if mode == 0 then |
| 445 |
placeholder = ' ' |
| 446 |
end |
| 447 |
head, new = node.insert_before(head, item, d) |
| 448 |
|
| 449 |
elseif crep and crep.norule then |
| 450 |
-- 655360 = 10 pt = 10 * 65536 sp |
| 451 |
d = node.new(2, 3) -- (rule, empty) = \no*rule |
| 452 |
local quad = font.getfont(item_base.font).size or 655360 |
| 453 |
d.width = tovalue(crep.norule[1]) * quad |
| 454 |
d.height = tovalue(crep.norule[2]) * quad |
| 455 |
d.depth = tovalue(crep.norule[3]) * quad |
| 456 |
head, new = node.insert_before(head, item, d) |
| 457 |
|
| 458 |
elseif crep and crep.spacefactor then |
| 459 |
d = node.new(12, 13) -- (glue, spaceskip) |
| 460 |
local base_font = font.getfont(item_base.font) |
| 461 |
node.setglue(d, |
| 462 |
tovalue(crep.spacefactor[1]) * base_font.parameters['space'], |
| 463 |
tovalue(crep.spacefactor[2]) * base_font.parameters['space_stretch'], |
| 464 |
tovalue(crep.spacefactor[3]) * base_font.parameters['space_shrink']) |
| 465 |
if mode == 0 then |
| 466 |
placeholder = ' ' |
| 467 |
end |
| 468 |
head, new = node.insert_before(head, item, d) |
| 469 |
|
| 470 |
elseif mode == 0 and crep and crep.space then |
| 471 |
-- ERROR |
| 472 |
|
| 473 |
elseif crep and crep.kern then |
| 474 |
d = node.new(13, 1) -- (kern, user) |
| 475 |
local quad = font.getfont(item_base.font).size or 655360 |
| 476 |
d.attr = item_base.attr |
| 477 |
d.kern = tovalue(crep.kern) * quad |
| 478 |
head, new = node.insert_before(head, item, d) |
| 479 |
|
| 480 |
elseif crep and crep.node then |
| 481 |
d = node.new(crep.node[1], crep.node[2]) |
| 482 |
d.attr = item_base.attr |
| 483 |
head, new = node.insert_before(head, item, d) |
| 484 |
|
| 485 |
end -- i.e., replacement cases |
| 486 |
|
| 487 |
-- Shared by disc, space(factor), kern, node and penalty. |
| 488 |
if sc == 1 then |
| 489 |
word_head = head |
| 490 |
end |
| 491 |
if crep.insert then |
| 492 |
w = u.sub(w, 1, sc-1) .. placeholder .. u.sub(w, sc) |
| 493 |
table.insert(w_nodes, sc, new) |
| 494 |
last = last + 1 |
| 495 |
else |
| 496 |
w_nodes[sc] = d |
| 497 |
node.remove(head, item) |
| 498 |
w = u.sub(w, 1, sc-1) .. placeholder .. u.sub(w, sc+1) |
| 499 |
end |
| 500 |
|
| 501 |
last_match = utf8.offset(w, sc+1+step) |
| 502 |
|
| 503 |
::next:: |
| 504 |
|
| 505 |
end -- for each replacement |
| 506 |
|
| 507 |
if Babel.show_transforms then texio.write_nl('> ' .. w) end |
| 508 |
if Babel.debug then |
| 509 |
print('.....', '/') |
| 510 |
Babel.debug_hyph(w, w_nodes, sc, first, last, last_match) |
| 511 |
end |
| 512 |
|
| 513 |
if dummy_node then |
| 514 |
node.remove(head, dummy_node) |
| 515 |
dummy_node = nil |
| 516 |
end |
| 517 |
|
| 518 |
end -- for match |
| 519 |
|
| 520 |
end -- for patterns |
| 521 |
|
| 522 |
::next:: |
| 523 |
word_head = nw |
| 524 |
end -- for substring |
| 525 |
|
| 526 |
if Babel.show_transforms then texio.write_nl(string.rep('-', 32) .. '\n') end |
| 527 |
return head |
| 528 |
end |
| 529 |
|
| 530 |
-- This table stores capture maps, numbered consecutively |
| 531 |
Babel.capture_maps = {} |
| 532 |
|
| 533 |
function Babel.esc_hex_to_char(h) |
| 534 |
if tex.getcatcode(tonumber(h, 16)) ~= 11 and |
| 535 |
tex.getcatcode(tonumber(h, 16)) ~= 12 then |
| 536 |
return string.format([[\Uchar"%X ]], tonumber(h,16)) |
| 537 |
else |
| 538 |
return unicode.utf8.char(tonumber(h, 16)) |
| 539 |
end |
| 540 |
end |
| 541 |
|
| 542 |
-- The following functions belong to the next macro |
| 543 |
function Babel.capture_func(key, cap) |
| 544 |
local ret = "[[" .. cap:gsub('{([0-9])}', "]]..m[%1]..[[") .. "]]" |
| 545 |
local cnt |
| 546 |
local u = unicode.utf8 |
| 547 |
ret = u.gsub(ret, '{(%x%x%x%x+)}', '\x01%1\x04') |
| 548 |
ret, cnt = ret:gsub('{([0-9])|([^|]+)|(.-)}', Babel.capture_func_map) |
| 549 |
ret = u.gsub(ret, '\x01(%x%x%x%x+)\x04', Babel.esc_hex_to_char) |
| 550 |
ret = ret:gsub("%[%[%]%]%.%.", '') |
| 551 |
ret = ret:gsub("%.%.%[%[%]%]", '') |
| 552 |
return key .. [[=function(m) return ]] .. ret .. [[ end]] |
| 553 |
end |
| 554 |
|
| 555 |
function Babel.capt_map(from, mapno) |
| 556 |
return Babel.capture_maps[mapno][from] or from |
| 557 |
end |
| 558 |
|
| 559 |
-- Handle the {n|abc|ABC} syntax in captures |
| 560 |
function Babel.capture_func_map(capno, from, to) |
| 561 |
local u = unicode.utf8 |
| 562 |
from = u.gsub(from, '\x01(%x%x%x%x+)\x04', |
| 563 |
function (n) |
| 564 |
return u.char(tonumber(n, 16)) |
| 565 |
end) |
| 566 |
to = u.gsub(to, '\x01(%x%x%x%x+)\x04', |
| 567 |
function (n) |
| 568 |
return u.char(tonumber(n, 16)) |
| 569 |
end) |
| 570 |
local froms = {} |
| 571 |
for s in string.utfcharacters(from) do |
| 572 |
table.insert(froms, s) |
| 573 |
end |
| 574 |
local cnt = 1 |
| 575 |
table.insert(Babel.capture_maps, {}) |
| 576 |
local mlen = table.getn(Babel.capture_maps) |
| 577 |
for s in string.utfcharacters(to) do |
| 578 |
Babel.capture_maps[mlen][froms[cnt]] = s |
| 579 |
cnt = cnt + 1 |
| 580 |
end |
| 581 |
return "]]..Babel.capt_map(m[" .. capno .. "]," .. |
| 582 |
(mlen) .. ").." .. "[[" |
| 583 |
end |
| 584 |
|
| 585 |
-- Create/Extend reversed sorted list of kashida weights: |
| 586 |
function Babel.capture_kashida(key, wt) |
| 587 |
wt = tonumber(wt) |
| 588 |
if Babel.kashida_wts then |
| 589 |
for p, q in ipairs(Babel.kashida_wts) do |
| 590 |
if wt == q then |
| 591 |
break |
| 592 |
elseif wt > q then |
| 593 |
table.insert(Babel.kashida_wts, p, wt) |
| 594 |
break |
| 595 |
elseif table.getn(Babel.kashida_wts) == p then |
| 596 |
table.insert(Babel.kashida_wts, wt) |
| 597 |
end |
| 598 |
end |
| 599 |
else |
| 600 |
Babel.kashida_wts = { wt } |
| 601 |
end |
| 602 |
return 'kashida = ' .. wt |
| 603 |
end |
| 604 |
|
| 605 |
function Babel.capture_node(id, subtype) |
| 606 |
local sbt = 0 |
| 607 |
for k, v in pairs(node.subtypes(id)) do |
| 608 |
if v == subtype then sbt = k end |
| 609 |
end |
| 610 |
return 'node = {' .. node.id(id) .. ', ' .. sbt .. '}' |
| 611 |
end |
| 612 |
|
| 613 |
-- Experimental: applies prehyphenation transforms to a string (letters |
| 614 |
-- and spaces). |
| 615 |
function Babel.string_prehyphenation(str, locale) |
| 616 |
local n, head, last, res |
| 617 |
head = node.new(8, 0) -- dummy (hack just to start) |
| 618 |
last = head |
| 619 |
for s in string.utfvalues(str) do |
| 620 |
if s == 20 then |
| 621 |
n = node.new(12, 0) |
| 622 |
else |
| 623 |
n = node.new(29, 0) |
| 624 |
n.char = s |
| 625 |
end |
| 626 |
node.set_attribute(n, Babel.attr_locale, locale) |
| 627 |
last.next = n |
| 628 |
last = n |
| 629 |
end |
| 630 |
head = Babel.hyphenate_replace(head, 0) |
| 631 |
res = '' |
| 632 |
for n in node.traverse(head) do |
| 633 |
if n.id == 12 then |
| 634 |
res = res .. ' ' |
| 635 |
elseif n.id == 29 then |
| 636 |
res = res .. unicode.utf8.char(n.char) |
| 637 |
end |
| 638 |
end |
| 639 |
tex.print(res) |
| 640 |
end |