From a9765ab2db29434f92475d2cc6675b7b651ce9b1 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Sat, 2 May 2020 20:05:56 +0200
Subject: started programming \ekddiv

---
 ekdosis.dtx | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 177 insertions(+), 11 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 2b47132..8354459 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -316,6 +316,17 @@ Running "make install" installs the files in the local TeX tree.
   \ifx\@tempa\layout@float\@pkg@floattrue\fi
   \ifx\@tempa\layout@footins\@pkg@floatfalse\@pkg@footinstrue\fi
 }
+\newif\if@pkg@ekddivs
+\define@choicekey+{ekdosis.sty}{divs}{ekdosis, latex}[ekdosis]{
+  \edef\divs@ekdosis{ekdosis}
+  \edef\divs@latex{latex}
+  \edef\@tempa{#1}
+  \ifx\@tempa\divs@ekdosis\@pkg@ekddivstrue\fi
+  \ifx\@tempa\divs@latex
+    \@pkg@ekddivsfalse
+    \AtBeginDocument{\luadirect{ekdosis.setekddivsfalse()}}
+  \fi
+}{\PackageError{ekdosis}{divs option must be either 'ekdosis' or 'latex'}}
 \newif\if@parnotesroman
 \newif\if@pkg@parnotes
 \define@choicekey+{ekdosis.sty}{parnotes}{true, false, roman}[true]{%
@@ -345,7 +356,7 @@ Running "make install" installs the files in the local TeX tree.
   \AtBeginDocument{\luadirect{ekdosis.openteistream()}}%
   \AtEndDocument{\luadirect{ekdosis.closeteistream("tidy")}}
   \else\fi\fi\fi}
-\ExecuteOptionsX{layout}
+\ExecuteOptionsX{layout,divs}
 \ProcessOptionsX\relax
 \RequirePackage{luacode}
 \RequirePackage{paracol}
@@ -1083,7 +1094,7 @@ Running "make install" installs the files in the local TeX tree.
   initial tcols = 2,
   initial lcols = 1,
   initial texts = edition;translation,
-  initial apparatus = edition,
+  initial apparatus = edition
 }
 \NewDocumentCommand{\SetEkdosisAlignment}{m}{
   \ekvset{ekd@align}{#1}
@@ -1151,6 +1162,52 @@ Running "make install" installs the files in the local TeX tree.
   }
 }
 %    \end{macrocode}
