<commit_before><?xml version="1.0" encoding="utf-8"?>
<!--
  XSLT module for processing PSML links and cross-references.

  @author <NAME>
  @author <NAME>
  @author <NAME>
-->
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
                xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
                xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                xmlns:config="http://pageseeder.org/docx/config"
                xmlns:fn="http://pageseeder.org/docx/function"
                exclude-result-prefixes="#all">

<!--
  Inline cross-references
-->
<xsl:template match="xref" mode="psml">
  <xsl:choose>
    <!-- TODO check requirements for generate-cross-references -->
    <xsl:when test="config:generate-cross-references()">
      <w:r>
        <w:fldChar w:fldCharType="begin"/>
      </w:r>
      <w:r>
        <w:instrText xml:space="preserve"><xsl:value-of select="concat('REF ',replace(concat(@uriid,if(@frag != 'default') then '_' else '',if(@frag != 'default') then @frag else ''),'\W','_'),' \r \h ')"/></w:instrText>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="separate"/>
      </w:r>
      <w:r>
        <xsl:call-template name="apply-run-style" />
        <w:t><xsl:value-of select="."/></w:t>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="end"/>
      </w:r>
    </xsl:when>

    <!-- Cross-reference to a URL -->
    <xsl:when test="@external = 'true'">
      <w:r>
        <w:fldChar w:fldCharType="begin" />
      </w:r>
      <w:r>
        <w:instrText xml:space="preserve"> HYPERLINK "<xsl:value-of select="@href" />" </w:instrText>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="separate" />
      </w:r>
      <w:r>
        <xsl:call-template name="apply-run-style" />
        <w:t><xsl:value-of select="." /></w:t>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="end" />
      </w:r>
    </xsl:when>

    <!-- TODO check requirements for mathml processing -->
    <xsl:when test="starts-with(@href, '_external/') and config:generate-mathml()">
      <!-- External xref: choose to copy or not based on type and config -->
      <xsl:variable name="referenced-document" select="document(@href)" />
      <xsl:choose>
        <xsl:when test="$referenced-document//section/media-fragment[@mediatype='application/mathml+xml'] and config:generate-mathml()">
          <xsl:variable name="mathml">
            <m:math>
              <xsl:sequence select="$referenced-document//section/media-fragment[@mediatype='application/mathml+xml']/*"/>
            </m:math>
          </xsl:variable>
          <xsl:apply-templates select="$mathml//m:math"/>
        </xsl:when>
        <xsl:otherwise>
          <w:r>
            <xsl:call-template name="apply-run-style" />
            <w:t><xsl:value-of select="." /></w:t>
          </w:r>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>

    <!-- Cross-reference to a non PSML document -->
    <xsl:when test="@href[not(starts-with(., '#'))][not(ends-with(., '.psml'))]">
      <w:hyperlink w:anchor="{@href}" w:history="1">
        <w:r>
          <w:rPr>
            <xsl:choose>
              <xsl:when test="$xref-style != ''">
                <w:rStyle w:val="{$xref-style}"/>
              </xsl:when>
              <xsl:otherwise>
                <w:color w:val="0000FF"/>
                <w:u w:val="single"/>
              </xsl:otherwise>
            </xsl:choose>
          </w:rPr>
          <w:t xml:space="preserve"><xsl:value-of select="." /></w:t>
        </w:r>
      </w:hyperlink>
    </xsl:when>

    <!-- Cross-reference to a PSML document -->
    <xsl:when test="@href[not(starts-with(., '#'))]">
      <w:r>
        <xsl:call-template name="apply-run-style" />
        <w:t xml:space="preserve"><xsl:value-of select="." /></w:t>
      </w:r>
    </xsl:when>

    <!-- Internal cross-reference (i.e. to another fragment) -->
    <xsl:otherwise>
      <w:hyperlink w:anchor="{concat('fragment-', substring-after(@href, '#'))}" w:history="1">
        <w:r>
          <xsl:choose>
            <xsl:when test="$xref-style != ''">
              <w:rPr>
                <w:rStyle w:val="{$xref-style}"/>
              </w:rPr>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="apply-run-style" />
            </xsl:otherwise>
          </xsl:choose>
          <w:t><xsl:value-of select="." /></w:t>
        </w:r>
      </w:hyperlink>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>

