-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.go
80 lines (71 loc) · 2.37 KB
/
lib.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
package main
import (
"regexp"
"strconv"
"strings"
)
// ConvertGithubMarkup converts the github markup file to Wiki MarkDown file
func ConvertGithubMarkup(filename string) (string, error) {
result := []string{}
output, err := ReadFile(filename)
if err != nil {
return "", err
} else {
logger.Info("Total Lines", len(output))
for _, v := range output {
transformations := []func(string) string{replaceHeaders, replaceInfoMessages, replaceHyperLinks, replaceCodeBlocks, replaceBoldText, replaceInlineCodeBlocks}
for _, fn := range transformations {
v = fn(v)
}
result = append(result, v)
}
return strings.Join(result, "\n"), nil
}
}
// replaceHeaders replaces the header hashes ## with h2 markup
func replaceHeaders(input string) string {
r, _ := regexp.Compile("^[ ]{0,3}[#]{1,}")
headerSize := r.FindString(input)
if len(headerSize) > 0 {
// input = len(strings.TrimSpace(headerSize))
wikiMarkup := "h" + strconv.Itoa(len(strings.TrimSpace(headerSize))) + "."
return strings.Replace(input, headerSize, wikiMarkup, 1)
}
return input
}
// replaceInfoMessages replaces the '>' markup with {info} block
func replaceInfoMessages(input string) string {
r, _ := regexp.Compile("^[ ]{0,3}[>]{1}")
if r.MatchString(input) {
input = strings.TrimSpace(strings.Replace(input, ">", "", 1))
return "{info}" + input + "{info}"
}
return input
}
// replaceHyperLinks replaces the hyperlinks [name](link) to [name|link]
// Regex Used to match \[(.*?)\]\((.*?)\)
func replaceHyperLinks(input string) string {
r, _ := regexp.Compile("\\[(.*?)\\]\\((.*?)\\)")
matches := r.FindAllString(input, -1)
if len(matches) > 0 {
for _, v := range matches {
linkName := strings.Split(v[1:], "]")[0]
linkURL := strings.Split(strings.Split(v, "(")[1], ")")[0]
wikiMarkup := "[" + linkName + "|" + linkURL + "]"
input = strings.Replace(input, v, wikiMarkup, -1)
}
}
return input
}
// replaceCodeBlocks replaces the back ticks ``` with underscores {code} to get codeblock markup
func replaceCodeBlocks(input string) string {
return strings.Replace(input, "```", "{code}", -1)
}
// replaceBoldText
func replaceBoldText(input string) string {
return strings.Replace(input, "**", "*", -1)
}
// replaceInlineCodeBlocks replaces the back ticks ` with underscores _ to get italicized
func replaceInlineCodeBlocks(input string) string {
return strings.Replace(input, "`", "_", -1)
}