%D \module
%D   [       file=strc-tag,
%D        version=2010.07.16,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Tags,
%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.

% key/values and other names might change (and probably will)

\writestatus{loading}{ConTeXt Structure Macros / Tags}

\registerctxluafile{strc-tag}{1.001}

\unprotect

%D Eventually these labels will either move to the modules
%D where they're used, or they will en dup in mult-tag.

\def\t!document           {document}               % Div

\def\t!division           {division}               % Div
\def\t!paragraph          {paragraph}              % P
\def\t!p                  {p}                      % P
\def\t!construct          {construct}              % Span
\def\t!highlight          {highlight}              % Span

\def\t!section            {section}                % Sect
\def\t!sectiontitle       {sectiontitle}           % H
\def\t!sectionnumber      {sectionnumber}          % H
\def\t!sectioncontent     {sectioncontent}         % Div

\def\t!itemgroup          {itemgroup}              % L
\def\t!item               {item}                   % Li
\def\t!itemtag            {itemtag}                % Lbl
\def\t!itemcontent        {itemcontent}            % LBody

\def\t!description        {description}            % Li
\def\t!descriptiontag     {descriptiontag}         % Lbl
\def\t!descriptioncontent {descriptioncontent}     % LBody
\def\t!descriptionsymbol  {descriptionsymbol}      % Span

\let\t!construction       \t!description
\let\t!constructiontag    \t!descriptiontag
\let\t!constructioncontent\t!descriptioncontent
\let\t!constructionsymbol \t!descriptionsymbol

\def\t!verbatimblock      {verbatimblock}          % Code
\def\t!verbatimlines      {verbatimlines}          % Code
\def\t!verbatimline       {verbatimline}           % Code
\def\t!verbatim           {verbatim}               % Code

\def\t!lines              {lines}                  % Code
\def\t!line               {line}                   % Code

\def\t!sorting            {sorting}                % Span
\def\t!synonym            {synonym}                % Span

\def\t!register           {register}               % Div
\def\t!registersection    {registersection}        % Div
\def\t!registertag        {registertag}            % Span
\def\t!registerentries    {registerentries}        % Div
\def\t!registerentry      {registerentry}          % Span
\def\t!registersee        {registersee}            % Span
\def\t!registerpages      {registerpages}          % Span
\def\t!registerpage       {registerpage}           % Span
\def\t!registerpagerange  {registerpagerange}      % Span

\def\t!table              {table}                  % Table
\def\t!tablerow           {tablerow}               % TR
\def\t!tablecell          {tablecell}              % TD
\def\t!tabulate           {tabulate}               % Table
\def\t!tabulaterow        {tabulaterow}            % TR
\def\t!tabulatecell       {tabulatecell}           % TD

\def\t!math               {math}                   % math
\def\t!mathtable          {mtable}                 % Table
\def\t!mathtablerow       {mtr}                    % TR
\def\t!mathtablecell      {mtd}                    % TD
\def\t!mathaction         {maction}                %

\def\t!list               {list}                   % TOC
\def\t!listitem           {listitem}               % TOCI
\def\t!listtag            {listtag}                % Lbl
\def\t!listcontent        {listcontent}            % P
\def\t!listdata           {listdata}               % P
\def\t!listpage           {listpage}               % Reference

\def\t!delimitedblock     {delimited}              % BlockQuote
\def\t!delimited          {delimited}              % Quote
\def\t!subsentence        {subsentence}            % Span

\def\t!float              {float}                  % Div
\def\t!floatcaption       {floatcaption}           % Caption
\def\t!floatlabel         {floatlabel}             % Span
\def\t!floattext          {floattext}              % Span
\def\t!floatnumber        {floatnumber}            % Span
\def\t!floatcontent       {floatcontent}           % P

\def\t!image              {image}                  % P

\def\t!mpgraphic          {mpgraphic}              % P

\def\t!formulaset         {formulaset}             % Div
\def\t!formula            {formula}                % Div
\def\t!formulacaption     {formulacaption}         % Span
\def\t!formulalabel       {formulalabel}           % Span
\def\t!formulanumber      {formulanumber}          % P
\def\t!formulacontent     {formulacontent}         % P
\def\t!subformula         {subformula}             % Div

\def\t!link               {link}                   % Link

\def\t!margintext         {margintext}             % Span
\def\t!margintextblock    {margintextblock}        % Div

% we might opt for verbose variants so this is experimental:

\def\t!label              {label}                  % Span
\def\t!number             {number}                 % Span

\def\t!ignore             {ignore}                 % Span

\def\t!mid                {mid}                    % Span
\def\t!sub                {sub}                    % Span
\def\t!sup                {sup}                    % Span
\def\t!subsup             {subsup}                 % Span

\def\t!unit               {unit}                   % Span
\def\t!quantity           {quantity}               % Span
\def\t!number             {number}                 % Span

\def\t!combination        {combination}            % Span
\def\t!combinationpair    {combinationpair}        % Span
\def\t!combinationcontent {combinationcontent}     % Span
\def\t!combinationcaption {combinationcaption}     % Span

% \setuptaglabeltext
%   [en]
%   [\t!document=document]

% the real code

\definesystemattribute[tagged][public]
\definesystemattribute[image] [public]

\unexpanded\def\setelementbackendtag{\dodoubleargument\strc_tags_set_backend}
\unexpanded\def\setelementnature    {\dodoubleargument\strc_tags_set_nature}

\def\strc_tags_set_backend[#1][#2]{\ctxcommand{settagproperty("#1","backend","#2")}}
\def\strc_tags_set_nature [#1][#2]{\ctxcommand{settagproperty("#1","nature", "#2")}}

\installcorenamespace{tagging}

\installsetuponlycommandhandler \??tagging {tagging}

\unexpanded\def\strc_tags_element_start_yes{\dodoubleempty\strc_tags_element_start_yes_indeed}
\unexpanded\def\strc_tags_element_start_nop{\dodoubleempty\strc_tags_element_start_nop_indeed}

\unexpanded\def\strc_tags_element_start_yes_indeed
  {\iftrialtypesetting
     \expandafter\strc_tags_element_start_nop_indeed
   \else
     \expandafter\strc_tags_element_start_yes_indeed_yes
   \fi}

\unexpanded\def\strc_tags_element_stop
  {\iftrialtypesetting
     \expandafter\strc_tags_element_stop_nop
   \else
     \expandafter\strc_tags_element_stop_yes
   \fi}

\unexpanded\def\strc_tags_element_start_yes_indeed_yes[#1][#2]%
  {\ctxcommand{starttag("#1",{label="\dogetupsometaglabeltext{#1}",userdata=\!!bs#2\!!es})}}

\unexpanded\def\strc_tags_element_stop_yes
  {\ctxcommand{stoptag()}}

\unexpanded\def\strc_tags_element_start_nop_indeed[#1][#2]%
  {}

\unexpanded\def\strc_tags_element_stop_nop
  {}

\unexpanded\def\strc_tags_enable_elements
  {\setuplanguage[\s!default][\s!righthyphenchar="AD]% for the moment here
   \let\startelement\strc_tags_element_start_yes
   \let\stopelement \strc_tags_element_stop_yes}

\unexpanded\def\strc_tags_disable_elements
  {\let\startelement\strc_tags_element_start_nop
   \let\stopelement \strc_tags_element_stop_nop}

% beware: making these unexpanded spoils tables (noalign problem)

\def\strc_tags_enabled_start
  {\iftrialtypesetting
     \expandafter\strc_tags_start_nop
   \else
     \expandafter\strc_tags_start_yes
   \fi}

\def\strc_tags_enabled_stop
  {\iftrialtypesetting
     \expandafter\strc_tags_stop_nop
   \else
     \expandafter\strc_tags_stop_yes
  \fi}

\def\strc_tags_start_yes#1#2% we could have a fast labeltext resolver
  {\ctxcommand{starttag("#1",{label="\dogetupsometaglabeltext{#1}",detail="#2"})}}

\def\strc_tags_stop_yes
  {\ctxcommand{stoptag()}}

\def\strc_tags_start_nop#1#2%
  {}

\def\strc_tags_stop_nop
  {}

\unexpanded\def\strc_tags_enable
  {\let\dostarttagged\strc_tags_start_yes
   \let\dostoptagged \strc_tags_stop_yes}

\unexpanded\def\strc_tags_disable
  {\let\dostarttagged\strc_tags_start_nop
   \let\dostoptagged \strc_tags_stop_nop}

%D The triggers:

\newtoks\everyenableelements
\newtoks\everydisableelements

\appendtoks
    \strc_tags_enable_elements
    \doifelse{\taggingparameter\c!method}\v!auto\strc_tags_enable\strc_tags_disable
\to \everyenableelements

\appendtoks
    \strc_tags_disable_elements
    \strc_tags_disable
\to \everydisableelements

\appendtoks
   \doifelse{\taggingparameter\c!state}\v!start{\the\everyenableelements}{\the\everydisableelements}%
\to \everysetuptagging

\setuptagging
  [\c!state=\v!stop,
   \c!method=\v!auto]

% \unexpanded\def\startparagraph
%   {\dostarttagged\t!paragraph\empty}
%
% \unexpanded\def\stopparagraph
%   {% \removeunwantedspaces % yes or no
%    \dostoptagged
%    \par}

% Cf suggestion by Wolfgang we now have named paragraphs. Watch out, the content
% is grouped but only when we have an instance.
%
% \defineparagraph[red] [color=red]
% \defineparagraph[bold][style=bold]
%
% \startparagraph       \input ward \stopparagraph
% \startparagraph[red]  \input ward \stopparagraph
% \startparagraph[bold] \input ward \stopparagraph

\installcorenamespace               {paragraph}
\installcommandhandler \??paragraph {paragraph} \??paragraph

\setupparagraph % someday maybe also strut (beg/end)
  [\c!color=,
   \c!style=]

\unexpanded\def\startparagraph
   {\dodoubleempty\paragraph_start}

\def\paragraph_start
   {\iffirstargument
      \ifsecondargument
        \doubleexpandafter\paragraph_start_two
      \else
        \doubleexpandafter\paragraph_start_one
      \fi
    \else
      \expandafter\paragraph_start_zero
    \fi}

\def\paragraph_start_two[#1][#2]%
   {\endgraf % we end before the group
    \begingroup
    \let\stopparagraph\paragraph_stop_indeed
    \edef\currentparagraph{#1}%
    \setupcurrentparagraph[#2]%
    \paragraph_start_indeed}

\def\paragraph_start_one[#1][#2]%
   {\endgraf % we end before the group
    \begingroup
    \let\stopparagraph\paragraph_stop_indeed
    \doifassignmentelse{#1}
      {\let\currentparagraph\empty
       \setupcurrentparagraph[#1]}
      {\edef\currentparagraph{#1}}%
    \paragraph_start_indeed}

\def\paragraph_start_indeed
  {\useparagraphstyleandcolor\c!style\c!color
   \usealignparameter\paragraphparameter
   \usesetupsparameter\paragraphparameter
   \dostarttagged\t!paragraph\currentparagraph}

\def\paragraph_start_zero[#1][#2]%
   {\let\currentparagraph\empty
    \paragraph_start_indeed}

\unexpanded\def\paragraph_stop_indeed
   {\dostoptagged
    \endgraf % we end inside the group
    \endgroup}

\unexpanded\def\stopparagraph
   {\dostoptagged}

\let\startpar\startparagraph
\let\stoppar \stopparagraph

\appendtoks
    \dostarttagged\t!document\empty
\to \everystarttext

\appendtoks
    \dostoptagged
\to \everystoptext

\appendtoks
    \strc_tags_disable_elements
    \strc_tags_disable
\to \everybeforepagebody

% \doifinelementelse{structure:section}            {yes} {no}
% \doifinelementelse{structure:chapter}            {yes} {no}
% \doifinelementelse{division:*-structure:chapter} {yes} {no}

\unexpanded\def\doifinelementelse#1%
  {\ctxcommand{testcase(structures.atlocation("#1"))}}

\unexpanded\def\taggedlabeltexts#1#2#3% experimental: label, numberdetail, numbercontent
  {\begingroup
   \dostarttagged\t!label{#1}%
   \labeltexts{#1}%
     {\dostoptagged
      \dostarttagged\t!number{#2}%
      #3%
      \dostoptagged
      \dostarttagged\t!label{#1}}%
   \dostoptagged
   \endgroup}

\unexpanded\def\namedtaggedlabeltexts#1#2#3#4#5% experimental: labeltag label numbertag numberdetail numbercontent
  {\begingroup
   \dostarttagged{#1}{#2}%
   \labeltexts{#2}%
     {\dostoptagged
      \dostarttagged{#3}{#4}%
      #5%
      \dostoptagged
      \dostarttagged{#1}{#2}}%
   \dostoptagged
   \endgroup}

%D Metadata is added after the following structure element so
%D here we get some as child of the document root and some as child
%D of the chapter element.
%D
%D \settaggedmetadata[title=Hello World!,author=Hans Hagen]
%D
%D \starttyping
%D \starttext
%D   \startelement[ignore]
%D     \input tufte
%D   \stopelement
%D   \par \input ward \par
%D   \settaggedmetadata[whatever=Again and Again]
%D   \startchapter[title=test]
%D     \input ward
%D   \stopchapter
%D \stoptext
%D \stoptyping

\unexpanded\def\settaggedmetadata[#1]%
  {\ctxlua{structures.tags.registermetadata(\!!bs#1\!!es)}}

%D An overload:

\let\strc_tagged_saved_bpar\bpar
\let\strc_tagged_saved_epar\epar

\unexpanded\def\bpar{\dostarttagged\t!paragraph\empty\strc_tagged_saved_bpar}
\unexpanded\def\epar{\strc_tagged_saved_epar\dostoptagged}

\protect
