<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:e="http://exslt.org/common"
xmlns:s="http://exslt.org/strings"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="e s"
>
<xsl:template match="@classes">
<xsl:param name="add" select="/.."/>
<xsl:if test=".|$add">
<xsl:attribute name="class"><xsl:value-of select="."/> <xsl:value-of select="$add"/></xsl:attribute>
</xsl:if>
</xsl:template>
<xsl:template match="/document/title" />
<xsl:template match="/document/docinfo" />
<xsl:template match="field_list" />
<xsl:template match="comment" />
<xsl:template match="emphasis">
<em><xsl:apply-templates /></em>
</xsl:template>
<xsl:template match="literal">
<tt class="docutils literal"><span class="pre"><xsl:apply-templates /></span></tt>
</xsl:template>
<xsl:template match="title_reference">
<cite><xsl:apply-templates/></cite>
</xsl:template>
<xsl:template match="superscript">
<sup><xsl:apply-templates/></sup>
</xsl:template>
<xsl:template match="subscript">
<sub><xsl:apply-templates/></sub>
</xsl:template>
<xsl:template match="reference">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="@refid">
<xsl:value-of select="concat('#', @refid)" />
</xsl:when>
<xsl:when test="@refuri">
<xsl:value-of select="@refuri" />
</xsl:when>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="strong">
<strong><xsl:apply-templates /></strong>
</xsl:template>
<xsl:template match="image">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="@uri" />
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="@alt" />
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="@width" />
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="@height" />
</xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template match="figure">
<div class="figure {@classes}">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="caption">
<div class="caption {@classes}">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="line_block">
<div class="line-block {@classes}">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="line_block/line">
<div class="line"><xsl:apply-templates /></div>
</xsl:template>
<xsl:template match="literal_block">
<pre class="literal-block {@classes}"><xsl:apply-templates /></pre>
</xsl:template>
<xsl:template match="literal_block/br">
<br />
</xsl:template>
<xsl:template match="doctest_block">
<pre class="doctest-block {@classes}"><xsl:apply-templates /></pre>
</xsl:template>
<xsl:template match="paragraph">
<p><xsl:apply-templates select="@classes"/><xsl:apply-templates /></p>
</xsl:template>
<xsl:template match="substitution_definition" />
<xsl:template match="block_quote">
<blockquote>
<xsl:apply-templates select="@classes"/>
<xsl:apply-templates/>
</blockquote>
</xsl:template>
<xsl:template match="block_quote/attribution" >
<p class="attribution {@classes}">
<xsl:text>— </xsl:text>
<xsl:apply-templates />
</p>
</xsl:template>
<xsl:template match="footnote_reference">
<a class="footnote-reference" id="{@ids}" href="#{@refid}">
<xsl:text>[</xsl:text>
<xsl:apply-templates />
<xsl:text>]</xsl:text>
</a>
</xsl:template>
<xsl:template match="footnote">
<xsl:variable name="label" select="label"/>
<xsl:variable name="backrefs" select="s:split(@backrefs,' ')/token"/>
<div class="footnote {@classes}" id="{@ids}">
<xsl:choose>
<xsl:when test="count($backrefs) <= 1">
<span class="label"><a class="fn-backref" href="#{$backrefs}">[<xsl:value-of select="$label"/>]</a></span>
</xsl:when>
<xsl:otherwise>
<span class="label">[<xsl:value-of select="$label"/>]</span>
<em><xsl:text> (</xsl:text>
<xsl:for-each select="$backrefs">
<a class="fn-backref" href="#{.}" title="return to content">
<xsl:value-of select="position()" />
</a>
<xsl:if test="following-sibling::token">, </xsl:if>
</xsl:for-each>
<xsl:text>)</xsl:text></em>
</xsl:otherwise>
</xsl:choose>
<xsl:text> </xsl:text>
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="footnote/label" />
<xsl:template name="list-class">
<xsl:attribute name="class">
<xsl:value-of select="@enumtype"/>
<xsl:value-of select="@classes"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="bullet_list">
<ul>
<xsl:call-template name="list-class"/>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="enumerated_list">
<ol>
<xsl:call-template name="list-class"/>
<xsl:if test="@start">
<xsl:attribute name="start"><xsl:value-of select="@start"/></xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</ol>
</xsl:template>
<xsl:template match="list_item">
<li><xsl:apply-templates select="@classes"/><xsl:apply-templates /></li>
</xsl:template>
<xsl:template match="list_item/paragraph">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="definition_list">
<dl><xsl:apply-templates select="@classes"/><xsl:apply-templates /></dl>
</xsl:template>
<xsl:template match="definition_list_item">
<xsl:apply-templates select="term|definition"/>
</xsl:template>
<xsl:template match="definition_list_item/term">
<dt><xsl:apply-templates select="@classes"/><xsl:apply-templates /> <xsl:apply-templates select="../classifier"/></dt>
</xsl:template>
<xsl:template match="definition_list_item/definition">
<dd><xsl:apply-templates select="@classes"/><xsl:apply-templates /></dd>
</xsl:template>
<xsl:template match="definition_list_item/classifier">
<span class="classifier-delimiter">:</span>
<xsl:text> </xsl:text>
<span class="classifier"><xsl:apply-templates/></span>
</xsl:template>
<xsl:template match="option_list">
<table class="docutils option-list {@classes}" cellpadding="0" cellspacing="0">
<col class="option" /><col class="description" />
<xsl:apply-templates />
</table>
</xsl:template>
<xsl:template match="option_list_item">
<tr><xsl:apply-templates /></tr>
</xsl:template>
<xsl:template match="option_group">
<td class="option-group {@classes}">
<xsl:for-each select="option">
<xsl:apply-templates select="." />
<xsl:if test="following-sibling::option">, </xsl:if>
</xsl:for-each>
</td>
</xsl:template>
<xsl:template match="option_group/option">
<kbd><span class="option"><xsl:value-of select="option_string" /></span>
<xsl:if test="option_argument">
<span class="option-delimiter"><xsl:value-of select="option_argument/@delimiter" /></span>
<var><span class="option-argument"><xsl:value-of select="option_argument" /></span></var>
</xsl:if>
</kbd>
</xsl:template>
<xsl:template match="option_list_item/description">
<td><xsl:apply-templates /></td>
</xsl:template>
<xsl:template match="raw[@format='html']">
<xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>
<xsl:template match="section/title">
<xsl:element name="{concat('h', format-number(count(ancestor::section), '#'))}">
<xsl:attribute name="id">
<xsl:value-of select="parent::section/@ids" />
</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="title" priority="-1">
<h1 class="{@classes}">
<xsl:apply-templates/>
</h1>
</xsl:template>
<xsl:template match="subtitle" priority="-1">
<h2 class="{@classes}">
<xsl:apply-templates/>
</h2>
</xsl:template>
<xsl:template match="attention|caution|danger|error|hint|important|note|tip|warning">
<div class="{local-name()} {@classes}">
<p class="admonition-title"><xsl:apply-templates select="." mode="admonition-title"/></p>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="*" mode="admonition-title">
<xsl:value-of select="local-name()"/>
</xsl:template>
<xsl:template match="admonition">
<div class="admonition {@classes}">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="admonition/title">
<p class="admonition-title"><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="topic|sidebar">
<div class="{local-name()} {@classes}">
<xsl:if test="@ids">
<xsl:attribute name="id"><xsl:value-of select="@ids"/></xsl:attribute>
<a name="{@ids}" />
</xsl:if>
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="sidebar/title|sidebar/subtitle|topic/title|topic/subtitle">
<p class="{local-name(parent::*)}-{local-name()} {@classes}">
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="rubric">
<p class="rubric {@classes}"><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="table">
<table class="docutils {@classes}" border="1">
<xsl:apply-templates />
</table>
</xsl:template>
<xsl:template match="thead">
<thead><xsl:apply-templates /></thead>
</xsl:template>
<xsl:template match="thead/row">
<tr><xsl:apply-templates /></tr>
</xsl:template>
<xsl:template match="thead/row/entry">
<xsl:element name="th">
<xsl:if test="@morecols">
<xsl:attribute name="colspan">
<xsl:value-of select="format-number(1 + @morecols, '#')" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@morerows">
<xsl:attribute name="rowspan">
<xsl:value-of select="format-number(1 + @morerows, '#')" />
</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="tbody">
<tbody><xsl:apply-templates /></tbody>
</xsl:template>
<xsl:template match="tbody/row">
<tr><xsl:apply-templates /></tr>
</xsl:template>
<xsl:template match="tbody/row/entry">
<xsl:element name="td">
<xsl:if test="@morecols">
<xsl:attribute name="colspan">
<xsl:value-of select="format-number(1 + @morecols, '#')" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@morerows">
<xsl:attribute name="rowspan">
<xsl:value-of select="format-number(1 + @morerows, '#')" />
</xsl:attribute>
</xsl:if>
<xsl:if test="ancestor::table/preceding-sibling::*[1]/field/field_name[text()='table-cell-halign']">
<xsl:attribute name="align">
<xsl:value-of select="ancestor::table/preceding-sibling::*[1]/field/field_name[text()='table-cell-halign']/../field_body/paragraph" />
</xsl:attribute>
</xsl:if>
<xsl:if test="ancestor::table/preceding-sibling::*[1]/field/field_name[text()='table-cell-valign']">
<xsl:attribute name="valign">
<xsl:value-of select="ancestor::table/preceding-sibling::*[1]/field/field_name[text()='table-cell-valign']/../field_body/paragraph" />
</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="transition">
<hr />
</xsl:template>
</xsl:stylesheet>