<commit_before><?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" 
  xmlns:c="http://www.w3.org/ns/xproc-step"
  xmlns:cx="http://xmlcalabash.com/ns/extensions"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:docx2hub="http://transpect.io/docx2hub"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
  xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  xmlns:tr="http://transpect.io"
  version="1.0" 
  name="mathtype2mml"
  type="docx2hub:mathtype2mml">

  <p:input port="source" primary="true">
    <p:documentation>The result of docx2hub:single-tree (or apply-changemarkup)</p:documentation>
  </p:input>
  <p:input port="zip-manifest">
    <p:documentation>The zip-manifest output port of docx2hub:single-tree</p:documentation>
  </p:input>
  <p:input port="params">
    <p:documentation>The params output port of docx2hub:single-tree</p:documentation>
  </p:input>
  <p:input port="schematron">
    <p:document href="../sch/mathtype2mml.sch.xml"/>
  </p:input>
  <p:input port="custom-font-maps" primary="false" sequence="true">
    <p:documentation xmlns="http://www.w3.org/1999/xhtml">
      <p>A sequence of &lt;symbols&gt; documents, containing mapped characters as found in the regular docs2hub fontmaps.</p>
      <p>Each &lt;symbols&gt; is required to contain the name of its font-family as an attribute @name.</p>
      <p>Example, the value of @char is the unicode character that will be in the mml output:</p>
      <pre>&lt;symbols name="Times New Roman">
  &lt;symbol number="002F" entity="&#x002f;" char="&#x002f;"/>
