From 2dffe397f9a027ae551037e6f1c0b27e2350bf08 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 28 Apr 2020 16:51:04 +0200
Subject: handle inconsistently numbered divs

---
 ekdosis.dtx | 62 +++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 44 insertions(+), 18 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 5ec9df1..e46a6d5 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -1843,7 +1843,9 @@ local function section_totei(str)
    return str
 end
 
-local function close_divs_at_end(str)
+local used_ndivs = {}
+
+local function close_ndivs_at_end(str)
    local isdiv = false
    if string.find(str, "</div>$")
    then
@@ -1851,17 +1853,36 @@ local function close_divs_at_end(str)
       str = string.gsub(str, "(.*)(</div>)$", "%1")
    else
    end
-   firstdiv = string.match(str, "<div%d") or ""
-   firstdiv = tonumber(string.match(firstdiv, "%d")) or ""
-   lastdiv = string.match(string.reverse(str), "%dvid<") or ""
-   lastdiv = tonumber(string.match(lastdiv, "%d")) or ""
+   -- collect used div numbers
+   for i in string.gmatch(str, "<div%d")
+   do
+      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%d") or ""
+   firstdiv = string.match(firstdiv, "%d") or ""
+   local lastdiv = string.match(string.reverse(str), "%dvid<") 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 tonumber(firstdiv)
+   if isintable(used_ndivs, firstdiv)
    then
-      while lastdiv >= firstdiv
+      while lastdivindex >= firstdivindex
       do
-	 closedivs = closedivs.."</div"..lastdiv..">"
-	 lastdiv = lastdiv - 1
+	 closedivs = closedivs.."</div"..used_ndivs[lastdivindex].a..">"
+	 lastdivindex = lastdivindex - 1
       end
    end
 --   str = string.gsub(str, "(<div)(%d)(.->)", "%1%3")
@@ -1873,20 +1894,24 @@ local function close_divs_at_end(str)
    end
 end
 
-local function close_divs_in_between(str)
+local function close_ndivs_in_between(str)
    for ndivi = 0, 9
    do
       str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)(%d)(.->)",
 			function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
-			   firstdiv = tonumber(ndivi)
-			   lastdiv = tonumber(ndivii)
+			   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 firstdiv >= lastdiv
+			   if firstdivindex >= lastdivindex
 			   then
-			      while firstdiv >= lastdiv
+			      while firstdivindex >= lastdivindex
 			      do
-				 closedivs = closedivs.."</div"..firstdiv..">"
-				 firstdiv = firstdiv - 1
+			   	 closedivs = closedivs.."</div"..used_ndivs[firstdivindex].a..">"
+			   	 firstdivindex = firstdivindex - 1
 			      end
 			   end
 			   return string.format("%s%s%s%s%s%s%s%s",
@@ -1894,6 +1919,7 @@ local function close_divs_in_between(str)
 			
    end)
    end
+   used_ndivs = {}
    return str
 end
 
@@ -1911,8 +1937,8 @@ local function textotei(str)
    str = self_close_tags(str)
    str = partotei(str)
    str = checkpars(str)
-   str = close_divs_at_end(str)
-   str = close_divs_in_between(str)
+   str = close_ndivs_at_end(str)
+   str = close_ndivs_in_between(str)
    return str
 end
 
-- 
cgit v1.2.3

