% -----------------------------------------------------------------------
%   webquiz.cfg | webquiz TeX4ht configuration file
% -----------------------------------------------------------------------
%
%   Copyright (C) Andrew Mathas and Don Taylor, University of Sydney
%
%   Distributed under the terms of the GNU General Public License (GPL)
%               http://www.gnu.org/licenses/
%
%   This file is part of the WebQuiz system.
%
%   <Andrew.Mathas@sydney.edu.au>
% ----------------------------------------------------------------------

\NeedsTeXFormat{LaTeX2e}[1996/12/01]
\ProvidesFile{webquiz.cfg}[\webquiz{release date} v\webquiz{version} WebQuiz tex4ht configuration]

\makeatletter% quite surprisingly, this is needed!

% shortcut for inserting newlines into xml ifle
\newcommand\HNewLine{\HCode{\Hnewline}}

% Generate HTML5 + MathML code
\Preamble{xhtml,mathml,html5,NoFonts,charset="utf-8",ext=xml,svg}

% Don't output xml version tag
\Configure{VERSION}{}

% Output a WebQuiz doctype instead of the default for HTML5
\Configure{DOCTYPE}{\HCode{<?xml version="1.0" encoding="UTF-8" ?>\Hnewline
<!DOCTYPE webquiz>}}

\ifWQ@Debugging\def\WQ@debugging{true}\else\def\WQ@debugging{false}\fi

% store the quiz specifications as attributes to <quiz>
\Configure{HTML}{%
  \HNewLine%
  \Tg<webquiz debugging="\WQ@debugging"
                 hide_side_menu="\WQ@ClassOption{hidesidemenu value}"
                 language="\WQ@ClassOption{language}"
                 one_page="\WQ@ClassOption{onepage value}"
                 pst2pdf="\WQ@ClassOption{pst2pdf value}"
                 random_order="\WQ@ClassOption{randomorder value}"
                 src="\jobname.tex"
                 theme="\WQ@ClassOption{theme}">
  \HNewLine%
  \Tg<title>\@title\Tg</title>%
  \HNewLine%
  \Tg<breadcrumb breadcrumbs="\WQ@breadcrumbs">\WQ@breadcrumb\Tg</breadcrumb>%
  \HNewLine%
  \Tg<unit_name url="\uos@url" quizzes_url="\WQ@quizzesUrl">\uos@name\Tg</unit_name>%
  \HNewLine%
  \Tg<unit_code>\uos@code\Tg</unit_code>%
  \HNewLine%
  \Tg<department url="\WQ@departmentURL">\WQ@department\Tg</department>%
  \HNewLine%
  \Tg<institution url="\WQ@institutionURL">\WQ@institution\Tg</institution>%
  \HNewLine%
}{\Tg</webquiz>}

% reset all configurations
\Configure{HEAD}{}{}
\Configure{BODY}{}{}
\Configure{TITLE}{}{}
\Configure{TITLE+}{}
\Configure{thanks author date and}{}{}{}{}{}{}{}{}
\renewcommand{\maketitle}{}

% convert pictures to svg
\Configure{Picture}{.svg}
%\Configure{Picture*}{.svg}

%\Configure{textit}{\HCode{<span class="textit">}\NoFonts}{\EndNoFonts\HCode{</span>}}
%\Configure{textsf}{\HCode{<span class="textsf">}\NoFonts}{\EndNoFonts\HCode{</span>}} 

% The webquiz macros and environments need to write opening and closing tags to
% the xml file. Opening these tags is easy. To close them we use \WQ@closeXXX
% and \WQ@@closeXXX macros. Each time a tag is opened we \let the \WQ@closeTag
% macro to the corresponding \WQ@@close macro. The \WQ@@close macro closes the
% tag and then \let's the \WQ@closeTag macro to \relax. This allows us to use
% the \WQ@close macros at the start of the tag routines below, which results in
% fairly transparent and readable code.

% make the @close tags \relax initially
\let\WQ@closeTag\relax % closes \correct, \incorrect, \feedback, \whenRight and \whenWrong
\let\WQ@closeText\relax   % closes text

% text tags - everything in wrapped inside <text>...</text> tags
\def\WQ@openText{\Tg<text>\HtmlParOff\HCode{<![CDATA[}\let\WQ@closeText\WQ@@closeText}
\def\WQ@@closeText{\HCode{]]>}\HtmlParOn\Tg</text>\HNewLine\global\let\WQ@closeText\relax}

% -----------------------------------------------------------------------
% discussion environment
% -----------------------------------------------------------------------

% discussion environments are number -1, -2, ... in the webquiz.js
\renewcommand\thediscussion{-\arabic{discussion}}

