<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:xhtml="https://www.w3.org/1999/xhtml/" exclude-result-prefixes="xs tei xhtml xsl" version="2.0">
    <xsl:output encoding="utf-8" method="xhtml" indent="yes"/>
    
    
    <xsl:template match="/tei:TEI">
        <html>
            <head>
                <title>
                    <xsl:value-of select="/tei:TEI/tei:teiHeader[1]/tei:fileDesc[1]/tei:titleStmt[1]/tei:title[1]"/>
                </title>
                <style>
                    /* Container: Use flex to align verse number and text */
                    p.verse {
                    display: flex;
                    align-items: flex-start;
                    padding-left: 1em;
                    margin: 0;
                    line-height: 1.4;
                    word-wrap: break-word; /* Allow long words to break */
                        position: relative;
                    }
                    
                    /* Verse number: Fixed width, right-aligned, no negative padding */
                    .verse-nr {
                    color: #808080;
                    font-weight: bold;
                    margin-right: 0.5em;
                    width: 2.5em; /* Adjust to fit "1:1" or "10:1" */
                    text-align: right;
                    white-space: nowrap;
                    }
                    
                    /* Verse text: Takes remaining space, wraps normally */
                    span.verse-text {
                    flex: 1; /* Takes available space */
                    white-space: normal;
                    word-break: break-word;
                    }
                    
                    /* Hidden verse ID (will appear on hover) */
                    span.verse-id {
                    display: none;
                    color: #6d01a9;
                    margin: 0 1em 0 1em;
                    white-space: nowrap;
                    }
                    

                    /* On hover, show the floating bubble */
                    p.verse:hover span.verse-id {
                    display: block;
                    position: absolute;
                    top: -2em;
                    left: 0;
                    background: #333;
                    color: #fff;
                    font-size: 0.8em;
                    padding: 2px 6px;
                    border-radius: 4px;
                    white-space: nowrap;
                    z-index: 100;
                    border: 1px solid #555;
                    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
                    }
                    
                    div.chapter {
                    margin: 2em 0 0 0;
                    }
                    
                    ul.metadata {
                    padding: 0 0 0 2em !important;
                    list-style: disc !important;
                    }
                    
                </style>
            </head>
            <body>
                <xsl:call-template name="title"/>
                <xsl:apply-templates select="./node()"/>
            </body>
        </html>
    </xsl:template>
    
    
    
    
    <xsl:template match="tei:teiHeader">
        <h2>Metadata from the <pre style="display: inline;">teiHeader</pre></h2>
        <ul class="metadata">
            <li>                    
                <xsl:value-of select="./tei:fileDesc/tei:sourceDesc/tei:bibl[@type='work']/tei:title" /> (<xsl:value-of select="substring(./../tei:text/tei:body/tei:div/@xml:id, 3)" />)
            </li>
            <li>
                <xsl:value-of select="./tei:fileDesc/tei:sourceDesc/tei:bibl/tei:date" /> 
            </li>
            <li>
                Language: <xsl:value-of select="./tei:profileDesc/tei:langUsage/tei:language/text()" /> (<xsl:value-of select="./tei:profileDesc/tei:langUsage/tei:language/tei:idno" />)
            </li>
            <li>
                Work's Wikidata-ID:
                <xsl:element name="a">
                    <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:attribute name="href"><xsl:value-of select="./tei:fileDesc/tei:sourceDesc/tei:bibl/tei:idno[@type='wikidata']/@corresp"/></xsl:attribute>
                    <xsl:value-of select="./tei:fileDesc/tei:sourceDesc/tei:bibl/tei:idno[@type='wikidata']" />
                </xsl:element>
            </li>
            <li>
                Work's GND-ID:
                <xsl:element name="a">
                    <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:attribute name="href"><xsl:value-of select="./tei:fileDesc/tei:sourceDesc/tei:bibl/tei:idno[@type='gnd']/@corresp"/></xsl:attribute>
                    <xsl:value-of select="./tei:fileDesc/tei:sourceDesc/tei:bibl/tei:idno[@type='gnd']" />
                </xsl:element>
            </li>
            <li>
                <xsl:element name="a">
                    <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:attribute name="href">https://wiki.k10plus.de/spaces/K10PLUS/pages/437452809</xsl:attribute>
                    Basic Classification:
                </xsl:element>
                
                <xsl:value-of select="./tei:profileDesc/tei:textClass/tei:keywords[@scheme='http://uri.gbv.de/terminology/bk/']/tei:term[2]/@key" /><xsl:text> </xsl:text><xsl:value-of select="./tei:profileDesc/tei:textClass/tei:keywords[@scheme='http://uri.gbv.de/terminology/bk/']/tei:term[2]" />
            </li>

            <li>
                Testament (GND):                     

                <xsl:element name="a">
                    <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:attribute name="href"><xsl:value-of select="./tei:profileDesc/tei:textClass/tei:keywords[@scheme='https://d-nb.info/gnd/']/tei:term[@type='testament']/@ref"/></xsl:attribute>
                    
                    <xsl:value-of select="./tei:profileDesc/tei:textClass/tei:keywords[@scheme='https://d-nb.info/gnd/']/tei:term[@type='testament']" />
                </xsl:element>
            </li>
            <li>
                Part (GND):                     
                
                <xsl:element name="a">
                    <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:attribute name="href"><xsl:value-of select="./tei:profileDesc/tei:textClass/tei:keywords[@scheme='https://d-nb.info/gnd/']/tei:term[@type='part']/@ref"/></xsl:attribute>
                    
                    <xsl:value-of select="./tei:profileDesc/tei:textClass/tei:keywords[@scheme='https://d-nb.info/gnd/']/tei:term[@type='part']" />
                </xsl:element>
            </li>
        </ul>
    </xsl:template>
    
    
    
    <xsl:template match="tei:text//tei:ab">
        <xsl:element name="p">
            <xsl:attribute name="class"><xsl:value-of select="./@type"/></xsl:attribute>
            <xsl:attribute name="xml:id"><xsl:value-of select="./@xml:id"/></xsl:attribute>
            <span class="verse-nr" >
                <xsl:variable name="parts" select="tokenize(./@xml:id, '\.')"/>
                <!-- Remove leading zeros from chapter (parts[3]) and verse (parts[4]) -->
                <xsl:value-of select="concat(
                    number($parts[3]), 
                    ':', 
                    number($parts[4])
                    )"/>
            </span>
            <span class="verse-id" >
                <xsl:value-of select="string(./@xml:id)"/>
            </span>
            
            <span class="verse-text">
                <xsl:apply-templates/>
            </span>
        </xsl:element>
    </xsl:template>
    
    
    
    
    <xsl:template match="tei:text//tei:head">
        <xsl:element name="h2">
            <xsl:attribute name="xml:id"><xsl:value-of select="./../@xml:id"/>.chapter</xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>       
    </xsl:template>
    
    
    
    
    <xsl:template match="tei:text//tei:div">
        <xsl:element name="div">
            <xsl:attribute name="class"><xsl:value-of select="./@type"/></xsl:attribute>
            <xsl:attribute name="xml:id"><xsl:value-of select="./@xml:id"/></xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    
    
    
    
    <xsl:template name="title">
        <h1>
            <xsl:value-of select="//tei:TEI/tei:teiHeader[1]/tei:fileDesc[1]/tei:titleStmt[1]/tei:title[1]"/>
        </h1>
    </xsl:template>
    
    
    
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    
    
    <xsl:template match="tei:body|tei:back|tei:front|tei:text|tei:back/tei:div|tei:front/tei:div|tei:head">
        <xsl:apply-templates/>
    </xsl:template>
    
    
    
</xsl:stylesheet>
