%:strip
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part of the ptx2pdf macro package for formatting USFM text
% copyright (c) 2007-2022 by SIL International
%
% Permission is hereby granted, free of charge, to any person obtaining  
% a copy of this software and associated documentation files (the  
% "Software"), to deal in the Software without restriction, including  
% without limitation the rights to use, copy, modify, merge, publish,  
% distribute, sublicense, and/or sell copies of the Software, and to  
% permit persons to whom the Software is furnished to do so, subject to  
% the following conditions:
%
% The above copyright notice and this permission notice shall be  
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
% NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% Except as contained in this notice, the name of SIL International  
% shall not be used in advertising or otherwise to promote the sale,  
% use or other dealings in this Software without prior written  
% authorization from SIL International.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Macros to handle periphery sections.
\def\periph@@@{} % Eventually will hold the current peripheral type


\def\endofperiph{\relax}
{\catcode13=12 %
\gdef\@@rem{\bgroup\catcode13=12 \unc@tcodespecials\catcode`\\=12\@@rem@}%
\gdef\@@rem@#1^^M{\egroup\trace{s}{rem: Grabbed #1 to ignore}\@@realrem }% Swallow first line like I thought it was designed to.
%Code to read periph sections line by line.
\gdef\@periph{%
  \trace{per}{periph: \sh@wstuff}%
  \ifinperiph\endp@riph\else%
    \ifsk@pping\egroup\fi%
  \fi%
  \endlastp@rstyle{periph}\ifhe@dings\endhe@dings\fi%
  \x@\ifx\csname ch@pter\g@tdstat waiting\endcsname\relax\else%
    \message{Malformed input near \c@rref: periph called while there was a pending chapter number (\csname ch@pter\g@tdstat waiting\endcsname)}%
    \x@\global\x@\let\csname ch@pter\g@tdstat waiting\endcsname\relax%
  \fi%
  \ifn@npublishable \message{Warning! periph is set nonpublishable (hidden) in general. This may be a mistake!}%
    \egroup%
  \fi%
  \trace{per}{Starting periph group: \sh@wstuff}%
  \bgroup\catcode13=12 \@@periph}%
\gdef\@@periph #1^^M{\x@\@@@periph #1||\E}%
\gdef\g@tp@riphline#1^^M{\gdef\p@riphtst{#1}\trace{T}{Got line '\p@riphtst'}\ifx\p@riphtst\endofperiph\trace{per}{relax found}\let\getp@riphline\endp@riph\else\ifx\p@riphtst\empty\else\testp@riph{#1}\fi\fi\getp@riphline}%
%% DO NOT COMMENT END OF FOLLOWING LINE:
\gdef\getp@riphlines{\bgroup\inperiphtrue\codesforperiphgr@b\let\p@riphlines\empty\let\getp@riphline\g@tp@riphline\everyeof{\relax
\trace{per}{EOF2 \sh@wstuff}\ifinperiph\endp@riph\fi\noexpand} \getp@riphline}%
\gdef\n@wline{
}%
}

\gdef\n@wperiph{\expandafter\newperiphb\p@riphtst\E}%
%Code to check periph sections line by line until the next periph section or end of file. Slash is made inactive
\catcode`\~=0 \catcode`\\=12 
  ~gdef~@@testendperiph{\zendperiph}%
  ~gdef~testp@riph #1{~edef~t@mp{#1}~ifx~t@mp~@@testendperiph~inperiphfalse~fi
    ~ifinperiph~expandafter~@testp@riph#1\periph~E~else~let~getp@riphline~endp@riph~E~fi}%
{~catcode13=12 %
  ~gdef~@testp@riph #1\periph#2~E{% NOTE that line feeds are not spaces any more! Comment them!
    ~def~tstb{#1}~ifx~tstb~empty%
      ~trace{T}{New periph starting}%
      ~let~getp@riphline~n@wperiph%
    ~else%
       ~ifx~p@riphlines~empty~global~let~p@riphlines~p@riphtst%
       ~else%DO NOT COMMENT THE END OF THE NEXT LINE!
         ~x@~x@~x@~gdef~x@~x@~x@~p@riphlines~x@~x@~x@{~x@~p@riphlines~x@
         ~p@riphtst}%
       ~fi%
    ~fi}% DO NOT COMMENT THE END OF THE NEXT LINE!
  ~gdef~newperiphb #1\periph#2~E{~x@~x@~x@~gdef~x@~x@~x@~p@riphlines~x@~x@~x@{~x@~p@riphlines~n@wline #1}~endp@riph ~periph #2
}%
}
~catcode`\=0 
\catcode`\~=13

\def\StorePeriph#1{\x@\global\x@\let\csname storeperiph-#1\endcsname\relax}
\def\NoStorePeriph#1{\x@\global\x@\let\csname storeperiph-#1\endcsname\undefined} %A thing let to undefined does not ifcsname
\def\KeepPeriph#1{\x@\global\x@\let\csname keepperiph-#1\endcsname\relax}

\StorePeriph{intbible}
\StorePeriph{intnt}
\StorePeriph{intot}
\StorePeriph{intpent}
\StorePeriph{inthistory}
\StorePeriph{intpoetry}
\StorePeriph{intprophesy}
\StorePeriph{intdc}
\StorePeriph{intgospels}
\StorePeriph{intepistles}
\StorePeriph{intletters}
\StorePeriph{frontcover}
\StorePeriph{backcover}
\StorePeriph{cover}
\StorePeriph{spine}
\StorePeriph{coverwhole}
\StorePeriph{coverfront}
\StorePeriph{coverback}
\StorePeriph{coverspine}
\newm@rknum{pdftag}% registers and defines m@rknumpdftag

\def\endp@riph{%
  \trace{per}{endp@riph:\c@rrdstat:\sh@wstuff}%
  \global\let\periphattr@b\periph@@@
  \ifst@ringperiph
    \egroup\egroup
    \let\tmp\p@riphlines
	\trace{per}{save periph contents: periphcontents\c@rrdstat=\periphattr@b}%
    \x@\gdef\csname periphcontents\c@rrdstat=\periphattr@b\x@\endcsname\x@{%
      \x@\kill@PossParamCache\x@\p@sheof\x@\scantokens\x@{\tmp}\endlastp@rstyle{endperiph}\p@peof
      \ifhe@dings\endhe@dings\fi
      }\everyeof{}%
    \trace{per}{Saved periph \periphattr@b as \meaning\tmp}%
    \st@ringperiphfalse
  \else
    \endlastp@rstyle{endperiph}%
    \ifhe@dings\endhe@dings\fi
    \egroup
    \trace{per}{ended group: \sh@wstuff}%
    \ifnum\periphdepth=0 
      \everyeof{}%
      \kill@PossParamCache
      \ifPagebreakAfterPeriph\pagebreak\fi
    \fi
  \fi}%\let\periph@@@\empty}

\def\zendperiph{\endlastp@rstyle{zendperiph}%
  \ifhe@dings\endhe@dings\fi
  \ifinperiph\endp@riph\fi}

\newif\ifStorePeriph\StorePeriphtrue % are *any* periphs stored?
\newif\ifStoreAllPeriphs \StoreAllPeriphsfalse
\newif\ifPagebreakAfterPeriph\PagebreakAfterPeriphtrue
% Some periphs should be pulled out of the print-run and printed as part of a separate run 
\def\PullPeriph#1{\x@\global\x@\let\csname @pullperiph-#1\endcsname\tr@e} 
\def\NoPullPeriph#1{\x@\global\x@\let\csname @pullperiph-#1\endcsname\false}
\def\@chk@pull#1{\let\tmp\empty\ifcsname @pullperiph-#1\endcsname \x@\let\x@\tmp\csname @pullperiph-#1\endcsname\fi}
\PullPeriph{maps}
\def\@@@chkp@riphname#1::\E{%
  \edef\tmpb{#1}%
  %\global\let\periph@@@\tmpb
}
\def\@@chkp@riphname#1:#2:#3\E{%
  \edef\tmpa{#1}\edef\tmpb{#2}\edef\tmpc{#3}%
  \trace{per}{Checking #1 : #2 : #3 for \periph@sep@print@name}%
  \ifx\tmpa\periph@sep@print@name
    \ifx\tmpc\usc@re
      %\global\let\periph@@@\tmpb
    \else
      \x@\@@@chkp@riphname#2:#3\E
    \fi
    \trace{per}{marking insert for '\periph@@@'}%
    %\StorePeriph{\periph@@@}%
    \PullPeriph{\periph@@@}%
    \m@rkpdftag{\periph@@@}%
  \fi
}

\def\g@tperiphid#1#2{periphcontents#1=#2}

\x@\def\csname MS:zgetperiph\endcsname{%
  \ifhe@dings\endhe@dings\fi
  \get@ttribute{side}%
  \ifx\attr@b\relax
    \let\t@mpside\c@rrdstat
  \else
    \let\t@mpside\attr@b
  \fi
  \get@ttribute{id}%
  \trace{per}{getperiph(\g@tperiphid{\t@mpside}{\attr@b})}%
  \ifcsname\g@tperiphid{\t@mpside}{\attr@b}\endcsname
    \let\periph@@@\attr@b
    \@chk@pull{\periph@@@}%
    \let\p@llpages=\tmp
    \ifx\p@llpages\tr@e
      \endgraf
      \m@rkpdftag{\periph@@@}%
    \else
      %\marks\m@rknumpdftag{}%
    \fi
    \keeptriggerfalse
    \advance\periphdepth by 1
    \ifcsname keepperiph-\periph@@@\endcsname\keeptriggertrue\fi
    \csname\g@tperiphid{\t@mpside}{\periph@@@}\endcsname
    \advance\periphdepth by -1
    \ifkeeptrigger\else
      \x@\global\x@\let\csname\g@tperiphid{\t@mpside}{\periph@@@}\endcsname\undefined
    \fi
    \ifx\p@llpages\tr@e\ifnum\periphdepth=0
      \m@rkpdftag{}%
    \fi\fi
  \fi
}
\newcount\periphdepth
\def\m@rkpdftag#1{%
  \pagebreak
  \x@\marks\m@rknumpdftag{#1}%
}

\x@\def\csname MS:zmakeinsert\endcsname{%
  \trace{per}{zmakeinsert inperiph\ifinperiph true \else false \fi  \the\periphdepth =1?}%
  \ifinperiph
    \ifnum\periphdepth=1
      \endlastp@rstyle{makeinsert}%
      \ifhe@dings\endhe@dings\fi
      \get@ttribute{id}%
      \let\@@pdftag\attr@b
      \trace{per}{zmakeinsert tagging periph '\periph@@@' as '\@@pdftag' (\the\pagetotal, \the\pagegoal)}%
      \m@rkpdftag{\@@pdftag}%
      %\showlists
      \get@ttribute{nopagecount}%
      \ifx\attr@b\relax\else
        \x@\global\x@\let\csname periph-\@@pdftag-nocount\endcsname\attr@b % Page builder shouldn't increment page count for this.
      \fi
    \fi
  \fi
}
\newcount\an@nymousperiphs
\def\t@ganonymousperiph{\advance\an@nymousperiphs by 1
  \edef\periph@@@{anonymous\the\an@nymousperiphs}%
}
\def\@@@periph #1|#2|#3\E{%
  \trace{per}{@@@periph #1|#2 (\c@rrdstat)}%
  \def\tmp{#2}%
  \unset@ttribs
  \def\thisdefault@ttrkey{id}%
  \ifx\tmp\empty
    \t@ganonymousperiph
  \else\let\@ttributes\tmp
    \parse@ttribs{\@ttributes}%
    %\let\d@\apply@attr@specials
    %\x@\cstackdown \attribsus@d,\E
    \get@ttribute{id}%
    \ifx\attr@b\relax
      \t@ganonymousperiph
    \else
      \let\periph@@@\attr@b
      \x@\@@chkp@riphname\attr@b::\E
      \get@ttribute{insert}%
      \ifx\attr@b\tr@e \PullPeriph{\periph@@@}\m@rkpdftag{\periph@@@}\else
        \ifx\attr@b\false \NoPullPeriph{\periph@@@}\fi
      \fi
    \fi
  \fi
  \trace{per}{Set periph@@@ to `\periph@@@` (#2)}%
  \the\@idhooks
  \trace{per}{idhooks run}%
  \tempfalse
  \def\Tmp{}%
  \ifStoreAllPeriphs\temptrue\fi
  \ifStorePeriph
    \ifcsname storeperiph-\periph@@@\endcsname 
      \def\Tmp{(periph type)}\temptrue
    \fi
  \fi
  \kill@PossParamCache % By definition, all old cached values now invalid
  \t@stpublishability{periph}%
  \ifn@npublishable
    \edef\Tmp{\Tmp (unpublishable)}\temptrue
  \fi
  \iftemp
    %\tracingassigns=1
    \trace{per}{periph '\periph@@@' (\c@rrdstat) will be stored: \Tmp}%
    \st@ringperiphtrue
    \let\n@xt\getp@riphlines
  \else 
    \st@ringperiphfalse
    \trace{per}{periph '\periph@@@' will not be stored}%
    \advance\periphdepth by 1
    \inperiphtrue
    \catcode13=10
    \everyeof{\relax%
      \ifsk@pping\egroup\fi
      \trace{per}{EOF1 \sh@wstuff }%
      \ifhe@dings\endhe@dings\fi
    \ifinperiph\endp@riph\fi}%
    \aftergroup\@ndofperiphgroup%
    \let\n@xt\relax
  \fi
  \n@xt
}

\def\@ndofperiphgroup{\trace{per}{End of periph group '\periphattr@b'. pd=\the\periphdepth, inperiph\ifinperiph true\else false\fi}%
  \@chk@pull{\periph@@@}%
  \let\p@llpages=\tmp
  \marks\m@rknumpdftag{}%
  \ifx\p@llpages\tr@e
    \trace{per}{New Page to end insert}% 
    \pagebreak
  \fi
  %\let\periph@@@\empty
}
