From 098ab133a17ac089d9ad0ebd23c7fa8d9a8d8443 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Sun, 15 Sep 2019 21:16:44 +0200
Subject: new function ekdosis.newapparatus(). adapted ekdosis.appin()/appout()
 accordingly

---
 ekdosis.dtx | 69 +++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 49 insertions(+), 20 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 6b9b15f..8181714 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -757,6 +757,9 @@ local lnbrk = lpeg.Cs("\\\\")
 local poemline = lpeg.Cs(lnbrk * bsqbrackets^-1)
 local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1)
 
+-- Bind to local variables
+local next = next
+
 -- Booleans
 local tei_p_open = false
 
@@ -1361,17 +1364,46 @@ ekdosis.getabspg = function(pg) --not used
    end
 end
 
-local function mdvisintable(table, value)
-   for _, v in pairs(table) do
-      if v == value then return true end
+-- handle multiple layers in apparatuses
+--
+local apparatuses = {}
+function ekdosis.newapparatus(teitype, cmd)
+   if cmd ~= "app" or "note"
+   then
+	 -- tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
+	 --       .. [[The string `]] ..
+	 --       .. cmd ..
+	 --       [["' must be either `app' or `note'.}}]])
+   else
+      if isintable(apparatuses, teitype)
+      then
+	 -- tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
+	 --       .. teitype ..
+	 --       [[" already exists as an apparatus. ]]
+	 --       ..
+	 --       [[Please pick another teitype.}}]])
+      else
+	 table.insert(apparatuses, { a = teitype, b = cmd })
+      end
    end
-   return false
+   return true
 end
 
-
-function ekdosis.appin(str)
+function ekdosis.appin(str, teitype)
    local f = io.open(tex.jobname.."_tmp.ekd", "a+")
-   f:write("<", cur_abs_pg, ">", str, "</", cur_abs_pg, ">\n")
+   if next(apparatuses) == nil
+   then
+      f:write("<", cur_abs_pg, "-0>", str, "</", cur_abs_pg, "-0>\n")
+   else
+      for i = 1,#apparatuses
+      do
+	 if apparatuses[i].a == teitype then
+	    local appno = i
+	    break
+	 end
+      end
+      f:write("<", cur_abs_pg, "-", appno, ">", str, "</", cur_abs_pg, "-", appno, ">\n")
+   end
    f:close()
    return true
 end
@@ -1396,24 +1428,14 @@ function ekdosis.appout()
       t = f:read("*a")
       local output = {}
       for i in string.gmatch(t,
-			     "<T"..cur_abs_pg..">.-</T"..cur_abs_pg..">")
-      do
-	 table.insert(output, i)
-      end
-      if string.find(t, "<T"..cur_abs_pg..">")
-      then
-	 table.insert(output, "<T"..cur_abs_pg..">\\par</T"..cur_abs_pg..">")
-      else
-      end
-      for i in string.gmatch(t,
-			     "<"..cur_abs_pg..">.-</"..cur_abs_pg..">")
+			     "<"..cur_abs_pg.."%-0>.-</"..cur_abs_pg.."%-0>")
       do
 	 table.insert(output, i)
       end
       f:close()
       str = table.concat(output)
-      str = string.gsub(str, "</[A-Z0-9]?"..cur_abs_pg..">", "")
-      str = string.gsub(str, "<[A-Z0-9]?"..cur_abs_pg..">", " ")
+      str = string.gsub(str, "</"..cur_abs_pg.."%-0>", "")
+      str = string.gsub(str, "<"..cur_abs_pg.."%-0>", " ")
       return str
    else end
 end
@@ -1438,6 +1460,13 @@ local prev_lnlab = nil
 local current_lemma = nil
 local salt = 0
 
+local function mdvisintable(table, value)
+   for _, v in pairs(table) do
+      if v == value then return true end
+   end
+   return false
+end
+
 function ekdosis.dolnlab(str)
    prev_lnlab = current_lnlab
    current_lemma = str
-- 
cgit v1.2.3

