Welcome to mirror list, hosted at ThFree Co, Russian Federation.

toc_from_pages.html « funcs « partials « layouts - github.com/bep/docuapi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 456f8a6fecd7cb0d8d7d87510d6cbbfe759cdb3c (plain)
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
{{ $combined := slice }}
{{ range . }}
    {{ $headers := findRE "<h\\d.*?>(.|\n)*?</h\\d>" .Content }}
    {{ $combined = $combined | append $headers }}
{{ end }}

{{ $toc := slice }}

{{ $previousH1 := dict}}
{{ $previousH2 := dict}}
{{ $previousLevel := 0 }}

{{ $h2s := slice }}
{{ $h3s := slice }}

{{ range $combined }}
    {{ $level := int (substr . 2 1) }}
    {{ if le $level 3 }}
        {{ $idTitle := split (. | replaceRE "<h\\d id=\"(.*?)\">(.*)</h\\d>" "$1|$2") "|" }} 
        {{ $item := dict "level" $level "id" (index $idTitle 0) "title" (index $idTitle 1) }}

        {{ if eq $level 1 }}
            {{ if ne $previousLevel 0 }}
                {{ if or (eq $previousLevel 2) (eq $previousLevel 3) }}
                    {{ $tocItem := merge $previousH2 (dict "sub" $h3s) }}
                    {{ $h2s = $h2s | append $tocItem }}
                    {{ $h3s = slice }}
                {{ end }}

                {{ $tocItem := merge $previousH1 (dict "sub" $h2s) }}
                {{ $toc = $toc | append $tocItem }}
                {{ $h2s = slice }}
                {{ $previousH2 = slice }}
            {{ end }}
            {{ $previousH1 = $item }}
        {{ else }}
            {{ if eq $level 2 }}
                {{ if and (ne $previousLevel 1) (ne $previousLevel 2) }}
                    {{ $tocItem := merge $previousH2 (dict "sub" $h3s) }}
                    {{ $h2s = $h2s | append $tocItem }}
                    {{ $h3s = slice }}
                {{ end }}
                {{ if eq $previousLevel 2 }}
                    {{ $h2s = $h2s | append $previousH2 }}
                {{ end }}
                {{ $previousH2 = $item }}
            {{ else }}
                {{ $h3s = $h3s | append $item }}
            {{ end }}
        {{ end }}
        {{ $previousLevel = $level }}
    {{ end }}
{{ end }}

{{ if ne $previousLevel 0 }}
    {{ $tocItem := merge $previousH2 (dict "sub" $h3s) }}
    {{ $h2s = $h2s | append $tocItem }}

    {{ $item := merge $previousH1 (dict "sub" $h2s) }}
    {{ $toc = $toc | append $item }}
{{ end }}

{{ return $toc }}