%:strip
% ptx-plugins.tex: plugin processing for xetex paratext2.tex
% Copyright (c) 2021 by SIL International 
% written by David Gardner
% 
% This plug-in mechanism loads optional files to provide additional output
% options
%
% 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\kn@wnplugins{polyglot-simplecols,polyglot-simplepages,ornaments,cover}
\newif\ifpluginjustwarn
\pluginjustwarntrue %Should the code just warn, or try to load the code?
\def\sw@llowignoredarg #1\relax{}
\def\PluginOptions#1#2{% #2 is a comma-separated list of stylesheet entries that would trigger a warning if used.
  \def\d@##1\E{%
    \ifcsname ##1\endcsname\else
    \x@\def\csname ##1\endcsname{%
      \let\@fterpl@g\relax
      \message{\ifpluginjustwarn Stylesheet option '##1' requires missing \else Loading required \fi plugin: #1}%
      \ifpluginjustwarn
        \let\@fterpl@g\sw@llowignoredarg
      \else
        \x@\let\csname##1\endcsname\relax
        \catcode`\@=11
        \m@kedigitsother
        \plugin@check #1\E
        \catcode`\@=12
        \x@\let\x@\@fterpl@g\csname ##1\endcsname
      \fi\@fterpl@g}
    \fi}%
  \cstackdown#2,\E
}

\PluginOptions{ornaments}{BorderPatternTop,BorderPatternBot,BorderPatternLeft,BorderPatternRight,OrnamentScaleRef}


\def\plugins@ll{all}
\def\plugin@startif#1{%
  \ifcsname #1@plugin@loaded\endcsname\else
  \global\x@\let\csname #1@plugin@loaded\endcsname\empty
}

\let\plugin@endif\fi

\def\plugin@check#1\E{%
  \ifcsname #1@plugin@loaded\endcsname\else
    %\unc@tcodespecials
    \input #1.tex
  \fi}

\def\plugins@needed#1{% Expects a comma-separated list of plugins that should be loaded. For use in plugins.
  \let\do=\plugin@check
  \cstackdown#1,\E 
}

\ifcsname pluginlist\endcsname
  \x@\uppercase\x@{\x@\edef\x@\tmp\x@{\pluginlist}}% Python might fill with 'None'. There is no such plugin so don't try to input None.tex
  \ifx\tmp\fill@none\else
    \def\d@#1\E{\plugin@check #1\E}%
    \ifx\pluginlist\plugins@ll
      \x@\cstackdown\kn@wnplugins,\E 
    \else
      \x@\cstackdown\pluginlist,\E 
    \fi
  \fi
\else
 \lowercase{\message{n.b. You can load optional plugins with (some of) |def|pluginlist{\kn@wnplugins} or |def|pluginlist{all}}}
\fi
