From e465827405a0df4f9ace010a934ad6939359d584 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 2 Apr 2019 00:48:31 +0200
Subject: export listWit to teiHeader

---
 ekdosis.dtx | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 68 insertions(+), 10 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index c01189a..549308b 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -324,7 +324,11 @@ Running "make install" installs the files in the local TeX tree.
 }{\PackageWarning{ekdosis}{parnotes: erroneous input (ignored)}}
 \newif\iftei@export
 \define@boolkey{ekdosis.sty}[@pkg@]{teiexport}[true]{%
-  \if@pkg@teiexport\tei@exporttrue\else\fi}
+  \if@pkg@teiexport
+  \tei@exporttrue
+  \AtBeginDocument{\luadirect{ekdosis.openteistream()}}%
+  \AtEndDocument{\luadirect{ekdosis.closeteistream()}}
+    \else\fi}
 \ExecuteOptionsX{}
 \ProcessOptionsX\relax
 \RequirePackage{luacode}
@@ -349,7 +353,6 @@ Running "make install" installs the files in the local TeX tree.
 %    \begin{macrocode}
 \AtEndDocument{
   \luadirect{ekdosis.closestream()}
-  \luadirect{ekdosis.closeteistream()}
 }
 %    \end{macrocode}
 % Build and process the list of witnesses and hands.
@@ -366,10 +369,10 @@ idno={}, msName={}][3]{%
     |\luastringN|{\commandkey{idno}},
     |\luastringN|{\commandkey{msName}})}
 }
-\NewDocumentCommand{\DeclareNewWitness}{m m O{} +m}{%
+\NewDocumentCommand{\DeclareNewWitness}{m m O{} +O{}}{%
   \declare@new@witness[#3]{#1}{#2}{#4}
 }
-\NewDocumentCommand{\DeclareNewHand}{m m m +m}{
+\NewDocumentCommand{\DeclareNewHand}{m m m +O{}}{
   \luadirect{ekdosis.newhand(\luastringN{#1},
     \luastringN{#2},
     \luastringN{#3},
@@ -629,7 +632,7 @@ function ekdosis.newwitness(id,
 			    MsName)
    if xmlidfound(id)
    then
-      tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]] 
+      tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
       	 ..id..
       	 [[" already exists as a xml:id. ]]
       	 ..
@@ -647,7 +650,6 @@ function ekdosis.newwitness(id,
 				idno = Idno,
 				msName = MsName
 			     },
-			     physDesc = {},
 			     detailsDesc = description})
    end
    return true
@@ -656,7 +658,7 @@ end
 function ekdosis.newhand(id, witid, siglum, description)
    if xmlidfound(id) or not xmlidfound(witid)
    then
-      tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]] 
+      tex.print([[\unexpanded{\PackageWarning{ekdosis}{"]]
       	 ..id..
       	 [[" already exists as a xml:id. ]]
       	 ..
@@ -667,9 +669,7 @@ function ekdosis.newhand(id, witid, siglum, description)
       table.insert(idsRend, {xmlid = id, head = siglum})
       table.sort(idsRend, function(a ,b) return(#a.xmlid > #b.xmlid) end)
       local indexwit = getindex(witid, listWit)
-      table.insert(listWit[indexwit].physDesc, {xmlid = id,
-						head = siglum,
-						handDesc = description})
+      listWit[indexwit].handDesc = {xmlid = id, head = siglum, detailsDesc = description}
    end
    return true
 end
@@ -700,6 +700,7 @@ function ekdosis.getsiglum(str, opt)
 end
 
 -- begin totei functions
+
 local cmdtotags = {
    {a="footnote", b="note", c=" place=\"bottom\""},
    {a="textbf", b="hi", c=" rend=\"bold\""},
@@ -775,7 +776,64 @@ function ekdosis.textotei(str)
    return str
 end
 
+function ekdosis.openteistream()
+   local f = io.open(tex.jobname.."_tmp-tei.xml", "a+")
+   f:write("<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">", "\n")
+   f:write("<teiHeader>", "\n")
+   f:write("<listWit>", "\n")
+   for i = 1,#listWit do
+      f:write("<msDesc xml:id=\"", listWit[i].xmlid, "\">", "\n")
+      f:write("<head>", ekdosis.textotei(listWit[i].head), "</head>", "\n")
+      if listWit[i].msIdentifier.settlement == ""
+	 and listWit[i].msIdentifier.repository == ""
+	 and listWit[i].msIdentifier.idno == ""
+	 and listWit[i].msIdentifier.msName == ""
+      then
+	 f:write("<msIdentifier/>", "\n")
+      else
+	 f:write("<msIdentifier>", "\n")
+	 if listWit[i].msIdentifier.settlement ~= "" then
+	    f:write("<settlement>", ekdosis.textotei(listWit[i].msIdentifier.settlement), "</settlement>", "\n")
+	 else end
+	 if listWit[i].msIdentifier.repository ~= "" then
+	    f:write("<repository>", ekdosis.textotei(listWit[i].msIdentifier.repository), "</repository>", "\n")
+	 else end
+	 if listWit[i].msIdentifier.idno ~= "" then
+	    f:write("<idno>", ekdosis.textotei(listWit[i].msIdentifier.idno), "</idno>", "\n")
+	 else end
+	 if listWit[i].msIdentifier.msName ~= "" then
+	    f:write("<msName>", ekdosis.textotei(listWit[i].msIdentifier.msName), "</msName>", "\n")
+	 else end
+	 f:write("</msIdentifier>", "\n")
+      end
+      if listWit[i].handDesc ~= nil then
+	 f:write("<physDesc>", "\n")
+      	 f:write("<handDesc xml:id=\"", listWit[i].handDesc.xmlid, "\">", "\n")
+      	 f:write("<head>", ekdosis.textotei(listWit[i].handDesc.head), "</head>", "\n")
+	 if listWit[i].handDesc.detailsDesc == "" then else
+	    f:write("<p>", ekdosis.textotei(listWit[i].handDesc.detailsDesc), "</p>", "\n")
+	 end
+      	 f:write("</handDesc>", "\n")
+	 f:write("</physDesc>", "\n")
+      else end
+      if listWit[i].detailsDesc == "" then else
+	 f:write("<p>", ekdosis.textotei(listWit[i].detailsDesc), "</p>", "\n")
+      end
+      f:write("</msDesc>", "\n")
+   end
+   f:write("</listWit>", "\n")
+   f:write("</teiHeader>", "\n")
+   f:write("<text>", "\n")
+   f:write("<body>", "\n")
+   f:close()
+   return true
+end
+
 function ekdosis.closeteistream()
+   local f = io.open(tex.jobname.."_tmp-tei.xml", "a+")
+   f:write("\n", "</body>", "\n")
+   f:write("</text>", "\n")
+   f:write("</TEI>", "\n")
    os.remove(tex.jobname.."-tei.xml")
    os.rename(tex.jobname.."_tmp-tei.xml", tex.jobname.."-tei.xml")
    return true
-- 
cgit v1.2.3

