Skip to content

Instantly share code, notes, and snippets.

@tonyahowe
Last active November 7, 2017 15:33
Show Gist options
  • Save tonyahowe/9ec27e9261116bdb11b0cfc2cecc5ea7 to your computer and use it in GitHub Desktop.
Save tonyahowe/9ec27e9261116bdb11b0cfc2cecc5ea7 to your computer and use it in GitHub Desktop.
Working tei transformation xquery
module namespace tei2="http://exist-db.org/xquery/app/tei2html";
import module namespace console="http://exist-db.org/xquery/console";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare function tei2:tei2html($nodes as node()*) {
for $node in $nodes
return
typeswitch ($node)
case text() return
$node
case element(tei:TEI) return
tei2:tei2html($node/*)
case element(tei:teiHeader) return
tei2:header($node)
case element(tei:frontTitleTiny) return
<h4>{ tei2:tei2html($node/node()) }</h4>
case element(tei:frontTitleSm) return
<h3>{ tei2:tei2html($node/node()) }</h3>
case element(tei:frontTitleMed) return
<h2>{ tei2:tei2html($node/node()) }</h2>
case element(tei:frontTitleLg) return
<h1>{ tei2:tei2html($node/node()) }</h1>
case element(tei:head) return
<h3>{ tei2:tei2html($node/node()) }</h3>
case element(tei:lb) return
<br/>
case element(tei:add) return
<span style="color:red;float:right">{ tei2:tei2html($node/node()) }</span>
case element(tei:l) return
<l>{ tei2:tei2html($node/node()) }</l>
case element(tei:i) return
<i>{ tei2:tei2html($node/node()) }</i>
case element(tei:hr) return
<hr/>
case element(tei:pb) return
<span style="color:lightgray">[end page {tei2:tei2html($node/@n)}]{ tei2:tei2html($node/node()) }</span>
case element(tei:footer) return
<hr>{ tei2:tei2html($node/node()) }</hr>
case element(tei:front) return
tei2:front($node)
case element(tei:tooltip_notes) return
$node
case element(tei:rs) return
tei2:ttipster($node)
(: tei2:link($node) :)
case element(tei:quote) return
tei2:ttipster($node)
(: case element(tei:epigraph) return
tei2:ttipster($node) :)
(: case element(tei:cit) return
tei2:ttipster($node) :)
(: case element(tei:author) return
tei2:ttipster($node) :)
(: case element(tei:bibl) return
tei2:ttipster($node) :)
case attribute(test) return
<b>{ tei2:tei2html($node/node()) }</b>
case element(tei:note) return
tei2:ttipster($node)
case element(tei:footnote) return
tei2:footnote($node)
case element(tei:p) return
<p xmlns="http://www.w3.org/1999/xhtml" id="{tei2:get-id($node)}">{ tei2:tei2html($node/node()) }</p> (: THIS IS WHERE THE ANCHORS ARE INSERTED! :)
case element(exist:match) return
<mark xmlns="http://www.w3.org/1999/xhtml">{ $node/node() }</mark>
case element() return
tei2:tei2html($node/node())
default return
$node/string() (: what would this catch? comment node in the xml? :)
};
declare function tei2:italics($node as element()) {
if ($node/@rend eq 'italic') then
<span style="font-style:italic"> {
tei2:tei2html ($node/node()) } </span>
else
tei2:tei2html( $node/node())
};
(: declare function tei2:link($node as element()) {
if ($node/@link ne '' and $node/@link castable as xs:anyURI) then
<a href="{$node/@link}" data-toggle="tooltip" title="{$node/@title}">{
tei2:tei2html( $node/node() )
}</a>
else
tei2:tei2html( $node/node() )
}; :)
(: I think the addition of data-tooltip-content here will insert content stored in another location -where?- into tooltip; need to add the first layer to the xml pages, then find the place for the content templates; also, remove the title content from them. decide if will use content or title. content might be more updatatable. :)
declare function tei2:ttipster($node as element()) {
<a href="{$node/@link}" class="tooltip2" data-tooltip-content="{$node/@content}"> {
tei2:tei2html ( $node/node() )
}</a>
};
(: declare function tei2:note($node as element()) {
<span style="color:black" href="{$node/@link}" data-toggle="tooltip" title="{$node/@title}"> {
tei2:tei2html ( $node/node() )
}</span>
}; :)
(: the footnote function has not been updated to work with tooltipster :)
declare function tei2:footnote($node as element()) {
<a style="color:gray" href="{$node/@link}" data-toggle="tooltip" title="{$node/@title}"><sup> {
tei2:tei2html ( $node/node() )
}</sup></a>
};
declare function tei2:header($header as element(tei:teiHeader)) {
let $titleStmt := $header//tei:titleStmt
let $pubStmt := $header//tei:publicationStmt
let $sourceDesc := $header//tei:sourceDesc
let $authors := $header//tei:titleStmt/tei:author
let $resps := $header//tei:respStmt
let $names := $header//tei:respStmt/tei:name
let $availability := $header//tei:availability
let $imprints := $header//tei:sourceDesc/tei:imprint
let $onlineImprints := $header//tei:sourceDesc/tei:imprint/tei:extent[@type="online"]
let $publishers := $header//tei:sourceDesc/tei:imprint/tei:publisher
(: link issue: works, but don't want it to display with @type=physical extent. need an or operator? or something else? :)
return
<div xmlns="http://www.w3.org/1999/xhtml" class="text-header">
<h1>{$titleStmt/tei:title/text()}</h1>
<h1><small>By
{
let $author-full-names :=
for $author in $authors
return
concat($author//tei:forename, ' ', $author//tei:surname)
let $name-count := count($authors)
return
if ($name-count le 2) then
string-join($author-full-names, ' and ')
else
concat(
string-join(
$author-full-names[position() = (1 to last() - 1)]
,
', '),
', and ',
$author-full-names[last()]
)
}
</small></h1><p></p>
{
for $resp in $header
return
<li class="list-unstyled">
{concat($titleStmt/tei:respStmt/tei:resp, ': ' , string-join($titleStmt/tei:respStmt/tei:name, ', ')) }
</li>
}
<p class="bg-warning">
{ $availability/text() }
</p>
</div>
};
declare function tei2:front($front as element (tei:front)) {
let $titlePage := $front//tei:titlePage
let $titlePart := $front//tei:titlePart
let $epigraph := $front//tei:epigraph
let $note := $front//tei:note
let $quote := $front//tei:quote
let $bibl := $front//tei:bibl
let $author := $front//tei:author
return
<div xmlns="http://www.w3.org/1999/xhtml" class="main-text-frontmatter">
{
for $n in $titlePage
return
<center>
{ $titlePage/node()/tei2:tei2html(.) }
</center>
}
</div>
(: { :)
(: console:log($epigraph),:)
(: for $cit in $epigraph:)
(: return:)
(: <blockquote>:)
(: {:)
(: <a href="{$quote/@link}" data-toggle="tooltip" title="{$quote/@title}">{$quote/tei:l}</a>:)
(: :)
(: }<br/>:)
(: --:)
(: {:)
(: <a href="{$author/@link}" data-toggle="tooltip" title="{$author/@title}">{$author/text()}</a>:)
(: }:)
(: </blockquote>:)
(: }:)
(: </div> :)
};
(:declare function tei2:body($body as element (tei:body)) {:)
(: let $para := $body//tei:p:)
(: return :)
(: $para :)
(:};:)
(:declare function tei2:pageImages($pb as element (tei:pb)) {:)
(: let $facsPage := $pb/@facs:)
(: for $pb in "work":)
(: return:)
(: <img src="../images/{$facsPage}"/>:)
(:};:)
declare %private function tei2:get-id($node as element()) {
($node/@xml:id, $node/@exist:id)[1]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment