<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" indent="no" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    
    <!-- Controls page title output:
         0 - disabled [default]
         1 - enabled
         (see '//head' template for customization) -->
    <xsl:variable name="print-title" select="0"/>
    
    <!--xsl:variable name="paragraph-header" select="''"/>
    <xsl:variable name="paragraph-footer" select="'&#xa;&#xa;'"/-->
    <xsl:variable name="paragraph-header" select="'&#xa;#begin#&#xa;'"/>
    <xsl:variable name="paragraph-footer" select="'&#xa;#end#'"/>
    
    <xsl:template match="//head">
        <xsl:if test="$print-title">
            <xsl:copy-of select="concat(
                    '&#xa;            ',
                    normalize-space( title/text() ),
                    '&#xa;&#xa;&#xa;' )"/>
        </xsl:if>
    </xsl:template>
    <xsl:template match="/html/body">
        
        <xsl:apply-templates/>
        <!-- just a trailing caret return-->
        <xsl:text>&#xa;</xsl:text>
    </xsl:template>
    
    <xsl:template match="p">
        <xsl:value-of select="$paragraph-header"/>
        <xsl:for-each select="current()//text()">
            <xsl:value-of select="concat( normalize-space( current() ), ' ' )"/>
        </xsl:for-each>
        <xsl:value-of select="$paragraph-footer"/>
    </xsl:template>
    
    <xsl:template match="ul|ol">
        <xsl:value-of select="$paragraph-header"/>
        <xsl:apply-templates/>
        <xsl:value-of select="$paragraph-footer"/>
    </xsl:template>
    
    <xsl:template match="ul/li|ol/li">
        <xsl:value-of select="concat(' * ', normalize-space( current() ),'&#xa;')"/>
    </xsl:template>
    
    <xsl:template match="text()">
        <xsl:value-of select="$paragraph-header"/>
        <xsl:copy-of select="normalize-space( current() )"/>
        <xsl:value-of select="$paragraph-footer"/>
    </xsl:template>
    
</xsl:stylesheet>
