<?xml version="1.0" encoding="utf-8"?>
<!-- Created with nothing -->
<!-- expand-text for variable expansion in xsl:text -->
<xsl:stylesheet expand-text='yes' version="3.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:aw="http://www.abisource.com/awml.dtd" 
xmlns="http://www.abisource.com/awml.dtd"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ups="http://www.uplawski.eu"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<!-- copied from the Saxo documentation -->
<xsl:function name="ups:increment" as="xs:integer">
  <xsl:param name="in" as="xs:integer"/>
  <xsl:sequence select="$in + 1"/>
</xsl:function>

<!-- tabulator -->
<xsl:variable name="tab" select="'&#09;'"/>

<xsl:variable name="lowercase"
select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase"
select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<xsl:template match="/*">
  <xsl:message>root found </xsl:message>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="aw:metadata"/>

<xsl:template match="aw:section">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="aw:p[@style='Document Title']">
  <xsl:message>Found document title</xsl:message>
  <xsl:iterate select="1 to string-length()">
    <xsl:text>=</xsl:text>
  </xsl:iterate>
  <xsl:text>&#10;</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#10;</xsl:text>
  <xsl:iterate select="1 to string-length()">
    <xsl:text>=</xsl:text>
  </xsl:iterate>
  <xsl:text>&#10;&#10;</xsl:text>
</xsl:template>

<xsl:template match="aw:p[@style='Sub Doc Title']">
  <xsl:iterate select="1 to string-length()">
    <xsl:text>-</xsl:text>
  </xsl:iterate>
  <xsl:text>&#10;</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#10;</xsl:text>
  <xsl:iterate select="1 to string-length()">
    <xsl:text>-</xsl:text>
  </xsl:iterate>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<!-- Do nothing for the time, i.e. ignore the imput nodes -->
<xsl:template match="aw:p[starts-with(@style, 'TOC')]">
  <xsl:text></xsl:text>
</xsl:template>

<xsl:template match="aw:p[@style='Section Header']">
  <xsl:message>Section: <xsl:value-of select="."/></xsl:message>
  <xsl:text>.. _</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>:&#10;&#10;</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#10;</xsl:text>
  <xsl:iterate select="1 to string-length()">
    <xsl:text>=</xsl:text>
  </xsl:iterate>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<!-- Header 1 -->
<xsl:template match="aw:p[@style='Header 1' and string-length() > 0]">
  <xsl:message><xsl:text>  </xsl:text>Header: <xsl:value-of select="."/></xsl:message>
  <!-- create a bookmark for this header -->
  <xsl:text>.. _</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>:&#10;&#10;</xsl:text>

  <!-- reproduce bookmark from input if they are different from the heading -->
  <xsl:for-each select="aw:bookmark[@type='end']">
    <xsl:message select="concat('&#9;bookmark? ', @name)"/>
    <xsl:if test="string(@name) != string()">
      <xsl:text>.. _</xsl:text>
      <xsl:value-of select="@name"/>
      <xsl:text>:</xsl:text>
      <xsl:text>&#10;&#10;</xsl:text>
    </xsl:if>
  </xsl:for-each>

  <xsl:apply-templates/>

  <xsl:text>&#10;</xsl:text>
  <xsl:iterate select="1 to string-length()">
    <xsl:text>-</xsl:text>
  </xsl:iterate>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="aw:p[@style='Bullet List 1' and string-length() > 0]">
  <xsl:message><xsl:text>    </xsl:text>Bullet List</xsl:message>
  <xsl:text>* </xsl:text>
  <xsl:apply-templates/>
  <xsl:text>&#10;&#10;</xsl:text>
</xsl:template>

<!-- NUMBERED LIST: Match first item in numbered list -->
<!-- This template must handle siblings of the first list item,
  as AbiWord does not write a parent tag for lists. 
  ... erm. Maybe read that again and contemplate... You thought
  computers were there to facilitate things? Darn, man!

  Two templates. See also the next, below.
