<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform"
                              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:wwdoc="urn:WebWorks-Document-Schema"
                              exclude-result-prefixes="xsl wwdoc"
>
 <xsl:output method="text" />

 <xsl:template match="/">
  <xsl:apply-templates mode="wif-to-wiki" />
 </xsl:template>


 <!-- wif-to-wiki -->
 <!--             -->

 <xsl:template match="wwdoc:Document" mode="wif-to-wiki">
  <xsl:apply-templates mode="wif-to-wiki" />
 </xsl:template>

 <xsl:template match="wwdoc:Content" mode="wif-to-wiki">
  <!-- Table of Contents for this page -->
  <!--                                 -->
  <xsl:text>[[TableOfContents()]]
</xsl:text>

  <xsl:apply-templates mode="wif-to-wiki" />
 </xsl:template>

 <xsl:template match="wwdoc:Paragraph[@stylename = 'SectionTitle']" mode="wif-to-wiki">
  <xsl:param name="ParamParagraphElement" select="." />

  <xsl:apply-templates select="$ParamParagraphElement" mode="wiki-paragraph">
   <xsl:with-param name="ParamPrefix" select="'= '" />
   <xsl:with-param name="ParamSuffix" select="' ='" />
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="wwdoc:Paragraph[@stylename = 'Heading1']" mode="wif-to-wiki">
  <xsl:param name="ParamParagraphElement" select="." />

  <xsl:apply-templates select="$ParamParagraphElement" mode="wiki-paragraph">
   <xsl:with-param name="ParamPrefix" select="'== '" />
   <xsl:with-param name="ParamSuffix" select="' =='" />
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="wwdoc:Paragraph[@stylename = 'Heading2']" mode="wif-to-wiki">
  <xsl:param name="ParamParagraphElement" select="." />

  <xsl:apply-templates select="$ParamParagraphElement" mode="wiki-paragraph">
   <xsl:with-param name="ParamPrefix" select="'=== '" />
   <xsl:with-param name="ParamSuffix" select="' ==='" />
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="wwdoc:Paragraph[@stylename = 'Heading3']" mode="wif-to-wiki">
  <xsl:param name="ParamParagraphElement" select="." />

  <xsl:apply-templates select="$ParamParagraphElement" mode="wiki-paragraph">
   <xsl:with-param name="ParamPrefix" select="'==== '" />
   <xsl:with-param name="ParamSuffix" select="' ===='" />
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="wwdoc:Paragraph" mode="wif-to-wiki">
  <xsl:param name="ParamParagraphElement" select="." />

  <xsl:apply-templates select="$ParamParagraphElement" mode="wiki-paragraph">
   <xsl:with-param name="ParamPrefix" select="''" />
   <xsl:with-param name="ParamSuffix" select="''" />
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="*" mode="wif-to-wiki">
  <!-- Do not process -->
  <!--                -->
 </xsl:template>

 <xsl:template match="comment() | processing-instruction() | text()" mode="wif-to-wiki">
  <!-- Do not process -->
  <!--                -->
 </xsl:template>


 <!-- wiki-paragraph -->
 <!--                -->

 <xsl:template match="wwdoc:Paragraph" mode="wiki-paragraph">
  <xsl:param name="ParamPrefix" />
  <xsl:param name="ParamSuffix" />

  <!-- Determine paragraph contents -->
  <!--                              -->
  <xsl:variable name="VarParagraphContents">
   <xsl:apply-templates mode="wif-to-text" />
  </xsl:variable>

  <!-- Emit if not empty -->
  <!--                   -->
  <xsl:variable name="VarParagraphContentsNormalized" select="normalize-space($VarParagraphContents)" />
  <xsl:if test="string-length($VarParagraphContentsNormalized) &gt; 0">
   <!-- Prefix -->
   <!--        -->
   <xsl:value-of select="$ParamPrefix" />

   <!-- Paragraph contents -->
   <!--                    -->
   <xsl:value-of select="$VarParagraphContentsNormalized" />

   <!-- Suffix -->
   <!--        -->
   <xsl:value-of select="$ParamSuffix" />

   <!-- Separate with blank lines -->
   <!--                           -->
   <xsl:text>

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

 <xsl:template match="*" mode="wiki-paragraph">
  <xsl:param name="ParamPrefix" />
  <xsl:param name="ParamSuffix" />

  <!-- Do not process -->
  <!--                -->
 </xsl:template>

 <xsl:template match="comment() | processing-instruction() | text()" mode="wiki-paragraph">
  <xsl:param name="ParamPrefix" />
  <xsl:param name="ParamSuffix" />

  <!-- Do not process -->
  <!--                -->
 </xsl:template>


 <!-- wif-to-text -->
 <!--             -->

 <xsl:template match="wwdoc:TextRun" mode="wif-to-text">
  <xsl:apply-templates mode="wif-to-text" />
 </xsl:template>

 <xsl:template match="wwdoc:Text" mode="wif-to-text">
  <xsl:variable name="ParamTextElement" select="." />

  <xsl:value-of select="$ParamTextElement/@value" />
 </xsl:template>

 <xsl:template match="*" mode="wif-to-text">
  <!-- Do not process -->
  <!--                -->
 </xsl:template>

 <xsl:template match="comment() | processing-instruction() | text()" mode="wif-to-text">
  <!-- Do not process -->
  <!--                -->
 </xsl:template>
</xsl:stylesheet>
