Debian Codegraph @

texmf-dist/tex/lualatex/ekdosis/ekdosis.lua

raw
1 --[[
2 This file is part of the `ekdosis' package
3
4 ekdosis -- Typesetting TEI xml-compliant critical editions
5 Copyright (C) 2020--2021 Robert Alessi
6
7 Please send error reports and suggestions for improvements to Robert
8 Alessi <[email protected]>
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see
22 <http://www.gnu.org/licenses/>.
23 --]]
24
25 -- `'
26 -- This table will hold the functions:
27 ekdosis = {}
28
29 -- lpeg equivalent for string.gsub()
30 local function gsub(s, patt, repl)
31 patt = lpeg.P(patt)
32 patt = lpeg.Cs((patt / repl + 1)^0)
33 return lpeg.match(patt, s)
34 end
35
36 -- some basic patterns:
37 local letters = lpeg.R("az", "AZ")
38 local ascii = lpeg.R("az", "AZ", "@@")
39 local dblbkslash = lpeg.Cs("\\")
40 local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" }
41 local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" }
42 local spce = lpeg.Cs(" ")
43 local spcenc = lpeg.P(" ")
44 local cmdstar = lpeg.Cs(spce * lpeg.P("*"))
45 local bsqbracketsii = lpeg.Cs(bsqbrackets^-2)
46 local bcbracesii = lpeg.Cs(bcbraces^-2)
47 local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1)
48 local rawcmd = lpeg.Cs(dblbkslash * ascii^1)
49 local aftercmd = lpeg.Cs(lpeg.S("*[{,.?;:'`\"") + dblbkslash)
50 local cmdargs = lpeg.Cs(spce^-1 * bsqbracketsii * bcbracesii * bsqbrackets^-1)
51 local app = lpeg.Cs("app")
52 local lemrdg = lpeg.Cs(lpeg.Cs("lem") + lpeg.Cs("rdg"))
53 local note = lpeg.Cs("note")
54 local inlem = lpeg.Cs{ "<lem" * ((1 - (lpeg.P"<lem" + lpeg.P"</lem>")) + lpeg.V(1))^0 * "</lem>" }
55 local inanchor = lpeg.Cs{ "<anchor" * ((1 - (lpeg.P"<" + lpeg.P">")) + lpeg.V(1))^0 * ">" }
56 local inopeningnote = lpeg.Cs{ "<note" * ((1 - (lpeg.P"<" + lpeg.P">")) + lpeg.V(1))^0 * ">" }
57 local lnbrk = lpeg.Cs("\\\\")
58 local poemline = lpeg.Cs(lnbrk * spcenc^-1 * lpeg.S("*!")^-1 * bsqbrackets^-1 * spcenc^-1)
59 local poemlinebreak = lpeg.Cs(lnbrk * spcenc^-1 * (lpeg.P("&gt;") + lpeg.P("+")) * bsqbrackets^-1 * spcenc^-1)
60 local linegroup = lpeg.Cs{ "<lg" * ((1 - lpeg.S"<>") + lpeg.V(1))^0 * ">" }
61 local bclinegroup = lpeg.Cs(linegroup + lpeg.P("</lg>"))
62 local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1) -- not used
63 local sections = lpeg.Cs(lpeg.P("book") + lpeg.P("part") + lpeg.P("chapter")
64 + lpeg.P("section") + lpeg.P("subsection")
65 + lpeg.P("subsubsection"))
66 local par = lpeg.P(lpeg.P("\\par") * spce^0)
67 local parb = lpeg.P(lpeg.P("\\p@rb") * spce^0)
68 local para = lpeg.P(lpeg.P("\\p@ra") * spce^0)
69 local labelrefcmds = lpeg.Cs(lpeg.P("label")
70 + lpeg.P("linelabel")
71 + lpeg.P("lineref")
72 + lpeg.P("ref")
73 + lpeg.P("pageref")
74 + lpeg.P("vref")
75 + lpeg.P("vpageref"))
76 local citecmds = lpeg.Cs(lpeg.P("icite")
77 + lpeg.P("cite")
78 + lpeg.P("Cite")
79 + lpeg.P("cite *")
80 + lpeg.P("parencite")
81 + lpeg.P("Parencite")
82 + lpeg.P("parencite *")
83 + lpeg.P("footcite")
84 + lpeg.P("footcitetext")
85 + lpeg.P("textcite")
86 + lpeg.P("Textcite")
87 + lpeg.P("smartcite")
88 + lpeg.P("Smartcite")
89 + lpeg.P("autocite")
90 + lpeg.P("Autocite")
91 + lpeg.P("autocite *")
92 + lpeg.P("Autocite *")
93 )
94 --
95 -- Bind to local variables
96 local next = next
97
98 -- General
99 local xmlids = {}
100 table.insert(xmlids, {xmlid = "scholars"} )
101
102 local function xmlidfound(element)
103 for i = 1,#xmlids do
104 if xmlids[i].xmlid == element then
105 return true
106 end
107 end
108 return false
109 end
110
111 local function checkxmlid(str)
112 if string.find(str, "^[0-9]")
113 or string.find(str, "[%{%}%[%]%(%):; ]")
114 then
115 return false
116 else
117 return true
118 end
119 end
120
121 -- Witnesses
122 local listWit = {}
123 -- Persons/Scholars
124 local listPerson = {}
125
126 local idsRend = {}
127 local shorthands = {}
128
129 local function isfound(table, value)
130 for i = 1,#table do
131 if table[i] == value then
132 return true
133 end
134 end
135 return false
136 end
137
138 local function isintable(table, value)
139 for i = 1,#table do
140 if table[i].a == value then
141 return true
142 end
143 end
144 return false
145 end
146
147 local function get_a_index(id, table)
148 local idfound = nil
149 for i = 1,#table
150 do
151 if table[i].a == id then
152 idfound = i
153 break
154 end
155 end
156 return idfound
157 end
158
159 local function getindex(id, table)
160 local idfound = nil
161 for i = 1,#table
162 do
163 if table[i].xmlid == id then
164 idfound = i
165 break
166 end
167 end
168 return idfound
169 end
170
171 function ekdosis.newwitness(id,
172 siglum,
173 description,
174 Settlement,
175 Institution,
176 Repository,
177 Collection,
178 Idno,
179 MsName,
180 OrigDate,
181 Locus)
182 if xmlidfound(id)
183 then
184 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
185 ..id..
186 "\" already exists as an xml:id. "
187 ..
188 "Please pick another id.}}")
189 elseif not checkxmlid(id)
190 then
191 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
192 ..id..
193 "\" is not a valid xml:id. \\MessageBreak "
194 ..
195 "Please pick another id.}}")
196 else
197 table.insert(xmlids, {xmlid = id})
198 table.sort(xmlids, function(a ,b) return(#a.xmlid > #b.xmlid) end)
199 table.insert(idsRend, {xmlid = id, abbr = siglum})
200 table.sort(idsRend, function(a ,b) return(#a.xmlid > #b.xmlid) end)
201 table.insert(listWit, {xmlid = id,
202 abbr = siglum,
203 detailsDesc = description,
204 msIdentifier = {
205 settlement = Settlement,
206 institution = Institution,
207 repository = Repository,
208 collection = Collection,
209 idno = Idno,
210 msName = MsName}
211 })
212 local indexwit = getindex(id, listWit)
213 if OrigDate ~= "" then
214 listWit[indexwit].history = {}
215 listWit[indexwit].history.origin = {origDate = OrigDate}
216 end
217 if Locus ~= "" then
218 listWit[indexwit].msContents = {}
219 listWit[indexwit].msContents.msItemStruct = {locus = Locus}
220 end
221 end
222 return true
223 end
224
225 function ekdosis.newhand(id, witid, siglum, description)
226 if xmlidfound(id) or not xmlidfound(witid)
227 then
228 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
229 ..id..
230 "\" already exists as an xml:id. "
231 ..
232 "Please pick another id.}}")
233 elseif not checkxmlid(id)
234 then
235 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
236 ..id..
237 "\" is not a valid xml:id. \\MessageBreak "
238 ..
239 "Please pick another id.}}")
240 else
241 table.insert(xmlids, {xmlid = id})
242 table.sort(xmlids, function(a ,b) return(#a.xmlid > #b.xmlid) end)
243 table.insert(idsRend, {xmlid = id, abbr = siglum})
244 table.sort(idsRend, function(a ,b) return(#a.xmlid > #b.xmlid) end)
245 local indexwit = getindex(witid, listWit)
246 -- listWit[indexwit].handDesc = {xmlid = id, abbr = siglum, handNote = description}
247 if listWit[indexwit].handDesc == nil
248 then
249 listWit[indexwit].handDesc = {}
250 else
251 end
252 table.insert(listWit[indexwit].handDesc,
253 {xmlid = id, abbr = siglum, detailsDesc = description})
254 end
255 return true
256 end
257
258 function ekdosis.newshorthand(id, rend, xmlids)
259 if isintable(shorthands, id)
260 then
261 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
262 ..id..
263 "\" already exists as a shorthand. "
264 ..
265 "Please pick another shorthand.}}")
266 else
267 table.insert(shorthands, { a = id, b = rend, c = xmlids })
268 table.sort(shorthands, function(a ,b) return(#a.a > #b.a) end)
269 table.insert(idsRend, {xmlid = id, abbr = rend})
270 table.sort(idsRend, function(a ,b) return(#a.xmlid > #b.xmlid) end)
271 end
272 return true
273 end
274
275 function ekdosis.newscholar(id,
276 siglum,
277 rawname,
278 Forename,
279 Surname,
280 AddName,
281 Note)
282 if xmlidfound(id)
283 then
284 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
285 ..id..
286 "\" already exists as an xml:id. "
287 ..
288 "Please pick another id.}}")
289 elseif not checkxmlid(id)
290 then
291 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
292 ..id..
293 "\" is not a valid xml:id. \\MessageBreak "
294 ..
295 "Please pick another id.}}")
296 else
297 table.insert(xmlids, {xmlid = id})
298 table.sort(xmlids, function(a ,b) return(#a.xmlid > #b.xmlid) end)
299 table.insert(idsRend, {xmlid = id, abbr = siglum})
300 table.sort(idsRend, function(a ,b) return(#a.xmlid > #b.xmlid) end)
301 if rawname ~= ""
302 then
303 table.insert(listPerson, {xmlid = id,
304 abbr = siglum,
305 note = Note,
306 persName = {
307 name = rawname}
308 })
309 else
310 table.insert(listPerson, {xmlid = id,
311 abbr = siglum,
312 note = Note,
313 persName = {
314 forename = Forename,
315 surname = Surname,
316 addName = AddName}
317 })
318 end
319 end
320 return true
321 end
322
323 local xmlbibresource = nil
324
325 function ekdosis.addxmlbibresource(str)
326 if string.find(str, "%.xml$")
327 then
328 xmlbibresource = str
329 else
330 xmlbibresource = str..".xml"
331 end
332 return true
333 end
334
335 function ekdosis.newsource(id, siglum)
336 if xmlidfound(id)
337 then
338 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
339 ..id..
340 "\" already exists as an xml:id. "
341 ..
342 "Please pick another id.}}")
343 elseif not checkxmlid(id)
344 then
345 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
346 ..id..
347 "\" is not a valid xml:id. \\MessageBreak "
348 ..
349 "Please pick another id.}}")
350 else
351 table.insert(xmlids, {xmlid = id})
352 table.sort(xmlids, function(a ,b) return(#a.xmlid > #b.xmlid) end)
353 table.insert(idsRend, {xmlid = id, abbr = siglum})
354 table.sort(idsRend, function(a ,b) return(#a.xmlid > #b.xmlid) end)
355 end
356 return true
357 end
358
359 local familysep = nil
360
361 function ekdosis.setfamilysep(str)
362 if str == "reset"
363 then
364 familysep = nil
365 else
366 familysep = str
367 end
368 return true
369 end
370
371 function ekdosis.getsiglum(str, opt)
372 str = str..","
373 str = string.gsub(str, "%s-(%,)", "%1")
374 ctrl = str
375 if opt == "tei" then
376 for i = 1,#shorthands do
377 local tempa = string.gsub(shorthands[i].a, "([%-%.%_])", "%%%1")
378 str = string.gsub(str, tempa, shorthands[i].c)
379 end
380 for i = 1,#idsRend do
381 local tempb = string.gsub(idsRend[i].xmlid, "([%-%.%_])", "%%%1")
382 str = string.gsub(str, "(%f[%w%-%.%_])"..tempb.."(%,)",
383 "%1#"..idsRend[i].xmlid.."%2")
384 ctrl = string.gsub(ctrl, tempb.."%,", "")
385 end
386 str = string.gsub(str, "%,(%s-)([%#])", " %2")
387 str = string.gsub(str, "%,$", "")
388 else
389 for i = 1,#idsRend do
390 local tempc = string.gsub(idsRend[i].xmlid, "([%-%.%_])", "%%%1")
391 str = string.gsub(str, tempc.."%,", idsRend[i].abbr)
392 ctrl = string.gsub(ctrl, tempc.."%,", "")
393 end
394 if familysep ~= nil
395 then
396 str = string.gsub(str, "%s+", familysep)
397 end
398 end
399 -- if string.find(ctrl, "[A-Za-z0-9]")
400 if string.find(ctrl, "%S")
401 then
402 return "<??>"
403 else
404 return str
405 end
406 end
407
408 -- begin totei functions
409
410 local cmdtotags = {
411 {a="textsuperscript", b="hi", c=" rend=\"sup\""},
412 {a="textsubscript", b="hi", c=" rend=\"sub\""},
413 {a="LRfootnote", b="note", c=" place=\"bottom\""},
414 {a="RLfootnote", b="note", c=" place=\"bottom\""},
415 {a="enquote *", b="quote", c=""},
416 {a="marginpar", b="note", c=" place=\"margin\""},
417 {a="footnote", b="note", c=" place=\"bottom\""},
418 {a="enquote", b="quote", c=""},
419 {a="prname *", b="span",
420 c=" xml:lang=\"ar-Latn\" type=\"transliterated\""},
421 {a="prname", b="span",
422 c=" xml:lang=\"ar-Latn\" type=\"transliterated\" subtype=\"arabtex\""},
423 {a="txtrans", b="s", c=" xml:lang=\"ar-Latn\" type=\"transliterated\""},
424 {a="textbf", b="hi", c=" rend=\"bold\""},
425 {a="textit", b="hi", c=" rend=\"italic\""},
426 {a="textsc", b="hi", c=" rend=\"smallcaps\""},
427 {a="textsf", b="hi", c=" rend=\"sf\""},
428 {a="arbup", b="hi", c=" rend=\"sup\""},
429 {a="txarb", b="foreign", c=" xml:lang=\"arb\""},
430 {a="arb", b="span",
431 c=" xml:lang=\"ar-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
432 }
433
434 local texpatttotags = {
435 {a="\\addentries%s+%[(.-)%]{(.-)}", b=""},
436 {a="\\addentries%s+{(.-)}", b=""},
437 {a="\\vmodulolinenumbers%s+%[(.-)%]", b=""},
438 {a="\\vmodulolinenumbers%s+", b=""},
439 {a="\\modulolinenumbers%s+%[(.-)%]", b=""},
440 {a="\\modulolinenumbers%s+", b=""},
441 {a="\\setverselinenums%s+{(.-)}{(.-)}", b=""},
442 {a="\\resetvlinenumber%s+%[(.-)%]", b=""},
443 {a="\\resetvlinenumber%s+", b=""},
444 {a="\\resetlinenumber%s+%[(.-)%]", b=""},
445 {a="\\resetlinenumber%s+", b=""},
446 {a="\\ekdresethfmarks%s+", b=""},
447 {a="\\indentpattern%s+{(.-)}", b=""},
448 {a="\\ekdnohfmark%s+", b=""},
449 {a="\\settowidth%s+{(.-)}{(.-)}", b=""},
450 {a="\\poemlines%s+{(.-)}", b=""},
451 {a="\\pagebreak%s+%[[1-4]%]", b=""},
452 {a="\\pagebreak%s+", b=""},
453 {a="\\teidirect%s+(%b[]){(.-)}(%b{})", b="<%2 ekd@os%1ekd@cs>ekd@ob%3ekd@cb</%2>"},
454 {a="\\teidirect%s+{(.-)}(%b{})", b="<%1>ekd@ob%2ekd@cb</%1>"},
455 {a="\\altrfont%s+", b=""},
456 {a="\\endmark%s+", b=""},
457 {a="\\ekdpb%s+%*?%[(.-)%]{(.-)}", b=""},
458 {a="\\ekdpb%s+%*?{(.-)}", b=""},
459 {a="\\ekdpb%s+%*\\?", b=""},
460 {a="\\mbox%s+(%b{})", b="ekd@ob%1ekd@cb"},
461 {a="\\LR%s+(%b{})", b="ekd@ob%1ekd@cb"},
462 {a="\\RL%s+(%b{})", b="ekd@ob%1ekd@cb"},
463 {a="\\%=%=%=%s?", b="—"},
464 {a="\\%-%-%-%s?", b="—"},
465 {a="\\%=%=%s?", b="–"},
466 {a="\\%-%-%s?", b="–"},
467 {a="\\%=%/%s?", b="‐"},
468 {a="\\%-%/%s?", b="‐"},
469 {a="\\vin%s+", b=""},
470 {a="\\uc%s+(%b{})", b="ekd@ob%1ekd@cb"}
471 }
472
473 local envtotags = {
474 {a="flushright", b="p", c=" rend=\"align(right)\""},
475 {a="flushleft", b="p", c=" rend=\"align(left)\""},
476 {a="quotation", b="quote", c=""},
477 {a="txarabtr", b="p", c=" xml:lang=\"ar-Latn\" type=\"transliterated\""},
478 {a="quoting", b="quote", c=""},
479 {a="ekdpar", b="p", c=""},
480 {a="txarab", b="p", c=" xml:lang=\"arb\""},
481 {a="center", b="p", c=" rend=\"align(center)\""},
482 {a="arab", b="p",
483 c=" xml:lang=\"ar-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
484 }
485
486 local close_p = {
487 "p",
488 "lg"
489 }
490
491 local forbid_xmlid = true
492
493 function ekdosis.newcmdtotag(cmd, tag, attr)
494 if forbid_xmlid
495 then
496 attr = string.gsub(attr, "xml:id", "n") -- xml:id is not allowed here
497 else
498 end
499 if isintable(cmdtotags, cmd)
500 then
501 local index = get_a_index(cmd, cmdtotags)
502 table.remove(cmdtotags, index)
503 table.insert(cmdtotags, {a = cmd, b = tag, c = " "..attr})
504 table.sort(cmdtotags, function(a ,b) return(#a.a > #b.a) end)
505 else
506 table.insert(cmdtotags, {a = cmd, b = tag, c = " "..attr})
507 table.sort(cmdtotags, function(a ,b) return(#a.a > #b.a) end)
508 end
509 return true
510 end
511
512 function ekdosis.newpatttotag(pat, repl)
513 pat = string.gsub(pat, "([%[%]])", "%%%1")
514 pat = string.gsub(pat, "%@b[1-9]", "(%%b{})")
515 pat = string.gsub(pat, "%@s[1-9]", "(%%b[])")
516 pat = string.gsub(pat, "%#[1-9]", "(.-)")
517 repl = string.gsub(repl, "%#([1-9])", "%%%1")
518 repl = string.gsub(repl, "%@b([1-9])", "ekd@ob%%%1ekd@cb")
519 repl = string.gsub(repl, "%@s([1-9])", "ekd@os%%%1ekd@cs")
520 if isintable(texpatttotags, pat)
521 then
522 local index = get_a_index(pat, texpatttotags)
523 table.remove(texpatttotags, index)
524 table.insert(texpatttotags, { a = pat, b = repl })
525 table.sort(texpatttotags, function(a ,b) return(#a.a > #b.a) end)
526 else
527 table.insert(texpatttotags, { a = pat, b = repl })
528 table.sort(texpatttotags, function(a ,b) return(#a.a > #b.a) end)
529 end
530 return true
531 end
532
533 function ekdosis.newenvtotag(env, tag, attr, closep)
534 if forbid_xmlid
535 then
536 attr = string.gsub(attr, "xml:id", "n") -- xml:id is not allowed here
537 else
538 end
539 if isintable(envtotags, env)
540 then
541 local index = get_a_index(env, envtotags)
542 table.remove(envtotags, index)
543 table.insert(envtotags, {a = env, b = tag, c = " "..attr, d = closep})
544 table.sort(envtotags, function(a ,b) return(#a.a > #b.a) end)
545 else
546 table.insert(envtotags, {a = env, b = tag, c = " "..attr, d = closep})
547 table.sort(envtotags, function(a ,b) return(#a.a > #b.a) end)
548 end
549 return true
550 end
551
552 -- Get values of attributes
553 local function get_attr_value(str, attr)
554 str = str..","
555 str = string.gsub(str, "%b{}", function(body)
556 body = string.gsub(body, attr, attr.."@ekd")
557 return string.format("%s", body)
558 end)
559 local attrval = string.match(str, "%f[%w]"..attr.."%s?%=%s?%b{}")
560 or string.match(str, "%f[%w]"..attr.."%s?%=%s?.-%,")
561 or ""
562 attrval = string.gsub(attrval, attr.."%s?%=%s?(%b{})", function(bbraces)
563 bbraces = string.sub(bbraces, 2, -2)
564 return string.format("%s", bbraces)
565 end)
566 attrval = string.gsub(attrval, attr.."%s?%=%s?(.-)%s?%,", "%1")
567 str = string.gsub(str, attr.."@ekd", attr)
568 return attrval
569 end
570
571 local function xml_entities(str)
572 str = string.gsub(str, "%<", "&lt;")
573 str = string.gsub(str, "%>", "&gt;")
574 return str
575 end
576
577 local function note_totei(str)
578 str = gsub(str,
579 dblbkslash *
580 note *
581 spcenc^-1 *
582 bsqbrackets *
583 bcbraces *
584 spcenc^-1,
585 function(bkslash, cmd, opt, arg)
586 opt = string.sub(opt, 2, -2)
587 arg = string.sub(arg, 2, -2)
588 teitype = get_attr_value(opt, "type")
589 if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
590 right = get_attr_value(opt, "labelb")
591 left = get_attr_value(opt, "labele")
592 if right == ""
593 then
594 return string.format("<%s>%s</%s>", cmd, arg, cmd)
595 else
596 if left ~= ""
597 then
598 return string.format(
599 "<%s%s target=\"#range(right(%s),left(%s))\">%s</%s><anchor xml:id=\"%s\"/>",
600 cmd, teitype, right, left, arg, cmd, right)
601 elseif left == ""
602 then
603 return string.format(
604 "<%s%s target=\"#right(%s)\">%s</%s><anchor xml:id=\"%s\"/>",
605 cmd, teitype, right, arg, cmd, right)
606 end
607 end
608 end)
609 return str
610 end
611
612 local function remove_extra_anchors(str)
613 str = gsub(str, lpeg.Cs("</note>") * inanchor * inopeningnote, function(enote, anchor, bnote)
614 local id_one = string.gsub(anchor, "(%<anchor )(.-)(/%>)", "%2")
615 id_one = string.sub(get_attr_value(id_one, "xml:id"), 2, -2)
616 local id_two = string.match(bnote, "target%=.-right%((.-)%)")
617 id_two = string.gsub(id_two, ".-right%((.-)%)", "%1")
618 if id_one == id_two
619 then
620 return string.format("%s%s", enote, bnote)
621 else
622 return string.format("%s%s%s", enote, anchor, bnote)
623 end
624 end)
625 return str
626 end
627
628 local function app_totei(str)
629 str = gsub(str,
630 dblbkslash *
631 app *
632 spcenc^-1 *
633 bsqbrackets *
634 bcbraces *
635 spcenc^-1,
636 function(bkslash, cmd, opt, arg)
637 opt = string.sub(opt, 2, -2)
638 arg = string.sub(arg, 2, -2)
639 opt = get_attr_value(opt, "type")
640 if opt ~= "" then opt = " type=\""..opt.."\"" else end
641 return app_totei(string.format("<%s%s>%s</%s>",
642 cmd, opt, arg, cmd))
643 end)
644 return str
645 end
646
647 local function rdgGrp_totei(str)
648 str = gsub(str,
649 dblbkslash *
650 lpeg.Cs("rdgGrp") *
651 spcenc^-1 *
652 bsqbrackets *
653 bcbraces *
654 spcenc^-1,
655 function(bkslash, cmd, opt, arg)
656 opt = string.sub(opt, 2, -2)
657 arg = string.sub(arg, 2, -2)
658 teitype = get_attr_value(opt, "type")
659 if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
660 if opt == ""
661 then
662 return rdgGrp_totei(string.format("<%s>%s</%s>",
663 cmd, arg, cmd))
664 else
665 return rdgGrp_totei(string.format("<%s%s>%s</%s>",
666 cmd, teitype, arg, cmd))
667 end
668 end)
669 return str
670 end
671
672 local function lem_rdg_totei(str)
673 str = gsub(str,
674 spcenc^-1 *
675 dblbkslash *
676 lemrdg *
677 spcenc^-1 *
678 bsqbrackets *
679 bcbraces *
680 spcenc^-1,
681 function(bkslash, cmd, opt, arg)
682 opt = string.sub(opt, 2, -2)
683 arg = string.sub(arg, 2, -2)
684 -- opt = get_attr_value(opt, "wit")
685 --
686 teiwit = get_attr_value(opt, "wit")
687 if teiwit ~= "" then teiwit = " wit=\""..ekdosis.getsiglum(teiwit, "tei").."\"" else end
688 teisource = get_attr_value(opt, "source")
689 if teisource ~= "" then teisource = " source=\""..ekdosis.getsiglum(teisource, "tei").."\"" else end
690 teiresp = get_attr_value(opt, "resp")
691 if teiresp ~= "" then teiresp = " resp=\""..ekdosis.getsiglum(teiresp, "tei").."\"" else end
692 teitype = get_attr_value(opt, "type")
693 if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
694 --
695 if opt == ""
696 then
697 return lem_rdg_totei(string.format("<%s>%s</%s>",
698 cmd, arg, cmd))
699 else
700 -- opt = ekdosis.getsiglum(opt, "tei")
701 return lem_rdg_totei(string.format("<%s%s%s%s%s>%s</%s>",
702 cmd, teiwit, teisource, teiresp, teitype, arg, cmd))
703 end
704 end)
705 str = gsub(str, spcenc^-0 * dblbkslash * lemrdg * spcenc^-1 * bcbraces * spcenc^-1,
706 function(bkslash, cmd, arg)
707 arg = string.sub(arg, 2, -2)
708 return lem_rdg_totei(string.format("<%s>%s</%s>", cmd, arg, cmd))
709 end)
710 return str
711 end
712
713 local function relocate_notes(str)
714 str = gsub(str, inlem, function(arg)
715 local notes = {}
716 for i in string.gmatch(arg, "%<note.-%>.-%<%/note%>")
717 do
718 table.insert(notes, i)
719 end
720 allnotes = table.concat(notes)
721 arg = string.gsub(arg, "%<note.->.-%<%/note%>", "")
722 return string.format("%s%s", arg, allnotes)
723 end)
724 return str
725 end
726
727 local function linestotei(str)
728 if not string.find(str, "^%s?<lg")
729 then
730 str = "\n<l>"..str
731 end
732 str = gsub(str, poemline * spcenc^0 * bclinegroup, "</l>\n%2")
733 str = gsub(str, linegroup * -(spcenc^0 * bclinegroup), "%1\n<l>")
734 str = gsub(str, lpeg.Cs("</lg>") * -(spcenc^0 * (bclinegroup + -1)), "%1\n<l>")
735 -- str = gsub(str, poemline * spcenc^-1 * -1, "</l>\n")
736 str = gsub(str, poemlinebreak, "<lb/> ")
737 -- str = gsub(str, poemline * spcenc^-1 * lpeg.Cs("<lg"), "</l>%2")
738 -- str = gsub(str, lpeg.Cs("</lg>") * spcenc^1 * -lpeg.P("<l"), "%1\n<l>")
739 str = gsub(str, poemline, "</l>\n<l>")
740 return str
741 end
742
743 local function stanzatotei(str)
744 str = string.gsub(str, "\\begin%s?%{ekdstanza%}(%b[])(.-)\\end%s?%{ekdstanza%}", function(opt, arg)
745 arg = string.gsub(arg, "\\par%s?", "")
746 opt = string.sub(opt, 2, -2)
747 teitype = get_attr_value(opt, "type")
748 if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
749 if opt == ""
750 then
751 return string.format("<lg>%s</lg>", arg)
752 else
753 return string.format("<lg%s>%s</lg>", teitype, arg)
754 end
755 end)
756 str = string.gsub(str, "\\begin%s?%{ekdstanza%}(.-)\\end%s?%{ekdstanza%}", function(arg)
757 arg = string.gsub(arg, "\\par%s?", "")
758 return string.format("<lg>%s</lg>", arg)
759 end)
760 return str
761 end
762
763 -- better use lpeg: look into this later
764 local function versetotei(str)
765 str = string.gsub(str, "\\begin%s?%{ekdverse%}(%b[])(.-)\\end%s?%{ekdverse%}", function(opt, arg)
766 arg = string.gsub(arg, "\\par%s?", "")
767 arg = string.gsub(arg, "\\begin%s?%{patverse%*?%}", "")
768 arg = string.gsub(arg, "\\end%s?%{patverse%*?%}", "")
769 arg = string.gsub(arg, "\\indentpattern%s?%b{}", "")
770 opt = string.sub(opt, 2, -2)
771 teitype = get_attr_value(opt, "type")
772 if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
773 if opt == ""
774 then
775 return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
776 else
777 return "\\p@rb "..linestotei(string.format("<lg%s>%s</lg>", teitype, arg)).."\\p@ra "
778 end
779 end)
780 str = string.gsub(str, "\\begin%s?%{ekdverse%}(.-)\\end%s?%{ekdverse%}", function(arg)
781 arg = string.gsub(arg, "\\par%s?", "")
782 arg = string.gsub(arg, "\\begin%s?%{patverse%*?%}", "")
783 arg = string.gsub(arg, "\\end%s?%{patverse%*?%}", "")
784 arg = string.gsub(arg, "\\indentpattern%s?%b{}", "")
785 return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
786 end)
787 str = string.gsub(str, "\\begin%s?%{verse%}%b[](.-)\\end%s?%{verse%}", function(arg)
788 arg = string.gsub(arg, "\\par%s?", "")
789 return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
790 end)
791 str = string.gsub(str, "\\begin%s?%{verse%}(.-)\\end%s?%{verse%}", function(arg)
792 arg = string.gsub(arg, "\\par%s?", "")
793 return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
794 end)
795 return str
796 end
797
798 local function envtotei(str)
799 for i = 1,#envtotags
800 do
801 if envtotags[i].b ~= ""
802 then
803 if isfound(close_p, envtotags[i].b) or envtotags[i].d == "yes"
804 then
805 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
806 * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
807 * bsqbracketsii * bcbracesii * spcenc^-1,
808 "\\p@rb <"..envtotags[i].b..envtotags[i].c..">")
809 str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
810 * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
811 "</"..envtotags[i].b..">\\p@ra ")
812 else
813 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
814 * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
815 * bsqbracketsii * bcbracesii * spcenc^-1,
816 "<"..envtotags[i].b..envtotags[i].c..">")
817 str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
818 * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
819 "</"..envtotags[i].b..">")
820 end
821 else
822 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
823 * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
824 * bsqbracketsii * bcbracesii * spcenc^-1,
825 "")
826 str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
827 * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
828 "")
829 end
830 end
831 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
832 * lpeg.Cs(ascii^1) * lpeg.P("}") * bsqbracketsii
833 * bcbracesii * spcenc^-1,
834 "<%1>")
835 str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
836 * lpeg.Cs(ascii^1) * lpeg.P("}") * bsqbracketsii
837 * bcbracesii,
838 "</%1>")
839 return str
840 end
841
842 local function texpatttotei(str)
843 for i = 1,#texpatttotags do
844 str = string.gsub(str, texpatttotags[i].a, texpatttotags[i].b)
845 str = string.gsub(str, "ekd@ob%{", "")
846 str = string.gsub(str, "%}ekd@cb", "")
847 str = string.gsub(str, "ekd@os%[", "")
848 str = string.gsub(str, "%]ekd@cs", "")
849 end
850 return str
851 end
852
853 local function icitetotei(str)
854 str = gsub(str, lpeg.P("\\")
855 * citecmds
856 * spcenc^-1
857 * bsqbrackets
858 * bsqbrackets
859 * bcbraces
860 * (bsqbrackets + bcbraces)^-1,
861 function(cmd, pre, post, body, opt)
862 pre = string.sub(pre, 2, -2)
863 post = string.sub(post, 2, -2)
864 body = string.sub(body, 2, -2)
865 if not checkxmlid(body)
866 then
867 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
868 ..body..
869 "\" is not a valid xml:id. \\MessageBreak "
870 ..
871 "Please pick another id.}}")
872 else
873 end
874 return string.format("%s <bibl corresp=\"#%s\"><biblScope>%s</biblScope></bibl>",
875 pre, body, post)
876 end)
877 str = gsub(str, lpeg.P("\\")
878 * citecmds
879 * spcenc^-1
880 * bsqbrackets
881 * bcbraces
882 * (bsqbrackets + bcbraces)^-1,
883 function(cmd, post, body, opt)
884 post = string.sub(post, 2, -2)
885 body = string.sub(body, 2, -2)
886 if not checkxmlid(body)
887 then
888 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
889 ..body..
890 "\" is not a valid xml:id. \\MessageBreak "
891 ..
892 "Please pick another id.}}")
893 else
894 end
895 return string.format("<bibl corresp=\"#%s\"><biblScope>%s</biblScope></bibl>", body, post)
896 end)
897 str = gsub(str, lpeg.P("\\")
898 * citecmds
899 * spcenc^-1
900 * bcbraces
901 * (bsqbrackets + bcbraces)^-1,
902 function(cmd, body, opt)
903 body = string.sub(body, 2, -2)
904 if not checkxmlid(body)
905 then
906 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
907 ..body..
908 "\" is not a valid xml:id. \\MessageBreak "
909 ..
910 "Please pick another id.}}")
911 else
912 end
913 return string.format("<ptr target=\"#%s\"/>", body)
914 end)
915 return str
916 end
917
918 local function cmdtotei(str)
919 for i = 1,#cmdtotags
920 do
921 str = gsub(str, lpeg.P("\\") * lpeg.Cs(cmdtotags[i].a) * spcenc^-1 * bsqbrackets * -bcbraces, "\\%1%2{}")
922 str = gsub(str, lpeg.P("\\") * lpeg.Cs(cmdtotags[i].a) * spcenc^-1 * -(bsqbrackets + bcbraces), "\\%1[]{}")
923 str = string.gsub(str, "(\\"..cmdtotags[i].a..")%s?%*?(%b{})", "%1[]%2")
924 str = string.gsub(str, "(\\"..cmdtotags[i].a..")%s?%*?(%b[])(%b{})",
925 function(cmd, arg, body)
926 body = string.sub(body, 2, -2)
927 arg = string.sub(arg, 2, -2)
928 arg = string.gsub(arg, "(%b{})", function(braces)
929 braces = string.sub(braces, 2, -2)
930 return string.format("\"%s\"", braces)
931 end)
932 body = cmdtotei(body)
933 -- return string.format("<"..cmdtotags[i].b..cmdtotags[i].c.." %s>%s</"..cmdtotags[i].b..">", arg, body)
934 if cmdtotags[i].b ~= ""
935 then
936 return string.format("<"..cmdtotags[i].b..cmdtotags[i].c..">%s</" ..
937 cmdtotags[i].b..">", body)
938 else
939 return ""
940 end
941 end)
942 end
943 -- temporarily:
944 str = string.gsub(str, "\\(getsiglum)%s?(%b{})",
945 function(cmd, body)
946 body = string.sub(body, 2, -2)
947 teisiglum = ekdosis.getsiglum(body, "tei")
948 printsiglum = ekdosis.getsiglum(body)
949 -- body = cmdtotei(body)
950 return string.format("<ref target=\"%s\">%s</ref>",
951 teisiglum, printsiglum)
952 end)
953 str = string.gsub(str, "\\(gap)%s?(%b{})",
954 function(cmd, body)
955 body = string.sub(body, 2, -2)
956 teireason = get_attr_value(body, "reason")
957 if teireason ~= "" then teireason = " reason=\""..teireason.."\"" else end
958 teiunit = get_attr_value(body, "unit")
959 if teiunit ~= "" then teiunit = " unit=\""..teiunit.."\"" else end
960 teiquantity = get_attr_value(body, "quantity")
961 if teiquantity ~= "" then teiquantity = " quantity=\""..teiquantity.."\"" else end
962 teiextent = get_attr_value(body, "extent")
963 if teiextent ~= "" then teiextent = " extent=\""..teiextent.."\"" else end
964 return string.format("<gap%s%s%s%s/>", teireason, teiunit, teiquantity, teiextent)
965 end)
966 str = gsub(str, lpeg.P("\\") * labelrefcmds * spcenc^-1 * bcbraces,
967 function(cmd, body)
968 body = string.sub(body, 2, -2)
969 if not checkxmlid(body)
970 then
971 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
972 ..body..
973 "\" is not a valid xml:id. \\MessageBreak "
974 ..
975 "Please pick another id.}}")
976 else
977 end
978 if string.find(cmd, "label")
979 then
980 return string.format("<anchor xml:id=\"%s\"/>", body)
981 else
982 return string.format("<ptr target=\"#%s\"/>", body)
983 end
984 end)
985 str = string.gsub(str, "\\(%a+)%s?%*?(%b[])(%b{})",
986 function(cmd, opt, body)
987 body = string.sub(body, 2, -2)
988 body = cmdtotei(body)
989 return string.format("<%s>%s</%s>", cmd, body, cmd)
990 end)
991 str = string.gsub(str, "\\(%a+)%s?%*?(%b{})",
992 function(cmd, body)
993 body = string.sub(body, 2, -2)
994 body = cmdtotei(body)
995 return string.format("<%s>%s</%s>", cmd, body, cmd)
996 end)
997 str = string.gsub(str, "(%s)(%>)", "%2")
998 return str
999 end
1000
1001 local teiautopar = true
1002
1003 function ekdosis.setteiautopar(choice)
1004 if choice == "yes"
1005 then
1006 teiautopar = true
1007 else
1008 teiautopar = false
1009 end
1010 return true
1011 end
1012
1013 local function partotei(str)
1014 if teiautopar
1015 then
1016 str = gsub(str, lpeg.P(lpeg.P("\\par") * spcenc^1)^1, "\\par ")
1017 str = gsub(str, ((para + parb) * par^-1)^2, "\\p@r ")
1018 str = string.gsub(str, "\\p@ra%s+", "<p>")
1019 str = string.gsub(str, "\\p@rb%s+", "</p>")
1020 str = string.gsub(str, "\\p@r%s+", "")
1021 str = string.gsub(str, "%s?\\par%s?", "<p>", 1)
1022 str = string.gsub(str, "(%<p%>)(%s-)(%<%/?div%d?)", "%3")
1023 str = string.gsub(str, "%s?\\par%s?", "</p><p>")
1024 str = string.gsub(str, "<p>%s?</p>", "")
1025 str = string.gsub(str, "(%<p%>)%s?(%</div%>)$", "%2")
1026 str = string.gsub(str, "(%<p%>)%s?$", "")
1027 str = string.gsub(str, "(<p>)%s?(<div.->)", "%2%1")
1028 else
1029 str = gsub(str, par + para + parb, "")
1030 end
1031 return str
1032 end
1033
1034 local function self_close_tags(str)
1035 str = gsub(str, lpeg.P("<" * -lpeg.S("/"))
1036 * lpeg.Cs(letters^1)
1037 * lpeg.Cs((1 - lpeg.S"<>")^0)
1038 * lpeg.P(">")
1039 * lpeg.P("</")
1040 * lpeg.Cs(letters^1)
1041 * lpeg.P(">"), function(ftag, arg, ltag)
1042 if ftag == ltag
1043 then
1044 return string.format("<%s%s/>", ftag, arg)
1045 else
1046 end
1047 end)
1048 return str
1049 end
1050
1051 --headers and footers
1052 local hfmarks = {}
1053
1054 function ekdosis.storehfmark(page, mk, opt)
1055 if opt == "endmk"
1056 then
1057 local emark = hfmarks[#hfmarks].mark
1058 table.insert(hfmarks, {a = page, mark = emark})
1059 else
1060 table.insert(hfmarks, {a = page, mark = mk})
1061 end
1062 return true
1063 end
1064
1065 local printhfmarks = {
1066 HEL = true,
1067 HEC = true,
1068 HER = true,
1069 HOL = true,
1070 HOC = true,
1071 HOR = true,
1072 FOL = true,
1073 FOC = true,
1074 FOR = true,
1075 FEL = true,
1076 FEC = true,
1077 FER = true,
1078 }
1079
1080 function ekdosis.nohfmark()
1081 printhfmarks.HEL = false
1082 printhfmarks.HEC = false
1083 printhfmarks.HER = false
1084 printhfmarks.HOL = false
1085 printhfmarks.HOC = false
1086 printhfmarks.HOR = false
1087 printhfmarks.FOL = false
1088 printhfmarks.FOC = false
1089 printhfmarks.FOR = false
1090 printhfmarks.FEL = false
1091 printhfmarks.FEC = false
1092 printhfmarks.FER = false
1093 end
1094
1095 function ekdosis.resethfmark()
1096 printhfmarks.HEL = true
1097 printhfmarks.HEC = true
1098 printhfmarks.HER = true
1099 printhfmarks.HOL = true
1100 printhfmarks.HOC = true
1101 printhfmarks.HOR = true
1102 printhfmarks.FOL = true
1103 printhfmarks.FOC = true
1104 printhfmarks.FOR = true
1105 printhfmarks.FEL = true
1106 printhfmarks.FEC = true
1107 printhfmarks.FER = true
1108 end
1109
1110 function ekdosis.printmark(str, mk)
1111 if printhfmarks[mk]
1112 then
1113 return str
1114 else
1115 printhfmarks[mk] = true
1116 return ""
1117 end
1118 end
1119
1120 function ekdosis.gethfmark(page)
1121 local indexpage = get_a_index(page, hfmarks)
1122 if hfmarks[indexpage] ~= nil
1123 then
1124 return hfmarks[indexpage].mark
1125 else
1126 return hfmarks[#hfmarks-1].mark or ""
1127 end
1128 end
1129
1130 -- divisions of the body text
1131 local divdepth = {
1132 book = 1,
1133 part = 2,
1134 chapter = 3,
1135 section = 4,
1136 subsection = 5,
1137 subsubsection = 6
1138 }
1139
1140 function ekdosis.mkdivdepths(...)
1141 divdepth = {}
1142 local num = 1
1143 for _, y in ipairs{...}
1144 do
1145 if y == "book" or "part" or "chapter"
1146 or "section" or "subsection" or "subsubsection"
1147 then
1148 divdepth[y] = num
1149 num = num + 1
1150 else
1151 end
1152 end
1153 return true
1154 end
1155
1156 -- LaTeX side: format the divisions
1157 local fmtdiv = {}
1158
1159 function ekdosis.fmtdiv(n, fmtb, fmte)
1160 if isintable(fmtdiv, n)
1161 then
1162 local index = get_a_index(n, fmtdiv)
1163 table.remove(fmtdiv, index)
1164 else
1165 end
1166 table.insert(fmtdiv, { a = n, formatb = fmtb, formate = fmte} )
1167 return true
1168 end
1169
1170 function ekdosis.getfmtdiv(n, pos)
1171 local index = get_a_index(n, fmtdiv)
1172 if index ~= nil
1173 then
1174 if pos == "b"
1175 then
1176 return fmtdiv[index].formatb
1177 elseif pos == "e"
1178 then
1179 return fmtdiv[index].formate
1180 end
1181 else
1182 return ""
1183 end
1184 end
1185
1186 local ekddivs = true
1187
1188 function ekdosis.setekddivsfalse()
1189 ekddivs = false
1190 end
1191
1192 local function ekddivs_totei(str)
1193 str = gsub(str, dblbkslash * lpeg.Cs("ekddiv") * spce^-1 * bcbraces,
1194 function(bkslash, cmd, space, arg)
1195 if ekddivs
1196 then
1197 arg = string.sub(arg, 2, -2)
1198 teitype = get_attr_value(arg, "type")
1199 tein = get_attr_value(arg, "n")
1200 teihead = get_attr_value(arg, "head")
1201 teidepth = get_attr_value(arg, "depth")
1202 if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
1203 if tein ~= "" then tein = " n=\""..tein.."\"" else end
1204 if teidepth ~= ""
1205 then
1206 teidepth = " depth=\""..teidepth.."\""
1207 else
1208 teidepth = " depth=\"1\""
1209 end
1210 return string.format("\\p@rb <div%s%s%s><head>%s</head>\\p@ra ",
1211 teitype, tein, teidepth, teihead)
1212 else
1213 return ""
1214 end
1215 end)
1216 return str
1217 end
1218
1219 local function section_totei(str)
1220 str = gsub(str, dblbkslash * sections * spce^-1 * bcbraces, "%1%2%3[]%4")
1221 str = gsub(str, dblbkslash * sections * spce^-1 * bsqbrackets * bcbraces,
1222 function(bkslash, secname, space, opt, arg)
1223 if ekddivs
1224 then
1225 return ""
1226 else
1227 ctr = divdepth[secname]
1228 arg = string.sub(arg, 2, -2)
1229 return string.format("\\p@rb <div%s type=\"%s\"><head>%s</head>\\p@ra ",
1230 ctr, secname, arg)
1231 end
1232 end)
1233 return str
1234 end
1235
1236 local used_ndivs = {}
1237
1238 local function close_ekddivs_at_end(str)
1239 local isdiv = false
1240 if string.find(str, "</div>$")
1241 then
1242 isdiv = true
1243 str = string.gsub(str, "(.*)(</div>)$", "%1")
1244 else
1245 end
1246 -- collect used depth numbers
1247 for i in string.gmatch(str, "<div .-depth%=\"%d\".->")
1248 do
1249 i = string.match(i, "depth=\"%d\"")
1250 i = string.match(i, "%d")
1251 if isintable(used_ndivs, i)
1252 then
1253 else
1254 table.insert(used_ndivs, {a = i} )
1255 end
1256 end
1257 if next(used_ndivs) ~= nil
1258 then
1259 table.sort(used_ndivs, function(a ,b) return(#a.a > #b.a) end)
1260 else
1261 end
1262 local firstdiv = string.match(str, "<div .-depth%=\"%d\".->") or ""
1263 firstdiv = string.match(firstdiv, "depth%=\"%d\"") or ""
1264 firstdiv = string.match(firstdiv, "%d") or ""
1265 local lastdiv = string.match(string.reverse(str), ">.-\"%d\"%=htped.- vid<") or ""
1266 lastdiv = string.match(lastdiv, "\"%d\"%=htped") or ""
1267 lastdiv = string.match(lastdiv, "%d") or ""
1268 local firstdivindex = get_a_index(firstdiv, used_ndivs)
1269 local lastdivindex = get_a_index(lastdiv, used_ndivs)
1270 firstdivindex = tonumber(firstdivindex)
1271 lastdivindex = tonumber(lastdivindex)
1272 local closedivs = ""
1273 if isintable(used_ndivs, firstdiv)
1274 then
1275 while lastdivindex >= firstdivindex
1276 do
1277 closedivs = closedivs.."</div>"
1278 lastdivindex = lastdivindex - 1
1279 end
1280 end
1281 if isdiv
1282 then
1283 return str..closedivs.."</div>"
1284 else
1285 return str..closedivs
1286 end
1287 end
1288
1289 local function close_ndivs_at_end(str)
1290 local isdiv = false
1291 if string.find(str, "</div>$")
1292 then
1293 isdiv = true
1294 str = string.gsub(str, "(.*)(</div>)$", "%1")
1295 else
1296 end
1297 -- collect used div numbers
1298 for i in string.gmatch(str, "<div[1-6]")
1299 do
1300 i = string.match(i, "[1-6]")
1301 if isintable(used_ndivs, i)
1302 then
1303 else
1304 table.insert(used_ndivs, {a = i} )
1305 end
1306 end
1307 if next(used_ndivs) ~= nil
1308 then
1309 table.sort(used_ndivs, function(a ,b) return(#a.a > #b.a) end)
1310 else
1311 end
1312 local firstdiv = string.match(str, "<div[1-6]") or ""
1313 firstdiv = string.match(firstdiv, "[1-6]") or ""
1314 local lastdiv = string.match(string.reverse(str), "[1-6]vid<") or ""
1315 lastdiv = string.match(lastdiv, "[1-6]") or ""
1316 local firstdivindex = get_a_index(firstdiv, used_ndivs)
1317 local lastdivindex = get_a_index(lastdiv, used_ndivs)
1318 firstdivindex = tonumber(firstdivindex)
1319 lastdivindex = tonumber(lastdivindex)
1320 local closedivs = ""
1321 if isintable(used_ndivs, firstdiv)
1322 then
1323 while lastdivindex >= firstdivindex
1324 do
1325 closedivs = closedivs.."</div"..used_ndivs[lastdivindex].a..">"
1326 lastdivindex = lastdivindex - 1
1327 end
1328 end
1329 if isdiv
1330 then
1331 return str..closedivs.."</div>"
1332 else
1333 return str..closedivs
1334 end
1335 end
1336
1337 local function close_ekddivs_in_between(str)
1338 local maxdepth = 1
1339 for i in string.gmatch(str, "<div.-[Dd]epth=\"(%d)\".->", "%1")
1340 do
1341 if tonumber(i) > tonumber(maxdepth)
1342 then
1343 maxdepth = i
1344 else
1345 end
1346 end
1347 for ndivi = 1, maxdepth
1348 do
1349 str = string.gsub(str, "(<div [^%>]-[Dd]epth%=\")("..ndivi..")(\".->)(.-)(<div [^%>]-depth%=\")(%d)(\".->)",
1350 function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
1351 local firstdiv = ndivi
1352 local lastdiv = ndivii
1353 local firstdivindex = get_a_index(firstdiv, used_ndivs)
1354 local lastdivindex = get_a_index(lastdiv, used_ndivs)
1355 firstdivindex = tonumber(firstdivindex)
1356 lastdivindex = tonumber(lastdivindex)
1357 local closedivs = ""
1358 if firstdivindex >= lastdivindex
1359 then
1360 while firstdivindex >= lastdivindex
1361 do
1362 closedivs = closedivs.."</div>"
1363 firstdivindex = firstdivindex - 1
1364 bdivii = string.gsub(bdivii, "depth", "Depth")
1365 end
1366 end
1367 return string.format("%s%s%s%s%s%s%s%s",
1368 bdivi, ndivi, edivi, between,
1369 closedivs, bdivii, ndivii, edivii)
1370 end)
1371 end
1372 return str
1373 end
1374
1375 local function clean_ekddivs(str)
1376 str = string.gsub(str, "(<div.-)(%s[Dd]epth%=\"%d\")(.->)", "%1%3")
1377 used_ndivs = {}
1378 return str
1379 end
1380
1381 local function close_ndivs_in_between(str)
1382 for ndivi = 1, 6
1383 do
1384 str = string.gsub(str, "(<[Dd]iv)("..ndivi..")(.->)(.-)(<div)([1-6])(.->)",
1385 function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
1386 local firstdiv = ndivi
1387 local lastdiv = ndivii
1388 local firstdivindex = get_a_index(firstdiv, used_ndivs)
1389 local lastdivindex = get_a_index(lastdiv, used_ndivs)
1390 firstdivindex = tonumber(firstdivindex)
1391 lastdivindex = tonumber(lastdivindex)
1392 local closedivs = ""
1393 if firstdivindex >= lastdivindex
1394 then
1395 while firstdivindex >= lastdivindex
1396 do
1397 closedivs = closedivs.."</div"..used_ndivs[firstdivindex].a..">"
1398 firstdivindex = firstdivindex - 1
1399 bdivii = string.gsub(bdivii, "div", "Div")
1400 end
1401 end
1402 return string.format("%s%s%s%s%s%s%s%s",
1403 bdivi, ndivi, edivi, between,
1404 closedivs, bdivii, ndivii, edivii)
1405 end)
1406 end
1407 return str
1408 end
1409
1410 local function clean_latexdivs(str)
1411 str = string.gsub(str, "(<Div)([1-6])(.->)", "<div%2%3")
1412 used_ndivs = {}
1413 return str
1414 end
1415
1416 local function textotei(str)
1417 str = xml_entities(str)
1418 str = texpatttotei(str)
1419 str = note_totei(str)
1420 str = remove_extra_anchors(str)
1421 str = app_totei(str)
1422 str = rdgGrp_totei(str)
1423 str = lem_rdg_totei(str)
1424 str = relocate_notes(str)
1425 str = stanzatotei(str)
1426 str = versetotei(str)
1427 str = envtotei(str)
1428 str = ekddivs_totei(str)
1429 str = section_totei(str)
1430 str = icitetotei(str)
1431 str = cmdtotei(str)
1432 str = self_close_tags(str)
1433 str = partotei(str)
1434 if ekddivs
1435 then
1436 str = close_ekddivs_at_end(str)
1437 str = close_ekddivs_in_between(str)
1438 str = close_ekddivs_in_between(str)
1439 str = clean_ekddivs(str)
1440 else
1441 str = close_ndivs_at_end(str)
1442 str = close_ndivs_in_between(str)
1443 str = close_ndivs_in_between(str)
1444 str = clean_latexdivs(str)
1445 end
1446 return str
1447 end
1448
1449 local teifilename = tex.jobname.."-tei"
1450
1451 function ekdosis.setteifilename(str)
1452 teifilename = str
1453 return true
1454 end
1455
1456 function ekdosis.openteistream()
1457 local f = io.open(teifilename.."_tmp.xml", "a+")
1458 f:write('<?xml version="1.0" encoding="utf-8"?>', "\n")
1459 f:write("<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">", "\n")
1460 f:write("<teiHeader>", "\n")
1461 f:write("<fileDesc>", "\n")
1462 f:write("<titleStmt>", "\n")
1463 f:write("<title><!-- Title --></title>", "\n")
1464 f:write("<respStmt>", "\n")
1465 f:write("<resp><!-- Edited by --></resp>", "\n")
1466 f:write("<name><!-- Name --></name>", "\n")
1467 f:write("</respStmt>", "\n")
1468 f:write("</titleStmt>", "\n")
1469 f:write("<publicationStmt>", "\n")
1470 f:write("<distributor><!-- Distributor name --></distributor>", "\n")
1471 f:write("</publicationStmt>", "\n")
1472 f:write("<sourceDesc>", "\n")
1473 if next(listWit) == nil and next(listPerson) == nil
1474 then
1475 f:write("<p>No source, born digital</p>", "\n")
1476 else
1477 if next(listWit) ~= nil
1478 then
1479 f:write("<listWit>", "\n")
1480 for i = 1,#listWit do
1481 f:write('<witness xml:id=\"', listWit[i].xmlid, "\">", "\n")
1482 f:write('<abbr type="siglum">', textotei(listWit[i].abbr), "</abbr>", "\n")
1483 f:write(textotei(listWit[i].detailsDesc), "\n")
1484 f:write("<msDesc>", "\n")
1485 if listWit[i].msIdentifier.settlement == ""
1486 and listWit[i].msIdentifier.institution == ""
1487 and listWit[i].msIdentifier.repository == ""
1488 and listWit[i].msIdentifier.collection == ""
1489 and listWit[i].msIdentifier.idno == ""
1490 and listWit[i].msIdentifier.msName == ""
1491 then
1492 f:write("<msIdentifier/>", "\n")
1493 else
1494 f:write("<msIdentifier>", "\n")
1495 if listWit[i].msIdentifier.settlement ~= "" then
1496 f:write("<settlement>", textotei(listWit[i].msIdentifier.settlement), "</settlement>", "\n")
1497 else end
1498 if listWit[i].msIdentifier.institution ~= "" then
1499 f:write("<institution>", textotei(listWit[i].msIdentifier.institution), "</institution>", "\n")
1500 else end
1501 if listWit[i].msIdentifier.repository ~= "" then
1502 f:write("<repository>", textotei(listWit[i].msIdentifier.repository), "</repository>", "\n")
1503 else end
1504 if listWit[i].msIdentifier.collection ~= "" then
1505 f:write("<collection>", textotei(listWit[i].msIdentifier.collection), "</collection>", "\n")
1506 else end
1507 if listWit[i].msIdentifier.idno ~= "" then
1508 f:write("<idno>", textotei(listWit[i].msIdentifier.idno), "</idno>", "\n")
1509 else end
1510 if listWit[i].msIdentifier.msName ~= "" then
1511 f:write("<msName>", textotei(listWit[i].msIdentifier.msName), "</msName>", "\n")
1512 else end
1513 f:write("</msIdentifier>", "\n")
1514 end
1515 if listWit[i].handDesc ~= nil then
1516 f:write("<physDesc>", "\n")
1517 f:write("<handDesc hands=\"", #listWit[i].handDesc, "\">", "\n")
1518 local j = 1
1519 while listWit[i].handDesc[j]
1520 do
1521 f:write("<handNote xml:id=\"", listWit[i].handDesc[j].xmlid, "\">", "\n")
1522 f:write('<abbr type="siglum">', textotei(listWit[i].handDesc[j].abbr), "</abbr>", "\n")
1523 f:write("<p>", textotei(listWit[i].handDesc[j].detailsDesc), "</p>", "\n")
1524 f:write("</handNote>", "\n")
1525 j = j + 1
1526 end
1527 f:write("</handDesc>", "\n")
1528 f:write("</physDesc>", "\n")
1529 else end
1530 if listWit[i].msContents ~= nil then
1531 f:write("<msContents>", "\n")
1532 f:write("<msItemStruct>", "\n")
1533 f:write("<locus>", textotei(listWit[i].msContents.msItemStruct.locus), "</locus>", "\n")
1534 f:write("</msItemStruct>", "\n")
1535 f:write("</msContents>", "\n")
1536 end
1537 if listWit[i].history ~= nil then
1538 f:write("<history>", "\n")
1539 f:write("<origin>", "\n")
1540 f:write("<origDate>", textotei(listWit[i].history.origin.origDate), "</origDate>", "\n")
1541 f:write("</origin>", "\n")
1542 f:write("</history>", "\n")
1543 end
1544 f:write("</msDesc>", "\n")
1545 f:write("</witness>", "\n")
1546 end
1547 f:write("</listWit>", "\n")
1548 end
1549 if next(listPerson) ~= nil
1550 then
1551 f:write("<listPerson xml:id=\"scholars\">", "\n")
1552 for i = 1,#listPerson do
1553 f:write('<person xml:id=\"', listPerson[i].xmlid, "\">", "\n")
1554 f:write('<persName>', "\n")
1555 f:write('<abbr type="siglum">', textotei(listPerson[i].abbr), "</abbr>", "\n")
1556 if listPerson[i].persName.name ~= nil
1557 then
1558 f:write(textotei(listPerson[i].persName.name))
1559 else
1560 if listPerson[i].persName.forename ~= ""
1561 then
1562 f:write("<forename>", textotei(listPerson[i].persName.forename), "</forename>", "\n")
1563 else
1564 f:write("<forename><!-- forename --></forename>", "\n")
1565 end
1566 if textotei(listPerson[i].persName.surname) ~= ""
1567 then
1568 f:write("<surname>", textotei(listPerson[i].persName.surname), "</surname>", "\n")
1569 else
1570 f:write("<surname><!-- surname --></surname>", "\n")
1571 end
1572 if textotei(listPerson[i].persName.addName) ~= ""
1573 then
1574 f:write("<addName>", textotei(listPerson[i].persName.addName), "</addName>", "\n")
1575 end
1576 end
1577 if listPerson[i].note ~= ""
1578 then
1579 f:write("<note>", textotei(listPerson[i].note), "</note>", "\n")
1580 end
1581 f:write('</persName>', "\n")
1582 f:write('</person>', "\n")
1583 end
1584 f:write("</listPerson>", "\n")
1585 end
1586 end
1587 f:write("</sourceDesc>", "\n")
1588 f:write("</fileDesc>", "\n")
1589 f:write("<encodingDesc>", "\n")
1590 f:write('<variantEncoding method="parallel-segmentation" location="internal"/>', "\n")
1591 f:write("</encodingDesc>", "\n")
1592 f:write("</teiHeader>", "\n")
1593 f:write("<text>", "\n")
1594 f:write("<body>", "\n")
1595 f:close()
1596 return true
1597 end
1598
1599 local tidy = nil
1600
1601 local function cleanup_tei()
1602 local f = assert(io.open(teifilename.."_tmp.xml", "r"))
1603 t = f:read("*a")
1604 t = string.gsub(t, "%<p%>%s?%</p%>", "")
1605 t = string.gsub(t, "^\n", "")
1606 f:close()
1607 local fw = assert(io.open(teifilename.."_tmp.xml", "w"))
1608 fw:write(t)
1609 fw:close()
1610 return true
1611 end
1612
1613 function ekdosis.closeteistream(opt)
1614 local f = io.open(teifilename.."_tmp.xml", "a+")
1615 f:write("\n", "</body>", "\n")
1616 if xmlbibresource ~= nil then
1617 bibf = assert(io.open(xmlbibresource, "r"))
1618 t = bibf:read("*a")
1619 t = string.gsub(t, "%s+corresp%=%b\"\"", "")
1620 t = string.gsub(t, "\n\n", "\n")
1621 f:write("<back>", "\n")
1622 f:write("<listBibl>", "\n")
1623 for i in string.gmatch(t, "<biblStruct.->.-</biblStruct>")
1624 do
1625 f:write(i, "\n")
1626 end
1627 f:write("</listBibl>", "\n")
1628 f:write("</back>", "\n")
1629 bibf:close()
1630 else
1631 end
1632 f:write("</text>", "\n")
1633 f:write("</TEI>", "\n")
1634 f:close()
1635 cleanup_tei()
1636 os.remove(teifilename..".xml")
1637 os.rename(teifilename.."_tmp.xml", teifilename..".xml")
1638 if opt == "tidy" then
1639 os.execute("tidy -qmi -xml --output-xml yes "..teifilename..".xml")
1640 else
1641 end
1642 return true
1643 end
1644
1645 function ekdosis.exporttei(str)
1646 local f = io.open(teifilename.."_tmp.xml", "a+")
1647 -- f:write("\n<p>")
1648 str = textotei(str)
1649 f:write(str)
1650 f:close()
1651 return true
1652 end
1653 -- end totei functions
1654
1655 -- begin basic TeX Conspectus siglorum
1656 function ekdosis.basic_cs(msid)
1657 local indexwit = getindex(msid, listWit)
1658 siglum = listWit[indexwit].abbr
1659 -- if listWit[indexwit].detailsDesc == ""
1660 -- then
1661 -- name = listWit[indexwit].msIdentifier.msName
1662 -- else
1663 -- name = listWit[indexwit].msIdentifier.msName
1664 -- .."\\thinspace\\newline\\bgroup\\footnotesize{}"..
1665 -- listWit[indexwit].detailsDesc
1666 -- .."\\egroup{}"
1667 -- end
1668 name = listWit[indexwit].detailsDesc
1669 if listWit[indexwit].history ~= nil
1670 and
1671 listWit[indexwit].history.origin ~= nil
1672 then
1673 date = listWit[indexwit].history.origin.origDate
1674 else
1675 date = ""
1676 end
1677 if listWit[indexwit].msContents ~= nil
1678 and
1679 listWit[indexwit].msContents.msItemStruct ~= nil
1680 then
1681 locus = listWit[indexwit].msContents.msItemStruct.locus
1682 else
1683 locus = ""
1684 end
1685 return siglum.."&"..name.." "..locus.."&"..date
1686 end
1687 -- end basic TeX Conspectus siglorum
1688
1689 function ekdosis.removesp(str)
1690 str = gsub(str, cmd * cmdargs * spcenc^-1, "%1%2")
1691 return str
1692 end
1693
1694 function ekdosis.closestream()
1695 os.remove(tex.jobname..".ekd")
1696 os.rename(tex.jobname.."_tmp.ekd", tex.jobname..".ekd")
1697 return true
1698 end
1699
1700 local cur_abs_pg = 0
1701 local ekd_abs_pg = 0
1702 local pg_i = nil
1703 local pg_ii = nil
1704 local prevcol = nil
1705 local curcol = "x"
1706
1707 local check_resetlineno = {}
1708
1709 function ekdosis.update_abspg(n) -- not used
1710 cur_abs_pg = n
1711 return true
1712 end
1713
1714 function ekdosis.storeabspg(n, pg)
1715 if tonumber(n) > tonumber(cur_abs_pg)
1716 then
1717 ekd_abs_pg = ekd_abs_pg + 1
1718 end
1719 cur_abs_pg = n
1720 n = ekd_abs_pg
1721 if pg == "pg_i" then
1722 pg_i = n
1723 elseif pg == "pg_ii" then
1724 pg_ii = n
1725 table.insert(check_resetlineno, curcol.."-"..pg_ii)
1726 end
1727 return true
1728 end
1729
1730 function ekdosis.getekdabspg()
1731 return ekd_abs_pg
1732 end
1733
1734 function ekdosis.checkresetlineno()
1735 if isfound(check_resetlineno, curcol.."-"..pg_i)
1736 then
1737 return ""
1738 else
1739 return "\\resetlinenumber"
1740 end
1741 end
1742
1743 --
1744 -- Build environments to be aligned
1745 --
1746
1747 local cur_alignment = "-"
1748 local cur_alignment_patt = "%-"
1749 local cur_alignment_cnt = 1
1750
1751 local newalignment = false
1752 function ekdosis.newalignment(str)
1753 if str == "set"
1754 then
1755 newalignment = true
1756 cur_alignment = "-"..cur_alignment_cnt.."-"
1757 cur_alignment_patt = "%-"..cur_alignment_cnt.."%-"
1758 cur_alignment_cnt = cur_alignment_cnt + 1
1759 elseif str == "reset"
1760 then
1761 newalignment = false
1762 cur_alignment = "-"
1763 cur_alignment_patt = "%-"
1764 end
1765 return true
1766 end
1767
1768 local aligned_texts = {}
1769 local texts_w_apparatus = {}
1770 local coldata_totei = {}
1771
1772 local function sanitize_envdata(str) -- look for a better way to achieve this
1773 str = string.gsub(str, "(%a+)%s+(%b[])", "%1%2")
1774 str = string.gsub(str, "(%a+)(%b[])%s+", "%1%2")
1775 str = string.gsub(str, "%s+(%a+)(%b[])", "%1%2")
1776 str = gsub(str, lpeg.Cs(letters^1)
1777 * spcenc^-1
1778 * -bsqbrackets
1779 * lpeg.Cs(";"), "%1[]%2")
1780 str = string.gsub(str, "%s+(%a+)(%b[])", "%1%2")
1781 return str
1782 end
1783
1784 function ekdosis.mkenvdata(str, opt)
1785 if not string.find(str, "%;", -1) then str = str .. ";" else end
1786 -- str = str ..";"
1787 -- str = string.gsub(str, "%s+", "")
1788 local fieldstart = 1
1789 local col = 0
1790 if opt == "texts" then
1791 str = sanitize_envdata(str)
1792 repeat
1793 local _s, nexti = string.find(str, "%b[]%s-%;", fieldstart)
1794 local namediv = string.gsub(string.sub(str, fieldstart, nexti-1), "(%a+)%s-(%b[])", "%1")
1795 local attr = string.gsub(string.sub(str, fieldstart, nexti-1), "(%a+)%s-(%b[])", "%2")
1796 attr = string.sub(attr, 2, -2)
1797 if forbid_xmlid
1798 then
1799 attr = string.gsub(attr, "xml:id", "n") -- xml:id is not allowed here
1800 else
1801 end
1802 table.insert(aligned_texts, { text = namediv,
1803 attribute = attr,
1804 column = col })
1805 table.insert(coldata_totei, { environment = namediv,
1806 data = {} })
1807 col = col + 1
1808 fieldstart = nexti + 1
1809 until fieldstart > string.len(str)
1810 return aligned_texts
1811 elseif opt == "apparatus" then
1812 repeat
1813 local nexti = string.find(str, "%;", fieldstart)
1814 table.insert(texts_w_apparatus, string.sub(str, fieldstart, nexti-1))
1815 fieldstart = nexti +1
1816 until fieldstart > string.len(str)
1817 return texts_w_apparatus
1818 end
1819 end
1820
1821 -- Reminder: the following two variables are already set above
1822 -- local prevcol = nil
1823 -- local curcol = "x"
1824
1825 function ekdosis.storecurcol(n)
1826 curcol = n
1827 return true
1828 end
1829
1830 function ekdosis.flushcolnums()
1831 prevcol = nil
1832 curcol = "x"
1833 return true
1834 end
1835
1836 function ekdosis.flushenvdata()
1837 aligned_texts = {}
1838 texts_w_apparatus = {}
1839 coldata_totei = {}
1840 return true
1841 end
1842
1843 function ekdosis.storecoldata(nthcol, chunk)
1844 local tindex = tonumber(nthcol) + 1
1845 table.insert(coldata_totei[tindex].data, chunk)
1846 return true
1847 end
1848
1849 local environment_div = {}
1850
1851 local function build_envdiv(str)
1852 if not environment_div[str]
1853 then
1854 environment_div[str] = 1
1855 else
1856 environment_div[str] = environment_div[str] + 1
1857 end
1858 local div = "div-"..str.."_"..environment_div[str]
1859 if xmlidfound(div)
1860 then
1861 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
1862 ..div..
1863 "\" already exists as an xml:id. "
1864 ..
1865 "ekdosis has generated some random id.}}")
1866 return "div-"..math.random(1000,9999)
1867 elseif not checkxmlid(div)
1868 then
1869 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
1870 ..div..
1871 "\" is not a valid xml:id. \\MessageBreak "
1872 ..
1873 "Please pick another id.}}")
1874 else
1875 table.insert(xmlids, {xmlid = div})
1876 table.sort(xmlids, function(a ,b) return(#a.xmlid > #b.xmlid) end)
1877 return div
1878 end
1879 end
1880
1881 function ekdosis.mkenv()
1882 local environments = {}
1883 for i = 1,#aligned_texts
1884 do
1885 if isfound(texts_w_apparatus, aligned_texts[i].text)
1886 then
1887 table.insert(environments, "\\NewDocumentEnvironment{".. aligned_texts[i].text.."}{+b}"
1888 .."{\\begin{nthcolumn}{".. aligned_texts[i].column.."}"
1889 .."\\par"
1890 .."\\EkdosisColStart"
1891 .."\\EkdosisOn#1"
1892 .."}{\\EkdosisOff"
1893 .."\\EkdosisColStop"
1894 .."\\end{nthcolumn}"
1895 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
1896 .. aligned_texts[i].column
1897 ..", \\luastringN{\\par#1\\par})}\\fi"
1898 .."}")
1899 table.insert(environments, "\\NewDocumentEnvironment{".. aligned_texts[i].text.."*}{+b}"
1900 .."{\\begin{nthcolumn*}{".. aligned_texts[i].column.."}[]"
1901 .."\\par"
1902 .."\\EkdosisColStart"
1903 .."\\EkdosisOn#1"
1904 .."}{\\EkdosisOff"
1905 .."\\EkdosisColStop"
1906 .."\\end{nthcolumn*}"
1907 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
1908 .. aligned_texts[i].column
1909 ..", \\luastringN{\\par#1\\par})}\\fi"
1910 .."}")
1911 else
1912 table.insert(environments, "\\NewDocumentEnvironment{".. aligned_texts[i].text.."}{+b}"
1913 .."{\\begin{nthcolumn}{".. aligned_texts[i].column.."}"
1914 .."\\par"
1915 .."#1"
1916 .."}{\\end{nthcolumn}"
1917 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
1918 .. aligned_texts[i].column
1919 ..", \\luastringN{\\par#1\\par})}\\fi"
1920 .."}")
1921 table.insert(environments, "\\NewDocumentEnvironment{".. aligned_texts[i].text.."*}{+b}"
1922 .."{\\begin{nthcolumn*}{"..aligned_texts[i].column.."}[]"
1923 .."\\par"
1924 .."#1"
1925 .."}{"
1926 .."\\end{nthcolumn*}"
1927 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
1928 .. aligned_texts[i].column
1929 ..", \\luastringN{\\par#1\\par})}\\fi"
1930 .."}")
1931 end
1932 forbid_xmlid = false
1933 if aligned_texts[i].attribute ~= ""
1934 then
1935 ekdosis.newenvtotag(aligned_texts[i].text, "div",
1936 "xml:id=\""
1937 ..build_envdiv(aligned_texts[i].text)
1938 .."\" "
1939 ..aligned_texts[i].attribute)
1940 else
1941 ekdosis.newenvtotag(aligned_texts[i].text, "div",
1942 "xml:id=\""
1943 ..build_envdiv(aligned_texts[i].text)
1944 .."\"")
1945 end
1946 forbid_xmlid = true
1947 end
1948 str = table.concat(environments)
1949 return str
1950 end
1951
1952 function ekdosis.export_coldata_totei()
1953 for i = 1,#coldata_totei
1954 do
1955 ekdosis.exporttei("\\begin{".. coldata_totei[i].environment .."}"
1956 .. table.concat(coldata_totei[i].data)
1957 .. "\\end{".. coldata_totei[i].environment .."}")
1958 end
1959 end
1960
1961 -- handle multiple layers in apparatuses
1962 --
1963 local apparatuses = {}
1964 local bagunits = {}
1965
1966 function ekdosis.newapparatus(teitype,
1967 appdir,
1968 apprule,
1969 appdelim,
1970 appsep,
1971 appsubsep,
1972 appbhook,
1973 appehook,
1974 applimit,
1975 applang,
1976 appnotelang)
1977 if isintable(apparatuses, teitype)
1978 then
1979 tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
1980 ..teitype..
1981 "\" already exists.}}")
1982 else
1983 table.insert(apparatuses, {a = teitype,
1984 direction = appdir,
1985 rule = apprule,
1986 delim = appdelim,
1987 sep = appsep,
1988 subsep = appsubsep,
1989 bhook = appbhook,
1990 ehook = appehook,
1991 limit = applimit,
1992 lang = applang,
1993 notelang = appnotelang})
1994 end
1995 bagunits[teitype] = 1
1996 return true
1997 end
1998
1999 function ekdosis.getapplang(teitype)
2000 i = get_a_index(teitype, apparatuses)
2001 if apparatuses[i].lang ~= ""
2002 then
2003 return apparatuses[i].lang
2004 else
2005 return "\\languagename"
2006 end
2007 end
2008
2009 function ekdosis.getappnotelang(teitype)
2010 i = get_a_index(teitype, apparatuses)
2011 if apparatuses[i].notelang ~= ""
2012 then
2013 return apparatuses[i].notelang
2014 else
2015 return "\\languagename"
2016 end
2017 end
2018
2019 function ekdosis.getappdelim(str)
2020 for i = 1,#apparatuses
2021 do
2022 if apparatuses[i].a == str then
2023 delimfound = apparatuses[i].delim
2024 break
2025 end
2026 end
2027 return delimfound
2028 end
2029
2030 function ekdosis.get_bagunits(teitype)
2031 return bagunits[teitype]
2032 end
2033
2034 local function getapplimit(teitype)
2035 for i = 1,#apparatuses
2036 do
2037 if apparatuses[i].a == teitype then
2038 limitfound = apparatuses[i].limit
2039 break
2040 end
2041 end
2042 if tonumber(limitfound) ~= nil
2043 then
2044 if tonumber(limitfound) < 10
2045 then
2046 return 0
2047 else
2048 return limitfound
2049 end
2050 else
2051 return 0
2052 end
2053 end
2054
2055 function ekdosis.limit_bagunits(teitype)
2056 local limit = tonumber(getapplimit(teitype))
2057 if limit >= 10 and bagunits[teitype] >= limit
2058 then
2059 bagunits[teitype] = 2
2060 return "\\pagebreak"
2061 else
2062 return ""
2063 end
2064 end
2065
2066 function ekdosis.addto_bagunits(teitype, n)
2067 if tonumber(getapplimit(teitype)) ~= 0
2068 then
2069 n = tonumber(n)
2070 bagunits[teitype] = bagunits[teitype] - n
2071 end
2072 end
2073
2074 function ekdosis.increment_bagunits(teitype)
2075 bagunits[teitype] = (bagunits[teitype] or 0) + 1
2076 end
2077
2078 local function reset_bagunits()
2079 for i = 1,#apparatuses
2080 do
2081 bagunits[apparatuses[i].a] = 1
2082 end
2083 end
2084
2085 function ekdosis.appin(str, teitype)
2086 local f = io.open(tex.jobname.."_tmp.ekd", "a+")
2087 if next(apparatuses) == nil
2088 then
2089 f:write("<", ekd_abs_pg, cur_alignment, curcol, "-0>", str, "</",
2090 ekd_abs_pg, cur_alignment, curcol, "-0>\n")
2091 else
2092 for i = 1,#apparatuses
2093 do
2094 if apparatuses[i].a == teitype then
2095 appno = i
2096 break
2097 end
2098 end
2099 f:write("<", ekd_abs_pg, cur_alignment, curcol, "-",
2100 appno, ">", str, "</", ekd_abs_pg, cur_alignment, curcol, "-", appno, ">\n")
2101 end
2102 f:close()
2103 return true
2104 end
2105
2106 function ekdosis.appout()
2107 local file = io.open(tex.jobname..".ekd", "r")
2108 if file ~= nil then io.close(file)
2109 f = assert(io.open(tex.jobname..".ekd", "r"))
2110 t = f:read("*a")
2111 local output = {}
2112 if next(apparatuses) == nil then
2113 -- table.insert(output, "BEGIN")
2114 table.insert(output, "\\csname ekd@default@rule\\endcsname\\NLS")
2115 table.insert(output, "\\csname ekd@begin@apparatus\\endcsname\\ignorespaces")
2116 -- table.insert(output, "\\noindent ")
2117 for i in string.gmatch(t,
2118 "<"..ekd_abs_pg
2119 ..cur_alignment_patt
2120 ..curcol.."%-0>.-</"
2121 ..ekd_abs_pg
2122 ..cur_alignment_patt
2123 ..curcol.."%-0>")
2124 do
2125 table.insert(output, i)
2126 end
2127 -- table.insert(output, "END")
2128 else
2129 local appinserted = false
2130 local n = 1
2131 while apparatuses[n]
2132 do
2133 if string.match(t, "<"..ekd_abs_pg
2134 ..cur_alignment_patt
2135 ..curcol.."%-"..n..">.-</"
2136 ..ekd_abs_pg
2137 ..cur_alignment_patt
2138 ..curcol.."%-"..n..">")
2139 then
2140 -- table.insert(output, "BEGIN")
2141 table.insert(output, "\\bgroup{}")
2142 if apparatuses[n].direction == "LR"
2143 then
2144 table.insert(output, "\\pardir TLT\\leavevmode\\textdir TLT{}")
2145 elseif apparatuses[n].direction == "RL"
2146 then
2147 table.insert(output, "\\pardir TRT\\leavevmode\\textdir TRT{}")
2148 end
2149 if apparatuses[n].rule == "none"
2150 then
2151 if n > 1
2152 then
2153 if appinserted
2154 then
2155 table.insert(output, "\\NLS{}")
2156 end
2157 else
2158 table.insert(output, "\\noindent ")
2159 end
2160 elseif apparatuses[n].rule ~= ""
2161 then
2162 if n > 1
2163 then
2164 if appinserted
2165 then
2166 table.insert(output, "\\NLS{}" .. apparatuses[n].rule .. "\\NLS{}")
2167 else
2168 table.insert(output, apparatuses[n].rule .. "\\NLS{}")
2169 end
2170 else
2171 -- table.insert(output, "\\noindent ")
2172 table.insert(output, apparatuses[n].rule .. "\\NLS{}")
2173 end
2174 else
2175 if n > 1
2176 then
2177 if appinserted
2178 then
2179 table.insert(output, "\\NLS\\csname ekd@default@rule\\endcsname\\NLS{}")
2180 else
2181 table.insert(output, "\\csname ekd@default@rule\\endcsname\\NLS{}")
2182 end
2183 else
2184 -- table.insert(output, "\\noindent ")
2185 table.insert(output, "\\csname ekd@default@rule\\endcsname\\NLS{}")
2186 end
2187 end
2188 if apparatuses[n].sep ~= ""
2189 then
2190 table.insert(output, "\\edef\\ekdsep{" .. apparatuses[n].sep .. "}")
2191 else
2192 end
2193 if apparatuses[n].subsep ~= ""
2194 then
2195 table.insert(output, "\\edef\\ekdsubsep{" .. apparatuses[n].subsep .. "}")
2196 end
2197 if apparatuses[n].bhook ~= ""
2198 then
2199 table.insert(output, apparatuses[n].bhook)
2200 else
2201 table.insert(output, "\\relax")
2202 end
2203 for i in string.gmatch(t,
2204 "<"..ekd_abs_pg
2205 ..cur_alignment_patt
2206 ..curcol.."%-"..n..">.-</"
2207 ..ekd_abs_pg
2208 ..cur_alignment_patt
2209 ..curcol.."%-"..n..">")
2210 do
2211 table.insert(output, i)
2212 appinserted = true
2213 end
2214 if apparatuses[n].ehook ~= ""
2215 then
2216 table.insert(output, apparatuses[n].ehook)
2217 else
2218 end
2219 table.insert(output, "\\egroup{}")
2220 -- table.insert(output, "END")
2221 end
2222 n = n + 1
2223 end
2224 end
2225 f:close()
2226 str = table.concat(output)
2227 str = string.gsub(str, "</"..ekd_abs_pg..cur_alignment_patt..curcol.."%-[0-9]>", "")
2228 str = string.gsub(str, "<"..ekd_abs_pg..cur_alignment_patt..curcol.."%-[0-9]>", " ")
2229 return str
2230 else
2231 end
2232 end
2233
2234 function ekdosis.appin_out(str, nl)
2235 local f = io.open(tex.jobname.."_tmp.ekd", "a+")
2236 if nl == "yes" then
2237 f:write(str, "\n")
2238 else
2239 f:write(str)
2240 end
2241 f:close()
2242 return true
2243 end
2244
2245 local curcol_curabspg = {}
2246
2247 function ekdosis.testapparatus()
2248 if isfound(curcol_curabspg, curcol.."-"..ekd_abs_pg)
2249 then
2250 if newalignment
2251 then
2252 if next(apparatuses) ~= nil then
2253 reset_bagunits()
2254 end
2255 newalignment = false
2256 return "\\booltrue{do@app}"
2257 else
2258 return "\\boolfalse{do@app}"
2259 end
2260 else
2261 table.insert(curcol_curabspg, curcol.."-"..ekd_abs_pg)
2262 if next(apparatuses) ~= nil then
2263 reset_bagunits()
2264 end
2265 newalignment = false
2266 return "\\booltrue{do@app}"
2267 end
2268 end
2269
2270 local function get_ln_prefix(x, y)
2271 for index = 1, string.len(x)
2272 do
2273 if string.sub(x, index, index) ~= string.sub(y, index, index)
2274 then
2275 return string.sub(x, 1, index - 1)
2276 end
2277 end
2278 end
2279
2280 function ekdosis.numrange(x, y)
2281 xstr = tostring(x)
2282 ystr = tostring(y)
2283 if x == y -- which will never apply
2284 then
2285 return "\\LRnum{" .. xstr .. "}"
2286 elseif string.len(xstr) ~= string.len(ystr)
2287 then
2288 return "\\LRnum{" .. xstr .. "}--\\LRnum{" .. ystr .. "}"
2289 else
2290 common = get_ln_prefix(xstr, ystr)
2291 if string.len(common) == 0
2292 then
2293 return "\\LRnum{" .. xstr .. "}--\\LRnum{" .. ystr .. "}"
2294 elseif string.sub(xstr, -2, -2) == "1"
2295 then
2296 return "\\LRnum{"
2297 .. string.sub(common, 1, -2)
2298 .. string.sub(xstr, string.len(common), -1)
2299 .. "}--\\LRnum{"
2300 .. string.sub(ystr, string.len(common), -1)
2301 .. "}"
2302 else
2303 return "\\LRnum{"
2304 .. string.sub(common, 1, -1)
2305 .. string.sub(xstr, string.len(common) + 1, -1)
2306 .. "}--\\LRnum{"
2307 .. string.sub(ystr, string.len(common) + 1, -1)
2308 .. "}"
2309 end
2310 end
2311 end
2312
2313 local lnlabs = {}
2314 local lnlab_salt = 0
2315 local current_lnlab = nil
2316 local prev_lnlab = nil
2317 local prev_prev_lnlab = nil
2318 local notelabs = {}
2319 local current_notelab = nil
2320 local prev_notelab = nil
2321 local current_lemma = nil
2322 local salt = 0
2323
2324 local function mdvisintable(table, value)
2325 for _, v in pairs(table) do
2326 if v == value then return true end
2327 end
2328 return false
2329 end
2330
2331 function ekdosis.dolnlab(str)
2332 if prev_lnlab ~= nil then prev_prev_lnlab = prev_lnlab end
2333 prev_lnlab = current_lnlab
2334 current_lemma = str
2335 i = md5.sumhexa(str)
2336 if not mdvisintable(lnlabs, i) then
2337 table.insert(lnlabs, i)
2338 else
2339 i = i..salt
2340 table.insert(lnlabs, i)
2341 salt = salt + 1
2342 end
2343 current_lnlab = i
2344 return true
2345 end
2346
2347 function ekdosis.getlnlab()
2348 return current_lnlab
2349 end
2350
2351 function ekdosis.getprevlnlab()
2352 return prev_lnlab
2353 end
2354
2355 function ekdosis.getprevprevlnlab()
2356 return prev_prev_lnlab
2357 end
2358
2359 function ekdosis.setnotelab(str)
2360 current_notelab = str
2361 if isfound(notelabs, current_notelab)
2362 then
2363 return ""
2364 else
2365 table.insert(notelabs, current_notelab)
2366 return "\\linelabel{" .. current_notelab .. "}"
2367 end
2368 end
2369
2370 function ekdosis.getnotelab()
2371 return current_notelab
2372 end
2373
2374 function ekdosis.setprevnotelab(str)
2375 prev_notelab = str
2376 return true
2377 end
2378
2379 function ekdosis.getprevnotelab()
2380 return prev_notelab
2381 end
2382
2383 local cmdstorm = lpeg.P(lpeg.P("note")
2384 + lpeg.P("linelabel")
2385 + lpeg.P("index"))
2386
2387 local function remove_from_app(str)
2388 str = gsub(str, dblbkslash * cmdstorm * cmdargs, "")
2389 return str
2390 end
2391
2392 function ekdosis.mdvappend(str, teitype)
2393 if teitype == nil
2394 then
2395 return "\\linelabel{" .. current_lnlab .. "-b}\\wordboundary{}"
2396 ..
2397 current_lemma
2398 ..
2399 "\\linelabel{" .. current_lnlab .. "-e}"
2400 ..
2401 "\\csname append@app\\endcsname{"
2402 .. remove_from_app(str) .. "}"
2403 else
2404 return "\\linelabel{" .. current_lnlab .. "-b}\\wordboundary{}"
2405 ..
2406 current_lemma
2407 ..
2408 "\\linelabel{" .. current_lnlab .. "-e}"
2409 ..
2410 "\\csname append@app\\endcsname" .. "[" .. teitype .. "]{"
2411 .. remove_from_app(str) .. "}"
2412 end
2413 end
2414