toc-api.xqm 2.6 KB
Newer Older
Mathias Goebel's avatar
toc api  
Mathias Goebel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
xquery version "3.1";
(:~
 : Provides a REST-API to get table of contents
 :
 : @author Mathias Göbel
 : @version 1.0
 : @since 2.6
 : :)

module namespace toc="https://fontane-nb.dariah.eu/tocapi";

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace test="http://exist-db.org/xquery/xqsuite";
declare namespace xhtml="http://www.w3.org/1999/xhtml";

import module namespace rest="http://exquery.org/ns/restxq";
import module namespace config="http://textgrid.de/ns/SADE/config" at "../config/config.xqm";

declare variable $toc:dataPath := "/db/sade-projects/textgrid/data/xml/data/";

declare
  %rest:GET
  %rest:path("fontane/toc/{$uri}")
  %output:method("html")

  %test:name("entities per page")

function toc:table-of-contents($uri as xs:string)
{
  let $doc := doc($toc:dataPath || $uri || ".xml")
  let $toc := $doc//tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:msContents/tei:ab/tei:list[@type="editorial"]
  return
    element div {
      element div {
        attribute class {"panel-group"},
        for $item at $pos in $toc/tei:item
        let $heading := string($item) => substring-before(" (")
        return
          element div {
            attribute class {"panel panel-default"},
            element div { (: heading :)
              attribute class {"panel-heading"},
              element h4 {
                attribute class {"panel-title"},
                element a {
                  attribute href { "#collapse" || $pos },
                  attribute data-parent {"#accordion"},
                  attribute data-toggle { "collapse" },
                  text {$heading}
                }
              }
            }, (: end heading :)
            element div {
              attribute class { "panel-collapse collapse" },
              attribute id { "collapse" || $pos },
              element div {
                attribute class {"panel-body tocview"},
                element ul {
                  for $ref in $item//tei:ref
                  let $targetPage := $ref/@target => substring-after("[@n='") => substring-before("']")
                  return
                    element li {
                      attribute class { "target" || $targetPage },
                      element a {
                        attribute href { "edition.html?id=/xml/data/" || $uri || ".xml&page=" || $targetPage },
                        text { string($ref) => replace("\s*Blatt\s+", "") }
                      }
                    }
                }
              }
            }
          }
      }
    }
};