From fad7802560f18b513e1ce052533148ddb84c7c00 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Fri, 8 May 2020 13:03:42 +0200
Subject: use get_attr_value() whenever possible

---
 ekdosis.dtx | 158 +++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 93 insertions(+), 65 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index a30f801..9978cd0 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -1562,6 +1562,7 @@ end
 
 -- Get values of attributes
 local function get_attr_value(str, attr)
+   str = str..","
    local attrval = string.match(str, "%f[%w]"..attr.."%s?%=%s?%b{}")
       or string.match(str, "%f[%w]"..attr.."%s?%=%s?.-%,")
       or ""
@@ -1590,50 +1591,65 @@ local function note_totei(str)
 	      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><anchor xml:id=\"%s\"/>",
-					 cmd, teitype, right, left, arg, cmd, right)
-		 elseif left ~= "" and teitype == ""
+		 teitype = get_attr_value(opt, "type")
+		 if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
+		 -- 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 = get_attr_value(opt, "labelb")
+		 -- 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 = get_attr_value(opt, "labele")
+		 -- 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 ~= ""
 		 then
 		    return string.format(
-		       "<%s target=\"#range(right(%s),left(%s))\">%s</%s><anchor xml:id=\"%s\"/>",
-					 cmd, right, left, arg, cmd, right)
-		 elseif left == "" and teitype ~= ""
+		       "<%s%s target=\"#range(right(%s),left(%s))\">%s</%s><anchor xml:id=\"%s\"/>",
+		       cmd, teitype, right, left, arg, cmd, right)
+		 elseif left == ""
 		 then
 		    return string.format(
-		       "<%s type=\"%s\" target=\"#right(%s)\">%s</%s><anchor xml:id=\"%s\"/>",
-					 cmd, teitype, right, arg, cmd, right)		    
-		 else
-		    return string.format("<%s target=\"#right(%s)\">%s</%s><anchor xml:id=\"%s\"/>",
-					 cmd, right, arg, cmd, right)
+		       "<%s%s target=\"#right(%s)\">%s</%s><anchor xml:id=\"%s\"/>",
+		       cmd, teitype, right, arg, cmd, right)
 		 end
+		 --
+		 -- if left ~= "" and teitype ~= ""
+		 -- then
+		 --    return string.format(
+		 --       "<%s type=\"%s\" target=\"#range(right(%s),left(%s))\">%s</%s><anchor xml:id=\"%s\"/>",
+		 -- 			 cmd, teitype, right, left, arg, cmd, right)
+		 -- elseif left ~= "" and teitype == ""
+		 -- then
+		 --    return string.format(
+		 --       "<%s target=\"#range(right(%s),left(%s))\">%s</%s><anchor xml:id=\"%s\"/>",
+		 -- 			 cmd, right, left, arg, cmd, right)
+		 -- elseif left == "" and teitype ~= ""
+		 -- then
+		 --    return string.format(
+		 --       "<%s type=\"%s\" target=\"#right(%s)\">%s</%s><anchor xml:id=\"%s\"/>",
+		 -- 			 cmd, teitype, right, arg, cmd, right)		    
+		 -- else
+		 --    return string.format("<%s target=\"#right(%s)\">%s</%s><anchor xml:id=\"%s\"/>",
+		 -- 			 cmd, right, arg, cmd, right)
+		 -- end
    end)
    return str
 end
@@ -1649,16 +1665,26 @@ local function app_totei(str)
 	      function(bkslash, cmd, opt, arg)
 		 opt = string.sub(opt, 2, -2)
 		 arg = string.sub(arg, 2, -2)
-		 opt = string.match(opt, "%f[%w]type%s?%=%s?%b{}")
-		    or string.match(opt, "%f[%w]type%s?%=%s?%w+%f[%W]")
-		    or ""
-		 opt = string.gsub(opt, "type%s?%=%s?(%b{})", function(bbraces)
-				      bbraces = string.sub(bbraces, 2, -2)
-				      return string.format("%s", bbraces)
-		 end)
-		 opt = string.gsub(opt, "(type%s?%=%s?)(%w+%f[%W])", "%2")
-		 return app_totei(string.format("<%s type=\"%s\">%s</%s>",
-						    cmd, opt, arg, cmd))
+		 opt = get_attr_value(opt, "type")
+		 if opt ~= "" then opt = " type=\""..opt.."\"" else end
+		 -- opt = string.match(opt, "%f[%w]type%s?%=%s?%b{}")
+		 --    or string.match(opt, "%f[%w]type%s?%=%s?%w+%f[%W]")
+		 --    or ""
+		 -- opt = string.gsub(opt, "type%s?%=%s?(%b{})", function(bbraces)
+		 -- 		      bbraces = string.sub(bbraces, 2, -2)
+		 -- 		      return string.format("%s", bbraces)
+		 -- end)
+		 -- opt = string.gsub(opt, "(type%s?%=%s?)(%w+%f[%W])", "%2")
+		 return app_totei(string.format("<%s%s>%s</%s>",
+						cmd, opt, arg, cmd))
+		 -- if opt == ""
+		 -- then
+		 -- return app_totei(string.format("<%s>%s</%s>",
+		 -- 				cmd, arg, cmd))
+		 -- else
+		 -- return app_totei(string.format("<%s type=\"%s\">%s</%s>",
+		 -- 				cmd, opt, arg, cmd))
+		 -- end
    end)
    return str
 end
