<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
version="1.0">

<!--
$Id: xh2latex.xsl,v 1.3 2004/05/06 20:35:11 connolly Exp $

cribbed heavily from
XSLT from XHTML+MathML to LATEX
Eitan M. Gurari July 19, 2000
http://www.cse.ohio-state.edu/~gurari/docs/mml-00/xhm2latex.html#x1-20001

see also

Beginner's LaTeX Tutorial
http://www.csclub.uwaterloo.ca/u/sjbmann/tutorial.html
-->

<xsl:output method="text"/>

<xsl:param name="Bib" select='"bibdata"'/>
<xsl:param name="BibStyle" select='"theunsrt"'/>
<xsl:param name="Status" select='"prepub"'/>


<xsl:template match="xhtml:html">
  <xsl:text>\documentclass[a4paper,11pt]{article} % -*- coding: utf-8 -*-
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[expert]{lucidabr}

\usepackage{here}
\usepackage[labelfont={bf}]{caption}

\usepackage{fancyvrb}
\CustomVerbatimEnvironment{EiffelVerbatim}
  {Verbatim}
  {fontsize=\small,tabsize=4,obeytabs=true,commandchars=\\\{\}}

\makeatletter
\uc@dclc{9001}{default}{\ensuremath{\langle}}
\uc@dclc{9002}{default}{\ensuremath{\rangle}}
\makeatother

\usepackage{ifpdf}
\ifpdf
  \usepackage[pdftex]{graphicx}
  \usepackage[pdftex]{color}
  \usepackage[pdftex]{hyperref}
  \hypersetup{
    colorlinks=true,
    linkcolor=black,
    pdftitle={</xsl:text>

<xsl:value-of select="xhtml:body/xhtml:div[@class='maketitle']/xhtml:h1"/>

<xsl:text>},
    pdfauthor={Peter Brandt and Martin Geisler},
    pdfsubject={Object Oriented Software Construction},
    pdfkeywords={},
    pdfpagemode=none,
    pdfstartview=FitH
  }
\else
  \usepackage[dvips]{graphicx}
  \usepackage[dvips]{color}
\fi


\usepackage{url}

\definecolor{darkblue}{rgb}{0.0, 0.0, 0.6}
\definecolor{brown}{rgb}{0.5, 0.2, 0.0}
\definecolor{darkred}{rgb}{0.6, 0.0, 0.0}