<!--
  handles blockxref transformations;
  checks also for document labels so that styles are applied accordingly through the configuration
-->
<xsl:template match="blockxref" mode="psml">
  <xsl:choose>
    <xsl:when test="@mediatype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' and $manual-master = 'true'">
      <w:p>
        <w:pPr>
          <!-- TODO We copy this twice? maybe we can put this in variable to avoid recomputing -->
          <xsl:copy-of select="document(concat($_dotxfolder, '/word/document.xml'))//w:body/w:sectPr[last()]"/>
        </w:pPr>
      </w:p>
      <w:p>
        <w:pPr>
          <xsl:copy-of select="document(concat($_dotxfolder, '/word/document.xml'))//w:body/w:sectPr[last()]"/>
        </w:pPr>
        <w:subDoc r:id="{concat('rId',(count(document($_document-relationship)//*[name() = 'Relationship']) + 2 + count(preceding::blockxref[@mediatype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'])))}"/>
      </w:p>
    </xsl:when>
    <xsl:when test="document | fragment">
      <xsl:apply-templates mode="psml"/>
    </xsl:when>
    <xsl:otherwise>
      <!-- TODO generate internal link if target is internal -->
      <xsl:variable name="content" select="if (@title != '') then @title else @urititle" />
      <w:p>
        <w:r>
          <w:rPr>
            <w:rStyle w:val="reference" />
          </w:rPr>
          <w:t>
            <xsl:value-of select="$content" />
          </w:t><commit_msg>remove duplicate sectPr for <w:subDoc/> for Word master<commit_after><?xml version="1.0" encoding="utf-8"?>
<!--
  XSLT module for processing PSML links and cross-references.

  @author <NAME>
  @author <NAME>
  @author <NAME>
-->
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
                xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
                xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                xmlns:config="http://pageseeder.org/docx/config"
                xmlns:fn="http://pageseeder.org/docx/function"
                exclude-result-prefixes="#all">

<!--
  Inline cross-references