% inside a discussion environment any label defines two labels, the second of 
% which is a label to the short-title for the discussion item. Of there are many
% labels this is slightly in efficient but, because of tex4ht magic, it proved 
% too painful to extract the discussion number from the label.
\let\WQ@reallabel=\label
\newcommand\WQ@discussionLabel[1]{%
  \WQ@reallabel{#1}%
  \bgroup\let\@currentlabel\WQ@shortDiscussionTitle%
  \WQ@reallabel{discussion-title-#1}%
  \egroup%
}

% discussion environments
\RenewDocumentEnvironment{discussion}{O{Discussion}O{#1}}{%
    \WQ@DiscussionStart%
    \HNewLine%
    \Tg<discussion>\HNewLine
    \Tg<short_heading>#1\Tg</short_heading>\HNewLine
    \Tg<heading>#2\Tg</heading>\HNewLine
    \Tg<text>\HCode{<![CDATA[}% don't use \WQ@openText as we want paragraphs
    \def\WQ@shortDiscussionTitle{#1}%
    \let\label\WQ@discussionLabel%
}{\EndP\HCode{]]>}\Tg</text>\HNewLine\Tg</discussion>\HNewLine\WQ@DiscussionEnd}

% ---------------------------------------------------------------------------
% Cross referencing question and discussion environments
% ---------------------------------------------------------------------------

% tex4ht redefines the \ref internals so that r@<label> uses \rEfLiNK,
% so we temporarily redefine \rEfLiNK to access the question/discussion
% number
\def\WQ@rEfLiNK#1#2{#2}
% \WQ@Ref[optional *][optional text]{mandatory label}
\RenewDocumentCommand\WQ@ref{ m s o m }{%
  \HNewLine%
  \bgroup%
  \let\rEfLiNK\WQ@rEfLiNK%
  \IfBooleanTF{#2}{% link
    \HCode{<a onClick="gotoQuestion(}\ref{#4}\HCode{);">}%
    \IfNoValueTF{#3}{\ref{#1#4}}{#3}%
    \HCode{</a>}%
  }{% button
    \HCode{<span class="button blank" onClick="gotoQuestion(}\ref{#4}%
    \HCode{);">}%
    \IfNoValueTF{#3}{\ref{#1#4}}{#3}%
    \HCode{</span>}%
  }%
  \egroup%
}

\renewcommand\dref{\WQ@ref{discussion-title-}}
\renewcommand\qref{\WQ@ref{}}
\RenewDocumentCommand\Qref{ s O{#3} m }{%
  \HNewLine%
  \IfBooleanTF{#1}{% link
    \HCode{<a onClick="gotoQuestion(#3);">}#2\HCode{</a>}%
  }{% button
    \HCode{<span class="button blank" onClick="gotoQuestion(#3);">}#2\HCode{</span>}%
  }%
}

% -----------------------------------------------------------------------
% question environments
% -----------------------------------------------------------------------

\renewenvironment{question}{%
  \WQ@QuestionStart%
  \IgnorePar\HCode{\Hnewline<question>}% open the question tag
  \WQ@openText% start some text
}{\WQ@QuestionEnd\WQ@closeText\WQ@closeTag\Tg</question>\HNewLine}

% -----------------------------------------------------------------------
% choice environment, \correct and \incorrect choices and \feedback
% -----------------------------------------------------------------------

\RenewDocumentEnvironment{choice}{O{}}{%
  \WQ@ChoiceStart{#1}%
  \WQ@closeText% close any open text tags and then insert tex4ht code
  \IgnorePar%
  \Tg<choice type="\pgfkeysvalueof{/webquiz checker/mode}"
             columns="\pgfkeysvalueof{/webquiz checker/columns value}">%
  \let\WQ@closeTag\relax%
}{\WQ@ChoiceEnd\WQ@closeTag\Tg</choice>}

% correct and incorrect items
\def\WQ@@closeItem{\WQ@closeText\Tg</item>\HNewLine\let\WQ@closeTag\relax}
\def\WQ@Item#1{%
  \WQ@ItemStart%
  \WQ@closeTag%
  \Tg<item correct="#1" symbol="\thechoice">\WQ@openText%
  \let\WQ@closeTag\WQ@@closeItem%
}
\def\correct{\WQ@Item{true}}
\def\incorrect{\WQ@Item{false}}

% feedback
\def\WQ@@closeFeedback{\WQ@closeText\Tg</feedback>\HNewLine\let\WQ@closeTag\relax}
\def\feedback{%
  \WQ@FeedbackStart%
  \WQ@closeTag\Tg<feedback>\WQ@openText\let\WQ@closeTag\WQ@@closeFeedback%
}

% -----------------------------------------------------------------------
% The \answer macro and \whenRight, \whenWrong
% -----------------------------------------------------------------------

\RenewDocumentCommand\answer {sO{string}m}{%
  \WQ@AnswerStart{#2}%
  \WQ@closeText%
  \IfBooleanTF{#1}{\def\WQ@prompt{false}}{\def\WQ@prompt{true}}%
  \Tg<answer prompt="\WQ@prompt" comparison="#2">\HNewLine%
  \WQ@openText#3\WQ@closeText%
  \Tg</answer>%
  \HNewLine%
  \let\WQ@closeTag\relax%
  \WQ@openText%
}

% whenRight and whenWrong
\def\WQ@closeWhen{\Tg</when>\HNewLine\let\WQ@closeTag\relax}
\def\WQ@When#1{%
  \WQ@WhenStart{#1}%
  \WQ@closeText\WQ@closeTag%
  \IgnorePar\Tg<when type="#1">\WQ@openText%
  \let\WQ@closeTag\WQ@closeWhen%
}
\def\whenRight{\WQ@When{right}}
\def\whenWrong{\WQ@When{wrong}}

% -----------------------------------------------------------------------
% the quizindex environment and \quiz macro
% -----------------------------------------------------------------------

% quizindex environment and \quiz
\ConfigureEnv{quizindex}{\Tg<quizindex>\HNewLine}{\Tg</quizindex>\HNewLine}{}{}
\RenewDocumentCommand\quiz{som}{\refstepcounter{quiz}%
  \WQ@QuizStart%
  \IfBooleanTF{#1}{\def\WQ@prompt{false}}{\def\WQ@prompt{true}}%
  \IfNoValueTF{#2}{\def\WQ@url{quiz\thequiz.html}}{\def\WQ@url{#2}}%
  \Tg<index_item prompt="\WQ@prompt" url="\WQ@url">\WQ@openText #3 \WQ@closeText\Tg</index_item>\HNewLine%
}

% -----------------------------------------------------------------------
% making listing environment work with colour I
% - this code appears to break \color and \textcolor in the online manual
% -----------------------------------------------------------------------


% from https://tex.stackexchange.com/questions/225554/syntax-highlighting-in-an-html-presentation
% extract current color as hexadecimal value
\makeatletter
\ifdefined\lst@version
\newcommand\tsf@getColor[1][.]{%
      % colorname `.` holds current color
      \extractcolorspec{.}{\tsf@color}
      \expandafter\convertcolorspec\tsf@color{HTML}\tsf@color
      %\tmpcolor
  }

  % write css color for given css selector
  \newcommand\CssColor[1]{%on-l
      % save current color
      \tsf@getColor%
      \Css{#1{color:\#\tsf@color;}}%
  }
\fi

% Do not set indent and noindent classes on paragraphs
\Configure{HtmlPar}
    {\EndP\Tg<p>}
    {\EndP\Tg<p>}
    {\HCode{</p>\Hnewline}}
    {\HCode{</p>\Hnewline}}

\renewcommand\DisplayAsImage[2][]{%
  \csletcs{real:#2}{#2}%
  \NewConfigure{#2}{2}
  \csdef{#2}##1{\Picture+[#1]{}\csuse{real:#2}{##1}\EndPicture}
  \Configure{#2}{\Picture+[#1]{}}{\EndPicture}
}

\begin{document}

% disable \title after \begin{document}
\def\title{\WQ@Error{\@backslashchar title can only be used in the preamble}\@ehc}

% -----------------------------------------------------------------------
% making listing environment work with colour II
% - this code appears to break \color and \textcolor in the online manual
% -----------------------------------------------------------------------

%% this code is for processing the listing environments in the online manual
\ifdefined\lst@version
  % from https://www.mail-archive.com/tex4ht@tug.org/msg00116.html
  \let\savecolor\color
  \NewConfigure{color}[2]{\def\a@color{#1}\def\b@color{#2}}
  \def\color@@tmp#1{\a@color#1\b@color\savecolor{#1}\aftergroup\endspan}
  \let\color\color@@tmp
  \def\endspan{\Tg</span>}
  \Configure{color}{\HCode{<span style="color:}}{\HCode{">}}

  \ConfigureEnv{lstlisting}
     {\let\color\savecolor
      \ifvmode \IgnorePar\fi \EndP
      \gHAdvance\listingN by 1
      \HCode{<!--l. \the\inputlineno-->}%
      \gdef\start:LstLn{\HCode{<div class="lstlisting" id="listing-\listingN">}%
        \gdef\start:LstLn{\leavevmode\special{t4ht@+\string&{35}x00A0{59}}x%
      \HCode{<br/>}}}
      \bgroup
         \Configure{listings}
           {{\everypar{}\leavevmode}}
           {{\everypar{}\leavevmode}}
           {\start:LstLn \HCode{<span class="label">}}
           {\HCode{</span>}}%
     }
     {\egroup
      \ifvmode \IgnorePar\fi \EndP \HCode{</div>}\par}
     {}{}

  % for listings in webquiz-manual
  % from https://tex.stackexchange.com/questions/225554/syntax-highlighting-in-an-html-presentation
  \newcommand\LstCss[2]{%
      \bgroup%
          \csname lst@#2\endcsname%
          \CssColor{#1}%
      \egroup%
  }

  \LstCss{div.lstlisting .ecbx-1000}{keywordstyle}
  \LstCss{div.lstlisting .ecss-1000}{commentstyle}
  \LstCss{div.lstlisting .ectt-1000}{basicstyle}
\fi

\makeatother

\EndPreamble

% bug fix for \mathbf from http://tex.stackexchange.com/questions/362178
%\Configure{mathbf}{\HCode{<mi mathvariant="bold">}\PauseMathClass}{\EndPauseMathClass\HCode{</mi>}}

\endinput
%%
%% End of file `webquiz.cfg'.
