From a7ed6e42996771616d6810bb8e350c1f55d294a3 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Fri, 26 Apr 2019 14:18:12 +0200
Subject: new command \EnvtoTEI. check if <p> is open or not before inserting
 any other <p>

---
 ekdosis.dtx | 97 +++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 78 insertions(+), 19 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 93679ea..2d4579c 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -281,8 +281,8 @@ Running "make install" installs the files in the local TeX tree.
 %     \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);
+%     (0,3) {\scriptsize $\approx$ \unit[25]{pt}}; \draw [->] (0,2.9)
+%     -- (0,2);
 %   \end{tikzpicture}
 % \end{keyfigure}
 % 
@@ -425,6 +425,11 @@ idno={}, msName={}, origDate={}][3]{%
     \luastringN{#2},
     \luastringN{#3})}
 }
+\NewDocumentCommand{\EnvtoTEI}{m m m}{%
+  \luadirect{ekdosis.newenvtotag(\luastringN{#1},
+    \luastringN{#2},
+    \luastringN{#3})}
+}
 %    \end{macrocode}
 % Set |TEI| file name
 %    \begin{macrocode}
@@ -610,7 +615,8 @@ choice nordg={false,,true}][1]{%
   \IfBooleanTF{#1}{\ekd@note@star[#2]{#3}}
   {\ekd@note[#2]{#3}}%
 }
-\NewDocumentCommand{\apparatus}{}{\luadirect{tex.sprint(ekdosis.appout())}}
+\NewDocumentCommand{\apparatus}{}{%
+  \luadirect{tex.sprint(ekdosis.appout())}}
 \NewDocumentCommand{\test@apparatus}{}{%
   \luadirect{tex.sprint(ekdosis.testapparatus())}}
 \NewDocumentCommand{\EkdosisStart}{}{%
@@ -668,6 +674,9 @@ 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"))
 
+-- Booleans
+local tei_p_open = false
+
 -- General
 xmlids = {}
 
@@ -764,7 +773,8 @@ function ekdosis.newhand(id, witid, siglum, description)
 	 listWit[indexwit].handDesc = {}
       else
       end
-      table.insert(listWit[indexwit].handDesc, {xmlid = id, abbr = siglum, handNote = description})
+      table.insert(listWit[indexwit].handDesc,
+		   {xmlid = id, abbr = siglum, handNote = description})
    end
    return true
 end
@@ -819,7 +829,8 @@ function ekdosis.getsiglum(str, opt)
 	 str = string.gsub(str, shorthands[i].a, shorthands[i].c)
       end
       for i = 1,#idsRend do
-	 str  = string.gsub(str, "(%f[%w])"..idsRend[i].xmlid.."(%,)", "%1#"..idsRend[i].xmlid.."%2")
+	 str  = string.gsub(str, "(%f[%w])"..idsRend[i].xmlid.."(%,)",
+			    "%1#"..idsRend[i].xmlid.."%2")
 	 ctrl = string.gsub(ctrl, idsRend[i].xmlid.."%,", "")
       end
       str = string.gsub(str, "%,(%s-)([%#])", " %2")
@@ -851,12 +862,14 @@ local cmdtotags = {
    {a="textsf", b="hi", c=" rend=\"sf\""},
    {a="arbup", b="hi", c=" rend=\"sup\""},
    {a="txarb", b="s", c=" xml:lang=\"arb\""},
-   {a="arb", b="s", c=" xml:lang=\"arb-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
+   {a="arb", b="s",
+    c=" xml:lang=\"arb-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
 }
 
 local envtotags = {
    {a="center", b="p", c=" rend=\"centered\""},
-   {a="arab", b="p", c=" xml:lang=\"arb-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
+   {a="arab", b="p",
+    c=" xml:lang=\"arb-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
 }
 
 function ekdosis.newcmdtotag(cmd, tag, attr)
@@ -874,6 +887,21 @@ function ekdosis.newcmdtotag(cmd, tag, attr)
    return true
 end
 
+function ekdosis.newenvtotag(env, tag, attr)
+   if isintable(envtotags, env)
+   then
+      tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
+      	 ..cmd..
+      	 [[" already exists as a known environment to be processed to TEI. ]]
+      	 ..
+      	 [[Please pick another environment name.}}]])
+   else
+      table.insert(envtotags, {a = env, b = tag, c = " "..attr})
+      table.sort(envtotags, function(a ,b) return(#a.a > #b.a) end)
+   end
+   return true
+end
+
 local function xml_entities(str)
    str = string.gsub(str, "%<", "&lt;")
    str = string.gsub(str, "%>", "&gt;")
@@ -881,7 +909,13 @@ local function xml_entities(str)
 end
 
 local function lem_rdg_totei(str)
-   str = gsub(str, dblbkslash * lemrdg * spcenc^-1 * bsqbrackets * bcbraces * spcenc^-1,
+   str = gsub(str,
+	      dblbkslash *
+		 lemrdg *
+		 spcenc^-1 *
+		 bsqbrackets *
+		 bcbraces *
+		 spcenc^-1,
 	      function(bkslash, cmd, opt, arg)
 		 opt = string.sub(opt, 2, -2)
 		 arg = string.sub(arg, 2, -2)
@@ -891,7 +925,8 @@ local function lem_rdg_totei(str)
 				      bbraces = ekdosis.getsiglum(bbraces, "tei")
 				      return string.format("%s", bbraces)
 		 end)
-		 return string.format("<%s wit=\"%s\">%s</%s>", cmd, opt, arg, cmd)
+		 return string.format("<%s wit=\"%s\">%s</%s>",
+				      cmd, opt, arg, cmd)
    end)
    return str
 end
@@ -899,13 +934,24 @@ end
 local function envtotei(str)
    for i = 1,#envtotags
    do
-      str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
-		    * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
-		    * bsqbracketsii * bcbracesii * spcenc^-1,
-      		 "<"..envtotags[i].b..envtotags[i].c..">")
-      str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
-		    * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
-      		 "</"..envtotags[i].b..">")
+      if tei_p_open and envtotags[i].b == "p"
+      then
+	 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
+		       * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
+		       * bsqbracketsii * bcbracesii * spcenc^-1,
+		    "</p>\n<"..envtotags[i].b..envtotags[i].c..">")
+	 str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
+		       * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
+		    "</"..envtotags[i].b..">\n<p>")
+      else
+	 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
+		       * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
+		       * bsqbracketsii * bcbracesii * spcenc^-1,
+		    "<"..envtotags[i].b..envtotags[i].c..">")
+	 str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
+		       * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
+		    "</"..envtotags[i].b..">")
+      end
    end
    str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
 		 * lpeg.Cs(ascii^1) * lpeg.P("}") * bsqbracketsii
@@ -932,7 +978,8 @@ local function cmdtotei(str)
 			   end)
 			   body = cmdtotei(body)
 			   -- return string.format("<"..cmdtotags[i].b..cmdtotags[i].c.." %s>%s</"..cmdtotags[i].b..">", arg, body)
-			   return string.format("<"..cmdtotags[i].b..cmdtotags[i].c..">%s</"..cmdtotags[i].b..">", body)
+			   return string.format("<"..cmdtotags[i].b..cmdtotags[i].c..">%s</" ..
+						   cmdtotags[i].b..">", body)
       end)
    end
    str = string.gsub(str, "\\(%a+)%s?%*?(%b[])(%b{})",
@@ -953,7 +1000,13 @@ end
 
 function ekdosis.textotei(str)
    str = xml_entities(str)
-   str = string.gsub(str, "%s?\\par%s?", "</p>\n<p>")
+   if tei_p_open
+   then
+      str = string.gsub(str, "%s?\\par%s?", "</p>\n<p>")
+   else
+      str = string.gsub(str, "%s?\\par%s?", "\n<p>")
+      tei_p_open = true
+   end
    str = lem_rdg_totei(str)
    str = envtotei(str)
    str = cmdtotei(str)
@@ -1096,8 +1149,14 @@ function ekdosis.exporttei(str)
    local f = io.open(teifilename.."_tmp.xml", "a+")
    str = ekdosis.textotei(str)
    f:write("\n<p>")
+   tei_p_open = true
    f:write(str)
-   f:write("</p>")
+   if tei_p_open
+   then
+      f:write("</p>")
+      tei_p_open = false
+   else
+   end
    f:close()
    return true
 end
-- 
cgit v1.2.3

