From fc5bbc24e2d83973aec0e74a6f35f99f5c3b15f1 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Wed, 6 May 2020 22:21:14 +0200
Subject: added close_ekddivs_at_end(), close_ekddivs_in_between()

---
 ekdosis.dtx | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 92 insertions(+), 4 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 41025b5..a30f801 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -1080,7 +1080,7 @@ Running "make install" installs the files in the local TeX tree.
     \EkdosisOff
     % \EkdosisColStop
   \endrunninglinenumbers%
-  \iftei@export\luadirect{ekdosis.exporttei(\luastringN{\par#1\par})}\else\fi}
+  \iftei@export\luadirect{ekdosis.exporttei(\luastringN{\par #1\par })}\else\fi}
 %    \end{macrocode}
 % Alignment:---
 %    \begin{macrocode}
@@ -1990,8 +1990,13 @@ local function ekddivs_totei(str)
 		    --
 		    if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
 		    if tein ~= "" then tein = " n=\""..tein.."\"" else end
-		    if teidepth ~= "" then teidepth = " depth=\""..teidepth.."\"" else end
-		    return string.format("<div%s%s%s/><head>%s</head>",
+		    if teidepth ~= ""
+		    then
+		       teidepth = " depth=\""..teidepth.."\""
+		    else
+		       teidepth = " depth=\"1\""
+		    end
+		    return string.format("\\p@rb <div%s%s%s><head>%s</head>\\p@ra ",
 					 teitype, tein, teidepth, teihead)
 		 else
 		    return ""
@@ -2019,6 +2024,58 @@ end
 
 local used_ndivs = {}
 
+local function close_ekddivs_at_end(str)
+   local isdiv = false
+   if string.find(str, "</div>$")
+   then
+      isdiv = true
+      str = string.gsub(str, "(.*)(</div>)$", "%1")
+   else
+   end
+   -- collect used depth numbers
+   for i in string.gmatch(str, "<div .-depth%=\"%d\".->")
+   do
+      i = string.match(i, "depth=\"%d\"")
+      i = string.match(i, "%d")
+      if isintable(used_ndivs, i)
+      then
+      else
+	 table.insert(used_ndivs, {a = i} )
+      end
+   end
+   if next(used_ndivs) ~= nil
+   then
+      table.sort(used_ndivs, function(a ,b) return(#a.a > #b.a) end)
+   else
+   end
+   local firstdiv = string.match(str, "<div .-depth%=\"%d\".->") or ""
+   firstdiv = string.match(firstdiv, "depth%=\"%d\"") or ""
+   firstdiv = string.match(firstdiv, "%d") or ""
+   local lastdiv = string.match(string.reverse(str), ">.-\"%d\"%=htped.- vid<") or ""
+   lastdiv = string.match(lastdiv, "\"%d\"%=htped") or ""
+   lastdiv = string.match(lastdiv, "%d") or ""
+   local firstdivindex = get_a_index(firstdiv, used_ndivs)
+   local lastdivindex = get_a_index(lastdiv, used_ndivs)
+   firstdivindex = tonumber(firstdivindex)
+   lastdivindex = tonumber(lastdivindex)
+   local closedivs = ""
+   if isintable(used_ndivs, firstdiv)
+   then
+      while lastdivindex >= firstdivindex
+      do
+	 closedivs = closedivs.."</div>"
+	 lastdivindex = lastdivindex - 1
+      end
+   end
+--   str = string.gsub(str, "(<div)([1-6])(.->)", "%1%3")
+   if isdiv
+   then
+      return str..closedivs.."</div>"
+   else
+      return str..closedivs
+   end
+end
+
 local function close_ndivs_at_end(str)
    local isdiv = false
    if string.find(str, "</div>$")
@@ -2068,8 +2125,37 @@ local function close_ndivs_at_end(str)
    end
 end
 
+local function close_ekddivs_in_between(str)
+   for ndivi = 1, 7
+   do
+      str = string.gsub(str, "(<div .-depth%=\")("..ndivi..")(\".->)(.-)(<div .-depth%=\")(%d)(\".->)",
+			function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
+			   local firstdiv = ndivi
+			   local lastdiv = ndivii
+			   local firstdivindex = get_a_index(firstdiv, used_ndivs)
+			   local lastdivindex = get_a_index(lastdiv, used_ndivs)
+			   firstdivindex = tonumber(firstdivindex)
+			   lastdivindex = tonumber(lastdivindex)
+			   local closedivs = ""
+			   if firstdivindex >= lastdivindex
+			   then
+			      while firstdivindex >= lastdivindex
+			      do
+				 closedivs = closedivs.."</div>"
+				 firstdivindex = firstdivindex - 1
+			      end
+			   end
+			   return string.format("%s%s%s%s%s%s%s%s",
+						bdivi, ndivi, edivi, between, closedivs, bdivii, ndivii, edivii)
+      end)
+   end
+   str = string.gsub(str, "(<div.-)(%sdepth%=\"%d\")(.->)", "%1%3")
+   used_ndivs = {}
+   return str
+end
+
 local function close_ndivs_in_between(str)
-   for ndivi = 0, 9
+   for ndivi = 0, 6
    do
       str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)([1-6])(.->)",
 			function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
@@ -2114,6 +2200,8 @@ local function textotei(str)
 --   str = checkpars(str)
    if ekddivs
    then
+      str = close_ekddivs_at_end(str)
+      str = close_ekddivs_in_between(str)
    else
       str = close_ndivs_at_end(str)
       str = close_ndivs_in_between(str)
-- 
cgit v1.2.3

