%% cloze.sty
%% Copyright 2015-2025 Josef Friedrich
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Josef Friedrich.
%
% This work consists of the files cloze.lua, cloze.tex,
% and cloze.sty.

\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{cloze}
    [2025/07/04 v2.0.0 Package to typeset cloze worksheets or cloze tests]
%
% \subsection{The file \tt{cloze.sty}}
%
%    \begin{macrocode}
\input{cloze.tex}
%    \end{macrocode}
%
%    \begin{macrocode}
\ExplSyntaxOn
%    \end{macrocode}
%
% This packages are used to build \emph{cloze}:
%
% \subsubsection{Dependencies}
%
% The package \href{https://www.ctan.org/pkg/setspace}{setspace} is used
% by the environment |clozespace|.
%    \begin{macrocode}
\RequirePackage{setspace}
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareOption*{} % suppresses the error message: ! LaTeX Error: Unknown option
\ProcessOptions\relax
\RequirePackage{luakeys}
\directlua{
  cloze.parse_global_options('\LuakeysGetPackageOptions', 'global')
}
%    \end{macrocode}
%
% Overwrite the cloze font definition in the plain tex definition to
% avoid undefined old font command errors. In plain \TeX{} we use \cmd{\it}.
%    \begin{macrocode}
\def\clozefont{\itshape}
%    \end{macrocode}
%
% Load the cloze lua module and put all return values in the variable
% |cloze|.
%
% \subsubsection{Public macros}
%
% All public macros are prefixed with |\cloze|.
%
% \begin{macro}{\cloze}
%    \begin{macrocode}
\let\cloze=\undefined
\NewDocumentCommand{ \cloze }{ O{} m }
{
  \Cloze{basic}{\unexpanded{#1}}{#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozefix}
%    \begin{macrocode}
\let\clozefix=\undefined
\NewDocumentCommand{ \clozefix }{ O{} m }
{
  \Cloze{fix}{#1}{#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{clozepar}
%    \begin{macrocode}
\NewDocumentEnvironment{ clozepar }{ O{} }
{
  \par
  \ClozeStartMarker{par}{#1}
  \clozefont\relax
}
{
  \ClozeStopMarker{par}
  \par
  \directlua{cloze.unregister_callback('par')}
}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\clozeparplain}
%    \begin{macrocode}
\let\clozeparplain=\undefined
\NewDocumentCommand{ \clozeparplain }{ O{} m }
{
  \ClozePar{#1}{#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{clozebox}
% For the box rule a \cmd{\fbox} is used. We store the original rule
% thickness and restore the dimension after the clozebox is set.
% The dimension |fboxrule| have to be set before the environment
% |lrbox|.
%    \begin{macrocode}
\newsavebox{\ClozeBox}
\NewDocumentEnvironment{ clozebox }{ s O{} +b }
{
  \directlua
  {
    cloze.print_box('\luaescapestring{\unexpanded{#3}}','\luaescapestring{\unexpanded{#2}}','\luaescapestring{\unexpanded{#1}}')
  }
}
{
  \directlua
  {
    cloze.restore_fboxrule()
  }
}
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{clozespace}
% TODO: Realization without setspace package.
%    \begin{macrocode}
\NewDocumentEnvironment { clozespace } { O {} }
{
  \directlua{cloze.print_space('#1')}
}
{
  \end{spacing}
}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\clozeline}
%    \begin{macrocode}
\let\clozeline=\undefined
\NewDocumentCommand{ \clozeline }{ O{} }
{
  \ClozeLine{#1}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozelinefil}
%    \begin{macrocode}
\let\clozelinefil=\undefined
\NewDocumentCommand{ \clozelinefil }{ O{} }
{
  \ClozeLinefil{#1}

}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozefil}
%    \begin{macrocode}
\let\clozefil=\undefined
\NewDocumentCommand{ \clozefil} { O{} m }
{
  \cloze[#1]{#2}\clozelinefil[#1]
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozeextend}
%    \begin{macrocode}
\let\clozeextend=\undefined
\NewDocumentCommand{ \clozeextend } { O{} }
{
  \ClozeExtend{#1}
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\ExplSyntaxOff
%    \end{macrocode}