\newcommand\keyword{\begingroup\color{darkblue}\bfseries\urlstyle{sf}\Url}
\newcommand\class{\begingroup\color{blue}\itshape\urlstyle{sf}\Url}
\newcommand\feature{\begingroup\color{black}\itshape\urlstyle{sf}\Url}
\newcommand\variable{\begingroup\color{brown}\itshape\urlstyle{sf}\Url}
\newcommand\litteral{\begingroup\color{black}\urlstyle{sf}\Url}
\newcommand\comment[1]{\textcolor{darkred}{\textsf{#1}}}

%\usepackage{geometry}

\begin{document}
  </xsl:text>

  <xsl:apply-templates select="xhtml:body"/>

  <xsl:text>
\end{document}
  </xsl:text>
</xsl:template>


<!-- suppress online-only material -->
<xsl:template match="*[@class='online']">
  <!-- noop -->
</xsl:template>

<xsl:template match="*[@class='online']" mode="maketitle">
  <!-- noop -->
</xsl:template>


<!-- frontmatter: title page -->
<xsl:template match="xhtml:div[@class='maketitle']">
  <xsl:apply-templates mode="maketitle"/>

  <xsl:text>

\maketitle

  </xsl:text>

</xsl:template>

<xsl:template match="xhtml:div[@class='date']" mode="maketitle">
  <xsl:text>\date{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:h1" mode="maketitle">
  <xsl:text>\title{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:address" mode="maketitle">
  <xsl:text>\author{</xsl:text>
  <xsl:for-each select='xhtml:a[@rel="author"]'>
    <xsl:value-of select='.'/>
    <xsl:if test='following-sibling::xhtml:a[@rel="author"]'>
      <xsl:text> \and </xsl:text>
    </xsl:if>
  </xsl:for-each>
  
  <xsl:for-each select='*[@class="thanks"]'>
    <xsl:text>\thanks{</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>}</xsl:text>
  </xsl:for-each>
  
  <xsl:text>}
  </xsl:text>
  
</xsl:template>

<xsl:template match="id('toc')">

  <xsl:text>

\tableofcontents

  </xsl:text>

</xsl:template>

<!-- abstract -->
<xsl:template match="xhtml:div[@class='abstract']">
  <xsl:text>
\begin{abstract}
  </xsl:text>

  <xsl:apply-templates/>

  <xsl:text>
\end{abstract}
  </xsl:text>
</xsl:template>

<!-- suppress redundant heading -->
<xsl:template match="xhtml:h4[normalize-space()='Abstract']">
  <!-- noop -->
</xsl:template>


<xsl:template match="xhtml:br">
  <xsl:text>\\</xsl:text>
</xsl:template>

<!-- body sections -->
<xsl:template match="xhtml:h1">
  <xsl:text>\section{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}
  </xsl:text>
<!-- @@
  <xsl:apply-templates select="xhtml:a[@name]" mode="section-label"/>
-->
</xsl:template>


<xsl:template match="xhtml:h2">
  <xsl:text>\subsection{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}
  </xsl:text>
</xsl:template>

<xsl:template match="xhtml:h3">
  <xsl:text>\subsubsection{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}
  </xsl:text>
</xsl:template>

<!-- section labels. @@untested -->
<xsl:template match="xhtml:a[@name]" mode="section-label">
  <xsl:variable  name="var" select="concat('#',@name)" />
  <xsl:if test="//xhtml:a[@href=$var]
	  ">
    <xsl:text>\label{</xsl:text>
    <xsl:value-of select="@name" />
    <xsl:text>}</xsl:text>
  </xsl:if>
</xsl:template>


<!-- lists -->
<xsl:template match="xhtml:ul">
  <xsl:text>\begin{itemize}
  </xsl:text>
  <xsl:for-each select="xhtml:li">
    <xsl:text>\item </xsl:text>
    <xsl:apply-templates />
  </xsl:for-each>
  <xsl:text>
    \end{itemize}
  </xsl:text>
</xsl:template>

<xsl:template match="xhtml:ol">
  <xsl:text>\begin{enumerate}
  </xsl:text>
  <xsl:for-each select="xhtml:li">
    <xsl:text>\item </xsl:text>
    <xsl:apply-templates />
  </xsl:for-each>
  <xsl:text>
    \end{enumerate}
  </xsl:text>
</xsl:template>

<!-- ol, img code untested -->
<xsl:template match="xhtml:ol[@class='enumerate1']">
  <xsl:text>\begin{enumerate}</xsl:text>
  <xsl:for-each select="xhtml:li">
    <xsl:text>\item </xsl:text>
    <xsl:apply-templates />
  </xsl:for-each>
  <xsl:text>\end{enumerate}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:img">
  <xsl:text>
\begin{figure}[H]
\centering
\includegraphics{</xsl:text>
<xsl:value-of select="@src"/>
<xsl:text>}
\caption{</xsl:text>
<xsl:value-of select="@alt"/>
<xsl:text>}
\end{figure}
</xsl:text>
  
</xsl:template>


<!-- equasion -->
<xsl:template match="xhtml:p[@class='eqn-display']">
  <xsl:text>
    \begin{equation}
  </xsl:text>
  <xsl:apply-templates mode="math"/>
  <xsl:text>
    \end{equation}
  </xsl:text>
</xsl:template>

<!-- example, definition -->
<xsl:template match='xhtml:pre[@class]'>
  <xsl:text>\begin{</xsl:text>
  <xsl:value-of select='@class'/>
  <xsl:text>}</xsl:text>
  <xsl:text>\begin{verbatim}</xsl:text>
  <xsl:text>&#10;</xsl:text>
  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates mode="verbatim"/>
  <xsl:text>\end{verbatim}</xsl:text>
  <xsl:text>\end{</xsl:text>
  <xsl:value-of select='@class'/>
  <xsl:text>}</xsl:text>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<!-- misc pre/verbatim -->
<xsl:template match="xhtml:pre">
  <xsl:text>\begin{EiffelVerbatim}</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>\end{EiffelVerbatim}</xsl:text>
</xsl:template>


<!-- paragraphs -->

<xsl:template match="xhtml:p">
  <xsl:text>&#10;</xsl:text>
  <xsl:text>\par </xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="xhtml:p[@class='nopar']">
  <xsl:text>\empty </xsl:text>
  <xsl:apply-templates/>
  <xsl:text>\empty </xsl:text>
</xsl:template>

<xsl:template match='xhtml:p[@class="proposition" or @class="definition"]'>
  <xsl:text>\begin{</xsl:text>
  <xsl:value-of select='@class'/>
  <xsl:text>}</xsl:text>
  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>\end{</xsl:text>
  <xsl:value-of select='@class'/>
  <xsl:text>}</xsl:text>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<!-- phrase markup -->

<xsl:template match="xhtml:em">
  <xsl:text>\emph{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:code[@class='keyword']">
  <xsl:text>\keyword{</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:code[@class='class-name']">
  <xsl:text>\class{</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:code[@class='feature-name']">
  <xsl:text>\feature{</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:code[@class='string']">
  <xsl:text>\litteral{</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:code[@class='comment']">
  <xsl:text>\comment{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:code[@class='variable']">
  <xsl:text>\variable{</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:code">
  <xsl:text>\texttt{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:tt">
  <xsl:text>\texttt{</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:q">
  <xsl:text>``</xsl:text>
  <xsl:apply-templates />
  <xsl:text>''</xsl:text>
</xsl:template>

<xsl:template match="xhtml:samp">
  <!-- pass-thru, for \Sigma -->
  <xsl:text>$</xsl:text>
  <xsl:value-of select='.'/>
  <xsl:text>$</xsl:text>
</xsl:template>

<xsl:template match="xhtml:samp" mode="math">
  <!-- pass-thru, for \Sigma -->
  <xsl:value-of select='.'/>
</xsl:template>


<!-- citation links -->
<xsl:template match='xhtml:a[starts-with(., "[")]'>
  <xsl:text>\cite{</xsl:text>
  <xsl:value-of select="substring-after(@href,'#')"/>
  <xsl:text>}</xsl:text>
</xsl:template>

<!-- wierd phrase markup. @@not tested -->
<xsl:template match="xhtml:span[@class='cmtt-10']">
  <xsl:text>{\tt </xsl:text>
  <xsl:apply-templates />
  <xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="xhtml:span[@class='cmti-10']">
  <xsl:text>{\it </xsl:text>
  <xsl:apply-templates />
  <xsl:text>}</xsl:text>
</xsl:template>


<!-- text -->
<xsl:template match="text()">
  <!-- 
  per latex tutorial, the following need escaping: # $ % & ~ _ ^ \ { }
  -->

  <xsl:call-template name="esc">
    <xsl:with-param name="c" select='"#"'/>
    <xsl:with-param name="s">
      <xsl:call-template name="esc">
	<xsl:with-param name="c" select='"$"'/>
	<xsl:with-param name="s">
	  <xsl:call-template name="esc">
	    <xsl:with-param name="c" select='"%"'/>
	    <xsl:with-param name="s">
	      <xsl:call-template name="esc">
		<xsl:with-param name="c" select='"&amp;"'/>
		<xsl:with-param name="s">
		  <xsl:call-template name="esc">
		    <xsl:with-param name="c" select='"~"'/>
		    <xsl:with-param name="s">
		      <xsl:call-template name="esc">
			<xsl:with-param name="c" select='"_"'/>
			<xsl:with-param name="s">
			  <xsl:call-template name="esc">
			    <xsl:with-param name="c" select='"^"'/>
			    <xsl:with-param name="s">
			      <xsl:call-template name="esc">
				<xsl:with-param name="c" select='"{"'/>
				<xsl:with-param name="s">
				  <xsl:call-template name="esc">
				    <xsl:with-param name="c" select='"}"'/>
				    <xsl:with-param name="s">
				      <xsl:call-template name="esc">
					<xsl:with-param name="c" select='"\"'/>
					<xsl:with-param name="s" select='.'/>
				      </xsl:call-template>
				    </xsl:with-param>
				  </xsl:call-template>
				</xsl:with-param>
			      </xsl:call-template>
			    </xsl:with-param>
			  </xsl:call-template>
			</xsl:with-param>
		      </xsl:call-template>
		    </xsl:with-param>
		  </xsl:call-template>
		</xsl:with-param>
	      </xsl:call-template>
	    </xsl:with-param>
	  </xsl:call-template>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template name="esc">
  <xsl:param name="s"/>
  <xsl:param name="c"/>

  <xsl:choose>
    <xsl:when test='contains($s, $c)'>
      <xsl:value-of select='substring-before($s, $c)'/>
      <xsl:text>\</xsl:text>

      <xsl:choose>
	<xsl:when test='$c = "\"'>
	  <xsl:text>textbackslash </xsl:text>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select='$c'/>
	</xsl:otherwise>
      </xsl:choose>

      <xsl:call-template name="esc">
	<xsl:with-param name='c' select='$c'/>
	<xsl:with-param name='s' select='substring-after($s, $c)'/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select='$s'/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- bibliography -->
<xsl:template match="xhtml:h2[normalize-space()='References']">
  <!-- noop. suppress redundant heading -->
</xsl:template>

<xsl:template match="xhtml:dl[@class='bib']">
  <xsl:text>\bibliographystyle{</xsl:text>
  <xsl:value-of select='$BibStyle'/>
  <xsl:text>}</xsl:text>
  
  <xsl:text>\bibliography{</xsl:text>
  <xsl:value-of select='$Bib'/>
  <xsl:text>}</xsl:text>

  <!-- the content of this dl is handled by xh2bib.xsl -->
</xsl:template>


<!-- table markup. unused/untested@@ -->
<xsl:template match="xhtml:table[@class='tabular'
	      or @class='inline-tabular']">
  <xsl:if test="@class='tabular'">
    <xsl:text>\par </xsl:text>
  </xsl:if>
  <xsl:text>\begin{tabular}{</xsl:text>
  <xsl:value-of select="comment()[last()]"/>
  <xsl:text>}</xsl:text>
  <xsl:apply-templates select="xhtml:tr" mode="tabular"/>
  <xsl:text>\end{tabular}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:tr" mode="tabular">
  <xsl:apply-templates select="xhtml:td" mode="tabular"/>
   <xsl:if test="position()!=last()">
     <xsl:text>\\
     </xsl:text>
  </xsl:if>
</xsl:template>

<xsl:template match="xhtml:td" mode="tabular">
  <xsl:if test="position()!=1">
    <xsl:text>\@@ampersand@@%
    </xsl:text>
  </xsl:if>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="xhtml:tr[@class='hline']" mode="tabular">
  <xsl:text>\hline </xsl:text>
</xsl:template>
<xsl:template match="xhtml:tr[@class='hline']">
  <xsl:text>\hline </xsl:text>
</xsl:template>


<xsl:template match="xhtml:td[@class='figure']">
  <xsl:text>\begin{figure}</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>\end{figure}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:div[@class='caption']">
  <xsl:apply-templates select="xhtml:table" mode="caption" />
</xsl:template>

<xsl:template match="xhtml:table" mode="caption">
  <xsl:apply-templates select="xhtml:tr" mode="caption" />
</xsl:template>

<xsl:template match="xhtml:tr" mode="caption">
  <xsl:apply-templates select="xhtml:td[2]" mode="caption" />
</xsl:template>

<xsl:template match="xhtml:td" mode="caption">
  <xsl:text>\caption{</xsl:text>
  <xsl:apply-templates />
  <xsl:text>}</xsl:text>
</xsl:template>


<!-- @@hmm... links? untested -->
<xsl:template match="xhtml:a" >
  <xsl:choose>
    <xsl:when test="descendant::comment()[starts-with(.,'tex4ht:ref:')]">
      <xsl:text>\ref{</xsl:text>
      <xsl:value-of select="substring-after(@href,'#')"/>
      <xsl:text>}% </xsl:text>
      <xsl:apply-templates mode="ref"
			   select="descendant::comment()[starts-with(.,'tex4ht:ref:')]" />
      <xsl:text>
      </xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>\empty </xsl:text>
      <xsl:apply-templates />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template  mode="ref"
	       match="comment()[starts-with(.,'tex4ht:ref:')]">
  <xsl:value-of select="substring-after(.,'tex4ht:ref:')"/>
</xsl:template>

<xsl:template match="comment()[starts-with(.,'tex4ht:label?:')]">
  <xsl:text>\label{\empty </xsl:text>
  <xsl:value-of select="substring-after(.,'tex4ht:label?:')"/>
  <xsl:text>}</xsl:text>
</xsl:template>






</xsl:stylesheet>