&lt;/symbols></pre>
      <p>If the base name of the base URI’s file name part is not identical with the font name as encoded in MTEF, 
      you need to give the converter a hint by adding an attribute <code>/symbols/@mathtype-name</code>.</p>
    </p:documentation>
    <p:empty/>
  </p:input>

  <p:output port="result" primary="true">
    <p:documentation>The same basic structure as the primary source of the current step, but with equation OLE objects replaced with MathML</p:documentation>
    <p:pipe port="result" step="convert-mathtype2mml"/>
  </p:output>
  <p:output port="modified-zip-manifest">
    <p:pipe port="zip-manifest" step="convert-mathtype2mml"/>
  </p:output>
  <p:output port="report" sequence="true">
    <p:pipe port="report" step="convert-mathtype2mml"/>
  </p:output>

  <p:serialization port="result" omit-xml-declaration="false"/>
  
  <p:option name="debug" required="false" select="'no'"/>
  <p:option name="debug-dir-uri" required="false" select="'file:/tmp/debug'"/>
  <p:option name="active" required="false" select="'yes'">
    <p:documentation>Whether MathType→MathML conversion happens at all.</p:documentation>
  </p:option>
  <p:option name="mml-space-handling" select="'mspace'">
    <p:documentation>see corresponding documentation for docx2hub</p:documentation>
  </p:option>
  
  <p:import href="http://xmlcalabash.com/extension/steps/library-1.0.xpl"/>
  <p:import href="http://transpect.io/calabash-extensions/mathtype-extension/xpl/mathtype2mml-declaration.xpl"/>
  <p:import href="http://transpect.io/xproc-util/store-debug/xpl/store-debug.xpl"/>

  <p:choose name="convert-mathtype2mml">
    <p:when test="$active eq 'yes'">
      <p:output port="result" primary="true"/>
      <p:output port="zip-manifest">
        <p:pipe port="result" step="remove-converted-objects-from-zip-manifest"/>
      </p:output>
      <p:output port="report" sequence="true">
        <p:pipe port="result" step="check"/>
        <p:pipe port="result" step="extract-errors"/>
      </p:output>
      <p:variable name="basename" select="replace(/w:root/@local-href, '^.+/(.+)\.do[ct][mx]$', '$1')"/>
      
      <p:viewport match="/w:root/*[local-name() = ('document', 'footnotes', 'endnotes')]//w:object/o:OLEObject[@Type eq 'Embed' and starts-with(@ProgID, 'Equation')]"
                  name="mathtype2mml-viewport">
        <p:variable name="rel-id" select="o:OLEObject/@r:id"/>
        <p:variable name="rels-elt" select="if (contains(base-uri(/*), '/word/document'))
                                            then 'w:docRels'
                                            else if (contains(base-uri(/*), '/word/footnotes'))
                                              then 'w:footnoteRels'
                                              else if (contains(base-uri(/*), '/word/endnotes'))
                                                then 'w:endnoteRels'
                                                else ''"/>
        <p:variable name="equation-href" select="concat(/w:root/@xml:base, 'word/',
                                                        /w:root/*[name() = $rels-elt]/rel:Relationships/rel:Relationship[@Id eq $rel-id]/@Target
                                               )">
          <p:pipe port="source" step="mathtype2mml"/>
        </p:variable>
        
        <p:try name="mathtype2mml-wrapper">
          <p:group>
            <tr:mathtype2mml name="convert">
              <p:input port="additional-font-maps">
                <p:document href="http://transpect.io/fontmaps/Symbol.xml"/>
                <p:document href="http://transpect.io/fontmaps/Webdings.xml"/>
                <p:document href="http://transpect.io/fontmaps/Wingdings.xml"/>
                <p:document href="http://transpect.io/fontmaps/Wingdings_2.xml"/>
                <p:document href="http://transpect.io/fontmaps/Wingdings_3.xml"/>
                <p:pipe port="custom-font-maps" step="mathtype2mml"/>
              </p:input>
              <p:with-option name="href" select="$equation-href"/>
              <p:with-option name="debug" select="$debug"/>
              <p:with-option name="debug-dir-uri" select="concat($debug-dir-uri, '/docx2hub/', $basename, '/')"/>
            </tr:mathtype2mml>
            <p:choose>
              <p:when test="/c:errors">
                <p:set-attributes match="/*" name="set-atts">
                  <p:input port="source" select="/c:errors/c:error">
                    <p:pipe port="result" step="convert"/>
                  </p:input>
                  <p:input port="attributes">
                    <p:pipe port="result" step="convert"/>
                  </p:input>
                </p:set-attributes>
                <p:add-attribute name="add-srcpath" attribute-name="srcpath" match="/*">
                  <p:with-option name="attribute-value" select="/*/@srcpath">
                    <p:pipe port="current" step="mathtype2mml-viewport"/>
                  </p:with-option><commit_msg>use Euclid fontmaps in mtef2mml<commit_after><?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" 
  xmlns:c="http://www.w3.org/ns/xproc-step"
  xmlns:cx="http://xmlcalabash.com/ns/extensions"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:docx2hub="http://transpect.io/docx2hub"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
  xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  xmlns:tr="http://transpect.io"
  version="1.0" 
  name="mathtype2mml"
  type="docx2hub:mathtype2mml">

  <p:input port="source" primary="true">
    <p:documentation>The result of docx2hub:single-tree (or apply-changemarkup)</p:documentation>
  </p:input>
  <p:input port="zip-manifest">
    <p:documentation>The zip-manifest output port of docx2hub:single-tree</p:documentation>
  </p:input>
  <p:input port="params">
    <p:documentation>The params output port of docx2hub:single-tree</p:documentation>
  </p:input>
  <p:input port="schematron">
    <p:document href="../sch/mathtype2mml.sch.xml"/>
  </p:input>
  <p:input port="custom-font-maps" primary="false" sequence="true">
    <p:documentation xmlns="http://www.w3.org/1999/xhtml">
      <p>A sequence of &lt;symbols&gt; documents, containing mapped characters as found in the regular docs2hub fontmaps.</p>
      <p>Each &lt;symbols&gt; is required to contain the name of its font-family as an attribute @name.</p>
      <p>Example, the value of @char is the unicode character that will be in the mml output:</p>
      <pre>&lt;symbols name="Times New Roman">
  &lt;symbol number="002F" entity="&#x002f;" char="&#x002f;"/>
&lt;/symbols></pre>
      <p>If the base name of the base URI’s file name part is not identical with the font name as encoded in MTEF, 
      you need to give the converter a hint by adding an attribute <code>/symbols/@mathtype-name</code>.</p>
    </p:documentation>
    <p:empty/>
  </p:input>

  <p:output port="result" primary="true">
    <p:documentation>The same basic structure as the primary source of the current step, but with equation OLE objects replaced with MathML</p:documentation>
    <p:pipe port="result" step="convert-mathtype2mml"/>
  </p:output>
  <p:output port="modified-zip-manifest">
    <p:pipe port="zip-manifest" step="convert-mathtype2mml"/>
  </p:output>
  <p:output port="report" sequence="true">
    <p:pipe port="report" step="convert-mathtype2mml"/>
  </p:output>

  <p:serialization port="result" omit-xml-declaration="false"/>
  
  <p:option name="debug" required="false" select="'no'"/>
  <p:option name="debug-dir-uri" required="false" select="'file:/tmp/debug'"/>
  <p:option name="active" required="false" select="'yes'">
    <p:documentation>Whether MathType→MathML conversion happens at all.</p:documentation>
  </p:option>
  <p:option name="mml-space-handling" select="'mspace'">
    <p:documentation>see corresponding documentation for docx2hub</p:documentation>
  </p:option>
  
  <p:import href="http://xmlcalabash.com/extension/steps/library-1.0.xpl"/>
  <p:import href="http://transpect.io/calabash-extensions/mathtype-extension/xpl/mathtype2mml-declaration.xpl"/>
  <p:import href="http://transpect.io/xproc-util/store-debug/xpl/store-debug.xpl"/>

  <p:choose name="convert-mathtype2mml">
    <p:when test="$active eq 'yes'">
      <p:output port="result" primary="true"/>
      <p:output port="zip-manifest">
        <p:pipe port="result" step="remove-converted-objects-from-zip-manifest"/>
      </p:output>
      <p:output port="report" sequence="true">
        <p:pipe port="result" step="check"/>
        <p:pipe port="result" step="extract-errors"/>
      </p:output>
      <p:variable name="basename" select="replace(/w:root/@local-href, '^.+/(.+)\.do[ct][mx]$', '$1')"/>
      
      <p:viewport match="/w:root/*[local-name() = ('document', 'footnotes', 'endnotes')]//w:object/o:OLEObject[@Type eq 'Embed' and starts-with(@ProgID, 'Equation')]"
                  name="mathtype2mml-viewport">
        <p:variable name="rel-id" select="o:OLEObject/@r:id"/>
        <p:variable name="rels-elt" select="if (contains(base-uri(/*), '/word/document'))
                                            then 'w:docRels'
                                            else if (contains(base-uri(/*), '/word/footnotes'))
                                              then 'w:footnoteRels'
                                              else if (contains(base-uri(/*), '/word/endnotes'))
                                                then 'w:endnoteRels'
                                                else ''"/>
        <p:variable name="equation-href" select="concat(/w:root/@xml:base, 'word/',
                                                        /w:root/*[name() = $rels-elt]/rel:Relationships/rel:Relationship[@Id eq $rel-id]/@Target
                                               )">
          <p:pipe port="source" step="mathtype2mml"/>
        </p:variable>
        
        <p:try name="mathtype2mml-wrapper">
          <p:group>
            <tr:mathtype2mml name="convert">
              <p:input port="additional-font-maps">
                <p:document href="http://transpect.io/fontmaps/Symbol.xml"/>
                <p:document href="http://transpect.io/fontmaps/Webdings.xml"/>
                <p:document href="http://transpect.io/fontmaps/Wingdings.xml"/>
                <p:document href="http://transpect.io/fontmaps/Wingdings_2.xml"/>
                <p:document href="http://transpect.io/fontmaps/Wingdings_3.xml"/>
                <p:document href="http://transpect.io/fontmaps/Euclid_Fraktur.xml"/>
                <p:document href="http://transpect.io/fontmaps/Euclid_Math_One.xml"/>
                <p:document href="http://transpect.io/fontmaps/Euclid_Math_Two.xml"/>
                <p:pipe port="custom-font-maps" step="mathtype2mml"/>
              </p:input>
              <p:with-option name="href" select="$equation-href"/>
              <p:with-option name="debug" select="$debug"/>
              <p:with-option name="debug-dir-uri" select="concat($debug-dir-uri, '/docx2hub/', $basename, '/')"/>
            </tr:mathtype2mml>
            <p:choose>
              <p:when test="/c:errors">
                <p:set-attributes match="/*" name="set-atts">
                  <p:input port="source" select="/c:errors/c:error">
                    <p:pipe port="result" step="convert"/>
                  </p:input>
                  <p:input port="attributes">
                    <p:pipe port="result" step="convert"/>
                  </p:input>
                </p:set-attributes>
                <p:add-attribute name="add-srcpath" attribute-name="srcpath" match="/*">
                  <p:with-option name="attribute-value" select="/*/@srcpath">
                    <p:pipe port="current" step="mathtype2mml-viewport"/>
                  </p:with-option>
{'fraction_non_alphanumeric': 0.17477828250054078, 'fraction_numerical': 0.010454971519215516, 'mean_word_length': 2.679045092838196, 'pattern_counts': {'":': 0, '<': 216, '<?xml version=': 2, '>': 222, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 55}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '619127', 'n_tokens_mistral': 4614, 'n_tokens_neox': 4382, 'n_words': 743}