From c4310a214c7e5b0a04f823918a7521a02f5f7451 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 10 Mar 2020 18:30:36 +0100
Subject: start programming alignment features

---
 ekdosis.dtx | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 100 insertions(+), 1 deletion(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 66ec86d..83b3ec0 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -143,10 +143,11 @@ Running "make install" installs the files in the local TeX tree.
 \babelfont{tt}{NewComputerModern Mono}
 \usepackage{metalogox}
 \usepackage{hologo}
+\usepackage{latexcolors}
 \usepackage{hyperxmp}
 \usepackage{uri}
 \usepackage[numbered]{hypdoc}
-\hypersetup{unicode=true, colorlinks, allcolors=blue,
+\hypersetup{unicode=true, colorlinks, allcolors=cinnamon,
   linktocpage=true, pdfauthor={Robert Alessi}, pdftitle={The
     ekdosis package}, pdfcontactemail={alessi@robertalessi.net},
   pdfcontacturl={http://www.robertalessi.net/ekdosis},
@@ -1065,6 +1066,33 @@ Running "make install" installs the files in the local TeX tree.
   \end{linenumbers}%
   \iftei@export\luadirect{ekdosis.exporttei(\luastringN{#1})}\else\fi}
 %    \end{macrocode}
+% Alignment:---
+%    \begin{macrocode}
+\ekvdefinekeys{align}{
+  store tcols = \tcols@num,
+  store lcols = \lcols@num,
+  store texts = \texts@value,
+  store apparatus = \apparatus@value,
+  initial tcols = 2,
+  initial lcols = 1,
+  initial texts = edition;translation,
+  initial apparatus = edition,
+}
+\NewDocumentEnvironment{alignment}{O{}}
+{%
+  \ekvset{align}{#1}%
+  \luadirect{ekdosis.mkenvdata(
+    \luastring{\texts@value},
+    "texts"
+    )}
+  \luadirect{ekdosis.mkenvdata(
+    \luastring{\apparatus@value}, "apparatus")}
+  \luadirect{tex.sprint(ekdosis.mkenv())}
+  \begin{paracol}[\lcols@num]*{\tcols@num}
+  }
+  {\luadirect{ekdosis.flushenvdata()}
+  \end{paracol}}
+%    \end{macrocode}
 % Very basic implementation of poetry lines:---
 %    \begin{macrocode}
 \newlength{\ekdverseindentlength}
@@ -1884,6 +1912,77 @@ ekdosis.getabspg = function(pg) --not used
    end
 end
 
+-- Build environments to be aligned
+--
+local aligned_texts = {}
+local texts_w_apparatus = {}
+
+function ekdosis.mkenvdata(str, opt)
+   str = str ..";"
+   str = string.gsub(str, "%s+", "")
+   local fieldstart = 1
+   local col = 0
+   if opt == "texts" then
+      repeat
+	 local nexti = string.find(str, "%;", fieldstart)
+	 table.insert(aligned_texts, {text = string.sub(str, fieldstart, nexti-1),
+				      column = col})
+	 col = col + 1
+	 fieldstart = nexti + 1
+      until fieldstart > string.len(str)
+      return aligned_texts
+   elseif opt == "apparatus" then
+      repeat
+	 local nexti = string.find(str, "%;", fieldstart)
+	 table.insert(texts_w_apparatus, string.sub(str, fieldstart, nexti-1))
+	 fieldstart = nexti +1
+      until fieldstart > string.len(str)
+      return texts_w_apparatus
+   end
+end
+
+-- idea: store this # in a lua variable as in ekdosis.storeabspg()
+-- above, then use it in the apparatus-page attributes
+function ekdosis.getcurcol(str)
+   for i = 1,#aligned_texts
+   do
+      if aligned_texts[i].text == str then
+	 colfound = aligned_texts[i].column
+	 break
+      end
+   end
+   return colfound
+end
+
+function ekdosis.flushenvdata()
+   aligned_texts = {}
+   texts_w_apparatus = {}
+   return true
+end
+
+function ekdosis.mkenv()
+   local environments = {}
+   for i = 1,#aligned_texts
+   do
+      if isfound(texts_w_apparatus, aligned_texts[i].text)
+      then
+	 table.insert(environments, "\\NewDocumentEnvironment{"
+			 .. aligned_texts[i].text
+			 .. "}{}{\\begin{nthcolumn}{"
+			 .. aligned_texts[i].column
+			 .."}\\begin{ekdosis}}{\\end{ekdosis}\\end{nthcolumn}}")
+      else
+	 table.insert(environments, "\\NewDocumentEnvironment{"
+			 .. aligned_texts[i].text
+			 .. "}{}{\\begin{nthcolumn}{"
+			 .. aligned_texts[i].column
+			 .."}}{\\end{nthcolumn}}")
+      end
+   end
+   str = table.concat(environments)
+   return str
+end
+
 -- handle multiple layers in apparatuses
 --
 local apparatuses = {}
-- 
cgit v1.2.3