-->
<xsl:template match="xref" mode="psml">
  <xsl:choose>
    <!-- TODO check requirements for generate-cross-references -->
    <xsl:when test="config:generate-cross-references()">
      <w:r>
        <w:fldChar w:fldCharType="begin"/>
      </w:r>
      <w:r>
        <w:instrText xml:space="preserve"><xsl:value-of select="concat('REF ',replace(concat(@uriid,if(@frag != 'default') then '_' else '',if(@frag != 'default') then @frag else ''),'\W','_'),' \r \h ')"/></w:instrText>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="separate"/>
      </w:r>
      <w:r>
        <xsl:call-template name="apply-run-style" />
        <w:t><xsl:value-of select="."/></w:t>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="end"/>
      </w:r>
    </xsl:when>

    <!-- Cross-reference to a URL -->
    <xsl:when test="@external = 'true'">
      <w:r>
        <w:fldChar w:fldCharType="begin" />
      </w:r>
      <w:r>
        <w:instrText xml:space="preserve"> HYPERLINK "<xsl:value-of select="@href" />" </w:instrText>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="separate" />
      </w:r>
      <w:r>
        <xsl:call-template name="apply-run-style" />
        <w:t><xsl:value-of select="." /></w:t>
      </w:r>
      <w:r>
        <w:fldChar w:fldCharType="end" />
      </w:r>
    </xsl:when>

    <!-- TODO check requirements for mathml processing -->
    <xsl:when test="starts-with(@href, '_external/') and config:generate-mathml()">
      <!-- External xref: choose to copy or not based on type and config -->
      <xsl:variable name="referenced-document" select="document(@href)" />
      <xsl:choose>
        <xsl:when test="$referenced-document//section/media-fragment[@mediatype='application/mathml+xml'] and config:generate-mathml()">
          <xsl:variable name="mathml">
            <m:math>
              <xsl:sequence select="$referenced-document//section/media-fragment[@mediatype='application/mathml+xml']/*"/>
            </m:math>
          </xsl:variable>
          <xsl:apply-templates select="$mathml//m:math"/>
        </xsl:when>
        <xsl:otherwise>
          <w:r>
            <xsl:call-template name="apply-run-style" />
            <w:t><xsl:value-of select="." /></w:t>
          </w:r>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>

    <!-- Cross-reference to a non PSML document -->
    <xsl:when test="@href[not(starts-with(., '#'))][not(ends-with(., '.psml'))]">
      <w:hyperlink w:anchor="{@href}" w:history="1">
        <w:r>
          <w:rPr>
            <xsl:choose>
              <xsl:when test="$xref-style != ''">
                <w:rStyle w:val="{$xref-style}"/>
              </xsl:when>
              <xsl:otherwise>
                <w:color w:val="0000FF"/>
                <w:u w:val="single"/>
              </xsl:otherwise>
            </xsl:choose>
          </w:rPr>
          <w:t xml:space="preserve"><xsl:value-of select="." /></w:t>
        </w:r>
      </w:hyperlink>
    </xsl:when>

    <!-- Cross-reference to a PSML document -->
    <xsl:when test="@href[not(starts-with(., '#'))]">
      <w:r>
        <xsl:call-template name="apply-run-style" />
        <w:t xml:space="preserve"><xsl:value-of select="." /></w:t>
      </w:r>
    </xsl:when>

    <!-- Internal cross-reference (i.e. to another fragment) -->
    <xsl:otherwise>
      <w:hyperlink w:anchor="{concat('fragment-', substring-after(@href, '#'))}" w:history="1">
        <w:r>
          <xsl:choose>
            <xsl:when test="$xref-style != ''">
              <w:rPr>
                <w:rStyle w:val="{$xref-style}"/>
              </w:rPr>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="apply-run-style" />
            </xsl:otherwise>
          </xsl:choose>
          <w:t><xsl:value-of select="." /></w:t>
        </w:r>
      </w:hyperlink>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>

<!--
  handles blockxref transformations;
  checks also for document labels so that styles are applied accordingly through the configuration
-->
<xsl:template match="blockxref" mode="psml">
  <xsl:choose>
    <xsl:when test="@mediatype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' and $manual-master = 'true'">
      <w:p>
        <w:pPr>
          <xsl:copy-of select="document(concat($_dotxfolder, '/word/document.xml'))//w:body/w:sectPr[last()]"/>
        </w:pPr>
        <w:subDoc r:id="{concat('rId',(count(document($_document-relationship)//*[name() = 'Relationship']) + 2 + count(preceding::blockxref[@mediatype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'])))}"/>
      </w:p>
    </xsl:when>
    <xsl:when test="document | fragment">
      <xsl:apply-templates mode="psml"/>
    </xsl:when>
    <xsl:otherwise>
      <!-- TODO generate internal link if target is internal -->
      <xsl:variable name="content" select="if (@title != '') then @title else @urititle" />
      <w:p>
        <w:r>
          <w:rPr>
            <w:rStyle w:val="reference" />
          </w:rPr>
          <w:t>
            <xsl:value-of select="$content" />
          </w:t>
{'fraction_non_alphanumeric': 0.2016321820130245, 'fraction_numerical': 0.004781139230071717, 'mean_word_length': 2.312039312039312, 'pattern_counts': {'":': 0, '<': 320, '<?xml version=': 2, '>': 320, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 39}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1092041', 'n_tokens_mistral': 4368, 'n_tokens_neox': 4069, 'n_words': 672}