<?xml version="1.0" encoding="UTF-8"?><!--
/**
 * api: xslt
 * type: transform
 * title: mallard to xhp
 * description: converts help .page to openoffice .xhp
 * version: 0.3
 * depends: bin:saxonb-xslt
 *
 * Crude mapping from mallard help file to OpenOffice helpdocument/xhp.
 * Only handles paragraphs, titles, lists, sections. No media/images.
 * And info <link>s are just mapped to type=index or bookmark (type=guide)
 * entries, but type=seealso and href= links in the body kept as is.
 * No help.tree generation obviously.
 *
 */-->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:m="http://projectmallard.org/1.0/" exclude-result-prefixes="m">

<!-- cfg -->
<xsl:variable name="package" select="'vnd.include-once.pagetranslate'"/>
<xsl:variable name="filenam" select="replace(replace(base-uri(), '^.+/', ''), '(\.\w+){1,2}$', '.xhp')"/>
<xsl:variable name="fileref" select="concat('/help/',$package,'/',$filenam)"/>
<xsl:variable name="maptype" select="'gui=gui key=keystroke kbd=keystroke dialog=dialog ins=added del=removed span=span input=input output=output var=variable cmd=command app=menuitem sys=menuitem file=fileitem'"/>
<xsl:output indent="yes"/>

<!-- new document carcass -->
<xsl:template match="/m:page">
  <helpdocument version="1.0">
  <meta>
    <topic id="topic_{generate-id()}" indexer="include" status="PUBLISH">
      <title xml-lang="en" id="title_{generate-id()}"><xsl:value-of select="(//m:title)[1]"/></title>
      <filename><xsl:value-of select="$fileref"/></filename>
    </topic>
    <history>
      <created date="2020-02-02T22:22:22"></created>
      <lastedited date="{current-dateTime()}"></lastedited>
    </history>
  </meta>
  <body>
     <xsl:apply-templates/>
  </body>
  </helpdocument>
</xsl:template>

<!-- text nodes / paragraphs / sections -->
<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>
<xsl:template match="m:p">
  <paragraph id="par_{generate-id()}" role="paragraph" xml-lang="en">
    <xsl:apply-templates/>
  </paragraph>
</xsl:template>
<xsl:template match="m:title">
  <paragraph id="hd_{generate-id()}" role="heading" level="{count(ancestor::*)}" xml-lang="en">
    <xsl:value-of select="."/>
  </paragraph>
</xsl:template>
<xsl:template match="m:section|m:note">
  <xsl:choose>
    <xsl:when test="@style">
      <paragraph id="sect_{generate-id()}" role="{@style}" xml-lang="en"><xsl:apply-templates/></paragraph>
    </xsl:when>
    <xsl:otherwise>
      <paragraph id="sect_{generate-id()}" role="{local-name()}" xml-lang="en"><xsl:apply-templates/></paragraph>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
<xsl:template match="m:code|m:input|m:output">
  <paragraph id="par_{generate-id()}" role="code" xml-lang="en" xml:space="preserve">
    <xsl:apply-templates/>
  </paragraph>
</xsl:template>

<!-- inline -->
<xsl:template match="m:em"> <emph><xsl:apply-templates/></emph> </xsl:template>
<xsl:template match="m:guiseq|m:keyseq"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="m:gui|m:kbd|m:key|m:dialog|m:ins|m:del|m:span|m:var|m:cmd|m:app|m:sys|m:file">
  <item type="{replace($maptype, concat('^.*', local-name(), '=(\w+).*$'), '$1')}"><xsl:apply-templates/></item>
</xsl:template>

<!-- lists -->
<xsl:template match="m:terms|m:list">
  <list id="{local-name()}_{generate-id()}" xml-lang="en">
    <xsl:apply-templates/>
  </list>
</xsl:template>
<xsl:template match="m:steps|m:tree">
  <list id="{local-name()}_{generate-id()}" bullet="disc" type="ordered" xml-lang="en">
    <xsl:apply-templates/>
  </list>
</xsl:template>
<xsl:template match="m:item">
  <listitem id="item_{generate-id()}" xml-lang="en">
    <xsl:choose>
      <xsl:when test="m:title">
        <emph><xsl:value-of select="m:title"/></emph><br/>
        <xsl:apply-templates select="m:p"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </listitem>
</xsl:template>

<!-- meta -->
<xsl:template match="m:link">
<xsl:choose>
  <!-- meta links -->
  <xsl:when test="@type='guide'">
    <bookmark id="bm_{generate-id()}" branch="hid/{$package}:{@xref}" xml-lang="en">
      <bookmark_value><xsl:value-of select="//m:desc"/></bookmark_value>
    </bookmark>
  </xsl:when>
  <xsl:when test="@type='index'">
    <bookmark id="helpindex_{generate-id()}" branch="index" xml-lang="en">
      <bookmark_value><xsl:value-of select="@xref"/></bookmark_value>
    </bookmark>
  </xsl:when>
  <!-- body links -->
  <xsl:when test="@href">
    <link href="{@href}"><xsl:value-of select="."/></link>
  </xsl:when>
  <xsl:when test="@type='seealso'">
    <link href="{@xref}"><xsl:value-of select="."/></link>
  </xsl:when>
  <xsl:when test="@xref">
    <link href="{$package}/{@xref}.xhp"><xsl:value-of select="."/></link>
  </xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="m:info">
    <xsl:apply-templates select="m:link"/>
</xsl:template>

<!-- remove all else -->
<xsl:template match="*">
  <xsl:comment>xsl:skipped=<xsl:value-of select="concat('&lt;', name(), '&gt;')"/></xsl:comment>
</xsl:template>

</xsl:stylesheet>
