Reference Source Transformation

From OpenGL Reference
Jump to navigation Jump to search

XSL[edit]

Here is the complete code of the XSL used to transform the docbook xml provided by Khronos into wiki text.

Note: due to mod_security rules on the server, I had to change some of the namespace declarations. xmln should be xmln with an s.

<?xml version="1.0"?>

<xsl:stylesheet
  version="1.0"
  xmln:db="http://docbook.org/ns/docbook"
  xmln:xsl="http://www.w3.org/1999/XSL/Transform"
  xmln:mml="http://www.w3.org/1998/Math/MathML"
  xmln:xi="http://www.w3.org/2001/XInclude"
>

<xsl:output method="text" omit-xml-declaration="yes" />

<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<xsl:template match="db:refpurpose">
	<xsl:value-of select="concat(translate(substring(.,1,1), $vLower, $vUpper), substring(., 2), substring(' ', 1 div not(position()=last())))" />
	<xsl:if test="substring(., string-length(.), 1) != '.'">.</xsl:if>
</xsl:template>

<xsl:template match="db:funcdef">
	<xsl:apply-templates />
</xsl:template>

<xsl:template match="db:parameter">
	<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="db:funcprototype">
|-
|<code><xsl:apply-templates select="db:funcdef"/>( <xsl:for-each select="db:paramdef"><xsl:apply-templates/><xsl:if test="position() != last()">, </xsl:if></xsl:for-each> );</code></xsl:template>

<xsl:template match="db:funcsynopsis">
{| class="wikitable"<xsl:apply-templates select="db:funcprototype"/>
|}
</xsl:template>

<xsl:template match="text()"><xsl:value-of select="normalize-space()"/></xsl:template>

<xsl:template match="db:function"> '''<code><xsl:value-of select="."/></code>''' </xsl:template>

<xsl:template match="db:funcdef/db:function"> <xsl:value-of select="."/></xsl:template>

<xsl:template match="db:para/db:constant"> <code><xsl:value-of select="."/></code> </xsl:template>

<xsl:template match="db:para/db:parameter"> '''<code><xsl:value-of select="."/></code>''' </xsl:template>

<xsl:template match="db:para"><xsl:apply-templates />
<xsl:text>

</xsl:text>
</xsl:template>
<xsl:template match="db:listitem/db:para"><xsl:apply-templates />
</xsl:template>

<xsl:template match="db:itemizedlist">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="db:listitem">*<xsl:apply-templates /><xsl:text>

</xsl:text>
</xsl:template>

<xsl:template match="db:variablelist">
{|<xsl:apply-templates />
|}<xsl:text>

</xsl:text>
</xsl:template>

<xsl:template match="db:varlistentry">
|- style="vertical-align:top;"
|<xsl:apply-templates /></xsl:template>

<xsl:template match="db:varlistentry/db:listitem">
|<xsl:apply-templates /></xsl:template>

<xsl:template match="db:term">
<xsl:apply-templates />
| -</xsl:template>

<xsl:template match="db:parameter">'''<code><xsl:apply-templates /></code>'''</xsl:template>

<xsl:template match="db:paramdef/db:parameter"> <xsl:apply-templates /></xsl:template>

<xsl:template match="db:refentry">
	<xsl:apply-templates select="db:refnamediv/db:refpurpose" />
	
<xsl:apply-templates select="db:refsynopsisdiv" />

<xsl:apply-templates select="db:refsect1" />
	
__NOTOC__ 
{{DISPLAYTITLE:<xsl:value-of select="db:refmeta/db:refentrytitle" />}}

	
</xsl:template>

<xsl:template match="db:refsynopsisdiv">
<xsl:apply-templates select="db:title"/>
<xsl:apply-templates select="db:funcsynopsis"/>
</xsl:template>

<xsl:template match="db:refsect1"><xsl:apply-templates /></xsl:template>

<xsl:template match="db:programlisting"><code>
<xsl:value-of select="." />
</code>

</xsl:template>

<xsl:template match="db:title"><xsl:text>

</xsl:text>== <xsl:value-of select="."/> ==
</xsl:template>

<xsl:template match="db:citerefentry"> [[<xsl:apply-templates />]] </xsl:template>

<xsl:template match="db:refentrytitle"><xsl:value-of select="." /></xsl:template>

<xsl:template match="db:inlineequation"><xsl:apply-templates /></xsl:template>

<xsl:template match="mml:math"> <math><xsl:apply-templates select="mml:mfenced"/><xsl:apply-templates select="mml:mrow" />
<xsl:apply-templates select="mml:mi" /></math> </xsl:template>

<xsl:template match="mml:mrow">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="mml:mi">
<xsl:value-of select="text()"/>
</xsl:template>

<xsl:template match="mml:mo">
<xsl:value-of select="text()"/>
</xsl:template>

<xsl:template match="mml:mfenced"><xsl:value-of select="@open"/><xsl:for-each select="mml:mn"><xsl:value-of select="."/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each><xsl:apply-templates select="mml:msub" /><xsl:value-of select="@close" /></xsl:template>

<xsl:template match="mml:msub"><xsl:value-of select="mml:mi[1]"/>_{<xsl:value-of select="mml:mi[2]"/>}</xsl:template>

<xsl:template match="db:informaltable">

{| class="wikitable"<xsl:apply-templates />
|}</xsl:template>

<xsl:template match="db:tgroup">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="xi:include"></xsl:template>

<xsl:template match="xi:include[contains(@xpointer,'11')]">
|1.1+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'12')]">
|1.2+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'13')]">
|1.3+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'14')]">
|1.4+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'15')]">
|1.5+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'20')]">
|2.0+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'21')]">
|2.1+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'30')]">
|3.0+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'31')]">
|3.1+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'32')]">
|3.2+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'33')]">
|3.3+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'40')]">
|4.0+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'41')]">
|4.1+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'42')]">
|4.2+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'43')]">
|4.3+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'44')]">
|4.4+</xsl:template>
<xsl:template match="xi:include[contains(@xpointer,'45')]">
|4.5+</xsl:template>

<xsl:template match="db:thead">
<xsl:for-each select="db:row">
|-<xsl:for-each select="db:entry/db:emphasis">
!<xsl:value-of select="normalize-space()"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

<xsl:template match="db:tbody">
<xsl:for-each select="db:row">
|-<xsl:for-each select="db:entry">
|<xsl:apply-templates />
</xsl:for-each>
<xsl:apply-templates select="xi:include"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="db:trademark[@class='copyright']">
<xsl:text>©</xsl:text> </xsl:template>

<xsl:template match="db:constant"><code><xsl:value-of select="." /></code> </xsl:template>

<xsl:template match="db:link"> <xsl:value-of select="." />
</xsl:template>

<xsl:template match="*" mode="copy-no-namespaces">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()" mode="copy-no-namespaces" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>