From ac803d5170d7cd82c9658b1b6887a46f6a626239 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Wed, 5 Aug 2020 18:06:53 +0200
Subject: added support for biblatex through icite. improved label/ref to tei

---
 ekdosis.dtx | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 103 insertions(+), 12 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 3f50def..c85aec8 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -5400,6 +5400,13 @@ local sections = lpeg.Cs(lpeg.P("book") + lpeg.P("part") + lpeg.P("chapter")
 local par =  lpeg.P(lpeg.P("\\par") * spce^1)
 local parb = lpeg.P(lpeg.Cs("\\p@rb") * spce^1)
 local para = lpeg.P(lpeg.Cs("\\p@ra") * spce^1)
+local labelrefcmds = lpeg.Cs(lpeg.P("label")
+			       + lpeg.P("linelabel")
+			       + lpeg.P("lineref")
+			       + lpeg.P("ref")
+			       + lpeg.P("pageref")
+			       + lpeg.P("vref")
+			       + lpeg.P("vpageref"))
 
 -- Bind to local variables
 local next = next
@@ -5494,7 +5501,7 @@ function ekdosis.newwitness(id,
    then
       tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
       	 ..id..
-      	 "\" is not a valid xml:id. "
+      	 "\" is not a valid xml:id. \\MessageBreak "
       	 ..
       	 "Please pick another id.}}")
    else
@@ -5534,7 +5541,7 @@ function ekdosis.newhand(id, witid, siglum, description)
    then
       tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
       	 ..id..
-      	 "\" is not a valid xml:id. "
+      	 "\" is not a valid xml:id. \\MessageBreak "
       	 ..
       	 "Please pick another id.}}")
    else
@@ -5591,7 +5598,7 @@ function ekdosis.newscholar(id, siglum)
    then
       tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
       	 ..id..
-      	 "\" is not a valid xml:id. "
+      	 "\" is not a valid xml:id. \\MessageBreak "
       	 ..
       	 "Please pick another id.}}")
    else
@@ -5656,13 +5663,13 @@ local cmdtotags = {
 
 local texpatttotags = {
    {a="\\altrfont%s+", b=""},
-   {a="\\icite%s?%[(.-)%]%[(.-)%]{(.-)}", b="%1 <ref target=\"#%3\">%2</ref>"},
-   {a="\\icite%s?%[(.-)%]{(.-)}", b="<ref target=\"#%2\">%1</ref>"},
-   {a="\\icite%s?{(.-)}", b="<ptr target=\"#%1\"/>"},
-   {a="\\linelabel%s?{(.-)}", b="<anchor xml:id=\"%1\"/>"},
-   {a="\\label%s?{(.-)}", b="<anchor xml:id=\"%1\"/>"},
-   {a="\\v?pageref%s?{(.-)}", b="<ptr target=\"#%1\"/>"},
-   {a="\\v?ref%s?{(.-)}", b="<ptr target=\"#%1\"/>"},
+   -- {a="\\icite%s?%[(.-)%]%[(.-)%]{(.-)}", b="%1 <ref target=\"#%3\">%2</ref>"},
+   -- {a="\\icite%s?%[(.-)%]{(.-)}", b="<ref target=\"#%2\">%1</ref>"},
+   -- {a="\\icite%s?{(.-)}", b="<ptr target=\"#%1\"/>"},
+   -- {a="\\linelabel%s?{(.-)}", b="<anchor xml:id=\"%1\"/>"},
+   -- {a="\\label%s?{(.-)}", b="<anchor xml:id=\"%1\"/>"},
+   -- {a="\\v?pageref%s?{(.-)}", b="<ptr target=\"#%1\"/>"},
+   -- {a="\\v?ref%s?{(.-)}", b="<ptr target=\"#%1\"/>"},
    {a="\\LR%s+{(.-)}", b="%1"},
    {a="\\RL%s+{(.-)}", b="%1"}
 }
@@ -5928,6 +5935,70 @@ local function texpatttotei(str)
    return str
 end
 
+local function icitetotei(str)
+   str = gsub(str, lpeg.P("\\")
+		 * lpeg.Cs("icite")
+		 * spcenc^-1
+		 * bsqbrackets
+		 * bsqbrackets
+		 * bcbraces
+		 * bsqbrackets^-1,
+	      function(cmd, pre, post, body, opt)
+		 pre = string.sub(pre, 2, -2)
+		 post = string.sub(post, 2, -2)
+		 body = string.sub(body, 2, -2)
+		 if not checkxmlid(body)
+		 then
+		    tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
+				 ..body..
+				 "\" is not a valid xml:id. \\MessageBreak "
+				 ..
+				 "Please pick another id.}}")
+		 else
+		 end
+		 return string.format("%s <ref target=\"#%s\">%s</ref>", pre, body, post)
+   end)
+   str = gsub(str, lpeg.P("\\")
+		 * lpeg.Cs("icite")
+		 * spcenc^-1
+		 * bsqbrackets
+		 * bcbraces
+		 * bsqbrackets^-1,
+	      function(cmd, post, body, opt)
+		 post = string.sub(post, 2, -2)
+		 body = string.sub(body, 2, -2)
+		 if not checkxmlid(body)
+		 then
+		    tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
+				 ..body..
+				 "\" is not a valid xml:id. \\MessageBreak "
+				 ..
+				 "Please pick another id.}}")
+		 else
+		 end
+		 return string.format("<ref target=\"#%s\">%s</ref>", body, post)
+   end)
+   str = gsub(str, lpeg.P("\\")
+		 * lpeg.Cs("icite")
+		 * spcenc^-1
+		 * bcbraces
+		 * bsqbrackets^-1,
+	      function(cmd, body, opt)
+		 body = string.sub(body, 2, -2)
+		 if not checkxmlid(body)
+		 then
+		    tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
+				 ..body..
+				 "\" is not a valid xml:id. \\MessageBreak "
+				 ..
+				 "Please pick another id.}}")
+		 else
+		 end
+		 return string.format("<ptr target=\"#%s\"/>", body)
+   end)
+   return str
+end
+
 local function cmdtotei(str)
    for i = 1,#cmdtotags
    do
@@ -5963,6 +6034,25 @@ local function cmdtotei(str)
 			return string.format("<ref target=\"%s\">%s</ref>",
 					     teisiglum, printsiglum)
    end)
+   str = gsub(str, lpeg.P("\\") * labelrefcmds * spcenc^-1 * bcbraces,
+	      function(cmd, body)
+		 body = string.sub(body, 2, -2)
+		 if not checkxmlid(body)
+		 then
+		    tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
+				 ..body..
+				 "\" is not a valid xml:id. \\MessageBreak "
+				 ..
+				 "Please pick another id.}}")
+		 else
+		 end
+		 if string.find(cmd, "label")
+		 then
+		    return string.format("<anchor xml:id=\"%s\"/>", body)
+		 else
+		    return string.format("<ptr target=\"#%s\"/>", body)
+		 end
+   end)
    str = string.gsub(str, "\\(%a+)%s?%*?(%b[])(%b{})",
 		     function(cmd, opt, body)
 			body = string.sub(body, 2, -2)
@@ -6296,6 +6386,7 @@ local function textotei(str)
    str = envtotei(str)
    str = ekddivs_totei(str)
    str = section_totei(str)
+   str = icitetotei(str)
    str = cmdtotei(str)
    str = self_close_tags(str)
    str = partotei(str)
@@ -6654,7 +6745,7 @@ function ekdosis.newscholar(id, siglum)
    then
       tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
       	 ..id..
-      	 "\" is not a valid xml:id. "
+      	 "\" is not a valid xml:id. \\MessageBreak "
       	 ..
       	 "Please pick another id.}}")
    else
@@ -6687,7 +6778,7 @@ local function build_envdiv(str)
    then
       tex.print("\\unexpanded{\\PackageWarning{ekdosis}{\""
       	 ..div..
-      	 "\" is not a valid xml:id. "
+      	 "\" is not a valid xml:id. \\MessageBreak "
       	 ..
       	 "Please pick another id.}}")
    else
-- 
cgit v1.2.3

