From 04c2290b7aab466cbcfbfdf4fb660f8aa0121883 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Wed, 10 Apr 2019 16:52:21 +0200
Subject: moved xml entities to a function out of the recursion of textotei()

---
 ekdosis.dtx | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index deb5a4b..37fab9d 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -774,6 +774,12 @@ function ekdosis.newcmdtotag(cmd, tag, attr)
    return true
 end
 
+local function xml_entities(str)
+   str = string.gsub(str, "%<", "&lt;")
+   str = string.gsub(str, "%>", "&gt;")
+   return str
+end
+
 local function lem_rdg_totei(str)
    str = gsub(str, dblbkslash * lemrdg * spcenc^-1 * bsqbrackets * bcbraces * spcenc^-1,
 	      function(bkslash, cmd, opt, arg)
@@ -790,11 +796,7 @@ local function lem_rdg_totei(str)
    return str
 end
 
-function ekdosis.textotei(str)
-   str = string.gsub(str, "%s?\\par%s?", "</p>\n<p>")
-   str = string.gsub(str, "%<", "&lt;")
-   str = string.gsub(str, "%>", "&gt;")
-   str = lem_rdg_totei(str)
+local function textotei(str)
    for i = 1,#cmdtotags
    do
       str = string.gsub(str, "(\\"..cmdtotags[i].a..")%s?%*?(%b{})", "%1[]%2")
@@ -806,7 +808,7 @@ function ekdosis.textotei(str)
 						braces = string.sub(braces, 2, -2)
 						return string.format("\"%s\"", braces)
 			   end)
-			   body = ekdosis.textotei(body)
+			   body = textotei(body)
 			   -- return string.format("<"..cmdtotags[i].b..cmdtotags[i].c.." %s>%s</"..cmdtotags[i].b..">", arg, body)
 			   return string.format("<"..cmdtotags[i].b..cmdtotags[i].c..">%s</"..cmdtotags[i].b..">", body)
       end)
@@ -814,19 +816,27 @@ function ekdosis.textotei(str)
    str = string.gsub(str, "\\(%a+)%s?%*?(%b[])(%b{})",
 		     function(cmd, opt, body)
 			body = string.sub(body, 2, -2)
-			body = ekdosis.textotei(body)
+			body = textotei(body)
 			return string.format("<%s>%s</%s>", cmd, body, cmd)
    end)
    str = string.gsub(str, "\\(%a+)%s?%*?(%b{})",
 		     function(cmd, body)
 			body = string.sub(body, 2, -2)
-			body = ekdosis.textotei(body)
+			body = textotei(body)
 			return string.format("<%s>%s</%s>", cmd, body, cmd)
    end)
    str = string.gsub(str, "(%s)(%>)", "%2")
    return str
 end
 
+function ekdosis.textotei(str)
+   str = xml_entities(str)
+   str = string.gsub(str, "%s?\\par%s?", "</p>\n<p>")
+   str = lem_rdg_totei(str)
+   str = textotei(str)
+   return str
+end
+
 local teifilename = tex.jobname.."-tei"
 
 function ekdosis.setteifilename(str)
-- 
cgit v1.2.3

