From 28d29e5bd799ebc2e3dc7fad1eee9b26b4a4ab65 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 1 Oct 2019 20:23:50 +0200
Subject: added note_totei()

---
 ekdosis.dtx | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 9518bf0..fd1768e 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -938,6 +938,7 @@ idno={}, msName={}, origDate={}][3]{%
 % \fi
 % \begin{ekdlua}
 
+-- This table will hold the functions:
 ekdosis = {}
 
 -- lpeg equivalent for string.gsub()
@@ -963,6 +964,7 @@ local aftercmd = lpeg.Cs(lpeg.S("*[{,.?;:'`\"") + dblbkslash)
 local cmdargs = lpeg.Cs(spce^-1 * bsqbracketsii * bcbracesii * bsqbrackets^-1)
 local app = lpeg.Cs("app")
 local lemrdg = lpeg.Cs(lpeg.Cs("lem") + lpeg.Cs("rdg"))
+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)
@@ -1227,6 +1229,62 @@ local function xml_entities(str)
    return str
 end
 
+local function note_totei(str)
+   str = gsub(str,
+	      dblbkslash *
+		 note *
+		 spcenc^-1 *
+		 bsqbrackets *
+		 bcbraces *
+		 spcenc^-1,
+	      function(bkslash, cmd, opt, arg)
+		 opt = string.sub(opt, 2, -2)
+		 arg = string.sub(arg, 2, -2)
+		 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")
+		 right = string.match(opt, "%f[%w]labelb%s?%=%s?%b{}")
+		    or string.match(opt, "%f[%w]labelb%s?%=%s?%w+%f[%W]")
+		    or ""
+		 right = string.gsub(right, "labelb%s?%=%s?(%b{})", function(bbraces)
+				      bbraces = string.sub(bbraces, 2, -2)
+				      return string.format("%s", bbraces)
+		 end)
+		 right = string.gsub(right, "(labelb%s?%=%s?)(%w+%f[%W])", "%2")
+		 left = string.match(opt, "%f[%w]labele%s?%=%s?%b{}")
+		    or string.match(opt, "%f[%w]labele%s?%=%s?%w+%f[%W]")
+		    or ""
+		 left = string.gsub(left, "labele%s?%=%s?(%b{})", function(bbraces)
+				      bbraces = string.sub(bbraces, 2, -2)
+				      return string.format("%s", bbraces)
+		 end)
+		 left = string.gsub(left, "(labele%s?%=%s?)(%w+%f[%W])", "%2")
+		 
+		 if left ~= "" and teitype ~= ""
+		 then
+		    return string.format("<%s type=\"%s\" target=\"#range(right(%s),left(%s))\">%s</%s>",
+					 cmd, teitype, right, left, arg, cmd)
+		 elseif left ~= "" and teitype == ""
+		 then
+		    return string.format("<%s target=\"#range(right(%s),left(%s))\">%s</%s>",
+					 cmd, right, left, arg, cmd)
+		 elseif left == "" and teitype ~= ""
+		 then
+		    return string.format("<%s type=\"%s\" target=\"#right(%s)\">%s</%s>",
+					 cmd, teitype, right, arg, cmd)		    
+		 else
+		    return string.format("<%s target=\"#right(%s)\">%s</%s>",
+					 cmd, right, arg, cmd)
+		 end
+   end)
+   return str
+end
+
 local function app_totei(str)
    str = gsub(str,
 	      dblbkslash *
@@ -1284,6 +1342,7 @@ local function lem_rdg_totei(str)
 		 arg = string.sub(arg, 2, -2)
 		 return lem_rdg_totei(string.format("<%s>%s</%s>", cmd, arg, cmd))
    end)
+   str = string.gsub(str, "(%<lem.-%>.-)(%<note.-%<%/note%>)(.-%<%/lem%>)", "%1%3%2")
    return str
 end
 
@@ -1385,6 +1444,7 @@ local function textotei(str)
       str = string.gsub(str, "%s?\\par%s?", "\n<p>")
       tei_p_open = true
    end
+   str = note_totei(str)
    str = app_totei(str)
    str = lem_rdg_totei(str)
    str = versetotei(str)
-- 
cgit v1.2.3

