From 8e8c50f0fb7fe0a07b273469492accfe68b112d5 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Sat, 18 Apr 2020 19:15:36 +0200
Subject: have mkenvdata() process additional information to be used in TEI
 output

---
 ekdosis.dtx | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index c244e32..f69d083 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -1173,6 +1173,7 @@ local function gsub(s, patt, repl)
 end
 
 -- some basic patterns:
+local letters = lpeg.R("az", "AZ")
 local ascii = lpeg.R("az", "AZ", "@@")
 local dblbkslash = lpeg.Cs("\\")
 local bsqbrackets = lpeg.Cs{ "[" * ((1 - lpeg.S"[]") + lpeg.V(1))^0 * "]" }
@@ -1996,17 +1997,35 @@ local aligned_texts = {}
 local texts_w_apparatus = {}
 local coldata_totei = {}
 
+local function sanitize_envdata(str)  -- look for a better way to achieve this
+   str = string.gsub(str, "(%a+)%s+(%b[])", "%1%2")
+   str = string.gsub(str, "(%a+)(%b[])%s+", "%1%2")
+   str = string.gsub(str, "%s+(%a+)(%b[])", "%1%2")
+   str = gsub(str, lpeg.Cs(letters^1)
+		 * spcenc^-1
+		 * -bsqbrackets
+		 * lpeg.Cs(";"), "%1[]%2")
+   str = string.gsub(str, "%s+(%a+)(%b[])", "%1%2")
+   return str
+end
+
 function ekdosis.mkenvdata(str, opt)
-   str = str ..";"
-   str = string.gsub(str, "%s+", "")
+   if not string.find(str, "%;", -1) then str = str .. ";" else end
+   --   str = str ..";"
+   --   str = string.gsub(str, "%s+", "")
    local fieldstart = 1
    local col = 0
    if opt == "texts" then
+      str = sanitize_envdata(str)
       repeat
-	 local nexti = string.find(str, "%;", fieldstart)
-	 table.insert(aligned_texts, { text = string.sub(str, fieldstart, nexti-1),
+	 local _s, nexti = string.find(str, "%b[]%s-%;", fieldstart)
+	 local namediv = string.gsub(string.sub(str, fieldstart, nexti-1), "(%a+)%s-(%b[])", "%1")
+	 local attr = string.gsub(string.sub(str, fieldstart, nexti-1), "(%a+)%s-(%b[])", "%2")
+	 attr = string.sub(attr, 2, -2)
+	 table.insert(aligned_texts, { text = namediv,
+				       attribute = attr,
 				       column = col })
-	 table.insert(coldata_totei, { environment = string.sub(str, fieldstart, nexti-1),
+	 table.insert(coldata_totei, { environment = namediv,
 				       data = {} })
 	 col = col + 1
 	 fieldstart = nexti + 1
-- 
cgit v1.2.3

