
de.bottlecaps.railroad.xq.xhtml-to-md.xq Maven / Gradle / Ivy
module namespace m="de/bottlecaps/railroad/xq/xhtml-to-md.xq";
declare namespace svg="http://www.w3.org/2000/svg";
declare namespace xhtml="http://www.w3.org/1999/xhtml";
declare namespace xlink="http://www.w3.org/1999/xlink";
declare namespace xsl="http://www.w3.org/1999/XSL/Transform";
declare option saxon:output "method=text";
declare variable $m:serialization-options :=
;
declare function m:rewrite($nodes, $format, $referenced-by as xs:boolean)
{
for $node in $nodes
return
typeswitch ($node)
case document-node() return
m:rewrite($node/node(), $format, $referenced-by)
case element(svg:svg) return
if (empty($node/ancestor::xhtml:body)) then
()
else
let $name := $node/preceding::xhtml:p[1]/(.//xhtml:a, following::xhtml:a)[1]/@name
let $name :=
if ($name) then
$name
else
string(count($node/preceding::svg:svg) + 1)
let $ebnf := string-join($node/following::xhtml:div[@class="ebnf"][1]/xhtml:code/*, "
")
!replace(., " ", " ")
let $svg :=
element {node-name($node)}
{
$node/@*,
$node/preceding::xhtml:head/svg:svg/svg:defs,
$node/node()
}
let $img := concat("), ")")
return
if (empty($node/following::svg:svg)) then
(
"##
",
$img,
" generated by [RR - Railroad Diagram Generator][RR]
",
"
",
"[RR]: http://bottlecaps.de/rr/ui"
)
else
(
"**", string($name), ":**
",
$img,
"
",
if ($ebnf[.]) then
(
"```
",
$ebnf,
"
```
"
)
else
(),
let $referenced-by-links :=
if ($node/following-sibling::xhtml:p[1]/xhtml:div/@class = "ebnf") then
$node/following-sibling::xhtml:p[2]//xhtml:li/xhtml:a
else
$node/following-sibling::xhtml:p[1]//xhtml:li/xhtml:a
where exists($referenced-by-links)
return
(
"referenced by:
",
for $a in $referenced-by-links
return ("* ", data($a/@title), "
"),
"
"
)
)
case element() return
m:rewrite($node/node(), $format, $referenced-by)
default return
()
};
declare function m:transform($input as node()*) as xs:string
{
string-join(m:rewrite($input, "png", false()), "")
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy