From b3d7ebd008cea5f2c9fc532316ac9051650e67d8 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Sun, 26 Apr 2020 19:47:27 +0200
Subject: convert LaTeX sectioning commands to embedded TEI divs equivalents

---
 ekdosis.dtx | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 80 insertions(+), 5 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 63b3b75..5c142e2 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -1193,6 +1193,10 @@ local note = lpeg.Cs("note")
 local lnbrk = lpeg.Cs("\\\\")
 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"))
 
 -- Bind to local variables
 local next = next
@@ -1661,12 +1665,12 @@ local function envtotei(str)
 	    then
 	       str = gsub(str, (lpeg.P("\\par") + lpeg.P("<p>")) * spcenc^-1 * lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
 			     * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
-			     * bsqbracketsii * bcbracesii * spcenc^-1,
+			     * bcbracesii * spcenc^-1,
 			  "\n<"..envtotags[i].b..envtotags[i].c..">")
 	    else
 	       str = gsub(str, (lpeg.P("\\par") + lpeg.P("<p>")) * spcenc^-1 * lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
 			     * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
-			     * bsqbracketsii * bcbracesii * spcenc^-1,
+			     * bcbracesii * spcenc^-1,
 			  "</p>\n<"..envtotags[i].b..envtotags[i].c..">")
 	    end
 	    str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
@@ -1674,7 +1678,7 @@ local function envtotei(str)
 		       "</"..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,
+			  * 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("}"),
@@ -1682,7 +1686,7 @@ local function envtotei(str)
 	 else
 	    str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
 			  * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
-			  * bsqbracketsii * bcbracesii * spcenc^-1,
+			  * 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("}"),
@@ -1691,7 +1695,7 @@ local function envtotei(str)
       else
 	 str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{")
 		       * lpeg.Cs(envtotags[i].a) * lpeg.P("}")
-		       * bsqbracketsii * bcbracesii * spcenc^-1,
+		       * bcbracesii * spcenc^-1,
 		    "")
 	 str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{")
 		       * lpeg.Cs(envtotags[i].a) * lpeg.P("}"),
@@ -1770,6 +1774,8 @@ local function partotei(str)
    str = string.gsub(str, "<p>%s-</p>", "")
    str = string.gsub(str, "(%<p%>)%s-(%</div%>)$", "%2")
    str = string.gsub(str, "(%<p%>)%s-$", "")
+   str = string.gsub(str, "(<p>)%s-(<div.->)", "%2%1")
+   str = string.gsub(str, "(%<p%>)%s-(<head>.-</head>)", "%2%1")
    return str
 end
 
@@ -1812,6 +1818,72 @@ local function self_close_tags(str)
    return str
 end
 
+local divdepth = {
+   book = 0,
+   part = 1,
+   chapter = 2,
+   section = 3,
+   subsection = 4,
+   subsubsection = 5,
+   paragraph = 6,
+   subparagraph = 7
+}
+
+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,
+	      function(bkslash, secname, space, opt, arg)
+		 ctr = divdepth[secname]
+		 arg = string.sub(arg, 2, -2)
+		 -- return string.format("\\par <div%s type=\"%s\" depth=\"%s\"><head>%s</head>",
+		 -- 		      ctr, secname, ctr, arg)
+		 return string.format("\\par <div%s type=\"%s\"><head>%s</head>",
+				      ctr, secname, arg)
+   end)
+   return str
+end
+
+local function close_divs_at_end(str)
+   local isdiv = false
+   if string.find(str, "</div>$")
+   then
+      isdiv = true
+      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 ""
+   local closedivs = ""
+   if tonumber(firstdiv)
+   then
+      while lastdiv >= firstdiv
+      do
+	 closedivs = closedivs.."</div"..lastdiv..">"
+	 lastdiv = lastdiv - 1
+      end
+   end
+--   str = string.gsub(str, "(<div)(%d)(.->)", "%1%3")
+   if isdiv
+   then
+      return str..closedivs.."</div>"
+   else
+      return str..closedivs
+   end
+end
+
+local function close_divs_in_between(str)
+   for i = 0, 9
+   do
+      str = string.gsub(str, "(<div"..i..".->)(.-)(<div"..i..".->)", function(divi, between, divii)
+			   between = close_divs_in_between(between)
+			   return string.format("%s%s</div%s>%s", divi, between, i, divii)
+      end)
+   end
+   return str
+end
+
 local function textotei(str)
    str = xml_entities(str)
    str = texpatttotei(str)
@@ -1821,10 +1893,13 @@ local function textotei(str)
    str = relocate_notes(str)
    str = versetotei(str)
    str = envtotei(str)
+   str = section_totei(str)
    str = cmdtotei(str)
    str = self_close_tags(str)
    str = partotei(str)
    str = checkpars(str)
+   str = close_divs_at_end(str)
+   str = close_divs_in_between(str)
    return str
 end
 
-- 
cgit v1.2.3