@@ -1675,23 +1701,25 @@ local function lem_rdg_totei(str)
 	      function(bkslash, cmd, opt, arg)
 		 opt = string.sub(opt, 2, -2)
 		 arg = string.sub(arg, 2, -2)
-		 opt = string.match(opt, "%f[%w]wit%s?%=%s?%b{}")
-		    or string.match(opt, "%f[%w]wit%s?%=%s?%w+%f[%W]")
-		    or ""
+		 -- opt = string.match(opt, "%f[%w]wit%s?%=%s?%b{}")
+		 --    or string.match(opt, "%f[%w]wit%s?%=%s?%w+%f[%W]")
+		 --    or ""
+		 opt = get_attr_value(opt, "wit")
 		 if opt == ""
 		 then
 		    return lem_rdg_totei(string.format("<%s>%s</%s>",
 						       cmd, arg, cmd))
 		 else
-		    opt = string.gsub(opt, "wit%s?%=%s?(%b{})", function(bbraces)
-					 bbraces = string.sub(bbraces, 2, -2)
-					 bbraces = ekdosis.getsiglum(bbraces, "tei")
-					 return string.format("%s", bbraces)
-		    end)
-		    opt = string.gsub(opt, "(wit%s?%=%s?)(%w+%f[%W])", function(attr, value)
-					 value = ekdosis.getsiglum(value, "tei")
-					 return string.format("%s", value)
-		    end)
+		    -- opt = string.gsub(opt, "wit%s?%=%s?(%b{})", function(bbraces)
+		    -- 			 bbraces = string.sub(bbraces, 2, -2)
+		    -- 			 bbraces = ekdosis.getsiglum(bbraces, "tei")
+		    -- 			 return string.format("%s", bbraces)
+		    -- end)
+		    -- opt = string.gsub(opt, "(wit%s?%=%s?)(%w+%f[%W])", function(attr, value)
+		    -- 			 value = ekdosis.getsiglum(value, "tei")
+		    -- 			 return string.format("%s", value)
+		    -- end)
+		    opt = ekdosis.getsiglum(opt, "tei")
 		    return lem_rdg_totei(string.format("<%s wit=\"%s\">%s</%s>",
 						       cmd, opt, arg, cmd))
 		 end
@@ -1858,9 +1886,9 @@ end
 local function partotei(str)
    str = gsub(str, lpeg.P(lpeg.P("\\par") * spcenc^1)^1, "\\par ")
    str = gsub(str, ((para + parb) * par^-1)^2, "\\p@r ")
-   str = string.gsub(str, "\\p@ra%s?", "<p>")
-   str = string.gsub(str, "\\p@rb%s?", "</p>")
-   str = string.gsub(str, "\\p@r%s?", "")   
+   str = string.gsub(str, "\\p@ra%s+", "<p>")
+   str = string.gsub(str, "\\p@rb%s+", "</p>")
+   str = string.gsub(str, "\\p@r%s+", "")   
 --   str = gsub(str, lpeg.P(lpeg.P("\\p@r") * spcenc^1)^1, "\\p@r ")
 --   str = gsub(str, lpeg.P(lpeg.P("\\p@r") * spcenc^1 * (lpeg.P("\\par") * spcenc^1)^-1)^1, "\\p@r ")
    str = string.gsub(str, "%s?\\par%s?", "<p>", 1)
@@ -1981,7 +2009,7 @@ local function ekddivs_totei(str)
 		 if ekddivs
 		 then
 		    arg = string.sub(arg, 2, -2)
-		    arg = arg..","
+--		    arg = arg..","
 		    --
 		    teitype = get_attr_value(arg, "type")
 		    tein = get_attr_value(arg, "n")
-- 
cgit v1.2.3