-->
<xsl:template match="aw:p[@style='Numbered List 1'][not(preceding-sibling::aw:p[@style='Numbered List 1'][@listid = current()/@listid])]">
  <xsl:message>    Numbered List {@listid}</xsl:message>
  <xsl:if test="self::aw:p">
    <xsl:variable name="listid" select="@listid"/>

    <xsl:message>      Current listid: {$listid}</xsl:message>
    <xsl:message>      Following siblings with same listid: {count(following-sibling::aw:p[@listid = $listid])}</xsl:message>
    <xsl:message>      Total in sequence: {count((., following-sibling::aw:p[@listid = $listid]))} </xsl:message>
    <!-- Work through all the items in the same list. The present node
    is explicitly included with the '.' at the beginning of the sequence.-->
    <xsl:for-each select="(., following-sibling::aw:p[@listid = $listid])">
      <xsl:message>      Processing item {position()} {@xid}</xsl:message>
      <!-- extract the actual content -->
      <xsl:variable name="content" select="normalize-space(string-join(aw:c[@type='list_label'][normalize-space(.) != '']/text(), ' '))"/>
      <xsl:message>         content: {substring($content, 1, 20)} (...)</xsl:message>
      <xsl:if test="$content != ''">
        <!-- position() works here, as the list-items have already 
        been filtered.-->
        <xsl:value-of select="position()"/>
        <xsl:text>. </xsl:text>
        <xsl:value-of select="$content"/>
        <xsl:text>&#10;&#10;</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:if>
  <xsl:message>      - End Numbered List -</xsl:message>
  <!-- Put your flowery metaphor here. -->
</xsl:template>

<!-- Actively ignore subsequent list items, as they have been 
processed already in the preceding template for numbered lists.-->
<xsl:template match="aw:p[@style='Numbered List 1'][preceding-sibling::aw:p[@style='Numbered List 1'][@listid = current()/@listid]]"/>

<!-- This is complicated, as multi-line examples must be
handled explicitly. See below.-->

<!-- Beginning of a definition-->
<xsl:template match="aw:p[@style='Definition List' and child::aw:c[@style='Definition Item']]">
  <xsl:apply-templates/>
</xsl:template>

<!-- continuation of a definition-->
<xsl:template match="aw:p[@style='Definition List' and string-length > 0]">
  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="aw:c[@style='Definition Item']">
  <xsl:if test="normalize-space() != ''">
    <xsl:message select="concat('&#09;', 'definition item ', string())"/>

    <xsl:variable name="item"
                select="normalize-space(string-join(text(), ''))"/>

    <xsl:text>&#10;</xsl:text>
    <!-- Explicitly create a bookmark for each option,
      in case we have to reference it somewhere in a 
    link. -->
    <xsl:text>&#10;.. _</xsl:text>
    <xsl:value-of select="$item"/>
    <xsl:text>:</xsl:text>
    <xsl:text>&#10;</xsl:text>

    <!-- reproduce bookmark from input if they are different from the heading -->
    <xsl:for-each select="following-sibling::aw:bookmark[@type='end'] | preceding-sibling::aw:bookmark[@type='start']">
      <xsl:message select="concat('&#09;&#9;bookmark? ', @name)"/>
      <xsl:if test="string(@name) != $item">
        <xsl:text>.. _</xsl:text>
        <xsl:value-of select="@name"/>
        <xsl:text>:</xsl:text>
        <xsl:text>&#10;</xsl:text>
      </xsl:if>
    </xsl:for-each>

    <!-- write item -->
    <xsl:text>&#10;**</xsl:text>
    <xsl:value-of select="$item"/>
    <xsl:text>**&#10;&#10;</xsl:text>
  </xsl:if>
</xsl:template>

<!-- match bookmarks in abw input
<xsl:template match="aw:bookmark[@type='start']">
  <xsl:if test = "not(following-sibling/text()[1] = @name)">
    <xsl:message select="concat('&#09;', '&#09;', 'Bookmark: ', @name)"/>
    <xsl:value-of select="concat('&#10;.. _', @name, ':&#10;&#10;')"/>
  </xsl:if>
</xsl:template> -->

<!-- Handle those text-lines with one of the 
Prefixes Content, Default or Example -->
<xsl:template match="aw:c[@style='Definition Text']">
  <!-- not strictly necessary but it facilitates the construction of
  the conditions, below.-->
  <xsl:variable name="content" select="normalize-space(translate(., '&#09;', ''))"/>

  <xsl:choose>
    <!-- Not intuitive, '=' compares with the elements of the sequence,
    one by one.-->
    <xsl:when test="substring($content, 1, 7) = ('DEFAULT', 'CONTENT', 'EXAMPLE')">
      <xsl:text>&#10;&#10;</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>&#09;</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates/>
  <!-- To create a 'literal-block' in reST, find out, if the
  next <p/> is a Multiline Example. Add a second colon in this case. -->
  <xsl:if test="../following-sibling::aw:p[@style='Multiline Example' and position() = 1]">
    <xsl:text>:</xsl:text>
  </xsl:if>
</xsl:template>

<xsl:template match="aw:p[@style='Multiline Example']">
  <xsl:message>&#09;&#09;multiline example</xsl:message>
  <xsl:text>&#10;</xsl:text>
  <xsl:for-each-group select="node()" group-starting-with="aw:br">
    <xsl:variable name="part" select="current-group()[not(self::aw:br)]"/>
    <xsl:message>
      &#09;&#09;substring: <xsl:value-of select="normalize-space(string-join($part, ''))"/>
    </xsl:message>
    <xsl:text>&#10;&#09; </xsl:text>
    <xsl:value-of select="normalize-space(string-join($part, ''))"/>
  </xsl:for-each-group>
</xsl:template>

<!-- ordinary paragraphs -->
<xsl:template match="aw:p[@style='Normal']">
  <xsl:apply-templates/>
  <xsl:text>&#10;&#10;</xsl:text>
</xsl:template>

<xsl:template match="aw:br">
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<!-- Do nothing for the time, i.e. ignore the input nodes -->
<xsl:template match="aw:pbr">
  <xsl:text></xsl:text>
</xsl:template>

<!-- character style 'command' -->
<xsl:template match="aw:c[@style='Command']">
  <xsl:text>&#09;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<!-- simply bold text (font-weight) -->
<xsl:template match="aw:c[@props='font-weight:bold' and string-length() > 0]">
  <xsl:text>**</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>**</xsl:text>
</xsl:template>

<!-- simply italic text (font-style) -->
<xsl:template match="aw:c[@props='font-style:italic' and string-length() > 0]">
  <xsl:text>*</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>*</xsl:text>
</xsl:template>

<!-- link -->
<xsl:template match="aw:a[@xlink:href]">
  <xsl:choose>
    <!-- mailto -->
    <xsl:when test="contains(@xlink:href, '@')">
      <xsl:choose>
        <xsl:when test="starts-with(@xlink:href, 'mailto:')">
          <xsl:value-of select="substring(@xlink:href, 8)"/>
        </xsl:when>
        <!-- address without mailto -->
        <xsl:otherwise>
          <xsl:value-of select="@xlink:href"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <!-- write caption -->
      <xsl:text>`</xsl:text>
      <xsl:value-of select="."/>
      <!-- URL -->
      <xsl:if test = "starts-with(@xlink:href, 'http')">
        <!-- create link alias -->
        <xsl:text> &lt;</xsl:text>
        <xsl:value-of select="@xlink:href"/>
        <xsl:text>&gt;</xsl:text>
      </xsl:if>
      <xsl:text>`_</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- remove supplementary whitespace -->
<xsl:template match="text()[normalize-space() = '']"/>

</xsl:stylesheet>