%D \module
%D   [       file=back-exp,
%D        version=2010.08.22,
%D          title=\CONTEXT\ Backend Macros,
%D       subtitle=XML export,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% see t:/export/todo-fonts.tex for (future) experiment

\writestatus{loading}{ConTeXt Backend Macros / XML export}

\registerctxluafile{back-exp}        {autosuffix}
\registerctxluafile{back-exp-imp-tag}{autosuffix}
\registerctxluafile{back-exp-imp-mth}{autosuffix}
\registerctxluafile{back-exp-imp-ref}{autosuffix}
\registerctxluafile{back-exp-imp-fnt}{autosuffix}

%D This is an experimental exporter and a logical follow up on tagging. The
%D exporter assumes a properly tagged document. Some elements get a couple
%D of attributes because otherwise rendering information would get lost. In
%D general we assume that when the \XML\ is converted to \HTML\ some stylesheet
%D is applied anyway.

\unprotect

% we can replace this by a more generic attributeset mechanism where we bind
% to any element (needed anyway, see userdata thingies)

\definesystemattribute[taggedpar][public]

\permanent\tolerant\protected\def\setelementexporttag[#1]#*[#2]#*[#3]%
  {\ifarguments\or\or
     \clf_settagproperty{#1}{export}{#2}%
   \or
     \clf_settagproperty{#1}{#2}{#3}%
   \fi}

% todo: no need for calls when trialtypesetting

\permanent\def\taggedctxcommand
  {\iftrialtypesetting
     \expandafter\gobbleoneargument
   \else
     \expandafter\ctxcommand
   \fi}

% todo: \c_strc_tags_enabled for the setters

\appendtoks
    \enforced\permanent\protected\def\dotagTABLEcell
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagtablecell{\tablecellrows}{\tablecellcolumns}{\raggedstatus}%
       \fi}%
    % brrr, we need to tag empty cells (unless we start numbering)
    \enforced\permanent\protected\def\dotagTABLEsignal
      {\signalcharacter}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagtabulatecell
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagtabulatecell\c_tabl_tabulate_align\c_tabl_tabulate_kind
       \fi}%
    \enforced\permanent\protected\def\dotagtabulatesignal
      {\dontleavehmode\signalcharacter\ignorespaces\ignorepars}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsynonym
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagsynonym{\currentsynonymtag}%
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsorting
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagsorting{\currentsortingtag}%
       \fi}%
\to \everyenableelements

\appendtoks % frozen and assumed global per highlight class
    \enforced\permanent\protected\def\dotagconstruct
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagconstruct
           {\currentstartstop}%
           {\startstopparameter\c!style}%
           \c_attr_color
           \ifvmode\plusone\else\zerocount\fi
           \relax
       \fi}%
\to \everyenableelements

\appendtoks % frozen and assumed global per highlight class
    \enforced\permanent\protected\def\dotaghighlight
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settaghighlight
           {\currenthighlight}%
           {\highlightparameter\c!style}
           \c_attr_color
           \ifvmode\plusone\else\zerocount\fi
           \relax
       \fi}%
\to \everyenableelements

\mutable\lettonothing\figurecategory
\mutable\lettonothing\figureexported

\appendtoks % we can have differently scaled images
    \enforced\permanent\protected\def\dotagfigure
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagfigure
           {\figurefileoriginal}%
           {\figurefullname}%
           {\figurefilepage}%
           {\figurewidth}%
           {\figureheight}%
           {\figuredepth}%\relax
           {\figurelabel}%
           {\figurecategory}%
           {\figureexported}%
           {\figuredescriptiontext}%
           {\figurealternativetext}%
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagimage
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagimage
           {\figurelabel}%
           {\figuredescriptiontext}%
           {\figurealternativetext}%
       \fi}%
\to \everyenableelements


\appendtoks
    \enforced\permanent\protected\def\dotagmpgraphic
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagmpgraphic
           {\figuredescriptiontext}%
           {\figurealternativetext}%
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagcombination
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagcombination
            {\combinationparameter\c!nx}%
            {\combinationparameter\c!ny}%
       \fi}%
    \enforced\permanent\protected\def\dotagcombinationpair#1#2%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagcombinationpair{#1}{#2}%
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsetitemgroup
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagitemgroup
           \ifconditional\c_strc_itemgroups_pack     true\else false\fi\relaxedspace
           \ifconditional\c_strc_itemgroups_continue true\else false\fi\relaxedspace
           {\currentitemlevel}%
           {\currentitemgroupsymbol}%
       \fi}%
    \enforced\permanent\protected\def\dotagsetitem#1%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagitem{#1}%
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsetdescriptionindex#1#2%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagdescriptionindex{#1}{#2}\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsetnotation
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
        \clf_settagnotation{\currentnote}\currentnotenumber\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsetnotesymbol
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagnotationsymbol{\currentnote}\currentnotenumber\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagregisterlocation
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagregister{\currentregister}\currentregisternumber\relax
       \fi}%
\to \everyenableelements

\aliased\let\dotagsetreferencedescription\gobbletwoarguments

\appendtoks
    \enforced\permanent\protected\def\dotagsetreferencedescription#1#2%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagsetreferencedescription{#1}{#2}\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsetdelimitedsymbol#1%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagdelimitedsymbol{#1}\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagsetsubsentencesymbol#1%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagsubsentencesymbol{#1}\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagregisterfloat#1#2%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagfloat{#1}{#2}\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\let\specialfixedspace    \explicitfixedspace
    \enforced\let\specialobeyedspace   \explicitobeyedspace
    \enforced\let\specialstretchedspace\explicitstretchedspace
    \enforced\let\specialcontrolspace  \explicitcontrolspace
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagregisterformula#1#2%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagformulacontent{#1}{#2}%
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagmarginanchor#1%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagmarginanchor#1\relax
       \fi}%
    \enforced\permanent\protected\def\dotagmargintext#1%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagmargintext#1\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagpublication#1#2%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagpublication{#1}{#2}\relax
       \fi}%
\to \everyenableelements

\appendtoks
    \enforced\permanent\protected\def\dotagparagraph#1%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagparagraph{#1}\relax
       \fi}%
\to \everyenableelements

% bah bah bah

% \lettonothing\dotagmarklist

\appendtoks
    \enforced\permanent\protected\def\dotagmarklist
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \strc_lists_add_list_description
       \fi}%
\to \everyenableelements

% For more elements, so an extra argument:

\ifdefined\dotagcodepoint \else \aliased\let\dotagcodepoint\gobbletwoarguments \fi

