aboutsummaryrefslogtreecommitdiff
path: root/ekdosis.dtx
blob: 91a5662fbe5179ebe35fa37764ed5dca03e7124b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
% \iffalse meta-comment
% vim: textwidth=75
%<*internal>
\iffalse
%</internal>
%<*readme>
|
-------:| -----------------------------------------------------------------
ekdosis:| Typesetting TEI xml compliant critical editions
 Author:| Robert Alessi
 E-mail:| alessi@robertalessi.net
License:| Released under the GNU General Public License v3 or later
    See:| http://www.gnu.org/licenses/


Short description:
Some text about the package: probably the same as the abstract.
%</readme>
%<*luapre>
--[[
This file is part of the `ekdosis' package

ekdosis -- Typesetting TEI xml compliant critical editions
Copyright (C) 2016  Robert Alessi

Please send error reports and suggestions for improvements to Robert
Alessi <alessi@robertalessi.net>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see
<http://www.gnu.org/licenses/>.
--]]

%</luapre>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
-------:| -----------------------------------------------------------------
ekdosis:| Typesetting TEI xml compliant critical editions
 Author:| Robert Alessi
 E-mail:| alessi@robertalessi.net
License:| Released under the GNU General Public License v3 or later
    See:| http://www.gnu.org/licenses/

This file is part of the `ekdosis' package

ekdosis -- Typesetting TEI xml compliant critical editions
Copyright (C) 2016  Robert Alessi

Please send error reports and suggestions for improvements to Robert
Alessi <alessi@robertalessi.net>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see
<http://www.gnu.org/licenses/>.

This work consists of the file ekdosis.dtx and a Makefile.
Running "make" generates the derived files README, ekdosis.pdf and ekdosis.sty.
Running "make inst" installs the files in the user's TeX tree.
Running "make install" installs the files in the local TeX tree.
\endpreamble

\usedir{tex/lualatex/ekdosis}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
\nopreamble\nopostamble
\usedir{tex/lualatex/ekdosis}
\generate{
  \file{\jobname.lua}{
  \from{\jobname.dtx}{luapre}
  \from{\jobname.dtx}{lua}
  }
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/lualatex/ekdosis}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/lualatex/ekdosis}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{ekdosis.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{ekdosis}
%<*package>
    [2016/08/03 v1.0 Typesetting TEI xml compliant critical editions]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[letterpaper,margin=25mm,left=50mm,nohead]{geometry}
\usepackage{metalogox}
\usepackage{hologo}
\usepackage{hyperxmp}
\usepackage{uri}
\usepackage[numbered]{hypdoc}
\hypersetup{unicode=true, colorlinks, allcolors=blue,
  linktocpage=true, pdfauthor={Robert Alessi}, pdftitle={The
    ekdosis package}, pdfcontactemail={alessi@robertalessi.net},
  pdfcontacturl={http://www.robertalessi.net/ekdosis},
  pdfcopyright={Copyright (C) 2018 Robert Alessi
    <alessi@robertalessi.net>. This document is licensed under the
    Creative Commons Attribution-ShareAlike 4.0 International
    License. To view a copy of this license, visit
    http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to
    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.},
  pdflicenseurl={https://creativecommons.org/licenses/by-sa/4.0/legalcode},
  pdfmetalang={en-US}, pdftype={Text}, pdfkeywords={Arabic language,
    arabtex, luatex}}
\usepackage[lot]{multitoc}
\usepackage{ekdosis}
\usepackage{fontspec}
\setmainfont{Old Standard}[RawFeature={+ss05;+ss06}]
\usepackage{newunicodechar}
\newunicodechar{ǧ}{ǧ} % Old Standard does not include ǧ/Ǧ
\newunicodechar{Ǧ}{Ǧ} %
\usepackage{arabluatex}
\usepackage{relsize}
\usepackage{units}
\usepackage[newfloat=true]{minted}
\newminted[ekdlua]{lua}{linenos, fontsize=\relsize{-0.5},
  xleftmargin=12pt, breaklines,
  numberblanklines=false, numbersep=3pt, firstnumber=last}
\renewcommand{\theFancyVerbLine}{\rmfamily\smaller\arabic{FancyVerbLine}}
\usepackage[contents]{colordoc}
\newcommand{\package}[1]{\textsf{#1}\index{#1=#1 (package)}}
\usepackage{tikz}
\usepackage[breakable, skins, xparse, minted]{tcolorbox}
\usepackage{etoc}
\etocsettocdepth{paragraph}
\newcommand{\ekdtableofcontents}{%
  \begingroup
  \etocsetstyle{section}{}{}
  {\etocsavedsectiontocline{%
      \numberline{\etocnumber}\etocname}{\etocpage}}{}
  \etocsetstyle{subsection}{}{}
  {\etocsavedsubsectiontocline{%
      \numberline{\etocnumber}\etocname}{\etocpage}}{}%
  \etocsetstyle{subsubsection}{}{}
  {\etocsavedsubsubsectiontocline{%
      \numberline{\etocnumber}\etocname}{\etocpage}}{}%
  \etocsetstyle{paragraph}{}{\leftskip2cm\rightskip 2.2em \parfillskip
    0pt plus 1fil\relax \nobreak}
  {\noindent\etocname{} \etocpage{} }{\par}%
  \etocmulticolstyle[2]{\section*{Contents}}
  \pdfbookmark[1]{Contents}{toc}
  \tableofcontents
  \endgroup}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{\jobname.dtx}
% \DoNotIndex{\newcommand,\newenvironment}
%
% \title{\tcbox[colframe=black, enhanced, tikznode, drop lifted
% shadow, colback=white, boxrule=.25mm]%
% {ἔκδοσις\\
% Typesetting \texttt{TEI xml} compliant critical editions\\
% \fileversion\ --- \filedate}}
%  
% \author{Robert Alessi \\
% \href{mailto:alessi@robertalessi.net?Subject=ekdosis package}%
% {\texttt{alessi@robertalessi.net}}}
% 
% \date{}
%
% \maketitle
% \footnotesize
% \ekdtableofcontents
% \normalsize
%
%\changes{v1.00}{2016/08/03}{First public release}
%
% \begin{abstract}
% ==== Put abstract text here. ====
% \end{abstract}
%
% \section{Usage}
%
% ==== Put descriptive text here. ====
% 
% \DescribeMacro{\dummyMacro}
% This macro does nothing.\index{doing nothing|usage} It is merely an
% example.  If this were a real macro, you would put a paragraph here
% describing what the macro is supposed to do, what its mandatory and
% optional arguments are, and so forth.
% 
% \DescribeEnv{dummyEnv}
% This environment does nothing.  It is merely an example.
% If this were a real environment, you would put a paragraph here
% describing what the environment is supposed to do, what its
% mandatory and optional arguments are, and so forth.
%
% \section{Grid}
% \begin{keyfigure}[H]{c={Grid
%   typesetting},l={fig:grid-typesetting},t={Each square in the grid
%   has a side length of \unit[10]{pt}. Therefore, the distance
%   between the last line of the text and the first line of the
%   apparatus is here approximately \unit[25]{pt}.}}
%   \begin{tikzpicture}
%     \draw[lightgray,thin] (0.5,0.5) grid [step=10pt] (10,5); %
%     \path node [align=justify, text width=8cm] at (5.5,3) {%
%     I saw my friend Peter at the station yesterday. I saw my friend
%     Peter at the station yesterday. I saw my friend Peter at the
%     station yesterday. I saw my friend Peter at the station
%     yesterday.\par
%     \rule{3cm}{0.01cm}\par\footnotesize%
%     \textbf{1}~Peter A: John B \textbf{2}~Peter A: John B
%     \textbf{3}~Peter A: John B \textbf{4}~Peter A: John B}; \node at
%     (0,3) {\scriptsize $\approx$ \unit[25]{pt}}; \draw [->] (0,2.9) --
%     (0,2);
%   \end{tikzpicture}
% \end{keyfigure}
% 
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
% 
% \package{ekdosis} relies on Lua functions and tables. Read the
% |.lua| files that accompany \package{ekdosis} for more
% information.
%    \begin{macrocode}
\RequirePackage{ifluatex}
%    \end{macrocode}
% \package{ekdosis} requires {\LuaLaTeX} of course. Issue a warning
% if the document is processed with another engine.
%    \begin{macrocode}
\ifluatex\else
 \PackageError{ekdosis}{lualatex needed}{%
  Package `ekdosis' needs LuaTeX.\MessageBreak
  So you should use `lualatex' to process your document.\MessageBreak
  See documentation of `ekdosis' for further information.}%
  \expandafter\expandafter\expandafter\csname endinput\endcsname
\fi
%    \end{macrocode}
% Packages that are required by \package{ekdosis}:
%    \begin{macrocode}
\RequirePackage{xkeyval}
\newif\if@parnotesroman
\newif\if@pkg@parnotes
\define@choicekey+{ekdosis.sty}{parnotes}{true, false, roman}[true]{%
  \edef\@pntrue{true}\edef\@pnfalse{false}\edef\@pnrm{roman}
  \edef\@tempa{#1}
  \ifx\@tempa\@pnfalse
  \else
  \ifx\@tempa\@pntrue\@pkg@parnotestrue
  \else
  \ifx\@tempa\@pnrm
    \@pkg@parnotestrue\@parnotesromantrue
  \fi\fi\fi
}{\PackageWarning{ekdosis}{parnotes: erroneous input (ignored)}}
\newif\iftei@export
\define@boolkey{ekdosis.sty}[@pkg@]{teiexport}[true]{%
  \if@pkg@teiexport\tei@exporttrue\else\fi}
\ExecuteOptionsX{}
\ProcessOptionsX\relax
\RequirePackage{luacode}
\RequirePackage{paracol}
\RequirePackage{keycommand}
\RequirePackage{xparse}
\RequirePackage{etoolbox}
\PassOptionsToPackage{addpageno}{lineno}
\RequirePackage{lineno}
\RequirePackage{keyfloat}
\RequirePackage{refcount}
\RequirePackage{xspace}
\RequirePackage[user,abspage]{zref}
\if@pkg@parnotes
  \RequirePackage{parnotes}
\fi
%    \end{macrocode}
% Here begins the real work: load |ekdosis.lua|:
%    \begin{macrocode}
\luadirect{dofile(kpse.find_file("ekdosis.lua"))}
%    \end{macrocode}
%    \begin{macrocode}
\AtEndDocument{
  \luadirect{ekdosis.closestream()}
  \luadirect{ekdosis.closeteistream()}
}
%    \end{macrocode}
% Build an process the list of witnesses
%    \begin{macrocode}
\NewDocumentCommand{\DeclareNewWitness}{m m +m}{
  \luadirect{ekdosis.newwitness(\luastringN{#1},
    \luastringN{#2},
    \luastringN{#3})}
}
\NewDocumentCommand{\getsiglum}{m}{%
  \luadirect{tex.sprint(ekdosis.getsiglum(\luastringN{#1}))}%
}
%    \end{macrocode}
% Add a \hologo{(La)TeX} command to be processed as a |TEI xml| tag:
%    \begin{macrocode}
\NewDocumentCommand{\TeXtoTEI}{m m m}{%
  \luadirect{ekdosis.newcmdtotag(\luastringN{#1},
    \luastringN{#2},
    \luastringN{#3})}
}
%    \end{macrocode}
%    \begin{macrocode}
\newbool{ekd@started}
\newbool{do@app}
\newbool{ekd@state}
\newif\ifekd@isinapp
\providebool{al@rlmode}
\newcounter{ekd@lab}
\def\blfootnote{\gdef\@thefnmark{\relax}\@footnotetext}
\def\add@@apparatus{%
  % \blfootnote{\apparatus}%
  \if@pkg@parnotes\parnotes\else\fi%
  \keyparbox[b!]{}{%
    \rule{5cm}{0.5pt}\\
    \if@pkg@parnotes%
    \if@parnotesroman%
    \renewcommand*{\theparnotemark}{\roman{parnotemark}}\else\fi%
    \parnoteclear\else\fi%
    \footnotesize\apparatus%
    \if@pkg@parnotes\parnotes\parnotereset\else\fi%
  }%
}
\def\add@apparatus{%
  \notbool{ekd@started}{\add@@apparatus%
    \setbool{ekd@started}{true}}
  {\text@apparatus%
    \ifbool{do@app}{\add@@apparatus}{}}%
}
\NewDocumentCommand{\append@app}{+m}{%
  \ifekd@isinapp%
  \ifbool{ekd@state}{%
    \luadirect{ekdosis.appin(
      \luastring{<\zref@extract{ekd:\theekd@lab}{abspage}>},
      "no")}%
    \luadirect{ekdosis.appin(\luastringN{#1}, "no")}%
    \luadirect{ekdosis.appin(
      \luastring{</\zref@extract{ekd:\theekd@lab}{abspage}>},
      "yes")}%
  }{}\else\fi}
\NewDocumentCommand{\append@ln@app}{+m}%
  {\luadirect{tex.sprint(ekdosis.mdvappend(\luastringN{#1}))}}
\NewDocumentCommand{\app}{+m}{%
  \ekd@isinapptrue%
  \stepcounter{ekd@lab}%
  \zlabel{ekd:\theekd@lab}%
  \luadirect{%
    ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
    "pg_ii")}%
  \ifbool{ekd@state}{\add@apparatus}{}%
  #1%
  \ekd@isinappfalse}
\newkeycommand+[\|]{\lem}[wit, alt, pre, post, prewit, postwit,
choice nolem={false,,true}][1]{%
  #1%
  |\ifnum|%
  \commandkey{nolem}>0%
  \append@ln@app{}%
  |\else|%
  |\ifbool{al@rlmode}|%
  {\append@ln@app{%
      \bgroup%
      \setRL%
      \ifcommandkey{alt}{%
        \ifcommandkey{post}{ \commandkey{post} }{}%
        \commandkey{alt}%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
      }{%
        \ifcommandkey{post}{ \commandkey{post} }{}%
        #1%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
      }%
      \egroup{}%
      \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
      \ifcommandkey{postwit}{ \commandkey{postwit}}{}:%
    }}%
  {\append@ln@app{%
      \ifcommandkey{alt}{%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
        \commandkey{alt}%
        \ifcommandkey{post}{ \commandkey{post} }{}%
      }{%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
        #1%
        \ifcommandkey{post}{ \commandkey{post} }{}%
      }%
      \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
      \ifcommandkey{postwit}{ \commandkey{postwit}}{}:%
    }}%
  |\fi|}
% \define@key[ekd]{lemma}{wit}{\getsiglum{#1}}
% \define@key[ekd]{lemma}{tchic}{#1}
% \presetkeys[ekd]{lemma}{wit={},tchic={}}{}
% \def\mgetsiglum#1{\setkeys[ekd]{lemma}[tchic]{#1}}
% \def\mtchic#1{\setkeys[ekd]{lemma}[wit]{#1}}
% \NewDocumentCommand{\mrdg}{O{} +m}{%
% \append@app{#2 \mtchic{#1}\mgetsiglum{#1}\mtchic{#1}}%
% }
\newkeycommand+[\|]{\rdg}[wit, alt, pre, post, prewit, postwit][1]{%
  |\ifbool{al@rlmode}|%
  {\append@app{%
      \bgroup%
      \setRL%
      \ifcommandkey{alt}{%
        \ifcommandkey{post}{ \commandkey{post} }{}%
        \commandkey{alt}%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
      }{%
        \ifcommandkey{post}{ \commandkey{post} }{}%
        #1%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
      }%
      \egroup{}%
      \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
      \ifcommandkey{postwit}{ \commandkey{postwit}}{}%
    }}%
  {\append@app{%
      \ifcommandkey{alt}{%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
        \commandkey{alt}%
        \ifcommandkey{post}{ \commandkey{post} }{}%
      }{%
        \ifcommandkey{pre}{ \commandkey{pre} }{}%
        #1%
        \ifcommandkey{post}{ \commandkey{post} }{}%
      }%
      \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
      \ifcommandkey{postwit}{ \commandkey{postwit}}{}%
    }}%
}
\NewDocumentCommand{\note}{s +m}{%
  \IfBooleanTF{#1}{%
    \if@pkg@parnotes\append@app{\parnote{#2}}%
    \else\append@app{\footnote{#2}}\fi}{\append@app{#2}}%
}
\NewDocumentCommand{\apparatus}{}{\luadirect{tex.sprint(ekdosis.appout())}}
\NewDocumentCommand{\text@apparatus}{}{%
  \luadirect{tex.sprint(ekdosis.testapparatus())}}
\NewDocumentCommand{\EkdosisStart}{}{%
  \stepcounter{ekd@lab}%
  \zlabel{ekd:\theekd@lab}%
  \luadirect{%
    ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
    "pg_i")}%
}
\NewDocumentCommand{\EkdosisOn}{}{\setbool{ekd@state}{true}}
\NewDocumentCommand{\EkdosisOff}{}{%
  \setbool{ekd@state}{false}%
}
\NewDocumentEnvironment{ekdosis}{+b}{%
  \begin{linenumbers}
    \EkdosisStart%
    \EkdosisOn#1}{%
    \EkdosisOff
  \end{linenumbers}%
  \iftei@export\luadirect{ekdosis.exporttei(\luastringN{#1})}\else\fi}
%    \end{macrocode}
% \iffalse
%</package>
% \fi
% \StopEventually{}
% \Finale
% \iffalse `'
%<*lua>
% \fi
% \begin{ekdlua}

ekdosis = {}

-- lpeg equivalent for string.gsub()
local function gsub(s, patt, repl)
  patt = lpeg.P(patt)
  patt = lpeg.Cs((patt / repl + 1)^0)
  return lpeg.match(patt, s)
end

-- some basic patterns:
local ascii = lpeg.R("az", "AZ", "@@")
local dblbkslash = lpeg.Cs("\\")
local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" }
local bcbraces = lpeg.Cs{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" }
local spce = lpeg.Cs(" ")
local spcenc = lpeg.P(" ")
local cmdstar = lpeg.Cs(spce * lpeg.P("*"))
local bsqbracketsii = lpeg.Cs(bsqbrackets^-2)
local bcbracesii = lpeg.Cs(bcbraces^-2)
local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1)
local rawcmd = lpeg.Cs(dblbkslash * ascii^1)
local aftercmd = lpeg.Cs(lpeg.S("*[{,.?;:'`\"") + dblbkslash)
local cmdargs = lpeg.Cs(spce^-1 * bsqbracketsii * bcbracesii * bsqbrackets^-1)
local lemrdg = lpeg.Cs(lpeg.Cs("lem") + lpeg.Cs("rdg"))

-- Witnesses
listWit = {}

local function isintable(table, element)
   for i = 1,#table do
      if table[i].a == element then
	 return true
      end
   end
   return false
end

local sorted_no = 0

function ekdosis.newwitness(id, siglum, description)
   if isintable(listWit, id..",")
   then
      tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
      	 ..id..
      	 [[" already exists as a witness id. ]]
      	 ..
      	 [[Please pick another id.}}]])
   else
      sorted_no = sorted_no + 1
      id = id..","
      table.insert(listWit, {a = id, b = siglum, c = description, d = sorted_no})
      table.sort(listWit, function(a ,b) return(#a.a > #b.a) end)
   end
   return true
end

function ekdosis.getsiglum(str, opt)
   str = str..","
   str = string.gsub(str, "%s-(%,)", "%1")
   ctrl = str
   if opt == "tei" then
      for i = 1,#listWit do
	 str  = string.gsub(str, "(%f[%w])"..listWit[i].a, "%1#"..listWit[i].a)
	 ctrl = string.gsub(ctrl, listWit[i].a, "")
      end
      str = string.gsub(str, "%,(%s-)([%#])", " %2")
      str = string.gsub(str, "%,$", "")
   else
      for i = 1,#listWit do
	 str  = string.gsub(str, listWit[i].a, listWit[i].b)
	 ctrl = string.gsub(ctrl, listWit[i].a, "")
      end
   end
   if string.find(ctrl, "[A-Za-z0-9]")
   then
      return "<??>"
   else
      return str
   end
end

-- begin totei functions
local cmdtotags = {
   {a="footnote", b="note", c=" place=\"bottom\""},
   {a="textbf", b="hi", c=" rend=\"bold\""},
   {a="txarb", b="foreign", c=" xml:lang=\"arb\""},
   {a="emph", b="hi", c=" rend=\"italic\""},
   {a="arb", b="foreign", c=" xml:lang=\"arb\""},
   {a="app", b="app", c=""},
   {a="lem", b="lem", c=""},
   {a="rdg", b="rdg", c=""}
}

function ekdosis.newcmdtotag(cmd, tag, attr)
   if isintable(cmdtotags, cmd)
   then
      tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
      	 ..cmd..
      	 [[" already exists as a known command to be processed to TEI. ]]
      	 ..
      	 [[Please pick another command.}}]])
   else
      table.insert(cmdtotags, {a = cmd, b = tag, c = " "..attr})
      table.sort(cmdtotags, function(a ,b) return(#a.a > #b.a) end)
   end
   return true
end


local function lem_rdg_totei(str)
   str = gsub(str, dblbkslash * lemrdg * spcenc^-1 * bsqbrackets * bcbraces,
	      function(bkslash, cmd, opt, arg)
		 opt = string.sub(opt, 2, -2)
		 arg = string.sub(arg, 2, -2)
		 opt = string.gsub(opt, "wit%s?%=%s?(%b{})", function(bbraces)
				      bbraces = string.sub(bbraces, 2, -2)
				      bbraces = ekdosis.getsiglum(bbraces, "tei")
				      return string.format("%s", bbraces)
		 end)
		 return string.format("<%s wit=\"%s\">%s</%s>", cmd, opt, arg, cmd)
   end)
   return str
end

function ekdosis.textotei(str)
   str = string.gsub(str, "%s?\\par%s?", "</p>\n<p>")
   str = lem_rdg_totei(str)
   for i = 1,#cmdtotags
   do
      str = string.gsub(str, "(\\"..cmdtotags[i].a..")%s?(%b{})", "%1[]%2")
      str = string.gsub(str, "(\\"..cmdtotags[i].a..")%s?(%b[])(%b{})",
			function(cmd, arg, body)
			   body = string.sub(body, 2, -2)
			   arg = string.sub(arg, 2, -2)
			   arg = string.gsub(arg, "(%b{})", function(braces)
						braces = string.sub(braces, 2, -2)
						return string.format("\"%s\"", braces)
			   end)
			   body = ekdosis.textotei(body)
			   return string.format("<"..cmdtotags[i].b..cmdtotags[i].c.." %s>%s</"..cmdtotags[i].b..">", arg, body)
      end)
   end
   str = string.gsub(str, "\\(%a+)%s?%*?(%b[])(%b{})",
		     function(cmd, opt, body)
			body = string.sub(body, 2, -2)
			body = ekdosis.textotei(body)
			return string.format("<%s>%s</%s>", cmd, body, cmd)
   end)
   str = string.gsub(str, "\\(%a+)%s?%*?(%b{})",
		     function(cmd, body)
			body = string.sub(body, 2, -2)
			body = ekdosis.textotei(body)
			return string.format("<%s>%s</%s>", cmd, body, cmd)
   end)
   str = string.gsub(str, "(%s)(%>)", "%2")
   return str
end

function ekdosis.closeteistream()
   os.remove(tex.jobname.."-tei.xml")
   os.rename(tex.jobname.."_tmp-tei.xml", tex.jobname.."-tei.xml")
   return true
end

function ekdosis.exporttei(str)
   local f = io.open(tex.jobname.."_tmp-tei.xml", "a+")
   str = ekdosis.textotei(str)
   f:write("\n<p>")
   f:write(str)
   f:write("</p>")
   f:close()
   return true
end
-- end totei functions

function ekdosis.closestream()
   os.remove(tex.jobname..".ekd")
   os.rename(tex.jobname.."_tmp.ekd", tex.jobname..".ekd")
   return true
end

local pg_i = 0
local pg_ii = 0
local cur_abs_pg = 0

function ekdosis.update_abspg(n)
   cur_abs_pg = n
   return true
end

function ekdosis.get_abs_page()
   return cur_abs_pg
end

function ekdosis.storeabspg(n, pg)
   if pg == "pg_i" then
      pg_i = n
   elseif pg == "pg_ii" then
      pg_ii = n
   end
   cur_abs_pg = n
   return true
end

ekdosis.getabspg = function(pg) --not used
   if pg == "pg_i" then
      return pg_i
   elseif pg == "pg_ii" then
      return pg_ii
   end
end

local function mdvisintable(table, value)
   for _, v in pairs(table) do
      if v == value then return true end
   end
   return false
end

function ekdosis.appin(str, nl)
   local f = io.open(tex.jobname.."_tmp.ekd", "a+")
   if nl == "yes" then
      f:write(str, "\n")
   else
      f:write(str)
   end
   f:close()
   return true
end

function ekdosis.appout()
   local file = io.open(tex.jobname..".ekd", "r")
   if file ~= nil then io.close(file)
      f = assert(io.open(tex.jobname..".ekd", "r"))
      t = f:read("*a")
      local output = {}
      for i in string.gmatch(t,
			     "<"..cur_abs_pg..">.-</"..cur_abs_pg..">")
      do
	 table.insert(output, i)
      end
      f:close()
      str = table.concat(output, "\n")
      str = string.gsub(str, "</"..cur_abs_pg..">", " ")
      str = string.gsub(str, "<"..cur_abs_pg..">", "")
      return str
   else end
end

function ekdosis.testapparatus()
   if pg_ii == pg_i then
      return "\\boolfalse{do@app}"
   elseif pg_ii > pg_i then
      pg_i = pg_ii
      return "\\booltrue{do@app}"
   else
      return "\\boolfalse{do@app}"
   end
end

md5items = {}
local salt = 0

function ekdosis.mdvappend(str)
   i = md5.sumhexa(str)
   if not mdvisintable(md5items, i) then
      table.insert(md5items, i)
   else
      i = i..salt
      table.insert(md5items, i)
      salt = salt + 1
   end
   return "\\linelabel{"..i.."}"
   ..
   "\\csname append@app\\endcsname{\\textbf{\\getrefnumber{"..i.."}} "..str.."}"
end

% \end{ekdlua}
% \iffalse
%</lua>
% \fi
\endinput