From 216a8a300531d01c1fc8d206a750b249c527e8bb Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 28 Apr 2020 21:05:20 +0200
Subject: new function \MkBodyDivs

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

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index e46a6d5..2b47132 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -1137,6 +1137,20 @@ Running "make install" installs the files in the local TeX tree.
   \luadirect{ekdosis.flushcolnums()}
   }
 %    \end{macrocode}
+% Divisions of the Body
+%    \begin{macrocode}
+\NewDocumentCommand{\MkBodyDivs}{mmmmmm}{
+  \luadirect{ekdosis.mkdivdepths(
+    \luastringN{#1},
+    \luastringN{#2},
+    \luastringN{#3},
+    \luastringN{#4},
+    \luastringN{#5},
+    \luastringN{#6}
+    )
+  }
+}
+%    \end{macrocode}
 % Very basic implementation of poetry lines:---
 %    \begin{macrocode}
 \newlength{\ekdverseindentlength}
@@ -1195,8 +1209,7 @@ local poemline = lpeg.Cs(lnbrk * bsqbrackets^-1)
 local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1)
 local sections = lpeg.Cs(lpeg.P("book") + lpeg.P("part") + lpeg.P("chapter")
 			    + lpeg.P("section") + lpeg.P("subsection")
-			    + lpeg.P("subsubsection") + lpeg.P("paragraph")
-			    + lpeg.P("subparagraph"))
+			    + lpeg.P("subsubsection"))
 
 -- Bind to local variables
 local next = next
@@ -1819,16 +1832,29 @@ local function self_close_tags(str)
 end
 
 local divdepth = {
-   book = 0,
-   part = 1,
-   chapter = 2,
-   section = 3,
-   subsection = 4,
-   subsubsection = 5,
-   paragraph = 6,
-   subparagraph = 7
+   book = 1,
+   part = 2,
+   chapter = 3,
+   section = 4,
+   subsection = 5,
+   subsubsection = 6
 }
 
+function ekdosis.mkdivdepths(...)
+   divdepth = {}
+   local num = 1
+   for _, y in ipairs{...}
+   do
+      if y == "book" or "part" or "chapter" or "section" or "subsection" or "subsubsection"
+      then
+	 divdepth[y] = num
+	 num = num + 1
+      else
+      end
+   end
+   return true
+end
+
 local function section_totei(str)
    str = gsub(str, dblbkslash * sections * spce^-1 * bcbraces, "%1%2%3[]%4")
    str = gsub(str, dblbkslash * sections * spce^-1 * bsqbrackets * bcbraces,
@@ -1854,9 +1880,9 @@ local function close_ndivs_at_end(str)
    else
    end
    -- collect used div numbers
-   for i in string.gmatch(str, "<div%d")
+   for i in string.gmatch(str, "<div[1-6]")
    do
-      i = string.match(i, "%d")
+      i = string.match(i, "[1-6]")
       if isintable(used_ndivs, i)
       then
       else
@@ -1868,10 +1894,10 @@ local function close_ndivs_at_end(str)
       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 firstdiv = string.match(str, "<div[1-6]") or ""
+   firstdiv = string.match(firstdiv, "[1-6]") or ""
+   local lastdiv = string.match(string.reverse(str), "[1-6]vid<") or ""
+   lastdiv = string.match(lastdiv, "[1-6]") or ""
    local firstdivindex = get_a_index(firstdiv, used_ndivs)
    local lastdivindex = get_a_index(lastdiv, used_ndivs)
    firstdivindex = tonumber(firstdivindex)
@@ -1885,7 +1911,7 @@ local function close_ndivs_at_end(str)
 	 lastdivindex = lastdivindex - 1
       end
    end
---   str = string.gsub(str, "(<div)(%d)(.->)", "%1%3")
+--   str = string.gsub(str, "(<div)([1-6])(.->)", "%1%3")
    if isdiv
    then
       return str..closedivs.."</div>"
@@ -1897,7 +1923,7 @@ end
 local function close_ndivs_in_between(str)
    for ndivi = 0, 9
    do
-      str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)(%d)(.->)",
+      str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)([1-6])(.->)",
 			function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii)
 			   local firstdiv = ndivi
 			   local lastdiv = ndivii
-- 
cgit v1.2.3

