This repository was archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathutils.go
97 lines (76 loc) · 2.09 KB
/
utils.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
94
95
96
97
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"errors"
"fmt"
"os"
"strings"
"unicode"
bf "gopkg.in/russross/blackfriday.v2"
)
// fileExists returns true if the specified file exists, else false.
func fileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// splitLink splits a link like "foo.md#section-name" into a filename
// ("foo.md") and a section name ("section-name").
func splitLink(linkName string) (fileName, sectionName string, err error) {
if linkName == "" {
return "", "", errors.New("need linkName")
}
if !strings.Contains(linkName, anchorPrefix) {
return linkName, "", nil
}
fields := strings.Split(linkName, anchorPrefix)
expectedFields := 2
foundFields := len(fields)
if foundFields != expectedFields {
return "", "", fmt.Errorf("invalid link %s: expected %d fields, found %d", linkName, expectedFields, foundFields)
}
fileName = fields[0]
sectionName = fields[1]
return fileName, sectionName, nil
}
// validHeadingIDChar is a strings.Map() function used to determine which characters
// can appear in a heading ID.
func validHeadingIDChar(r rune) rune {
if unicode.IsLetter(r) ||
unicode.IsNumber(r) ||
unicode.IsSpace(r) ||
r == '-' || r == '_' {
return r
}
// Remove all other chars from destination string
return -1
}
// createHeadingID creates an HTML anchor name for the specified heading
func createHeadingID(headingName string) (id string, err error) {
if headingName == "" {
return "", fmt.Errorf("need heading name")
}
// Munge the original heading into an id by:
//
// - removing invalid characters.
// - lower-casing.
// - replace spaces
id = strings.Map(validHeadingIDChar, headingName)
id = strings.ToLower(id)
id = strings.Replace(id, " ", "-", -1)
return id, nil
}
func checkNode(node *bf.Node, expectedType bf.NodeType) error {
if node == nil {
return errors.New("node cannot be nil")
}
if node.Type != expectedType {
return fmt.Errorf("expected %v node, found %v", expectedType, node.Type)
}
return nil
}