<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:t="http://www.tei-c.org/ns/1.0" version="1.0">
    <!-- Template to do a best guess merge of paragraphs which span pages -->
    <xsl:output indent="yes" method="xml" encoding="UTF-8" media-type="text/xml"/>
    
    <xsl:template match="node()|@*">
        <!-- Identity Transform -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template name="p" match="t:p">
        <xsl:param name="first-preceding-p" select="preceding-sibling::*[local-name(.)='p'][1]"/>
        
        <xsl:choose>
            <xsl:when
                test="following-sibling::*[1][ local-name(.)='pb'] and not(contains('.?!&quot;',substring(normalize-space(.),string-length(normalize-space(.)),1)))">
                <!-- Skip this p since it needs to be merged with the next p -->
            </xsl:when>
            <xsl:when
                test="preceding-sibling::*[1][ local-name(.)='pb'] and not(contains('.?!&quot;',substring(normalize-space($first-preceding-p),string-length(normalize-space($first-preceding-p)),1)))">
                <xsl:copy>
                    <xsl:attribute name="xml:id">
                        <xsl:value-of select="@xml:id"/>
                    </xsl:attribute>
                    <xsl:apply-templates select="@*" />
                    <xsl:comment>Merged paragraph</xsl:comment>
                    <xsl:call-template name="recurse-p"/>
                    <xsl:apply-templates select="node()"/>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="recurse-p">
        <xsl:param name="first-preceding-p" select="preceding-sibling::*[local-name(.)='p'][1]"/>
        <xsl:param name="first-preceding-pb" select="preceding-sibling::*[local-name(.)='pb'][1]"/>
        <!-- Merge this p with the previous p -->
        <xsl:choose>
            <xsl:when
                test="preceding-sibling::*[1][ local-name(.)='pb'] and not(contains('.?!&quot;',substring(normalize-space($first-preceding-p),string-length(normalize-space($first-preceding-p)),1)))">
                <xsl:if
                    test="$first-preceding-p/preceding-sibling::*[1][ local-name(.)='pb'] and 
                    not(contains('.?!&quot;',substring(normalize-space($first-preceding-p/preceding-sibling::*[local-name(.)='p'][1]),string-length(normalize-space($first-preceding-p/preceding-sibling::*[local-name(.)='p'][1])),1)))">
                    <xsl:call-template name="recurse-p">
                        <xsl:with-param name="first-preceding-p"
                            select="$first-preceding-p/preceding-sibling::*[local-name(.)='p'][1]"/>
                        <xsl:with-param name="first-preceding-pb"
                            select="$first-preceding-p/preceding-sibling::*[local-name(.)='pb'][1]"/>
                    </xsl:call-template>
                </xsl:if>
                <xsl:copy-of select="$first-preceding-p/node()"/>
                <!-- <xsl:copy-of select="$first-preceding-pb"/>-->
                <xsl:call-template name="recurse-pb">
                    <xsl:with-param name="current-pb" select="$first-preceding-pb"/>
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="recurse-pb">
        <xsl:param name="current-pb" />
        <xsl:choose>        
            <xsl:when test="$current-pb/preceding-sibling::*[1][local-name(.)='pb']">
                <xsl:call-template name="recurse-pb">
                    <xsl:with-param name="current-pb" select="$current-pb/preceding-sibling::*[1][local-name(.)='pb']"/>
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
        <xsl:copy-of select="$current-pb"/>
    </xsl:template>
    
    <xsl:template match="t:pb">
        <xsl:param name="first-preceding-p" select="preceding-sibling::*[local-name(.)='p'][1]"/>
        
        <xsl:choose>
            <xsl:when
                test="not(contains('.?!&quot;',substring(normalize-space($first-preceding-p),string-length(normalize-space($first-preceding-p)),1)))">
                <!-- skip this pb since it will be merged into the following p -->
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>