\appendtoks
    \enforced\permanent\protected\def\dotagcodepoint#1#2%
      {\iftrialtypesetting\orelse\ifconditional\c_strc_tags_enabled
         \clf_settagcodepoint{#1}#2\relax
       \fi}%
\to \everyenableelements

% The action: \setupbackend[export=yes] % or filename

% maybe xhtml css settings will move to setupexport

% maybe some day a definer

\installcorenamespace{export}

\installparameterhandler \??export {export}
\installsetuphandler     \??export {export}

\setupexport
  [\c!align=\number\raggedstatus, % we pass as string
   \c!bodyfont=\bodyfontsize,
   \c!width=\textwidth,
   \c!title={\directinteractionparameter\c!title},
   \c!subtitle={\directinteractionparameter\c!subtitle},
   \c!author={\directinteractionparameter\c!author},
 % \c!firstpage=, % imagename
 % \c!lastpage=,  % imagename
   \c!properties=\v!no, % no: ignore, yes: as attribute, otherwise: use as prefix
   \c!hyphen=\v!no,
   \c!svgstyle=,
   \c!cssfile=,
   \c!file=]

\resetsystemmode\v!export

\permanent\protected\def\doinitializeexport
  {\ifempty{\backendparameter\c!export}\else
      % yes | xml
      \setuptagging[\c!state=\v!start]%
      \clf_initializeexport
      \setsystemmode\v!export
      \exportingtrue
      \writestatus\m!backend\empty % so messages will stand out a bit
      \expand\everyinitializeexport
      \writestatus\m!backend\empty % so messages will stand out a bit
      \enforced\glet\doinitializeexport\relax
   \fi}

\permanent\protected\def\dostartexport
  {%\glet\dostartexport\relax
   \lettonothing\currentexport
   \clf_setupexport
       % we catch up later
       align      {\exportparameter\c!align}%
       bodyfont   {\exportparameter\c!bodyfont}%
       width      {\exportparameter\c!width}%
       % till here
       properties {\exportparameter\c!properties}%
       hyphen     {\exportparameter\c!hyphen}%
       title      {\exportparameter\c!title}%
       subtitle   {\exportparameter\c!subtitle}%
       author     {\exportparameter\c!author}%
       firstpage  {\exportparameter\c!firstpage}%
       lastpage   {\exportparameter\c!lastpage}%
       svgstyle   {\exportparameter\c!svgstyle}%
       cssfile    {\exportparameter\c!cssfile}%
       file       {\exportparameter\c!file}%
       option     {\exportparameter\c!option}%
       export     {\backendparameter\c!export}%
   \relax}

\permanent\protected\def\dostopexport
  {\enforced\glet\dostopexport\relax
   \clf_setupexport
       align      {\exportparameter\c!align}%
       bodyfont   {\exportparameter\c!bodyfont}%
       width      {\exportparameter\c!width}%
   \relax
   \clf_finishexport}

\appendtoks
    \doinitializeexport
\to \everysetupbackend

\appendtoks
    \ifexporting
        \dostartexport
    \fi
\to \everystarttext

% better (before pdf gets closed, so we can embed), but it needs testing:

\appendtoks
    \ifexporting
        \dostopexport
    \fi
\to \everystoptext

\appendtoks
    \ifexporting
        \dostartexport % in case it is done inside \starttext
    \fi
\to \everystartdocument

%D This can replace the older (rather unknown) attachment feature because it
%D is more immediate (same run). I can also use this then for manuals.
%D
%D \setuptagging[state=start] % mandate here
%D
%D \starttyping
%D \startmathexport % [buffer=yes]
%D     test $ x = 1 + a $ test
%D \stopmathexport
%D
%D \typebuffer
%D
%D \startmathexport[buffer=math]
%D     test $ x = 1 + a^2 $ test
%D     test $ x = 1 + a $ test
%D     test $ x = z + \sqrt{a} $ test
%D \stopmathexport
%D
%D \typebuffer[math]
%D
%D \startmathexport[filename=foo.xml]
%D     test $ x = z + \sqrt{a} $ test
%D \stopmathexport
%D
%D \dorecurse{\collectedmath} {\page \collectedmath #1 \page}
%D \stoptyping
%D
%D This one also does buffers and files:
%D
%D \starttyping
%D \startboxexport
%D     \startparagraph
%D         \input tufte
%D     \stopparagraph
%D     \startitemize
%D         \startitem item one \stopitem
%D         \startitem item two \stopitem
%D     \stopitemize
%D     \startparagraph
%D         \input tufte
%D     \stopparagraph
%D \stopboxexport
%D
%D \typebuffer
%D \stoptyping

\installcorenamespace {boxexport}
\installcorenamespace {mathexport}

\installsetuponlycommandhandler \??boxexport  {boxexport}
\installsetuponlycommandhandler \??mathexport {mathexport}

\permanent\tolerant\protected\def\startboxexport[#S#1]%
  {\begingroup
   \ifempty{#1}%
      \letboxexportparameter\c!buffer\v!yes
   \orelse\ifhastok={#1}\relax
      \setupboxexport[#1]%
   \else
      \setboxexportparameter\c!buffer{#1}%
   \fi
   \setbox\nextbox\vbox\bgroup}

\permanent\protected\def\stopboxexport
  {\egroup
   \clf_exportbox \nextbox
     {\boxexportparameter\c!file}%
     {\boxexportparameter\c!buffer}%
   \endgroup}

\permanent\tolerant\protected\def\startmathexport[#S#1]%
  {\begingroup
   \ifempty{#1}%
      \letmathexportparameter\c!buffer\v!yes
   \orelse\ifhastok={#1}\relax
      \setupmathexport[#1]%
   \else
      \setmathexportparameter\c!buffer{#1}%
   \fi
   \clf_startcollectingmath}

\permanent\protected\def\stopmathexport
  {\clf_stopcollectingmath
   \clf_processcollectedmath
     {\mathexportparameter\c!file}%
     {\mathexportparameter\c!buffer}%
     {\mathexportparameter\c!n}%
     {\mathexportparameter\c!option}%
   \endgroup}

\permanent\tolerant\protected\def\savemathexport[#S#1]%
  {\begingroup
   \setupmathexport[#1]%
   \clf_processcollectedmath
     {\mathexportparameter\c!file}%
     {\mathexportparameter\c!buffer}%
     {\mathexportparameter\c!n}%
     {\mathexportparameter\c!option}%
   \endgroup}

% Defined at the \LUA\ end:
%
% \the\collectedmath : number of snippets
% \collectedmath n   : specific snippet

% \startluacode
%     function commands.doifelsesimplecollectedmath()
%         commands.doifelse(string.find(
%             buffers.getcontent("test"),
%             "<math.->%s*<m[nio]>[^<>]+</m[nio]>%s*</math>")
%         )
%     end
% \stopluacode
%
% \dorecurse {\collectedmath} {
%     \savemathexport[buffer=test,n=#1]
%     \ctxlua{commands.doifelsesimplecollectedmath()} {
%     } {
%         \startsubsubsubject[title={math blob #1}]
%             \typebuffer[test]
%         \stopsubsubsubject
%     }
% }

\protect \endinput