+% Divisions specific to ekdosis:---
+%    \begin{macrocode}
+\ekvdefinekeys{ekd@div}{
+  code type = \def\type@value{#1},
+  code n = \def\n@value{#1},
+  code head = \def\head@value{#1},
+  code barehead = \def\barehead@value{#1},
+  store depth = \depth@value,
+  choice toc = {book = \def\toc@value{book},
+                part = \def\toc@value{part},
+                chapter = \def\toc@value{chapter},
+                section = \def\toc@value{section},
+                subsection = \def\toc@value{subsection},
+                subsubsection = \def\toc@value{subsubsection},
+                paragraph = \def\toc@value{paragraph},
+                subparagraph = \def\toc@value{subparagraph}},
+  initial depth = 1
+}
+
+\NewDocumentCommand{\ekdfmtdiv}{m m m}{
+  \luadirect{ekdosis.fmtdiv(\luastring{#1}, \luastringN{#2}, \luastringN{#3})}
+}
+\NewDocumentCommand{\ekd@getfmtdiv}{m m}{%
+  \luadirect{tex.sprint(ekdosis.getfmtdiv(\luastringO{#1}, \luastringN{#2}))}%
+}
+\NewDocumentCommand{\ekddiv}{m}{
+  \begingroup
+  \ekvset{ekd@div}{#1}%
+  \ifdefined\head@value
+    \bgroup
+      \ekd@getfmtdiv{\depth@value}{b}%
+      \head@value
+      \ekd@getfmtdiv{\depth@value}{e}%
+    \egroup
+      \ifdefined\toc@value
+        \ltx@ifpackageloaded{hyperref}{\phantomsection}{}%
+          \ifdefined\barehead@value
+            \addcontentsline{toc}{\toc@value}{\barehead@value}%
+          \else
+            \addcontentsline{toc}{\toc@value}{\head@value}%
+          \fi
+      \fi
+  \fi
+  \endgroup
+}
+%    \end{macrocode}
 % Very basic implementation of poetry lines:---
 %    \begin{macrocode}
 \newlength{\ekdverseindentlength}
@@ -1845,7 +1902,8 @@ function ekdosis.mkdivdepths(...)
    local num = 1
    for _, y in ipairs{...}
    do
-      if y == "book" or "part" or "chapter" or "section" or "subsection" or "subsubsection"
+      if y == "book" or "part" or "chapter"
+	 or "section" or "subsection" or "subsubsection"
       then
 	 divdepth[y] = num
 	 num = num + 1
@@ -1855,16 +1913,119 @@ function ekdosis.mkdivdepths(...)
    return true
 end
 
+-- LaTeX side: format the divisions
+local fmtdiv = {}
+
+function ekdosis.fmtdiv(n, fmtb, fmte)
+   if isintable(fmtdiv, n)
+   then
+      local index = get_a_index(n, fmtdiv)
+      table.remove(fmtdiv, index)
+   else
+   end
+   table.insert(fmtdiv, { a = n, formatb = fmtb, formate = fmte} )
+   return true
+end
+
+function ekdosis.getfmtdiv(n, pos)
+   local index = get_a_index(n, fmtdiv)
+   if index ~= nil
+   then
+      if pos == "b"
+      then
+	 return fmtdiv[index].formatb
+      elseif pos == "e"
+      then
+	 return fmtdiv[index].formate
+      end
+   else
+      return ""
+   end
+end
+
+local ekddivs = true
+
+function ekdosis.setekddivsfalse()
+   ekddivs = false
+end
+
+--[[
+   teitype = string.match(opt, "%f[%w]type%s?%=%s?%b{}")
+   or string.match(opt, "%f[%w]type%s?%=%s?%w+%f[%W]")
+   or ""
+   teitype = string.gsub(teitype, "type%s?%=%s?(%b{})", function(bbraces)
+   bbraces = string.sub(bbraces, 2, -2)
+   return string.format("%s", bbraces)
+   end)
+   teitype = string.gsub(teitype, "(type%s?%=%s?)(%w+%f[%W])", "%2")
+--]]
+
+local function ekddivs_totei(str)
+   str = gsub(str, dblbkslash * lpeg.Cs("ekddiv") * spce^-1 * bcbraces,
+	      function(bkslash, cmd, space, arg)
+		 if ekddivs
+		 then
+		    arg = string.sub(arg, 2, -2)
+		    arg = arg..","
+		    --
+		    teitype = string.match(arg, "%f[%w]type%s?%=%s?%b{}")
+		       or string.match(arg, "%f[%w]type%s?%=%s?[%w%s]+%,")
+		       or ""
+		    teitype = string.gsub(teitype, "type%s?%=%s?(%b{})", function(bbraces)
+					     bbraces = string.sub(bbraces, 2, -2)
+					     return string.format("%s", bbraces)
+		    end)
+		    teitype = string.gsub(teitype, "(type%s?%=%s?)([%w%s]+)(%,)", "%2")
+		    --
+		    tein = string.match(arg, "%f[%w]n%s?%=%s?%b{}")
+		       or string.match(arg, "%f[%w]n%s?%=%s?[%w%s]+%,")
+		       or ""
+		    tein = string.gsub(tein, "n%s?%=%s?(%b{})", function(bbraces)
+					  bbraces = string.sub(bbraces, 2, -2)
+					  return string.format("%s", bbraces)
+		    end)
+		    tein = string.gsub(tein, "(n%s?%=%s?)([%w%s]+)(%,)", "%2")
+		    --
+		    teihead = string.match(arg, "%f[%w]head%s?%=%s?%b{}")
+		       or string.match(arg, "%f[%w]head%s?%=%s?[%w%s]+%,")
+		       or ""
+		    teihead = string.gsub(teihead, "head%s?%=%s?(%b{})", function(bbraces)
+					     bbraces = string.sub(bbraces, 2, -2)
+					     return string.format("%s", bbraces)
+		    end)
+		    teihead = string.gsub(teihead, "(head%s?%=%s?)([%w%s]+)(%,)", "%2")
+		    --
+		    teidepth = string.match(arg, "%f[%w]depth%s?%=%s?%b{}")
+		       or string.match(arg, "%f[%w]depth%s?%=%s?[%w%s]+%,")
+		       or ""
+		    teidepth = string.gsub(teidepth, "depth%s?%=%s?(%b{})", function(bbraces)
+					      bbraces = string.sub(bbraces, 2, -2)
+					      return string.format("%s", bbraces)
+		    end)
+		    teidepth = string.gsub(teidepth, "(depth%s?%=%s?)([%w%s]+)(%,)", "%2")
+		    --
+		    return string.format("\\par <div type=\"%s\" n=\"%s\" depth=\"%s\"/><head>%s</head>",
+					 teitype, tein, teidepth, teihead)
+		 else
+		    return ""
+		 end
+   end)
+   return str
+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,
 	      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)
+		 if ekddivs
+		 then
+		    return ""
+		 else
+		    ctr = divdepth[secname]
+		    arg = string.sub(arg, 2, -2)
+		    return string.format("\\par <div%s type=\"%s\"><head>%s</head>",
+					 ctr, secname, arg)
+		 end
    end)
    return str
 end
@@ -1958,13 +2119,18 @@ local function textotei(str)
    str = relocate_notes(str)
    str = versetotei(str)
    str = envtotei(str)
+   str = ekddivs_totei(str)
    str = section_totei(str)
    str = cmdtotei(str)
    str = self_close_tags(str)
    str = partotei(str)
    str = checkpars(str)
-   str = close_ndivs_at_end(str)
-   str = close_ndivs_in_between(str)
+   if ekddivs
+   then
+   else
+      str = close_ndivs_at_end(str)
+      str = close_ndivs_in_between(str)
+   end
    return str
 end
 
-- 
cgit v1.2.3

