%D \module
%D   [       file=setupe,
%D        version=2001.10.24,
%D          title=\CONTEXT\ Setup to XML
%D       subtitle=Help Generation,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for 
%C details. 

\let\wait\relax

\input setupa

\unprotect

\def\SETUPnamespace{xmlns:cd="http://www.pragma-ade.com/commands"}

\def\c!command!        {cd:command}
\def\c!dimension!      {cd:dimension}
\def\c!filename!       {cd:file}
\def\c!identifier!     {cd:name}
\def\c!character!      {cd:character}
\def\c!marker!         {cd:mark}
\def\c!number!         {cd:number}
\def\c!reference!      {cd:reference}
\def\c!plural!         {cd:plural}
\def\c!singular!       {cd:singular}
\def\c!text!           {cd:text}
\def\c!formula!        {cd:formula}
\def\c!font!           {cd:file}
\def\c!matrix!         {cd:matrix}
\def\c!list!           {cd:list}
\def\c!section!        {cd:section}
\def\c!sectionnumber!  {cd:sectionnumber}
\def\c!noargument!     {cd:noargument}
\def\c!oneargument!    {cd:oneargument}
\def\c!twoarguments!   {cd:twoarguments}
\def\c!threearguments! {cd:threearguments}

\def\subsetup#1{*#1} % ? 

\beginTEX 

\def\getinterfaceconstant#1%
  {\ifinterfacetranslation
     \ifx\csname\x!prefix!#1\endcsname\relax
       #1\else\csname\x!prefix!#1\endcsname
     \fi
   \else
     #1%
   \fi}

\def\getinterfacevariable#1%
  {\ifinterfacetranslation
     \ifx\csname\y!prefix!#1\endcsname\relax
       #1\else\csname\y!prefix!#1\endcsname
     \fi
   \else
     #1%
   \fi}

\endTEX

\beginETEX \ifcsname

\def\getinterfaceconstant#1%
  {\ifinterfacetranslation
     \ifcsname\x!prefix!#1\endcsname
       \csname\x!prefix!#1\endcsname\else#1%
     \fi
   \else
     #1%
   \fi}

\def\getinterfacevariable#1%
  {\ifinterfacetranslation
     \ifcsname\y!prefix!#1\endcsname
       \csname\y!prefix!#1\endcsname\else#1%
     \fi
   \else
     #1%
   \fi}

\endETEX

\let\typespec\empty

\long\def\dovalvar#1%
  {\increment\currentwhatever
   \scratchcounter=0
   \dostring{\space\space\space\space}{}{#1\typespec}
   \let\typespec\empty
   \currentsetup
   \let\typespec\empty
   \dostring{\space\space\space\space}{/}{#1}}

\long\def\doanother#1%
  {\dostring{\space\space\space\space}{}{#1\typespec/}
   \let\typespec\empty} 

\long\def\addtypespec#1#2%
  {\edef\typespec{\typespec\space#1="#2"}}

\def\c!opt!    {\addtypespec{optional}{yes}}
\def\c!optint! {\addtypespec{interactive}{yes}}
\def\c!alwint! {\addtypespec{interactive}{exclusive}}

\def\c!val!    {\dovalvar{keywords}}
\def\c!var!    {\dovalvar{assignments}}

\def\c!vals!   {\addtypespec{list}{yes}\dovalvar{keywords}}
\def\c!vars!   {\addtypespec{list}{yes}\dovalvar{assignments}}

\def\c!arg!    {\doanother{content}}    
\def\c!cmd!    {\doanother{csname}}    
\def\c!dest!   {\doanother{reference}}
\def\c!dis!    {\doanother{displaymath}}    
\def\c!fil!    {\doanother{file}}    
\def\c!idx!    {\doanother{index}}    
\def\c!mat!    {\doanother{math}}    
\def\c!nop!    {\doanother{nothing}}    
\def\c!pos!    {\doanother{position}}    
\def\c!ref!    {\doanother{reference}}    
\def\c!trip!   {\doanother{triplet}}   
\def\c!wrd!    {\doanother{word}}    

\def\c!args!   {\addtypespec{list}{yes}\c!arg!}
\def\c!dests!  {\addtypespec{list}{yes}\c!dest!}
\def\c!idxs!   {\addtypespec{list}{yes}\c!idx!}
\def\c!poss!   {\addtypespec{list}{yes}\c!pos!}   
\def\c!refs!   {\addtypespec{list}{yes}\c!ref!}   
\def\c!wrds!   {\addtypespec{list}{yes}\c!wrd!}   
\def\c!trips!  {\addtypespec{list}{yes}\c!trip!}

\def\c!par!    {\addtypespec{delimiter}{par}}       % \par
\def\c!sep!    {\addtypespec{separator}{backslash}} % \\    
\def\c!stp!    {}

\def\c!repeat! {} 
\def\c!tex!  #1{\addtypespec{command}{#1}\doanother{tex}}    
\def\c!or! #1#2{\doline{\space\space\space\space<choice>}%
                #1#2%
                \doline{\space\space\space\space</choice>}}  

% wrapper 

\def\startsetupfile
  {\immediate\openout\scratchwrite=cont-\currentlanguage.xml 
   \doline{\string<?xml version="1.0"?>}
   \doline{}
   \dostring{}{}{interface \SETUPnamespace\space name="context"}}

\def\stopsetupfile
  {\doline{}
   \dostring{}{/}{interface}
   \immediate\closeout\scratchwrite}

\let\documenteduntilhere\relax

\bgroup \catcode`\<=\@@other

\unexpanded\gdef\dostring#1#2#3{\immediate\write\scratchwrite{#1<#2cd:#3>}}
           \gdef\doline      #1{\immediate\write\scratchwrite{#1}}

\egroup

\def\doval#1#2#3%
  {\doifelse{#1}{#3}
     {\def\valattr{ default="yes"}}
     {\let\valattr\empty}%
   \doifdefinedelse{c!#3!}
     {\dostring{#2}{}{variable type="cd:#3"\valattr/}}
     {\dostring{#2}{}{constant type="#3"\valattr/}}}

\long\def\docommand[#1]%
  {\def\currentname{#1}}

\long\def\dotype[#1]%
  {\def\currenttype{#1}}

\long\def\dovalue[#1]#2[#3]%
  {\advance\scratchcounter 1 
   \ifnum\scratchcounter=\currentwhatever\relax
     \processcommalist[#1]{\doval{#3}{\space\space\space\space\space\space}}
   \fi}

\newif\ifinvariables

\long\def\dovariable[#1]#2[#3]#4[#5]% comes as sequence 
  {\ifinvariables\else\advance\scratchcounter 1 \fi  
   \ifnum\scratchcounter=\currentwhatever\relax
     \invariablestrue
     \dostring{\space\space\space\space\space\space}{}%
       {parameter name="\getinterfaceconstant{#1}"}
     \processcommalist[#3]
       {\doval{}{\space\space\space\space\space\space\space\space}} 
     \dostring{\space\space\space\space\space\space}{/}{parameter}
   \fi}

\long\def\doinheritvalues[#1]#2[#3]%
  {\advance\scratchcounter 1 
   \ifnum\scratchcounter=\currentwhatever\relax
     \dostring{\space\space\space\space\space\space}{}{inherit name="#1"/}
   \fi}

\long\def\doinheritvariables[#1]#2[#3]%
  {\ifinvariables\else \advance\scratchcounter 1 \fi
   \ifnum\scratchcounter=\currentwhatever\relax
     \dostring{\space\space\space\space\space\space}{}{inherit name="#1"/}
   \fi}

\long\def\nocommand               [#1]{}
\long\def\notype                  [#1]{}
\long\def\novalue           [#1]#2[#3]{}
\long\def\novariable  [#1]#2[#3]#4[#5]{}
\long\def\noinheritvalues   [#1]#2[#3]{}
\long\def\noinheritvariables[#1]#2[#3]{}

\bgroup \catcode`\<=\active 

\gdef\cleanupcurrentname{\def<<##1>>{##1}}

\gdef\findvariablename#1%
  {\bgroup
   \def<<##1>>{\gdef\varname{##1}}%
   \global\let\varname\empty
   \setbox\scratchbox=\hbox{#1}%
   \egroup}

\gdef\splitcurrentname{\@EA\dosplitcurrentname\currentname<<>><<>>\end}

\gdef\dosplitcurrentname#1<<#2>>#3<<>>#4\end
  {\def\prename{#1}\def\midname{#2}\def\posname{#3}}

\egroup

\newif\ifsetupisenvironment 

\long\def\startsetup#1\stopsetup
  {\bgroup
   \doline{}
   % zero pass 
   \long\def\currentsetup{#1}
   % first pass
   \let\command         \docommand
   \let\type            \dotype
   \let\value           \novalue
   \let\variable        \novariable
   \let\inheritvalues   \noinheritvalues
   \let\inheritvariables\noinheritvariables
   \currentsetup
   % second pass
   \convertargument\c!stp!\to\asciia
   \convertcommand\currenttype\to\asciib
   \let\envattr\empty
   \let\prename\empty
   \let\midname\empty
   \let\posname\empty
   \ExpandBothAfter\doifinstringelse{\asciia}{\asciib}
     {\expandafter\aftersplitstring\currentname\at start\to\currentname
      \def\envattr{ type="environment"}}
     {} % \def\envattr{ type="standalone"}}
   \convertargument<<\to\asciic
   \convertcommand\currentname\to\asciid
   \findvariablename\currentname 
   \ExpandBothAfter\doifinstringelse{\asciic}{\asciid}
     {\edef\envattr{\envattr\space generated="yes"}} 
     {} % \edef\envattr{\envattr\space generated="no"}} 
   \splitcurrentname
   \cleanupcurrentname
\doifinsetelse{\currentname}{remark,menubutton,marking*figure,referring*figure}{%
  \doline{<!-- \currentname\space skipped -->}%
}{%
   \dostring{}{}{command name="\currentname"\envattr}
     \def\next##1%
       {\ifx##1\empty
          % skip  
        \else\ifx##1\varname
          \dostring{\space\space\space\space}{}{variable value="##1"/}
        \else
          \dostring{\space\space\space\space}{}{string value="##1"/}
        \fi\fi}
     \dostring{\space\space}{}{sequence}
     \next\prename
     \next\midname
     \next\posname
   \dostring{\space\space}{/}{sequence}
   \ifx\currenttype\empty \else
     \dostring{\space\space}{}{arguments}
       \let\command         \nocommand
       \let\type            \notype
       \let\value           \dovalue
       \let\variable        \dovariable
       \let\inheritvalues   \doinheritvalues
       \let\inheritvariables\doinheritvariables
       \newcounter\currentwhatever \currenttype
     \dostring{\space\space}{/}{arguments}
   \fi
   \dostring{}{/}{command}
}%
   \egroup}

\let\stopsetup\relax

\def\convertsetupdata#1#2% only accept #1=\v!whatever
  {\doifsystemconstantelse{#1}{\edef#2{#1}}{\let#2\empty}}

{\catcode`\<=\active \input setupb }

\protect

\end
