From 29f699359d2040eb090a88ba45fb9876cd169538 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Mon, 23 Sep 2019 22:22:31 +0200
Subject: started implementing multi-layer apparatuses. improved tei exporting
 of <lem/> and <rdg/>

---
 ekdosis.dtx | 156 ++++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 119 insertions(+), 37 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 39cc3a7..c81489d 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -440,9 +440,18 @@ idno={}, msName={}, origDate={}][3]{%
 }
 \@onlypreamble\SetxmlBibResource
 %    \end{macrocode}
+% Multi-layer apparatuses
+%    \begin{macrocode}
+\newif\ifekd@mapps
+\NewDocumentCommand{\DeclareApparatus}{m}{
+  \unless\ifekd@mapps\global\ekd@mappstrue\fi
+  \luadirect{ekdosis.newapparatus(\luastringN{#1})}
+}
+\@onlypreamble\DeclareApparatus
+%    \end{macrocode}
 %    \begin{macrocode}
 \newbool{do@app}
-\newbool{ekd@state}
+\newif\ifekd@state
 \newif\ifekd@isinapp
 \providebool{al@rlmode}
 \@ifpackageloaded{arabluatex}{}{%
@@ -476,30 +485,39 @@ idno={}, msName={}, origDate={}][3]{%
     \footnotesize\apparatus\ekd@end@apparatus%
     \if@pkg@parnotes\parnotes\parnotereset\else\fi%
   }%
-  \unconditional@appin{\rule{5cm}{0.5pt}\newline}
+  % \unconditional@appin{\rule{5cm}{0.5pt}\newline}
 }
 \def\add@apparatus{%
   \test@apparatus%
   \ifbool{do@app}{\subsq@unitfalse\add@@apparatus}{}%
 }
-\NewDocumentCommand{\append@app}{+m}{%
+\NewDocumentCommand{\append@app}{o +m}{%
   \ifekd@isinapp%
-  \ifbool{ekd@state}{%
-    \luadirect{ekdosis.appin(\luastringO{#1})}%
-  }{}\else\fi}
-\NewDocumentCommand{\append@ln@app}{+m}%
-  {\luadirect{tex.sprint(ekdosis.mdvappend(\luastringO{#1}))}}
-\NewDocumentCommand{\app}{ > { \TrimSpaces } +m}{%
+    \ifekd@state%
+    \IfNoValueTF{#1}%
+      {\luadirect{ekdosis.appin(\luastringO{#2})}}%
+      {\luadirect{ekdosis.appin(\luastringO{#2}, \luastringO{#1})}}%
+    \fi%
+  \fi}
+\NewDocumentCommand{\append@ln@app}{o +m}{%
+  \IfNoValueTF{#1}
+  {\luadirect{tex.sprint(ekdosis.mdvappend(\luastringO{#2}))}}
+  {\luadirect{tex.sprint(ekdosis.mdvappend(\luastringO{#2},
+      \luastringO{#1}))}}}
+\define@cmdkey[ekd]{appnote}[ekdan@]{type}{}
+\NewDocumentCommand{\app}{O{} > { \TrimSpaces } +m}{%
+  \presetkeys[ekd]{appnote}{type=default}{}%
+  \setkeys[ekd]{appnote}{#1}%
   \ekd@isinapptrue%
   \stepcounter{ekd@lab}%
   \zlabel{ekd:\theekd@lab}%
   \luadirect{ekdosis.storeabspg(
     \luastring{\zref@extract{ekd:\theekd@lab}{abspage}})}%
-  \ifbool{ekd@state}{\add@apparatus}{}%
+  \ifekd@state\add@apparatus\fi%
   \luadirect{%
     ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
     "pg_ii")}%
-  \luadirect{tex.sprint(ekdosis.removesp(\luastringN{#1}))}%
+  \luadirect{tex.sprint(ekdosis.removesp(\luastringN{#2}))}%
   \ekd@isinappfalse}
 \def\current@ref{{\textdir TLT\noexpand\bfseries%
     \ifnum%
@@ -547,9 +565,10 @@ idno={}, msName={}, origDate={}][3]{%
   \bgroup%
   \setkeys[ekd]{lemrdg,lem}{#1}%
   \ifekdl@nolem\edef\lem@app{%
-      \hskip .75em\ifsubsq@unit%
-        \unexpanded\expandafter{\ekd@unit@delim}\else\fi%
-      \current@ref\hskip .25em}\else%
+    \hskip .75em\ifsubsq@unit%
+    \unexpanded\expandafter{\ekd@unit@delim}\else\fi%
+    \current@ref\hskip .25em}%
+  \else%
   \ifbool{al@rlmode}{%
     \edef\lem@app{%
       \hskip .75em\ifsubsq@unit%
@@ -608,14 +627,19 @@ idno={}, msName={}, origDate={}][3]{%
     }%
   }%
   \fi%
+  \ifekd@mapps%
+      \append@ln@app[\ekdan@type]{\lem@app}%
+  \else%
   \append@ln@app{\lem@app}%
+  \fi%
   \egroup%
   \subsq@unittrue%
 }
 \NewDocumentCommand{\rdg}{O{} m}{%
   \bgroup%
   \setkeys[ekd]{lemrdg,rdg}{#1}%
-  \ifekdr@nordg\append@app{}\else%
+  \ifekdr@nordg\append@app{}\else% do we need \append@app{} here? If
+                                % so, keep in mind \ifekd@mapps
   \ifbool{al@rlmode}{%
     \edef\rdg@app{%
       \ifdefined\ekdlr@alt
@@ -665,8 +689,12 @@ idno={}, msName={}, origDate={}][3]{%
         \space\unexpanded\expandafter{\ekdlr@postwit}\else\fi
     }%
   }%
+  \ifekd@mapps%
+      \append@app[\ekdan@type]{\rdg@app}%
+  \else%
   \append@app{\rdg@app}%
   \fi%
+  \fi%
   \egroup%
 }
 \newkeycommand+[\|]{\ekd@note}[pre, post][1]{%
@@ -707,9 +735,9 @@ idno={}, msName={}, origDate={}][3]{%
     ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
     "pg_i")}%
 }
-\NewDocumentCommand{\EkdosisOn}{}{\setbool{ekd@state}{true}}
+\NewDocumentCommand{\EkdosisOn}{}{\ekd@statetrue}
 \NewDocumentCommand{\EkdosisOff}{}{%
-  \setbool{ekd@state}{false}%
+  \ekd@statefalse%
 }
 \NewDocumentEnvironment{ekdosis}{+b}{%
   \begin{linenumbers}
@@ -752,6 +780,7 @@ 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 app = lpeg.Cs("app")
 local lemrdg = lpeg.Cs(lpeg.Cs("lem") + lpeg.Cs("rdg"))
 local lnbrk = lpeg.Cs("\\\\")
 local poemline = lpeg.Cs(lnbrk * bsqbrackets^-1)
@@ -1017,9 +1046,35 @@ local function xml_entities(str)
    return str
 end
 
-local function lem_rdg_totei(str)
+local function app_totei(str)
    str = gsub(str,
 	      dblbkslash *
+		 app *
+		 spcenc^-1 *
+		 bsqbrackets *
+		 bcbraces *
+		 spcenc^-1,
+	      function(bkslash, cmd, opt, arg)
+		 opt = string.sub(opt, 2, -2)
+		 arg = string.sub(arg, 2, -2)
+		 opt = string.match(opt, "%f[%w]type%s?%=%s?%b{}")
+		    or string.match(opt, "%f[%w]type%s?%=%s?%w+%f[%W]")
+		    or ""
+		 opt = string.gsub(opt, "type%s?%=%s?(%b{})", function(bbraces)
+				      bbraces = string.sub(bbraces, 2, -2)
+				      return string.format("%s", bbraces)
+		 end)
+		 opt = string.gsub(opt, "(type%s?%=%s?)(%w+%f[%W])", "%2")
+		 return app_totei(string.format("<%s type=\"%s\">%s</%s>",
+						    cmd, opt, arg, cmd))
+   end)
+   return str
+end
+
+local function lem_rdg_totei(str)
+   str = gsub(str,
+	      spcenc^-1 *
+		 dblbkslash *
 		 lemrdg *
 		 spcenc^-1 *
 		 bsqbrackets *
@@ -1028,15 +1083,26 @@ local function lem_rdg_totei(str)
 	      function(bkslash, cmd, opt, arg)
 		 opt = string.sub(opt, 2, -2)
 		 arg = string.sub(arg, 2, -2)
-		 opt = string.match(opt, "%f[%w]wit%s?%=%s?%b{}") or ""
+		 opt = string.match(opt, "%f[%w]wit%s?%=%s?%b{}")
+		    or string.match(opt, "%f[%w]wit%s?%=%s?%w+%f[%W]")
+		    or ""
 		 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)
+		 opt = string.gsub(opt, "(wit%s?%=%s?)(%w+%f[%W])", function(attr, value)
+				      value = ekdosis.getsiglum(value, "tei")
+				      return string.format("%s", value)
+		 end)
 		 return lem_rdg_totei(string.format("<%s wit=\"%s\">%s</%s>",
 						    cmd, opt, arg, cmd))
    end)
+   str = gsub(str, spcenc^-0 * dblbkslash * lemrdg * spcenc^-1 * bcbraces * spcenc^-1,
+	      function(bkslash, cmd, arg)
+		 arg = string.sub(arg, 2, -2)
+		 return lem_rdg_totei(string.format("<%s>%s</%s>", cmd, arg, cmd))
+   end)
    return str
 end
 
@@ -1138,6 +1204,7 @@ local function textotei(str)
       str = string.gsub(str, "%s?\\par%s?", "\n<p>")
       tei_p_open = true
    end
+   str = app_totei(str)
    str = lem_rdg_totei(str)
    str = versetotei(str)
    str = envtotei(str)
@@ -1367,6 +1434,7 @@ end
 -- handle multiple layers in apparatuses
 --
 local apparatuses = {}
+local bagtype = {}
 function ekdosis.newapparatus(teitype)
    if isintable(apparatuses, teitype)
    then
@@ -1376,6 +1444,7 @@ function ekdosis.newapparatus(teitype)
    else
       table.insert(apparatuses, { a = teitype })
    end
+   bagtype[type] = 1
    return true
 end
 
@@ -1388,7 +1457,7 @@ function ekdosis.appin(str, teitype)
       for i = 1,#apparatuses
       do
 	 if apparatuses[i].a == teitype then
-	    local appno = i
+	    appno = i
 	    break
 	 end
       end
@@ -1398,18 +1467,6 @@ function ekdosis.appin(str, teitype)
    return true
 end
 
-function ekdosis.appin_out(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)
@@ -1418,6 +1475,7 @@ function ekdosis.appout()
       local output = {}
       if next(apparatuses) == nil then
 	 -- table.insert(output, "BEGIN")
+	 table.insert(output, "\\par")
 	 for i in string.gmatch(t,
 				"<"..cur_abs_pg.."%-0>.-</"..cur_abs_pg.."%-0>")
 	 do
@@ -1429,6 +1487,7 @@ function ekdosis.appout()
 	 while apparatuses[n]
 	 do
 	    -- table.insert(output, "BEGIN")
+	    table.insert(output, "\\par")
 	    for i in string.gmatch(t,
 				   "<"..cur_abs_pg.."%-"..n..">.-</"..cur_abs_pg.."%-"..n..">")
 	    do
@@ -1446,6 +1505,17 @@ function ekdosis.appout()
    else end
 end
 
+function ekdosis.appin_out(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.testapparatus()
    if tonumber(pg_ii) < tonumber(pg_i)
    or tonumber(pg_ii) < tonumber(cur_abs_pg) then
@@ -1496,15 +1566,27 @@ function ekdosis.getprevlnlab()
    return prev_lnlab
 end
 
-function ekdosis.mdvappend(str)
-   return [[\linelabel{]] .. current_lnlab .. [[-b}\wordboundary{}]]
+function ekdosis.mdvappend(str, teitype)
+   if teitype == nil
+   then
+   return "\\linelabel{" .. current_lnlab .. "-b}\\wordboundary{}"
       ..
       current_lemma
       ..
-      [[\linelabel{]] .. current_lnlab .. [[-e}]]
+      "\\linelabel{" .. current_lnlab .. "-e}"
+      ..
+      "\\csname append@app\\endcsname{"
+      .. str .. "}"
+   else
+   return "\\linelabel{" .. current_lnlab .. "-b}\\wordboundary{}"
       ..
-      [[\csname append@app\endcsname{]]
-      .. str .. [[}]]
+      current_lemma
+      ..
+      "\\linelabel{" .. current_lnlab .. "-e}"
+      ..
+      "\\csname append@app\\endcsname" .. "[" .. teitype ..  "]{"
+      .. str .. "}"
+   end
 end
 
 % \end{ekdlua}
-- 
cgit v1.2.3

