From 3c7f90b3b3ba2bfb9fd7dc0ccd67f6a0584c00b4 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 7 May 2019 15:18:28 +0200
Subject: new function versetotei(). added star option to \EnvtoTEI{} that
 instructs ekdosis to close <p> if it is open

---
 ekdosis.dtx | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 56 insertions(+), 6 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index e612149..138b0a8 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -425,10 +425,17 @@ idno={}, msName={}, origDate={}][3]{%
     \luastringN{#2},
     \luastringN{#3})}
 }
-\NewDocumentCommand{\EnvtoTEI}{m m m}{%
-  \luadirect{ekdosis.newenvtotag(\luastringN{#1},
-    \luastringN{#2},
-    \luastringN{#3})}
+\NewDocumentCommand{\EnvtoTEI}{s m m m}{%
+  \IfBooleanTF{#1}{%
+  \luadirect{ekdosis.newenvtotag(\luastringN{#2},
+    \luastringN{#3},
+    \luastringN{#4},
+    "yes")}
+  }{%
+  \luadirect{ekdosis.newenvtotag(\luastringN{#2},
+    \luastringN{#3},
+    \luastringN{#4})}
+  }
 }
 %    \end{macrocode}
 % Set |TEI| file name
@@ -673,6 +680,9 @@ 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"))
+local lnbrk = lpeg.Cs("\\\\")
+local poemline = lpeg.Cs(lnbrk * lpeg.S(">")^-1 * bsqbrackets^-1)
+local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1)
 
 -- Booleans
 local tei_p_open = false
@@ -694,6 +704,15 @@ listWit = {}
 idsRend = {}
 shorthands = {}
 
+local function isfound(table, value)
+   for i = 1,#table do
+      if table[i] == value then
+	 return true
+      end
+   end
+   return false
+end
+
 local function isintable(table, value)
    for i = 1,#table do
       if table[i].a == value then
@@ -868,10 +887,16 @@ local cmdtotags = {
 
 local envtotags = {
    {a="center", b="p", c=" rend=\"centered\""},
+   {a="verse", b="lg", c=""},
    {a="arab", b="p",
     c=" xml:lang=\"arb-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
 }
 
+local close_p = {
+   "p",
+   "lg"
+}
+
 function ekdosis.newcmdtotag(cmd, tag, attr)
    if isintable(cmdtotags, cmd)
    then
@@ -887,7 +912,7 @@ function ekdosis.newcmdtotag(cmd, tag, attr)
    return true
 end
 
-function ekdosis.newenvtotag(env, tag, attr)
+function ekdosis.newenvtotag(env, tag, attr, closep)
    if isintable(envtotags, env)
    then
       tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
@@ -898,6 +923,9 @@ function ekdosis.newenvtotag(env, tag, attr)
    else
       table.insert(envtotags, {a = env, b = tag, c = " "..attr})
       table.sort(envtotags, function(a ,b) return(#a.a > #b.a) end)
+      if closep == "yes" then
+	 table.insert(close_p, tag)
+      else end
    end
    return true
 end
@@ -931,10 +959,31 @@ local function lem_rdg_totei(str)
    return str
 end
 
+local function linestotei(str)
+   str = "\n<l>"..str
+   str = gsub(str, endpoem, "</l>\n")
+   str = gsub(str, poemline, "</l>\n<l>")
+   return str
+end
+
+-- better use lpeg: look into this later
+local function versetotei(str)
+   str = string.gsub(str, "(\\begin%s?%{verse%})(%b[])(.-)(\\end%s?%{verse%})", function(benv, opt, arg, eenv)
+			arg = linestotei(arg)
+			return string.format("%s%s%s%s", benv, opt, arg, eenv)
+   end)
+   str = string.gsub(str, "(\\begin%s?%{verse%})(.-)(\\end%s?%{verse%})", function(benv, arg, eenv)
+			arg = linestotei(arg)
+			return string.format("%s%s%s", benv, arg, eenv)
+   end)
+   return str
+end
+
 local function envtotei(str)
    for i = 1,#envtotags
    do
-      if tei_p_open and envtotags[i].b == "p"
+      -- if tei_p_open and envtotags[i].b == "p" or envtotags[i].b == "lg"
+      if tei_p_open and isfound(close_p, envtotags[i].b)
       then
 	 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
 		       * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
@@ -1008,6 +1057,7 @@ local function textotei(str)
       tei_p_open = true
    end
    str = lem_rdg_totei(str)
+   str = versetotei(str)
    str = envtotei(str)
    str = cmdtotei(str)
    return str
-- 
cgit v1.2.3

