From 98651e3d1f13dc0cc7880b7fc5dab01e2bc55e50 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Mon, 8 Mar 2021 19:17:33 +0100
Subject: new environment ekdstanza

---
 ekdosis.dtx | 112 ++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 82 insertions(+), 30 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index 0badedd..3e8181a 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -401,7 +401,7 @@ along with this program.  If not, see
 \def\sg#1{\textancientgreek{#1}}
 \usepackage[Old Standard]{mathfont}
 \usepackage{arabluatex}
-\usepackage[parnotes=roman,teiexport=tidy,verse]{ekdosis}
+\usepackage[parnotes=roman, teiexport=tidy, poetry=verse]{ekdosis}
 \input{ekdosis-cfg}
 \AddxmlBibResource{bibdata.xml}
 \usepackage{booktabs}
@@ -6151,6 +6151,7 @@ Sample text with a \textcolor{red}{word} in red.
 \newif\if@parnotesroman
 \newif\if@pkg@parnotes
 \newif\iftei@export
+\newif\if@pkg@poetry@verse
 \ekvdefinekeys{ekdosis}{
   choice layout = {float = {\@pkg@floattrue},
     footins = {\@pkg@floatfalse\@pkg@footinstrue}},
@@ -6163,7 +6164,9 @@ Sample text with a \textcolor{red}{word} in red.
   initial divs = ekdosis,
   unknown-choice divs = \PackageError{ekdosis}{unknown divs=#1}{`divs'
     must be either `ekdosis' or `latex'.},
-  bool verse = \if@pkg@verse,
+  choice poetry = {verse = {\@pkg@poetry@versetrue}},
+  unknown-choice poetry = \PackageError{ekdosis}{unknown
+    poetry=#1}{`poetry' must be `verse' for now.},
   choice parnotes = {false = {},
     true = {\@pkg@parnotestrue},
     roman = {\@pkg@parnotestrue\@parnotesromantrue}},
@@ -6203,7 +6206,7 @@ Sample text with a \textcolor{red}{word} in red.
 \RequirePackage{ltxcmds}
 \RequirePackage{pdftexcmds}
 \RequirePackage{ifoddpage}
-\if@pkg@verse
+\if@pkg@poetry@verse
   \RequirePackage{verse}
 \fi
 \if@pkg@parnotes
@@ -6930,7 +6933,7 @@ Sample text with a \textcolor{red}{word} in red.
     lineation=#1}{`lineation' must be either `page' or `document'.},
   code modulonum = \chardef\c@linenumbermodulo#1\relax,
   noval modulo = \modulolinenumbers,
-  code vmodulo = \if@pkg@verse\poemlines{#1}\fi,
+  code vmodulo = \if@pkg@poetry@verse\poemlines{#1}\fi,
   initial vmodulo = 1,
   default vmodulo = 5,
   bool continuousvnum = \if@continuous@vnum,
@@ -7943,10 +7946,16 @@ Sample text with a \textcolor{red}{word} in red.
 %   depending on the value that is passed to the global option
 %   |verse|:---
 %    \begin{macrocode}
-\if@pkg@verse
+\if@pkg@poetry@verse
+\ekvdefinekeys{ekd@verse}{
+  dimen width = \vwidth@val,
+  initial width = \linewidth,
+  code type = \def\type@value{#1},
+}
 \verselinenumfont{\normalfont\footnotesize}
 \setcounter{poemline}{1}
-\NewDocumentEnvironment{ekdverse}{O{\linewidth}}{%
+\NewDocumentEnvironment{ekdverse}{O{}}{%
+  \ekvset{ekd@verse}{#1}%
   \if@continuous@vnum\setverselinenums{\thelinenumber}{0}\fi
   \nolinenumbers
   \let\linelabel\label
@@ -7961,10 +7970,10 @@ Sample text with a \textcolor{red}{word} in red.
           \setlength{\itemsep}{0pt}%
           \setlength{\topsep}{0pt}%
           \setlength{\partopsep}{0pt}%
-          \ifdim #1 < \linewidth
+          \ifdim\vwidth@val < \linewidth
             \rightmargin        \z@
             \setlength{\leftmargin}{\linewidth}%
-            \addtolength{\leftmargin}{-#1}%
+            \addtolength{\leftmargin}{-\vwidth@val}%
             \addtolength{\leftmargin}{-0.5\leftmargin}%
           \else
             \rightmargin        \leftmargin
@@ -7996,10 +8005,23 @@ Sample text with a \textcolor{red}{word} in red.
 % argument. 
 %    \begin{macrocode}
 \NewDocumentCommand{\resetvlinenumber}{O{1}}{%
+  \if@pkg@poetry@verse
   \setverselinenums{#1}{0}%
+  \fi
 }
 %    \end{macrocode}
 % \end{macro}
+% \begin{environment}{ekdstanza}
+%   \changes{v1.2}{2021/03/08}{new environment for stanzas}
+%    \begin{macrocode}
+\ekvdefinekeys{ekd@stanza}{
+  code type = \def\type@value{#1}  
+}
+\NewDocumentEnvironment{ekdstanza}{O{}}{%
+  \ekvset{ekd@stanza}{#1}%
+}{}
+%    \end{macrocode}
+% \end{environment}
 % \begin{environment}{ekdpar}
 %   When |autopar| is set to |false| by means of \cs{SetTEIxmlExport},
 %   \env{ekdpar}\---or any other environment set to be inserted within
@@ -8051,8 +8073,10 @@ local lemrdg = lpeg.Cs(lpeg.Cs("lem") + lpeg.Cs("rdg"))
 local note = lpeg.Cs("note")
 local lnbrk = lpeg.Cs("\\\\")
 local poemline = lpeg.Cs(lnbrk * spcenc^-1 * lpeg.S("*!")^-1 * bsqbrackets^-1 * spcenc^-1)
-local poemlineb = lpeg.Cs(lnbrk * spcenc^-1 * lpeg.P("&gt;") * bsqbrackets^-1 * spcenc^-1)
-local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1)
+local poemlinebreak = lpeg.Cs(lnbrk * spcenc^-1 * lpeg.P("&gt;") * bsqbrackets^-1 * spcenc^-1)
+local linegroup = lpeg.Cs{ "<lg" * ((1 - lpeg.S"<>") + lpeg.V(1))^0 * ">" }
+local bclinegroup = lpeg.Cs(linegroup + lpeg.P("</lg>"))
+local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1) -- not used
 local sections = lpeg.Cs(lpeg.P("book") + lpeg.P("part") + lpeg.P("chapter")
 			    + lpeg.P("section") + lpeg.P("subsection")
 			    + lpeg.P("subsubsection"))
@@ -8423,13 +8447,11 @@ local envtotags = {
    {a="flushright", b="p", c=" rend=\"align(right)\""},
    {a="flushleft", b="p", c=" rend=\"align(left)\""},
    {a="quotation", b="quote", c=""},
-   {a="ekdverse", b="lg", c=""},
    {a="txarabtr", b="p", c=" xml:lang=\"ar-Latn\" type=\"transliterated\""},
    {a="quoting", b="quote", c=""},
    {a="ekdpar", b="p", c=""},
    {a="txarab", b="p", c=" xml:lang=\"arb\""},
    {a="center", b="p", c=" rend=\"align(center)\""},
-   {a="verse", b="lg", c=""},
    {a="arab", b="p",
     c=" xml:lang=\"ar-Latn\" type=\"transliterated\" subtype=\"arabtex\""}
 }
@@ -8647,37 +8669,66 @@ local function relocate_notes(str)
 end
 
 local function linestotei(str)
-   str = "\n<l>"..str
---   str = string.gsub(str, "^%s?(.-)\\\\[%!%*]?%s?$", "\n<l>%1</l>\n")
-   str = gsub(str, poemline * spcenc^-1 * -1, "</l>\n")
---   str = gsub(str, endpoem, "</l>\n")
-   str = gsub(str, poemlineb, "<lb/> ")
+   if not string.find(str, "^%s?<lg")
+   then
+      str = "\n<l>"..str
+   end
+   str = gsub(str, poemline * spcenc^-1 * bclinegroup, "</l>\n%2")
+   str = gsub(str, linegroup * -(spcenc^-1 * bclinegroup), "%1\n<l>")
+   str = gsub(str, lpeg.Cs("</lg>") * -(spcenc^-1 * (bclinegroup + -1)), "%1\n<l>")
+   -- str = gsub(str, poemline * spcenc^-1 * -1, "</l>\n")
+   str = gsub(str, poemlinebreak, "<lb/> ")
+   -- str = gsub(str, poemline * spcenc^-1 * lpeg.Cs("<lg"), "</l>%2")
+   -- str = gsub(str, lpeg.Cs("</lg>") * spcenc^1 * -lpeg.P("<l"), "%1\n<l>")
    str = gsub(str, poemline, "</l>\n<l>")
---   str = str.."</l>\n"
+   return str
+end
+
+local function stanzatotei(str)
+   str = string.gsub(str, "\\begin%s?%{ekdstanza%}(%b[])(.-)\\end%s?%{ekdstanza%}", function(opt, arg)
+			arg = string.gsub(arg, "\\par%s?", "")
+			opt = string.sub(opt, 2, -2)
+			teitype = get_attr_value(opt, "type")
+			if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
+			if opt == ""
+			then
+			   return string.format("<lg>%s</lg>", arg)
+			else
+			   return string.format("<lg%s>%s</lg>", teitype, arg)
+			end
+   end)
+   str = string.gsub(str, "\\begin%s?%{ekdstanza%}(.-)\\end%s?%{ekdstanza%}", function(arg)
+			arg = string.gsub(arg, "\\par%s?", "")
+			return string.format("<lg>%s</lg>", arg)
+   end)
    return str
 end
 
 -- better use lpeg: look into this later
 local function versetotei(str)
-   str = string.gsub(str, "(\\begin%s?%{ekdverse%})(%b[])(.-)(\\end%s?%{ekdverse%})", function(benv, opt, arg, eenv)
+   str = string.gsub(str, "\\begin%s?%{ekdverse%}(%b[])(.-)\\end%s?%{ekdverse%}", function(opt, arg)
 			arg = string.gsub(arg, "\\par%s?", "")
-			arg = linestotei(arg)
-			return string.format("%s%s%s%s", benv, opt, arg, eenv)
+			opt = string.sub(opt, 2, -2)
+			teitype = get_attr_value(opt, "type")
+			if teitype ~= "" then teitype = " type=\""..teitype.."\"" else end
+			if opt == ""
+			then
+			   return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
+			else
+			   return "\\p@rb "..linestotei(string.format("<lg%s>%s</lg>", teitype, arg)).."\\p@ra "
+			end
    end)
-   str = string.gsub(str, "(\\begin%s?%{ekdverse%})(.-)(\\end%s?%{ekdverse%})", function(benv, arg, eenv)
+   str = string.gsub(str, "\\begin%s?%{ekdverse%}(.-)\\end%s?%{ekdverse%}", function(arg)
 			arg = string.gsub(arg, "\\par%s?", "")
-			arg = linestotei(arg)
-			return string.format("%s%s%s", benv, arg, eenv)
+			return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
    end)
-   str = string.gsub(str, "(\\begin%s?%{verse%})(%b[])(.-)(\\end%s?%{verse%})", function(benv, opt, arg, eenv)
+   str = string.gsub(str, "\\begin%s?%{verse%}%b[](.-)\\end%s?%{verse%}", function(arg)
 			arg = string.gsub(arg, "\\par%s?", "")
-			arg = linestotei(arg)
-			return string.format("%s%s%s%s", benv, opt, arg, eenv)
+			return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
    end)
-   str = string.gsub(str, "(\\begin%s?%{verse%})(.-)(\\end%s?%{verse%})", function(benv, arg, eenv)
+   str = string.gsub(str, "\\begin%s?%{verse%}(.-)\\end%s?%{verse%}", function(arg)
 			arg = string.gsub(arg, "\\par%s?", "")
-			arg = linestotei(arg)
-			return string.format("%s%s%s", benv, arg, eenv)
+			return "\\p@rb "..linestotei(string.format("<lg>%s</lg>", arg)).."\\p@ra "
    end)
    return str
 end
@@ -9223,6 +9274,7 @@ local function textotei(str)
    str = rdgGrp_totei(str)
    str = lem_rdg_totei(str)
    str = relocate_notes(str)
+   str = stanzatotei(str)
    str = versetotei(str)
    str = envtotei(str)
    str = ekddivs_totei(str)
-- 
cgit v1.2.3

