From e760359b4dd2765f148b865ecf9f755fee72e87b Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 26 Mar 2019 00:23:36 +0100
Subject: export 'wit' attribute to tei. added basic \DeclareNewWitness

---
 ekdosis.dtx | 88 +++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 71 insertions(+), 17 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 796482f..ff530af 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -353,13 +353,13 @@ Running "make install" installs the files in the local TeX tree.
 %    \end{macrocode}
 % Build an process the list of witnesses
 %    \begin{macrocode}
-\NewDocumentCommand{\NewWitness}{m m +m}{
+\NewDocumentCommand{\DeclareNewWitness}{m m +m}{
   \luadirect{ekdosis.newwitness(\luastringN{#1},
     \luastringN{#2},
     \luastringN{#3})}
 }
 \NewDocumentCommand{\getsiglum}{m}{%
-  \luadirect{tex.sprint(ekdosis.getsiglum(\luastring{#1}))}
+  \luadirect{tex.sprint(ekdosis.getsiglum(\luastringN{#1}))}
 }
 %    \end{macrocode}
 %    \begin{macrocode}
@@ -434,7 +434,7 @@ choice nolem={false,,true}][1]{%
       }%
       \egroup{}%
       \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
-      \ifcommandkey{wit}{ \commandkey{wit}}{}%
+      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
       \ifcommandkey{postwit}{ \commandkey{postwit}}{}:%
     }}%
   {|\append@ln@app|{%
@@ -448,7 +448,7 @@ choice nolem={false,,true}][1]{%
         \ifcommandkey{post}{ \commandkey{post} }{}%
       }%
       \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
-      \ifcommandkey{wit}{ \commandkey{wit}}{}%
+      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
       \ifcommandkey{postwit}{ \commandkey{postwit}}{}:%
     }}%
 |\fi|}
@@ -468,7 +468,7 @@ choice nolem={false,,true}][1]{%
       }%
       \egroup{}%
       \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
-      \ifcommandkey{wit}{ \commandkey{wit}}{}%
+      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
       \ifcommandkey{postwit}{ \commandkey{postwit}}{}%
     }}%
   {|\append@app|{%
@@ -482,7 +482,7 @@ choice nolem={false,,true}][1]{%
         \ifcommandkey{post}{ \commandkey{post} }{}%
       }%
       \ifcommandkey{prewit}{ \commandkey{prewit} }{}%
-      \ifcommandkey{wit}{ \commandkey{wit}}{}%
+      \ifcommandkey{wit}{ \getsiglum{\commandkey{wit}}}{}%
       \ifcommandkey{postwit}{ \commandkey{postwit}}{}%
     }}%
 }
@@ -525,9 +525,33 @@ choice nolem={false,,true}][1]{%
 
 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"))
+
 listWit = {}
 
 function ekdosis.newwitness(id, siglum, description)
+   id = id..","
    table.insert(listWit, {a = id, b = siglum, c = description})
    table.sort(listWit, function(a ,b) return(#a.a > #b.a) end)
    return true
@@ -542,15 +566,29 @@ function ekdosis.isintable(table, element)
    return false
 end
 
-function ekdosis.getsiglum(str)
---   if not ekdosis.isintable(listWit, str) then
---      str = "<??>"
---   else
+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, listWit[i].a, "#"..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
-   return str
+   end
+   if string.find(ctrl, "[A-Za-z0-9]")
+   then
+      return "<??>"
+   else
+      return str
+   end
 end
 
 -- begin totei functions
@@ -567,13 +605,29 @@ local tags = {
    {a="rdg", b="rdg", c=""}
 }
 
+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,#tags
    do
       str = string.gsub(str, "(\\"..tags[i].a..")%s?(%b{})", "%1[]%2")
       str = string.gsub(str, "(\\"..tags[i].a..")%s?(%b[])(%b{})",
-			function(tag, arg, body)
+			function(cmd, arg, body)
 			   body = string.sub(body, 2, -2)
 			   arg = string.sub(arg, 2, -2)
 			   arg = string.gsub(arg, "(%b{})", function(braces)
@@ -585,16 +639,16 @@ function ekdosis.textotei(str)
       end)
    end
    str = string.gsub(str, "\\(%a+)%s?%*?(%b[])(%b{})",
-		     function(tag, opt, body)
+		     function(cmd, opt, body)
 			body = string.sub(body, 2, -2)
 			body = ekdosis.textotei(body)
-			return string.format("<%s>%s</%s>", tag, body, tag)
+			return string.format("<%s>%s</%s>", cmd, body, cmd)
    end)
    str = string.gsub(str, "\\(%a+)%s?%*?(%b{})",
-		     function(tag, body)
+		     function(cmd, body)
 			body = string.sub(body, 2, -2)
 			body = ekdosis.textotei(body)
-			return string.format("<%s>%s</%s>", tag, body, tag)
+			return string.format("<%s>%s</%s>", cmd, body, cmd)
    end)
    str = string.gsub(str, "(%s)(%>)", "%2")
    return str
-- 
cgit v1.2.3

