% \enabletrackers[context*]

\usemodule[cals]

% \startxmlsetups xml:whatever:a
%     \xmlsetsetup {#1} {*} {xml:*}
% \stopxmlsetups

% \xmlregisterdocumentsetup{main}{xml:whatever:a}

\startxmlsetups xml:whatever:a
    \xmlsetsetup {#1} {
        article|
        section|
        simpara|
        itemizedlist|orderedlist|listitem|
        emphasis|guibutton|
        literal|programlisting|
        superscript|subscript|
        equation|inlineequation|
        figure|mediaobject|informalfigure
    } {xml:*}
    \xmlsetsetup {#1} {
        imageobject/imagedata
    } {xml:image}
    \xmlsetsetup {#1} {
        imageobject
    } {+}
    \xmlsetfunction {#1} {table} {moduledata.cals.table}
\stopxmlsetups

\xmlregisterdocumentsetup{main}{xml:whatever:a}

\startxmlsetups xml:article
    %\startstandardmakeup
    %    \setupalign[middle]
    %    \start
    %        \bfd \darkred \setupinterlinespace
    %        \begstrut
    %        \xmltext{#1}{/info/title}
    %        \endstrut
    %        \par
    %    \stop
    %    \blank[3*big]
    %    \xmldoiftext {#1} {/info/author/personname} {
    %        \start
    %            \bfa \setupinterlinespace
    %            \begstrut
    %            \xmltext{#1}{/info/author/personname/firstname}
    %            \space
    %            \xmltext{#1}{/info/author/personname/surname}
    %            \endstrut
    %            \par
    %        \stop
    %    }
    %    \blank[3*big]
    %    \xmldoiftext {#1} {/info/date} {
    %        \xmlfirst {#1} {/info/date}
    %    }
    %    \vfill
    %\stopstandardmakeup
    \startfrontmatter
        \starttitle[title={Table of Contents}]
            \placelist[section,subsection,subsubsection][criterium=text]
        \stoptitle
    \stopfrontmatter
    \startbodymatter
        \xmlfilter{#1}{/section/command(xml:section)}
    \stopbodymatter
\stopxmlsetups

\startxmlsetups xml:section
    \startsectionlevel[title={\xmltext{#1}{/title}}]
        \xmlall{#1}{/!title}
        %\xmlall{#1}{}
    \stopsectionlevel
\stopxmlsetups

\startxmlsetups xml:simpara
    \xmlflush{#1}\par
\stopxmlsetups

\startxmlsetups xml:itemizedlist
    \startitemize
        \xmlflush{#1}
    \stopitemize
\stopxmlsetups

\defineitemgroup[ordered]
\setupitemgroup[ordered][1][symbol=n]
\setupitemgroup[ordered][2][symbol=a]
\setupitemgroup[ordered][3][symbol=i]

\startxmlsetups xml:orderedlist
    \startitemgroup[ordered]
        \xmlflush{#1}
    \stopitemgroup
\stopxmlsetups

\startxmlsetups xml:listitem
    \startitem
        \xmlflush{#1}
    \stopitem
\stopxmlsetups

\xmlmapvalue{emphasis}{strong}{\bf}
\xmlmapvalue{emphasis}{normal}{\em}

\startxmlsetups xml:emphasis
    \dontleavehmode
    \start
    \xmlvalue{emphasis}{\xmlattdef{#1}{role}{normal}}{normal}
    \xmlflush{#1}
    \stop
\stopxmlsetups

\startxmlsetups xml:guibutton
    \dontleavehmode
    \start
    \bf
    %\inframed[offset=0pt,frame=off,leftframe=on,rightframe=on]{\kern2pt\bf
    $[$\xmlflush{#1}$]$%\kern2pt}
    \stop
\stopxmlsetups

\startxmlsetups xml:literal
    \dontleavehmode
    \start
    \tt
    \xmlflush{#1}
    \stop
\stopxmlsetups

\startxmlsetups xml:programlisting
    \dontleavehmode
    \start
    \obeylines
    \tt
    \xmlflush{#1}
    \stop
\stopxmlsetups

\startxmlsetups xml:superscript
    \high{\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups xml:subscript
    \low{\xmlflush{#1}}
\stopxmlsetups

\startluacode
    function xml.finalizers.tex.cdatatocontext(c)
        context(xml.cdata(c[1]))
    end
\stopluacode

\startxmlsetups xml:equation
    \startformula
        \xmlfilter{#1}{/mathphrase/cdatatocontext()}
    \stopformula
\stopxmlsetups

\startxmlsetups xml:inlineequation
    \startimath
        \xmlfilter{#1}{/mathphrase/cdatatocontext()}
    \stopimath
\stopxmlsetups

\startxmlsetups xml:image
    \removeunwantedspaces
    \dontleavehmode
    \externalfigure[\xmlatt{#1}{fileref}]
    \ignorespaces
\stopxmlsetups

\startxmlsetups xml:figure
    \startplacefigure[title={\xmltext{#1}{/title}}]
        % depends ...
        \ignorespaces
        \midaligned{\xmlall{#1}{/!title}}
        \removeunwantedspaces
    \stopplacefigure
\stopxmlsetups

\startxmlsetups xml:informalfigure
    \startlinecorrection
        \ignorespaces
        \xmlall{#1}{/!title}
        \removeunwantedspaces
    \stoplinecorrection
\stopxmlsetups

\startxmlsetups xml:mediaobject
    % depends ...
    \ignorespaces
    \xmlall{#1}{/!textobject}
    \removeunwantedspaces
\stopxmlsetups


\definepapersize
    [menu]
    [width=19.6cm,
     height=29.4cm]
\setuppapersize
    [menu]

\setuplayout
    [topspace=2.5cm,
     header=25pt,
     headerdistance=\lineheight,
     footer=0pt,
     footerdistance=0pt,
     height=middle]

\setupcolors
    [state=start,cmyk=yes,rgb=no]
\definecolor
    [dsblack]
%   [c=0.277,m=0.254,y=0.250,k=0.281]
    [c=0.71,m=0.65,y=0.64,k=0.72]
%   [x=232323]
\definecolor
    [dsgraphite]
    [c=0.68,m=0.61,y=0.60,k=0.47]
\definecolor
    [dssilver]
    [c=0.36,m=0.29,y=0.3,k=0]
\definecolor
    [dsorange]
%   [c=0,m=0.309,y=0.359,k=0]
    [c=0,m=0.79,y=0.92,k=0]
%   [x=F15D2D]

\setupbodyfont
    [dejavu,10pt]

\setuplayout
    [middle]

\setupwhitespace
    [big]

\setuphead
    [chapter]
    [style=\bfd,
     color=dsblack,
     number=no,
     placehead=no,
     before=,
     after=,
     incrementnumber=no,
     numberwidth=0pt]
\setuphead
    [title]
    [placehead=yes]
\setuphead
    [section]
    [style=\bfb,
     color=dsblack]
\setuphead
    [subsection]
    [style=\bfa,
     sectionnumber=no,
     ownnumber=yes,
     color=dsorange]
\setuphead
    [subsubsection]
    [style=\bf,
     color=dsgraphite]

\setupinteraction
    [state=start]

\setuppagenumbering
    [location=]

\setupitemize
    [packed]

%\setupbackgrounds
%    [page]
%    [background=color,
%     backgroundcolor=dsblack]
\setupcolors
    [textcolor=dsblack]

\setupexternalfigures
    [maxwidth=\textwidth]

\setupheader
    [strut=yes,style=\tfx]

%\setupheader
%  [text]
%  [before={\startframed[frame=off,bottomframe=on,framecolor=red]},
%   after={\stopframed}]

\setupbackgrounds
    [header]
    [text]
    [bottomframe=on,rulethickness=1pt]

\setupheadertexts
    [{\tfx\setupinterlinespace
      \framed[width=.5\textwidth,align=flushleft,frame=off]
        {\color[dsorange]{\bfx\getmarking[chapter]}\\
        {\tfxx It could work like this: \getmarking[title]}\\
        \strut}}]
    [{\raise1.5\lineheight\hbox{\framed[offset=overlay,frame=off]{\externalfigure[cow][height=\lineheight]}}}]
