From 5a2b496bee2c50ba6f8a6cc83cc78de4ea6d44b1 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Sat, 18 Apr 2020 00:47:32 +0200
Subject: improved partotei() and added checkpars()

---
 ekdosis.dtx | 44 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 10 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 9bc6304..fa7c9d5 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -1069,7 +1069,7 @@ Running "make install" installs the files in the local TeX tree.
     \EkdosisOff
     % \EkdosisColStop
   \endrunninglinenumbers%
-  \iftei@export\luadirect{ekdosis.exporttei(\luastringN{#1})}\else\fi}
+  \iftei@export\luadirect{ekdosis.exporttei(\luastringN{\par#1\par})}\else\fi}
 %    \end{macrocode}
 % Alignment:---
 %    \begin{macrocode}
@@ -1637,16 +1637,16 @@ local function envtotei(str)
 	 		  * 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("}") * spcenc^-1 * lpeg.P("\\par"),
+	    	       "</"..envtotags[i].b..">\n<p>")
 	    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("}" * spcenc^-1 * lpeg.P("\\par")),
-	 	       "</"..envtotags[i].b..">\n<p>")
 	    str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
 	 		  * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
-	 	       "</"..envtotags[i].b..">")
+	 	       "</"..envtotags[i].b..">\n<p>")
 	 else
 	    str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
 			  * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
@@ -1741,6 +1741,28 @@ local function partotei(str)
    return str
 end
 
+local function checkpars(str)
+   for i = 1,#envtotags
+   do
+      if isfound(close_p, envtotags[i].b)
+      then
+	 str = string.gsub(str, "(%<p[%>%s])(.-<"..envtotags[i].b.."[%>%s])", function(par, substr_tag)
+			      substr_tag = checkpars(substr_tag)
+			      substr = string.gsub(substr_tag, "(.-)(<"..envtotags[i].b.."[%>%s])", "%1")
+			      tag = string.gsub(substr_tag, "(.-)(<"..envtotags[i].b.."[%>%s])", "%2")
+			      if string.find(substr, "<%/p[%>%s]")
+			      then
+				 return string.format("%s%s%s", par, substr, tag)
+			      else
+				 return string.format("%s%s</p>%s", par, substr, tag)
+			      end
+	 end)
+      else
+      end
+   end
+   return str
+end
+
 local function textotei(str)
    str = xml_entities(str)
    str = texpatttotei(str)
@@ -1758,6 +1780,7 @@ local function textotei(str)
    str = envtotei(str)
    str = cmdtotei(str)
    str = partotei(str)
+   str = checkpars(str)
    return str
 end
 
@@ -1856,8 +1879,9 @@ local tidy = nil
 local function cleanup_tei()
    local f = assert(io.open(teifilename.."_tmp.xml", "r"))
    t = f:read("*a")
-   t = string.gsub(t, "%<p%>%</p%>\n", "")
+   t = string.gsub(t, "%<p%>%s?%</p%>", "")
    t = string.gsub(t, "^\n", "")
+   f:close()
    local fw = assert(io.open(teifilename.."_tmp.xml", "w"))
    fw:write(t)
    fw:close()
@@ -2061,7 +2085,7 @@ function ekdosis.mkenv()
 		         .."\\end{nthcolumn}"
 			 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
 			 .. aligned_texts[i].column
-			 ..", \\luastringN{#1})}\\fi"
+			 ..", \\luastringN{\\par#1\\par})}\\fi"
 			 .."}")
 	 table.insert(environments, "\\NewDocumentEnvironment{".. aligned_texts[i].text.."*}{+b}"
 			 .."{\\begin{nthcolumn*}{".. aligned_texts[i].column.."}[]"
@@ -2073,7 +2097,7 @@ function ekdosis.mkenv()
 		         .."\\end{nthcolumn*}"
 			 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
 			 .. aligned_texts[i].column
-			 ..", \\luastringN{#1})}\\fi"
+			 ..", \\luastringN{\\par#1\\par})}\\fi"
 			 .."}")
       else
 	 table.insert(environments, "\\NewDocumentEnvironment{".. aligned_texts[i].text.."}{+b}"
@@ -2083,7 +2107,7 @@ function ekdosis.mkenv()
 		         .."}{\\end{nthcolumn}"
 			 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
 			 .. aligned_texts[i].column
-			 ..", \\luastringN{#1})}\\fi"
+			 ..", \\luastringN{\\par#1\\par})}\\fi"
 			 .."}")
 	 table.insert(environments, "\\NewDocumentEnvironment{".. aligned_texts[i].text.."*}{+b}"
 			 .."{\\begin{nthcolumn*}{"..aligned_texts[i].column.."}[]"
@@ -2093,7 +2117,7 @@ function ekdosis.mkenv()
 		         .."\\end{nthcolumn*}"
 			 .."\\csname iftei@export\\endcsname\\luadirect{ekdosis.storecoldata("
 			 .. aligned_texts[i].column
-			 ..", \\luastringN{#1})}\\fi"
+			 ..", \\luastringN{\\par#1\\par})}\\fi"
 			 .."}")
       end
    end
-- 
cgit v1.2.3

