From c31e7ec1496d22362c3da0d6fc8ef45622cf3cbc Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Wed, 2 Apr 2025 15:43:06 +0200
Subject: robustify for now close_ekd|ndivs_in_between()

---
 ekdosis.dtx | 130 ++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 104 insertions(+), 26 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index bc7eadf..730de36 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -59,7 +59,7 @@ Alessi <alessi@robertalessi.net>
 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
 %<package>\ProvidesPackage{ekdosis}
 %<*package>
-    [2025/03/09 v1.5-dev Typesetting TEI xml-compliant critical editions]
+    [2025/04/02 v1.5-dev Typesetting TEI xml-compliant critical editions]
 %</package>
 %<*driver>
 \begin{filecontents}[noheader,overwrite]{bibdata.xml}
@@ -8520,7 +8520,7 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \paragraph{\textsf{ekdosis} Symbol}
 % \begin{macro}{\eKd}
-% \changes{v1.5}{2025/03/09}{Prints \textsf{ekdosis} indentifying
+% \changes{v1.5}{2025/04/02}{Prints \textsf{ekdosis} indentifying
 % symbol} As of v1.5, \pkg{ekdosis} has its own identifying
 % symbol. It is produced by \cs{eKd} and best printed with the Old
 % Standard Greek font.
@@ -8783,7 +8783,7 @@ Sample text with a \textcolor{red}{word} in red.
 % \begin{macro}{\teidirectE}
 % \changes{v1.3}{2021/08/18}{direct insertion of elements in the
 % \texttt{TEI xml} file}
-% \changes{v1.5}{2025/03/09}{direct insertion of empty elements in the
+% \changes{v1.5}{2025/04/02}{direct insertion of empty elements in the
 % \texttt{TEI xml} file}
 % \cs{teidirect}\oarg{xml attributes}\marg{xml element}\marg{code}
 % does nothing in \LaTeX. It is only used to insert elements in the
@@ -8798,7 +8798,7 @@ Sample text with a \textcolor{red}{word} in red.
 % \end{macro}
 % \end{macro}
 % \begin{macro}{\getTEIxmlid}
-% \changes{v1.5}{2025/03/09}{returns \texttt{TEI xml:ids} from a
+% \changes{v1.5}{2025/04/02}{returns \texttt{TEI xml:ids} from a
 % csv-list of ids} This command returns from a csv-list of unique
 % identifiers declared in commands such as \cs{DeclareWitness} and the
 % like a space-separated list of their corresponding |xml:id|s, each
@@ -9274,7 +9274,7 @@ Sample text with a \textcolor{red}{word} in red.
 \newlength{\ekd@app@localheight}
 %    \end{macrocode}
 % \begin{macro}{\localappheight}
-% \changes{v1.5}{2025/03/09}{changes the height of the apparatus
+% \changes{v1.5}{2025/04/02}{changes the height of the apparatus
 % criticus}
 % \cs{localappheight}\marg{dimen} can be used to change locally the
 % length of \cs{ekd@app@height} set by the |appheight| option of
@@ -9293,7 +9293,7 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \end{macro}
 % \begin{macro}{\addtoappheight}
-% \changes{v1.5}{2025/03/09}{increases or decreases the height of the
+% \changes{v1.5}{2025/04/02}{increases or decreases the height of the
 % apparatus criticus}
 % As the name suggests, in contrast to \cs{localappheight},
 % \cs{addtoappheight}\marg{dimen} is used to increase or decrease
@@ -9582,7 +9582,7 @@ Sample text with a \textcolor{red}{word} in red.
 % The following commands are provided to set and control the maximum
 % number of lines printed on each page.
 % \begin{macro}{\setmaxlines}
-% \changes{v1.5}{2025/03/09}{limits the number of lines per page}
+% \changes{v1.5}{2025/04/02}{limits the number of lines per page}
 % \cs{setmaxlines}\marg{n}, where \meta{n} is a positive integer
 % $\geq 1$, can be used either in the preamble or at any point of the
 % document to set the maximum number of lines to be printed on each
@@ -9593,7 +9593,7 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \end{macro}
 % \begin{macro}{\localmaxlines}
-% \changes{v1.5}{2025/03/09}{changes the maximum number of lines
+% \changes{v1.5}{2025/04/02}{changes the maximum number of lines
 % locally}
 % Once a maximum number of lines per page has been set,
 % \cs{localmaxlines}\marg{n} can be used to adjust this number on a
@@ -9606,7 +9606,7 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \end{macro}
 % \begin{macro}{\addtomaxlines}
-% \changes{v1.5}{2025/03/09}{adds or subtracts lines from a given page}
+% \changes{v1.5}{2025/04/02}{adds or subtracts lines from a given page}
 % Unlike \cs{localmaxlines}, \cs{addtomaxlines}\meta{n} takes as
 % argument the number of lines one wishes to add or substract from the
 % number that has been set by \cs{setmaxlines}. As a result, \meta{n}
@@ -9619,7 +9619,7 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \end{macro}
 % \begin{macro}{\nomaxlines}
-% \changes{v1.5}{2025/03/09}{unsets \cs{setmaxlines}}
+% \changes{v1.5}{2025/04/02}{unsets \cs{setmaxlines}}
 % \cs{nomaxlines} unsets any limit previously set by \cs{setmaxlines}.
 %    \begin{macrocode}
 \def\nomaxlines{\luadirect{tex.sprint(ekdosis.resetlocalmaxlines())}}
@@ -9683,7 +9683,7 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \end{macro}
 % \begin{macro}{\App}
-% \changes{v1.5}{2025/03/09}{To be used conjointly with
+% \changes{v1.5}{2025/04/02}{To be used conjointly with
 % \texttt{ekdosis.el}}
 % In contrast to \cs{app}, \cs{App} takes two mandatory arguments and
 % accepts one optional argument like so:
@@ -10668,7 +10668,7 @@ Sample text with a \textcolor{red}{word} in red.
 }
 %    \end{macrocode}
 % \begin{macro}{\SetFootnotes}
-%   \changes{v1.5}{2025/03/09}{footnotes combined into a single
+%   \changes{v1.5}{2025/04/02}{footnotes combined into a single
 %   paragraph} \cs{SetFootnotes}\marg{options} can be used in the
 %   preamble or at any point of the document. |layout=paragraph|
 %   instructs \pkg{ekdosis} to combine all footnotes into a single
@@ -10899,7 +10899,7 @@ Sample text with a \textcolor{red}{word} in red.
 % \end{macro}
 % \paragraph{Lacunae}
 % \begin{macro}{\ilabel}
-% \changes{v1.5}{2025/03/09}{recalls and sets the ending label of
+% \changes{v1.5}{2025/04/02}{recalls and sets the ending label of
 % lemmas used to mark lacunae in witnesses} When \cs{lem} has been
 % used with the optional argument |ilabel=<label>|,
 % \cs{ilabel}\marg{label} must be used to mark the point where the
@@ -11239,7 +11239,7 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \end{macro}
 % \begin{macro}{\ekdEOprint}
-% \changes{v1.5}{2025/03/09}{Sets headers and footers on
+% \changes{v1.5}{2025/04/02}{Sets headers and footers on
 % \enquote{paired} facing pages}
 % To set headers and footers on \enquote{paired} facing pages,
 % \cs{ekdEOprint} accepts two mandatory, self-evident arguments, like
@@ -11279,7 +11279,7 @@ Sample text with a \textcolor{red}{word} in red.
 % counter to set the value of the page numbers. This counter should be
 % incremented every two pages.
 % \begin{counter}{pairedpage}
-% \changes{v1.5}{2025/03/09}{A counter incremented every two pages}
+% \changes{v1.5}{2025/04/02}{A counter incremented every two pages}
 % |pairedpage| is first set as a global counter:---
 %    \begin{macrocode}
 \newcounter{pairedpage}
@@ -11287,19 +11287,19 @@ Sample text with a \textcolor{red}{word} in red.
 %    \end{macrocode}
 % \end{counter}
 % \begin{macro}{\setpairedpagenum}
-%   \changes{v1.5}{2025/03/09}{sets the same page number on paired
+%   \changes{v1.5}{2025/04/02}{sets the same page number on paired
 %   pages} \cs{setpairedpagenum}\marg{number} is used just ahead of
 %   the alignment environment to set the number of the first left-hand
 %   paired page.
 % \begin{macro}{\setpairedpage}
-%   \changes{v1.5}{2025/03/09}{sets the page number of the first paired
+%   \changes{v1.5}{2025/04/02}{sets the page number of the first paired
 %   page} \cs{setpairedpage} is an argument-less command meant to be
 %   issued in commands used to set headers or footers before
 %   \cs{thepage}. This command has the counter |pairedpage|
 %   incremented on right-hand pages only, and sets |page| $\leftarrow$
 %   |pairedpage| on every page.
 % \begin{macro}{\resetpagenumber}
-%   \changes{v1.5}{2025/03/09}{resets normal running page numbers}
+%   \changes{v1.5}{2025/04/02}{resets normal running page numbers}
 %   \cs{resetpagenumber} must be used right out of \enquote{mirrored}
 %   paired pages alignment environments. This argument-less command
 %   corrects any numbering error on the page following the edition
@@ -13101,6 +13101,7 @@ local function close_ndivs_at_end(str)
    end
 end
 
+--[[ -- this needs to be looked into, maybe in favor of the fn below
 local function close_ekddivs_in_between(str)
    local maxdepth = 1
    for i in string.gmatch(str, "<div.-[Dd]epth=\"(%d)\".->", "%1")
@@ -13138,6 +13139,47 @@ local function close_ekddivs_in_between(str)
    end
    return str
 end
+--]]
+
+---[[
+local function close_ekddivs_in_between(str)
+   local maxdepth = 1
+   for i in string.gmatch(str, "<div.-[Dd]epth=\"(%d)\".->", "%1")
+   do
+      if tonumber(i) > tonumber(maxdepth)
+      then
+	 maxdepth = i
+      else
+      end
+   end
+   for ndivi = 1, maxdepth
+   do
+      str = string.gsub(str, "(<div [^%>]-depth%=\")("..ndivi..")(\".->)(.-)(<div [^%>]-depth%=\")(%d)(\".->)",
+			function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
+			   local firstdiv = tonumber(ndivi)
+			   local firstdivcnt = tonumber(ndivi)
+			   local lastdiv = tonumber(ndivii)
+			   local closedivs = ""
+			   if not string.find(between, "</div>")
+			   then
+			      if firstdiv >= lastdiv
+			      then
+				 while firstdivcnt >= lastdiv
+				 do
+				    closedivs = closedivs.."</div>"
+				    firstdivcnt = firstdivcnt - 1
+				    bdivi = string.gsub(bdivi, "depth", "Depth")
+				 end
+			      end
+			   end
+			   return string.format("%s%s%s%s%s%s%s%s",
+						bdivi, ndivi, edivi, between,
+						closedivs, bdivii, ndivii, edivii)
+      end)
+   end
+   return str
+end
+--]]
 
 local function clean_ekddivs(str)
    str = string.gsub(str, "(<div.-)(%s[Dd]epth%=\"%d\")(.->)", "%1%3")
@@ -13145,10 +13187,11 @@ local function clean_ekddivs(str)
    return str
 end
 
+---[[ -- this needs to be looked into maybe in favor of the fn that follows
 local function close_ndivs_in_between(str)
    for ndivi = 1, 6
    do
-      str = string.gsub(str, "(<[Dd]iv)("..ndivi..")(.->)(.-)(<div)([1-6])(.->)",
+      str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)([1-6])(.->)",
 			function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
 			   local firstdiv = ndivi
 			   local lastdiv = ndivii
@@ -13157,22 +13200,57 @@ local function close_ndivs_in_between(str)
 			   firstdivindex = tonumber(firstdivindex)
 			   lastdivindex = tonumber(lastdivindex)
 			   local closedivs = ""
-			   if firstdivindex >= lastdivindex
+			   if not string.find(between, "</div[1-6]>")
 			   then
-			      while firstdivindex >= lastdivindex
-			      do
-			   	 closedivs = closedivs.."</div"..used_ndivs[firstdivindex].a..">"
-			   	 firstdivindex = firstdivindex - 1
-				 bdivii = string.gsub(bdivii, "div", "Div")
+			      if firstdivindex >= lastdivindex
+			      then
+				 while firstdivindex >= lastdivindex
+				 do
+				    closedivs = closedivs.."</div"..used_ndivs[firstdivindex].a..">"
+				    firstdivindex = firstdivindex - 1
+				    bdivi = string.gsub(bdivi, "div", "Div")
+				 end
 			      end
 			   end
 			   return string.format("%s%s%s%s%s%s%s%s",
 						bdivi, ndivi, edivi, between,
 						closedivs, bdivii, ndivii, edivii)
-   end)
+      end)
    end
    return str
 end
+--]]
+
+--[[
+local function close_ndivs_in_between(str)
+   for ndivi = 1, 6
+   do
+      str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)([1-6])(.->)",
+			function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
+			   local firstdiv = tonumber(ndivi)
+			   local firstdivcnt = tonumber(ndivi)
+			   local lastdiv = tonumber(ndivii)
+			   local closedivs = ""
+			   if not string.find(between, "</div[1-6]>")
+			   then
+			      if firstdiv >= lastdiv
+			      then
+				 while firstdivcnt >= lastdiv
+				 do
+				    closedivs = closedivs.."</div"..firstdiv..">"
+				    firstdivcnt = firstdivcnt - 1
+				    bdivi = string.gsub(bdivi, "div", "Div")
+				 end
+			      end
+			   end
+			   return string.format("%s%s%s%s%s%s%s%s",
+						bdivi, ndivi, edivi, between,
+						closedivs, bdivii, ndivii, edivii)
+      end)
+   end
+   return str
+end
+--]]
 
 local function clean_latexdivs(str)
    str = string.gsub(str, "(<Div)([1-6])(.->)", "<div%2%3")
-- 
cgit v1.2.3

