<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="urn:WebWorks-Engine-Files-Schema"
                              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                              xmlns:wwproject="urn:WebWorks-Publish-Project"
                              xmlns:wwfiles="urn:WebWorks-Engine-Files-Schema"
                              xmlns:wwlog="urn:WebWorks-XSLT-Extension-Log"
                              xmlns:wwfilesystem="urn:WebWorks-XSLT-Extension-FileSystem"
                              xmlns:wwprojext="urn:WebWorks-XSLT-Extension-Project"
                              xmlns:wwexsldoc="urn:WebWorks-XSLT-Extension-Document"
                              exclude-result-prefixes="xsl msxsl wwfiles wwlog wwfilesystem wwprojext wwexsldoc"
>
 <xsl:param name="GlobalInput" />
 <xsl:param name="GlobalPipelineName" />
 <xsl:param name="GlobalProject" />
 <xsl:param name="GlobalFiles" />
 <xsl:param name="ParameterDependsType" />
 <xsl:param name="ParameterType" />


 <xsl:include href="wwformat:Transforms/wif-to-wiki.xsl" />


 <xsl:output method="xml" encoding="UTF-8" indent="yes" />


 <xsl:template match="/">
  <wwfiles:Files version="1.0">

   <!-- Locate WIF input files -->
   <!--                        -->
   <xsl:variable name="VarWIFFiles" select="$GlobalFiles/wwfiles:Files/wwfiles:File[@type = $ParameterDependsType]" />

   <!-- Process each WIF file -->
   <!--                       -->
   <xsl:for-each select="$VarWIFFiles">
    <xsl:variable name="VarWIFFile" select="." />

    <!-- Determine file path -->
    <xsl:variable name="VarOutputDirectoryPath" select="wwfilesystem:Combine(wwprojext:GetTargetOutputDirectoryPath(), wwprojext:GetGroupName($VarWIFFile/@groupID))" />
    <xsl:variable name="VarFileName" select="concat(wwfilesystem:GetFileNameWithoutExtension($VarWIFFile/@path), '.wiki')" />
    <xsl:variable name="VarPath" select="wwfilesystem:Combine($VarOutputDirectoryPath, $VarFileName)" />

    <!-- Write file -->
    <!--            -->
    <xsl:variable name="VarResultAsXML">
     <xsl:apply-templates select="document($VarWIFFile/@path)" mode="wif-to-wiki" />
    </xsl:variable>
    <xsl:variable name="VarResult" select="msxsl:node-set($VarResultAsXML)" />
    <xsl:variable name="VarWriteDocument" select="wwexsldoc:Document($VarResult, $VarPath, 'utf-8', 'text')" />

    <!-- Report generated file -->
    <!--                       -->
    <wwfiles:File path="{$VarPath}"
                  type="{$ParameterType}"
                  groupID="{$VarWIFFile/@groupID}"
                  documentID="{$VarWIFFile/@documentID}"
                  category="pages"
                  use="external"
                  deploy="true" />
   </xsl:for-each>

  </wwfiles:Files>
 </xsl:template>
</xsl:stylesheet>
