%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% tei.xmt

%% Copyright 2000 Sebastian Rahtz, David Carlisle

%% This file is distributed under the LaTeX Project Public License
%% (LPPL) as found at http://www.latex-project.org/lppl.txt
%% Either version 1.0, or at your option, any later version.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\XMLname{div}{\DIV}
\XMLname{figure}{\FIGURE}
\XMLname{table}{\TABLE}
\XMLstring\type@equation<>equation</>
\XMLstring\type@display<>display</>
\XMLstring\type@subeqn<>subeqn</>

\XMLelement{TEI.2}{}
  { \documentclass{article}
     \usepackage[bookmarks=false]{hyperref}
     \usepackage{teixml}
     \begin{document}  }
  {\end{document}}

\XMLelement{p}{}
 {\par}
 {}

\newcount\SCOUNT
\global\SCOUNT-1\relax

\XMLelement{div}{
 \XMLattribute{id}{\idval}{\@nil}}
 {\advance\SCOUNT\@ne}
 {}

\XMLelement{head}{  }
  {\xmlgrab}
  {
  \ifx\XML@parent\DIV
   \protected@xdef\temp{
     \expandafter\noexpand\ifcase\SCOUNT
     \section\or\subsection\or\subsubsection\fi
     {#1}\ifx\idval\@nnil\else\noexpand\label{\idval}
   \fi}
   \aftergroup\temp
   \else
     \ifx\XML@parent\TABLE\caption{#1}\fi
     \ifx\XML@parent\FIGURE\caption{#1}\fi
   \fi

}


\XMLelement{emph}{}
 {\itshape}
 {}

\XMLelement{formula}
 {\XMLattribute{type}{\Formulatype}{}}
 {
  \ifx\Formulatype\type@equation
%       \begin{equation}
  \fi
  }
 {
  \ifx\Formulatype\type@equation
%       \end{equation}
  \fi
  }

\XMLelement{figure}
 {\XMLattribute{id}{\figid}{}
  \XMLattribute{width}{\FIGwidth}{}
  \XMLattribute{height}{\FIGheight}{}
  \XMLattribute{file}{\figfile}{}
  \XMLattribute{entity}{\figent}{}
  }
 {\begin{figure}
  \ifx\FIGwidth\@empty\else\setkeys{Gin}{width=\FIGwidth}\fi
  \ifx\FIGheight\@empty\else\setkeys{Gin}{height=\FIGheight}\fi
   \ifx\@empty\figfile
     \NDATAEntity\figent\@gobble\includegraphics
   \else
     \includegraphics{\figfile}%
   \fi}
 {\ifx\@empty\figid
  \else
    \label{\figid}
  \fi
  \end{figure}}

\XMLelement{code}{}
 {\ttfamily}
 {}

\XMLelement{gi}{}
 {\ttfamily\textless}
 {\textgreater}

\XMLelement{eg}{}
 {\begin{quote}\ttfamily\obeylines}
 {\end{quote}}

\XMLelement{teiHeader}{}
 {\xmlgrab}
 {\xmltexfirstchild#1\@empty\@}

\XMLelement{fileDesc}{}
 {}
 {}

\XMLelement{titleStmt}{}
 {\xmlgrab}
 {}

\XMLelement{sourceDesc}{}
 {\xmlgrab}
 {}

\XMLelement{availability}{}
 {\xmlgrab}
 {\gdef\header@availability{#1}}

\XMLelement{docTitle}{}
 {\xmlgrab}
 {\gdef\nopar{\let\par\relax}
  \title{#1}%
  % \ifx\\unskip\thanks{\protect\nopar\ignorespaces\header@availability}
  }

\XMLelement{xptr}
 {
   \XMLattribute{doc}{\xptrdoc}{}
   \XMLattribute{url}{\xptrurl}{}
 }
 {
 \ifx\@empty\xptrurl
  \NDATAEntity\xptrdoc\@gobble\url
 \else
   \url{\xptrurl}
 \fi
 }
 {}

\XMLelement{docDate}{}
 {\xmlgrab}
 {\date{#1}}

\XMLelement{docAuthor}{}
 {\xmlgrab}
 {\author{#1}}

\XMLelement{front}{}
 {}
 {\maketitle}

\XMLelement{item}
 {}
 {\csname Item\listtype\endcsname}
 {}

\XMLelement{label}
 {}
 {\xmlgrab}
 {\gdef\foo{\item[#1]}\aftergroup\foo}

\XMLelement{list}{
 \XMLattribute{type}{\listtype}{unordered}}
 {\csname List\listtype\endcsname}
 {\csname endList\listtype\endcsname}

\XMLelement{q}{
 \XMLattribute{rend}{\qrend}{}}
 {\begin{quote}
   \ifthenelse{\equal{\qrend}{eg}}{\ttfamily\obeylines}{}}
 {\end{quote}}

\XMLelement{ptr}
 {\XMLattribute{target}{\ptrtarget}{}}
 {\hyperref[\ptrtarget]{\ref{\ptrtarget}}}
 {}

\XMLelement{ref}
 {\XMLattribute{target}{\reftarget}{}}
 {\xmlgrab}
 {\hyperref[\reftarget]{#1}}


\XMLelement{table}
 {\XMLattribute{rend}{\tablerend}{}}
 {\begin{longtable}{*{99}{l}}}
 {\end{longtable}}

\XMLelement{row}{}
 {\xmlgrab}
 {\gdef\temp{#1\\}\aftergroup\temp}

\XMLelement{cell}{}
 {\xmlgrab}
 {\gdef\temp{#1\tabcellsep}\aftergroup\temp}



%%%% bibliography

\XMLelement{listBibl}
 {}
 {\begin{thebibliography}{99}}
 {\end{thebibliography}}

\XMLelement{bibl}
 {\XMLattribute{id}{\bibid}{}}
 {\bibitem{\bibid}}
 {}

% cheating, a bit
\XMLentity{TeX}{\TeX}
\XMLentity{LaTeX}{\LaTeX}
