From e5d27c17c5920c6b8f78eff920f5f25c7a1b8a66 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 6 Apr 2021 09:07:32 +0200
Subject: relocate_notes() operated outside <lem> in some instances: corrected

---
 ekdosis.dtx | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index ff9d2f9..3e2e6ed 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -9087,6 +9087,7 @@ 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 inlem = lpeg.Cs{ "<lem" * ((1 - (lpeg.P"<lem" + lpeg.P"</lem>")) + lpeg.V(1))^0 * "</lem>" }
 local lnbrk = lpeg.Cs("\\\\")
 local poemline = lpeg.Cs(lnbrk * spcenc^-1 * lpeg.S("*!")^-1 * bsqbrackets^-1 * spcenc^-1)
 local poemlinebreak = lpeg.Cs(lnbrk * spcenc^-1 * lpeg.P("&gt;") * bsqbrackets^-1 * spcenc^-1)
@@ -9683,7 +9684,16 @@ local function lem_rdg_totei(str)
 end
 
 local function relocate_notes(str)
-   str = string.gsub(str, "(%<lem.-%>.-)(%<note.->.-%<%/note%>)(.-%<%/lem%>)", "%1%3%2")
+   str = gsub(str, inlem, function(arg)
+		 local notes = {}
+		 for i in string.gmatch(arg, "%<note.->.-%<%/note%>")
+		 do
+		    table.insert(notes, i)
+		 end
+		 allnotes = table.concat(notes)
+		 arg = string.gsub(arg, "%<note.->.-%<%/note%>", "")
+		 return string.format("%s%s", arg, allnotes)
+   end)
    return str
 end
 
-- 
cgit v1.2.3

