forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
93 lines (73 loc) · 1.82 KB
/
extract.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
bf "gopkg.in/russross/blackfriday.v2"
)
// linkDescription extracts the description from the specified link node.
func linkDescription(l *bf.Node) (string, error) {
if err := checkNode(l, bf.Link); err != nil {
return "", err
}
// A link description can be comprised of various elements so scan
// through them to build up the final value.
text := ""
node := l.FirstChild
for node != nil {
switch node.Type {
case bf.Code:
text += string(node.Literal)
case bf.Text:
text += string(node.Literal)
default:
logger.WithField("node", node).Debug("ignoring node")
}
if node == l.LastChild {
break
}
node = node.Next
}
return text, nil
}
// headingName extracts the heading name from the specified Heading node in
// plain text, and markdown. The latter is used for creating TOC's which need
// to include the original markdown value.
func headingName(h *bf.Node) (name, mdName string, err error) {
if err = checkNode(h, bf.Heading); err != nil {
return "", "", err
}
// A heading can be comprised of various elements so scan
// through them to build up the final value.
node := h.FirstChild
for node != nil {
switch node.Type {
case bf.Code:
value := string(node.Literal)
name += value
mdName += fmt.Sprintf("`%s`", value)
case bf.Text:
value := string(node.Literal)
name += value
mdName += value
case bf.Link:
// yep, people do crazy things like adding links into titles!
descr, err := linkDescription(node)
if err != nil {
return "", "", err
}
name += descr
mdName += descr
default:
logger.WithField("node", node).Debug("ignoring node")
}
if node == h.LastChild {
break
}
node = node.Next
}
return name, mdName, nil
}