<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="filter.rnc" type="application/relax-ng-compact-syntax"?>
<alter-tan-file xmlns="tag:textalign.net,2015:ns">
    <!-- alter instructions to turn an expanded TAN-T(EI) file to HTML -->
    <phase-list>
        <phase xml:id="cleanup1-skipping-a"/>
        <breakpoint/>
        <phase xml:id="cleanup1-skipping-b"/>
        <phase xml:id="cleanup1-skipping-c"/>
        <phase xml:id="cleanup2-adding"/>
        <phase xml:id="html"/>
        <phase xml:id="test"/>
    </phase-list>
    <alter>
        <!-- cleanup: get rid of stuff, or change some names -->
        <where phases="cleanup1-skipping-a" node-type="attribute"/>
        <where phases="cleanup1-skipping-b" node-type="processing-instruction comment"/>
        <where phases="cleanup1-skipping-b" node-name="teiHeader" node-type="element"/>
        <where phases="cleanup1-skipping-b" node-type="element" node-name="error help warning"/>
        <!-- get rid of extra <ref>s and <n>s -->
        <where phases="cleanup1-skipping-c" node-type="element" node-name="n"
            evaluate="{exists($p1/preceding-sibling::*:n) and exists($p1/parent::*:div)}"/>
        <where phases="cleanup1-skipping-c" node-type="element" node-name="ref"
            evaluate="{exists($p1/preceding-sibling::*:ref)}"/>
        <skip shallow="false"/>
    </alter>
    <!-- <src> (TAN-T-merge) should be marked as a label -->
    <alter phases="cleanup1-skipping-c" node-name="src" node-type="element"
        evaluate="{exists($p1/parent::*:head)}">
        <rename new="label"/>
    </alter>

    <!-- cleanup: add stuff -->
    <!-- add class value for namespace whenever it changes -->
    <alter phases="cleanup2-adding" node-type="element"
        node-namespace="http://www.tei-c.org/ns/1.0"
        evaluate="{not(namespace-uri($p1/..) = 'http://www.tei-c.org/ns/1.0')}">
        <add-attributes class="tei" replace-existing="false"/>
    </alter>
    <alter phases="cleanup2-adding" node-type="element" node-namespace="tag:textalign.net,2015:ns"
        evaluate="{not(namespace-uri($p1/..) = 'tag:textalign.net,2015:ns')}">
        <add-attributes class="tan" replace-existing="false"/>
    </alter>
    <!-- Add a <label> to the root element -->
    <alter>
        <where phases="cleanup2-adding" node-type="element" evaluate="{not(exists($p1/parent::*))}"/>
        <prepend-content>
            <label>{name($p1)}</label>
        </prepend-content>
        <children-elements node-name="expansion">
            <group min-group-size="1" prepend-label="expansions"/>
        </children-elements>
    </alter>
    <!-- group head children, except anything marked as a label -->
    <alter phases="cleanup2-adding" node-name="head teiHeader">
        <children-elements node-name="*" evaluate="{not($p1/self::*:label)}">
            <group min-group-size="1"
                prepend-label="{name($p1)}{if (count($p2) gt 1) then concat('s (',count($p2),')') else ()}"
            />
        </children-elements>
    </alter>
    <!-- mark TAN-T-merge <div>s that have a leaf div to be formatted as <html:tr> -->
    <alter phases="cleanup2-adding" node-name="div"
        evaluate="{$p1/*:div[not(*:div)]/*:type = 'version'}">
        <add-attributes class="tr" replace-existing="false"/>
    </alter>
    <alter phases="cleanup2-adding" node-name="TAN-T-merge">
        <add-attributes class="sortable" replace-existing="false"/>
    </alter>
    <alter phases="cleanup2-adding" node-name="head" evaluate="{exists($p1/parent::*:TAN-T-merge)}">
        <add-attributes draggable="true"/>
    </alter>

    <alter phases="html" node-type="element" node-name="div">
        <add-attributes class="{$p1/tan:type}{for $i in $p1/tan:src return concat('src--',$i)}"
            replace-existing="false"/>
    </alter>
    <alter node-type="element" phases="html">
        <rename new="div" namespace="http://www.w3.org/1999/xhtml"/>
        <add-attributes class="{name($p1)}" replace-existing="false"/>
    </alter>

    <!-- tests, scrap -->
    <!--<alter phases="html" node-name="n" evaluate="{$p1/../@class = 'ref'}">
        <add-attributes class="span" replace-existing="false"/>
    </alter>-->
    <!--<alter phases="test" node-matches="\S" node-type="text">
        <prepend-siblings>{name($p1/..)}</prepend-siblings>
    </alter>-->
    <!--<alter phases="test" node-type="element" node-name="div ref">
        <!-\-<add-attributes class="span" replace-existing="false"/>-\->
        <add-attributes test1="{exists($p1/*:n)}" test2="{for $i in $p1/* return name($i)}"/>
    </alter>-->
    <!--<alter phases="test" node-name="n" evaluate="{exists($p1/parent::*:ref)}">
        <add-attributes test3="parent"/>
    </alter>-->
    <!--<alter phases="test" node-name="TAN-T-merge">
        <children-elements node-name="head">
            <group min-group-size="1" prepend-label="sources"/>
        </children-elements>
    </alter>-->
    <!--<alter>
        <!-\- in leaf divs get rid of text nodes if the tei version is also present -\->
        <where phases="test" node-type="text" node-matches="\S"
            evaluate="{exists($p1/parent::*:div/(* except (n, ref)))}"/>
        <!-\- in leaf divs, we assume tokenized content trumps tei content, so we suppress the latter -\->
        <where phases="test" node-namespace="http://www.tei-c.org/ns/1.0"
            evaluate="{exists($p1/../*:tok)}"/>
        <skip/>
    </alter>-->
</alter-tan-file>
