
\import{base.zmm}

\switch{\__device__}{
   {html}{}
   {roff}{
    \write{stderr}{txt}{Roff support must be fixed before use!\|\exit}
   }{
   \write{stderr}{txt}{No support for device "\__device__" (in \__fnin__)\|}\exit
   }
}

\:    _section_#3 is the main piece of work in here, together with all
\:    toc-related macros (note the dependencies). When hacking this
\:    stuff, pay attention that names do not shadow, (e.g. when using
\:    \_i_ in different macros), and note that some names are global.
\:    Toc-related macros are all hidden from user, except \"doc::maketoc".
\:    _section_#3 is not a user-level macro, thee should call one of
\:    sec1#2, sec1#3, sec2#2, sec2#3,  sec3#2, sec3#3 etc.

\:    first    argument is toc yes/no ('*' for no)
\:    second   is level,
\:    third    is anchor,
\:    fourth   is caption.

\formatted{

   \def{_section_#3}{\_section_{}{\1}{\2}{\3}}

   \def{_section_#4}{

      \setx{_toc_}{\1}
      \setx{_lev_}{\2}
      \setx{_ank_}{\3}     \: KDE or Dutch style anchor ('anker').
      \setx{_cap_}{\4}

      \ctrinc{doc::\_lev_} \: increment counter associated with this level.

                           \: \_num_ is possibly appended to below.
      \setx{_num_}{\ctrput{doc::1}}
      \setx{_hx_}{}        \: set below; size of header.

                           \:
                           \: reset section numbers if necessary.
                           \:
      \switch{\eqt{cp}{\_lev_}{\_doc_curlevel_}}{
         {-1}
            {  \ctrset{_i_}{\f{+}{\_lev_}{1}}
               \while{\eqt{lq}{\ctrput{_i_}}{\_doc_curlevel_}}{
                  \ctrset{doc::\ctrput{_i_}}{0}
                  \ctrinc{_i_}
               }
            }
         {0}
            {
            }
         {1}
            {  \ctrset{doc::\_lev_}{1}
                           \: ^^^^^^  that line redundant?
               \if{\eqt{ne}{\f{+}{\_doc_curlevel_}{1}}{\_lev_}}{
                  \write
                     {stderr}
                     {txt}
                     {\`[`Section gap: from \_doc_curlevel_ to \_lev_\|\`]`}
                  \exit
               }{}
            }
      }

                           \:
                           \: set the header size.
                           \:
      \switch{\_lev_}{
         {1} {\setx{_hx_}{h2}}
         {2} {\setx{_hx_}{h3}}
         {\setx{_hx_}{h4}}
      }

                           \:
                           \: create the section number, e.g. 2.5.4
                           \:
      \ctrset{_i_}{2}
      \while{\eqt{lq}{\ctrput{_i_}}{\_lev_}}{
         \setx{_num_}{\_num_.\ctrput{doc::\ctrput{_i_}}}
         \ctrinc{_i_}
      }

                           \:
                           \: create anchor if not given.
                           \:
      \if{\cmp{eq}{\_ank_}{}}{
         \setx{_ank_}{section_\tr{{from}{.}{to}{_}}{\_num_}}
      }{
      }

                           \:
                           \: write toc entry unless forbidden.
                           \: MQ need to cascade forbidden to nested sections.
                           \:
      \setx{_tocnum_}{
         \if{\eqt{eq}{\_lev_}{1}}{
            \_anch{toc\ctrput{doc::1}}{\_num_}
         }{
            \_num_
         }
      }
      \if{\cmp{eq}{\_toc_}{*}}{}{
         \write{\__fnbase__.zmt}{copy}{
            \!tocentry
               {\_ank_}
               {\_lev_}
               {Section}
               {\_tocnum_}
               {\_cap_}
               \`n`        \: interpreted by \formatted.
         }
      }
      \write{\__fnbase__.zmr}{copy}{
         \!refload{\_ank_}{
            {level}{\_lev_}
            {type}{Section}
            {num}{\_num_}
            {cap}{\_cap_}
         }
         \`n`           \: interpreted by \formatted.
      }

      \@{\C\P}\<a name="\_ank_">{}

      \<\_hx_>{
         \iref_quiet{toc\ctrput{doc::1}}{\_num_}
         \~\~\_cap_
      }

      \setx{_doc_curlevel_}{\_lev_}
   }
}

\def{"doc::html-style"}{
\@{
   div.box { border:solid; border-width:thin; width:100%; padding:1em}
   div.flushright { text-align: right }
}}

\def{sec1#2}{\sec1{}{\1}{\2}}
\def{sec1#3}{\_section_{\1}{1}{\2}{\3}}

\def{sec2#2}{\sec2{}{\1}{\2}}
\def{sec2#3}{\_section_{\1}{2}{\2}{\3}}

\def{sec3#2}{\sec3{}{\1}{\2}}
\def{sec3#3}{\_section_{\1}{3}{\2}{\3}}

\def{sec4#2}{\sec4{}{\1}{\2}}
\def{sec4#3}{\_section_{\1}{4}{\2}{\3}}

\def{sec5#2}{\sec5{}{\1}{\2}}
\def{sec5#3}{\_section_{\1}{5}{\2}{\3}}

\def{sec6#2}{\sec6{}{\1}{\2}}
\def{sec6#3}{\_section_{\1}{6}{\2}{\3}}


\def{"doc::html-title"}{t*i*t*l*e}
\def{"doc::html-keywords"}{}
\def{"doc::title"}{t*i*t*l*e}
\def{"doc::year-title"}{y*e*a*r}
\def{"doc::year"}{y*e*a*r}
\def{"doc::day"}{d*a*y}
\def{"doc::month"}{m*o*n*t*h}
\def{"doc::author"}{a*u*t*h*o*r}


\def{hrule}{\<hr noshade size="1"!>}
\def{prule}{\<div style="margin-top:1em">{\<hr noshade size="1"!>}}
\env{center}{\<div align=center>}{\</div>}

\def{"doc::preamble"}{\"doc::preamble"{{toc-and-date}{1}}}

\def{"doc::preamble"#1}{
   \set{%{"doc::preamble"}}{\1}
   \ctrset{doc::sec1}{0}
   \ctrset{doc::sec2}{0}
   \load{\__fnbase__.zmr}
   \setx{_doc_toclist}{\zinsert{\__fnbase__.zmt}}
   \'${roff}{
   }
   \'${html}{
\<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"!>
\<html>
\<!-- Copyright (c) \"doc::year" \"doc::author" --!>
\<head>{
   \<meta name="keywords" content="\"doc::html-keywords""!>
   \<style type="text/css">{
      \"gen::html-style-optional"
      \"gen::html-style-required"
      \"doc::html-style"
   }
   \<title>{\"doc::html-title"}
}

\<body>
\if{\%{"doc::preamble"}{toc-and-date}}{
\<table width="100%" summary="lay-out">{
   \<tr>{
      \<td>{\iref{toc}{TOC}}
      \<td valign=top>{
         \<div class=flushright>{
            \"doc::day" \"doc::month" \"doc::year"
         }
      }
   }
}}{}
\<h1 align=center>{\"doc::title"}
   }
}
\def{"doc::postamble"}{
   \'${html}{\</body>\@{\n}\</html>}
   \'${roff}{}
}

\env{quote}{\<blockquote>}{\</blockquote>}
\env{cite}{\<cite>}{\</cite>}

\def{secref#1}{
   \iref{\1}{\ref{\1}{cap}}
}

\constant{
   {'e}  {&eacute;}        \: 
   {`e}  {&egrave;}        \: 
   {(c)} {&copy;}          \: 
   {+-}  {&plusmn;}        \: 
}

\formatted{

   \ctrset{_toc_prevlevel_}{1}
   \ctrset{_toc_startlevel_}{1}
   \set{_doc_curlevel_}{1}

   \def{tocclose#3}{
      \if{\eqt{lt}{\ctrput{\1}}{\ctrput{\2}}
      }{ \3\ctrinc{\1}\tocclose{\1}{\2}{\3}
      }{
      }
   }

   \def{_doc_tocbegin}{
      \_anch{thetoc}{}
      \begin{itemize}{
         {compact}{1}
         {contiguous}{1}
         {margintop}{0}
         {w1}{1}
         {w2}{1}
      }
   }
   \def{_doc_tocend}{
      \tocclose
         {_toc_startlevel_}
         {_toc_prevlevel_}
         {\end{itemize}}
      \end{itemize}
   }
   \def{"doc::maketoc"}{
      \_doc_tocbegin
      \_doc_toclist
      \_doc_tocend
   }
}


\formatted{

\:
\:    tocentry arguments: anchor level type number caption
\:    MQ toc might need to close several entries at once.
\:
\:    arg 1:  anchor
\:    arg 2:  level
\:    arg 3:  type
\:    arg 4:  number arg.
\:    arg 5:  caption
\:
   \def{tocentry#5}{
      \ctrset{_toc_curlevel_}{\2}

      \switch{\eqt{cp}{\ctrput{_toc_curlevel_}}{\ctrput{_toc_prevlevel_}}}{
         {-1}
            {  \ctrset{_toc_tmplevel_}{\ctrput{_toc_curlevel_}}
               \tocclose
                  {_toc_tmplevel_}
                  {_toc_prevlevel_}
                  {\end{itemize}}
               \item{\4.}
                  \` `\_anch{toc_\1}{}\iref{\1}{\5}
            }
         {0}
            {  \item{\4.}
                  \` `\_anch{toc_\1}{}\iref{\1}{\5}
            }
         {1}
            {  \item{}
                  \begin{itemize}{
                     {compact}{1}
                     {contiguous}{1}
                     {margintop}{0}
                     {w1}{1}
                     {w2}{1}
                  }
                  \item{\4.}
                     \` `\_anch{toc_\1}{}\iref{\1}{\5}
            }
      }
      \ctrset{_toc_prevlevel_}{\ctrput{_toc_curlevel_}}
   }
}

