From 5842e21604d612a03721aa6b866283806c72c6fb Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Sun, 24 Mar 2019 17:59:52 +0100
Subject: export to tei: first implementation

---
 ekdosis.dtx | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 114 insertions(+), 4 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index a8426eb..796482f 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -321,6 +321,9 @@ Running "make install" installs the files in the local TeX tree.
     \@pkg@parnotestrue\@parnotesromantrue
   \fi\fi\fi
 }{\PackageWarning{ekdosis}{parnotes: erroneous input (ignored)}}
+\newif\iftei@export
+\define@boolkey{ekdosis.sty}[@pkg@]{teiexport}[true]{%
+  \if@pkg@teiexport\tei@exporttrue\else\fi}
 \ExecuteOptionsX{}
 \ProcessOptionsX\relax
 \RequirePackage{luacode}
@@ -343,7 +346,21 @@ Running "make install" installs the files in the local TeX tree.
 \luadirect{dofile(kpse.find_file("ekdosis.lua"))}
 %    \end{macrocode}
 %    \begin{macrocode}
-\AtEndDocument{\luadirect{ekdosis.closestream()}}
+\AtEndDocument{
+  \luadirect{ekdosis.closestream()}
+  \luadirect{ekdosis.closeteistream()}
+}
+%    \end{macrocode}
+% Build an process the list of witnesses
+%    \begin{macrocode}
+\NewDocumentCommand{\NewWitness}{m m +m}{
+  \luadirect{ekdosis.newwitness(\luastringN{#1},
+    \luastringN{#2},
+    \luastringN{#3})}
+}
+\NewDocumentCommand{\getsiglum}{m}{%
+  \luadirect{tex.sprint(ekdosis.getsiglum(\luastring{#1}))}
+}
 %    \end{macrocode}
 %    \begin{macrocode}
 \newbool{ekd@started}
@@ -488,12 +505,13 @@ choice nolem={false,,true}][1]{%
 \NewDocumentCommand{\EkdosisOff}{}{%
   \setbool{ekd@state}{false}%
 }
-\newenvironment{ekdosis}{%
+\NewDocumentEnvironment{ekdosis}{+b}{%
   \begin{linenumbers}
     \EkdosisStart%
-    \EkdosisOn}{%
+    \EkdosisOn#1}{%
     \EkdosisOff
-  \end{linenumbers}}
+  \end{linenumbers}%
+  \iftei@export\luadirect{ekdosis.exporttei(\luastringN{#1})}\else\fi}
 %    \end{macrocode}
 % \iffalse
 %</package>
@@ -507,6 +525,98 @@ choice nolem={false,,true}][1]{%
 
 ekdosis = {}
 
+listWit = {}
+
+function ekdosis.newwitness(id, siglum, description)
+   table.insert(listWit, {a = id, b = siglum, c = description})
+   table.sort(listWit, function(a ,b) return(#a.a > #b.a) end)
+   return true
+end
+
+function ekdosis.isintable(table, element)
+   for i = 1,#table do
+      if table[i].a == element then
+	 return true
+      end
+   end
+   return false
+end
+
+function ekdosis.getsiglum(str)
+--   if not ekdosis.isintable(listWit, str) then
+--      str = "<??>"
+--   else
+      for i = 1,#listWit do
+	 str  = string.gsub(str, listWit[i].a, listWit[i].b)
+      end
+--   end
+   return str
+end
+
+-- begin totei functions
+local tags = {
+   {a="footnote", b="note", c=" pos=\"foot\""},
+   {a="textbf", b="hi", c=" rend=\"bold\""},
+   {a="emph", b="hi", c=" rend=\"italic\""},
+   {a="textsf", b="hi", c=" rend=\"sans\""},
+   {a="txarb", b="foreign", c=" xml:lang=\"arb\""},
+   {a="arb", b="foreign", c=" xml:lang=\"arb\""},
+   {a="sg", b="foreign", c=" xml:lang=\"grc\""},
+   {a="app", b="app", c=""},
+   {a="lem", b="lem", c=""},
+   {a="rdg", b="rdg", c=""}
+}
+
+function ekdosis.textotei(str)
+   str = string.gsub(str, "%s?\\par%s?", "</p>\n<p>")
+   for i = 1,#tags
+   do
+      str = string.gsub(str, "(\\"..tags[i].a..")%s?(%b{})", "%1[]%2")
+      str = string.gsub(str, "(\\"..tags[i].a..")%s?(%b[])(%b{})",
+			function(tag, arg, body)
+			   body = string.sub(body, 2, -2)
+			   arg = string.sub(arg, 2, -2)
+			   arg = string.gsub(arg, "(%b{})", function(braces)
+						braces = string.sub(braces, 2, -2)
+						return string.format("\"%s\"", braces)
+			   end)
+			   body = ekdosis.textotei(body)
+			   return string.format("<"..tags[i].b..tags[i].c.." %s>%s</"..tags[i].b..">", arg, body)
+      end)
+   end
+   str = string.gsub(str, "\\(%a+)%s?%*?(%b[])(%b{})",
+		     function(tag, opt, body)
+			body = string.sub(body, 2, -2)
+			body = ekdosis.textotei(body)
+			return string.format("<%s>%s</%s>", tag, body, tag)
+   end)
+   str = string.gsub(str, "\\(%a+)%s?%*?(%b{})",
+		     function(tag, body)
+			body = string.sub(body, 2, -2)
+			body = ekdosis.textotei(body)
+			return string.format("<%s>%s</%s>", tag, body, tag)
+   end)
+   str = string.gsub(str, "(%s)(%>)", "%2")
+   return str
+end
+
+function ekdosis.closeteistream()
+   os.remove(tex.jobname.."-tei.xml")
+   os.rename(tex.jobname.."_tmp-tei.xml", tex.jobname.."-tei.xml")
+   return true
+end
+
+function ekdosis.exporttei(str)
+   local f = io.open(tex.jobname.."_tmp-tei.xml", "a+")
+   str = ekdosis.textotei(str)
+   f:write("\n<p>")
+   f:write(str)
+   f:write("</p>")
+   f:close()
+   return true
+end
+-- end totei functions
+
 function ekdosis.closestream()
    os.remove(tex.jobname..".ekd")
    os.rename(tex.jobname.."_tmp.ekd", tex.jobname..".ekd")
-- 
cgit v1.2.3

