%% LaTeX package mampf - version 1.51 (2020/4/30)
%% Source file for mampf.sty
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2019-20 by Dr. Hendrik Kasten
%% -------------------------------------------------------------------------------------------
%%
%% 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
%% https://www.latex-project.org/lppl.txt

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mampf}[2020/04/30 mampf Package]
\RequirePackage{marginnote,qrcode,mathtools}
\RequirePackage{hyperref}

\newcount\compileoptions
\compileoptions 1\relax
\newcount\languageoptions
\languageoptions 1\relax
\DeclareOption*{\compileoptions 1\relax \languageoptions 1\relax}
\DeclareOption{off}{\compileoptions 0\relax}
\DeclareOption{on}{\compileoptions 1\relax}
\DeclareOption{german}{\languageoptions 1\relax}
\DeclareOption{english}{\languageoptions 2\relax}
\ProcessOptions*

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Command defined:
%\setmampfurl{<url>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\@mampfurl}{[UNDEFINED]}
\newif\ifmampfurlundefined
\mampfurlundefinedtrue
\newcommand{\setmampfurl}[1]{\renewcommand{\@mampfurl}{#1}\mampfurlundefinedfalse}
\AtBeginDocument{%
\ifmampfurlundefined{\PackageWarningNoLine{mampf}{MaMpf URL not defined. MaMpf links will not work.}}\fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Command defined: 
%\checkmampftype{<type>}
%\checkstructurenames[<description>]{<type>}	
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newif\ifmampftypeundefined
\newcount\@language
\newcommand{\checkmampftype}[1]{%
  \mampftypeundefinedtrue%
  \def\@checktype{#1}%
  \@language 1\relax%
  \ifnum\languageoptions=\@language%
    \@for\@check:={Abbildung,Abschnitt,Algorithmus,Anmerkung,Aufgabe,Beispiel,Bemerkung,Definition,Folgerung,Gleichung,Hilfssatz,Kapitel,Korollar,Lemma,Markierung,Proposition,Satz,Theorem,Unterabschnitt}\do{%
      \ifx\@checktype\@check{%
        \global\mampftypeundefinedfalse%
      }\fi}%
  \fi%
  \@language 2\relax%
  \ifnum\languageoptions=\@language%
    \@for\@check:={algorithm,annotation,chapter,corollary,definition,equation,example,exercise,figure,label,lemma,proposition,remark,section,subsection,theorem}\do{%
      \ifx\@checktype\@check{%
	\global\mampftypeundefinedfalse%
      }\fi}%
  \fi%
  \ifmampftypeundefined{\PackageWarning{mampf}{Type '\@checktype' not listed in MaMpf.}}\fi%
}

\DeclareRobustCommand\checkstructurenames{\@ifnextchar[{\@@checkstructurenames}{\@checkstructurenames}}%
  \def\@@checkstructurenames[#1]#2{%
     \def\@structuredescription{#1}%
     \def\@structuretype{#2}%
     \@language 1\relax%
     \ifnum\languageoptions=\@language%
     \@for\@structure:={Abschnitt,Kapitel}\do{%
      \ifx\@structuretype\@structure{%
        \ifx\@structuredescription\empty{%
          \PackageWarning{mampf}{Missing description for '\@structure'. MaMpf will not import file structure.}%
        }\fi%
      }\fi}%
    \fi%
    \@language 2\relax%
    \ifnum\languageoptions=\@language%
    \@for\@structure:={chapter,section}\do{%
      \ifx\@structuretype\@structure{%
        \ifx\@structuredescription\empty{%
          \PackageWarning{mampf}{Missing description for '\@structure'. MaMpf will not import file structure.}%
        }\fi%
      }\fi}%
    \fi}
  \def\@checkstructurenames#1{%
     \def\@structuretype{#1}%
     \@language 1\relax%
     \ifnum\languageoptions=\@language%
     \@for\@structure:={Abschnitt,Kapitel}\do{%
      \ifx\@structuretype\@structure%
        \PackageWarning{mampf}{Missing description for '\@structure'. MaMpf will not import file structure.}%
      \fi}%
    \fi%
    \@language 2\relax%
    \ifnum\languageoptions=\@language%
    \@for\@structure:={chapter,section}\do{%
      \ifx\@structuretype\@structure%
        \PackageWarning{mampf}{Missing description for '\@structure'. MaMpf will not import file structure.}%
      \fi}%
    \fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Command defined:
%\crtrefnumber{<label>} (cf. package crossreftools, version 0.9)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\crt@refundefined}{[UNDEFINED]}

\AtBeginDocument{%
  \def\crt@@ref@@splitter@@first#1#2#3#4#5\@nil{#1}
}

\def\crt@ref@splitter@firstarg#1{\expandafter\crt@@ref@@splitter@@first#1\@nil}

\newcommand{\@@crtextr@ct@ref}[2]{%
  \expandafter\ifx\csname r@#2\endcsname\relax
  \crt@refundefined%
  \else
  \expandafter\expandafter\csname crt@ref@splitter@#1\endcsname\csname r@#2\endcsname%
  \fi
}

\let\crt@ref@splitter@reference\crt@ref@splitter@firstarg

\newcommand{\crtrefnumber}[1]{\@@crtextr@ct@ref{reference}{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Commands defined:
%\mampflabel[<description>]{<label>}{<type>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\@ifundefined{c@chapter}{%
  \newcounter{chapter}\setcounter{chapter}{0}
}{}

\reversemarginpar
\DeclareRobustCommand\mampflabel{\@ifnextchar[{\@@mampflabel}{\@mampflabel}}%
  \def\@@praemampflabel[#1]#2#3{\ifnum\compileoptions=0%
    \label{#2}%
  \else%  
    \checkmampftype{#3}\checkstructurenames[#1]{#3}%
    \hypertarget{#2}{}\label{#2}\pdfbookmark{MaMpf-Label|#2|#3|\crtrefnumber{#2}|#1|\thechapter|\thesection|\thesubsection|\getpagerefnumber{#2}}{MaMpf-Label{#2}}%
  \fi}
  \def\@@mampflabel[#1]#2#3{\ifmmode%
    \text{\@@praemampflabel[#1]{#2}{#3}}%
  \else%
    \@@praemampflabel[#1]{#2}{#3}%
  \fi}
  \def\@praemampflabel#1#2{\ifnum\compileoptions=0%
    \label{#1}%
  \else%  
    \checkmampftype{#2}\checkstructurenames{#2}%
    \hypertarget{#1}{}\label{#1}\pdfbookmark{MaMpf-Label|#1|#2|\crtrefnumber{#1}||\thechapter|\thesection|\thesubsection|\getpagerefnumber{#1}}{MaMpf-Label{#1}}%
  \fi}
  \def\@mampflabel#1#2{\ifmmode%
    \text{\@praemampflabel{#1}{#2}}%
  \else%
    \@praemampflabel{#1}{#2}%
  \fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Commands defined:
%\mampfvideo[<time>]{<media number>} (outdated!)
%\mampftext[<named destination>]{<media number>} (outdated!)
%
%\mampftime{<time>}{<media number>}{<anchor text>}
%\mampfpage{<page>}{<media number>}{<anchor text>}
%\mampfdest{<named destination>}{<media number>}{<anchor text>}
%\mampfquiz{<media number>}{<anchor text>}
%\mampfgeo{<media number>}{<anchor text>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
\newcount\@linknumber
\@linknumber 0\relax
\def\mampflinkprotokoll#1#2{\pdfbookmark{MaMpf-Link|#1}{MaMpf-Link{#2}}\!\!}
\DeclareRobustCommand\mampfvideo{\@ifnextchar[{\@@mampfvideo}{\@mampfvideo}}%
  \def\@@mampfvideo[#1]#2{\global\advance\@linknumber 1\relax%
  \PackageWarning{mampf}{Outdated command '\mampfvideo'}%
  \ifnum\compileoptions>0%
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#2/play/?time=#1}}[-.75\marginparwidth]
    \mampflinkprotokoll{#2}{\the\@linknumber}%
  \fi}
  \def\@mampfvideo#1{\global\advance\@linknumber 1\relax%
  \PackageWarning{mampf}{Outdated command '\mampfvideo'}%
  \ifnum\compileoptions>0%
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#1/play}}[-.75\marginparwidth]
    \mampflinkprotokoll{#1}{\the\@linknumber}%
  \fi}
\DeclareRobustCommand\mampftext{\@ifnextchar[{\@@mampftext}{\@mampftext}}%
  \def\@@mampftext[#1]#2{\global\advance\@linknumber 1\relax%
  \PackageWarning{mampf}{Outdated command '\mampftext'}%
  \ifnum\compileoptions>0%
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#2/display/?destination=#1}}[-.75\marginparwidth]
    \mampflinkprotokoll{#2}{\the\@linknumber}%
    \fi}
  \def\@mampftext#1{\global\advance\@linknumber 1\relax%
  \PackageWarning{mampf}{Outdated command '\mampftext'}%
  \ifnum\compileoptions>0%
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#1/display}}[-.75\marginparwidth]
    \mampflinkprotokoll{#1}{\the\@linknumber}%
  \fi}

\def\mampftime#1#2#3{\global\advance\@linknumber 1\relax%
  \ifnum\compileoptions>0%
    \href{\@mampfurl/media/#2/play/?time=#1}{#3}
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#2/play/?time=#1}}[-.75\marginparwidth]
    \mampflinkprotokoll{#2}{\the\@linknumber}%
  \fi}
\def\mampfpage#1#2#3{\global\advance\@linknumber 1\relax%
  \ifnum\compileoptions>0%
    \href{\@mampfurl/media/#2/display/?page=#1}{#3}
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#2/display/?page=#1}}[-.75\marginparwidth]
    \mampflinkprotokoll{#2}{\the\@linknumber}%
  \fi}
\def\mampfdest#1#2#3{\global\advance\@linknumber 1\relax%
  \ifnum\compileoptions>0%
    \href{\@mampfurl/media/#2/display/?destination=#1}{#3}
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#2/display/?destination=#1}}[-.75\marginparwidth]
    \mampflinkprotokoll{#2}{\the\@linknumber}%
  \fi}
\def\mampfquiz#1#2{\global\advance\@linknumber 1\relax%
  \ifnum\compileoptions>0%
    \href{\@mampfurl/quizzes/#1/take}{#2}
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/quizzes/#1/take}}[-.75\marginparwidth]
    \mampflinkprotokoll{#1}{\the\@linknumber}%
  \fi}
  \def\mampfgeo#1#2{\global\advance\@linknumber 1\relax%
  \ifnum\compileoptions>0%
    \href{\@mampfurl/media/#1/geogebra}{#2}
    \marginnote{\qrcode[height=\marginparwidth]{\@mampfurl/media/#1/geogebra}}[-.75\marginparwidth]
    \mampflinkprotokoll{#1}{\the\@linknumber}%
  \fi}
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
\makeatother

\endinput