forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.go
75 lines (58 loc) · 1.48 KB
/
toc.go
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
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"strings"
bf "gopkg.in/russross/blackfriday.v2"
)
// displayTOC displays a table of contents entry for the specified node.
func (d *Doc) displayTOC(node *bf.Node) error {
switch node.Type {
case bf.Heading:
return d.displayTOCEntryFromNode(node)
case bf.Text:
// handle blackfriday deficiencies
headings, err := d.forceCreateHeadings(node)
if err != nil {
return err
}
for _, heading := range headings {
err := d.displayTOCEntryFromHeading(heading)
if err != nil {
return err
}
}
}
return nil
}
// displayTOCEntryFromHeading displays a table of contents entry
// for the specified heading.
func (d *Doc) displayTOCEntryFromHeading(heading Heading) error {
const indentSpaces = 4
prefix := ""
level := heading.Level
// Indent needs to be zero for top level headings
level--
if level > 0 {
prefix = strings.Repeat(" ", level*indentSpaces)
}
entry := fmt.Sprintf("[%s](%s%s)", heading.MDName, anchorPrefix, heading.LinkName)
fmt.Printf("%s%s %s\n", prefix, listPrefix, entry)
return nil
}
// displayTOCEntryFromHeading displays a table of contents entry
// for the specified heading.
func (d *Doc) displayTOCEntryFromNode(node *bf.Node) error {
if err := checkNode(node, bf.Heading); err != nil {
return err
}
heading, err := d.makeHeading(node)
if err != nil {
return err
}
return d.displayTOCEntryFromHeading(heading)
}