-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
141 lines (132 loc) · 4.82 KB
/
parser.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// json_markd helps you create json from markdown lists.
//
// Useful for creating API docs, personal todo, which can be used as json anywhere.
package json_markd
import (
"fmt"
"io/ioutil"
"strings"
)
// Allowed markdown element types
type DataType int
const (
Object DataType = 0 // use object in markdown
String DataType = 1 // use string in markdown
Integer DataType = 2 // use integer in markdown
Double DataType = 3 // use double in markdown
Array DataType = 4 // use array in markdown
Invalid DataType = 5
)
var tabSpacesValue = 2
// SetTabSpaceValue is to set spaces used for tab in markdown. Eg. 2,4
func SetTabSpaceValue(val int) {
tabSpacesValue = val
}
// ParseMarkdown accepts a filepath to a markdown file and return JSON string for same
func ParseMarkdown(filepath string) (string, error) {
SetupLogger()
markdownData, err := ioutil.ReadFile(filepath)
if err != nil {
fmt.Println("File reading error", err)
return "", err
}
markdownDataList := strings.Split(strings.TrimSuffix(string(markdownData), "\n"), "\n")
var lineDataList []string
for _, line := range markdownDataList {
list := strings.Split(line, "\t")
if len(list) != 0 {
lineDataList = append(lineDataList, list[0])
}
}
markdownBlockList, err := createMarkdownBlockList(lineDataList)
if err != nil {
return "", err
}
result := generateMarkdownString(markdownBlockList)
return result, nil
}
func generateMarkdownString(markdownBlockList []markdownBlock) string {
result := "{\n"
s := itemStack{}
s.new()
for ind, markdownBlock := range markdownBlockList {
// if at last element
if (ind + 1) == len(markdownBlockList) {
topIndex := 0
if s.size() > 0 {
topIndex = (*s.top()).(int)
}
if markdownBlockList[topIndex].Value == Array {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatypeWhenParentIsArray() + markdownBlock.GetSuffixForDatatype() + "\n"
} else {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatype() + markdownBlock.GetSuffixForDatatype() + "\n"
}
for s.size() > 0 {
item, _ := s.pop()
index := (*item).(int)
result += strings.Repeat(" ", markdownBlockList[index].TabCount+1) + markdownBlockList[index].GetSuffixForDatatype() + "\n"
}
break
}
// if nested block present
if markdownBlock.TabCount < markdownBlockList[ind+1].TabCount {
// append start string to result
topIndex := 0
if s.size() > 0 {
topIndex = (*s.top()).(int)
}
if markdownBlockList[topIndex].Value == Array {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatypeWhenParentIsArray()
} else {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatype()
}
s.push(ind)
} else {
topIndex := 0
if s.size() > 0 {
topIndex = (*s.top()).(int)
}
if markdownBlock.TabCount == markdownBlockList[ind+1].TabCount {
if markdownBlockList[topIndex].Value == Array {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatypeWhenParentIsArray() + markdownBlock.GetSuffixForDatatype() + ",\n"
} else {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatype() + markdownBlock.GetSuffixForDatatype() + ",\n"
}
} else if markdownBlock.TabCount > markdownBlockList[ind+1].TabCount {
if markdownBlockList[topIndex].Value == Array {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatypeWhenParentIsArray() + markdownBlock.GetSuffixForDatatype() + "\n"
} else {
result += strings.Repeat(" ", markdownBlock.TabCount+1) + markdownBlock.GetPrefixForDatatype() + markdownBlock.GetSuffixForDatatype() + "\n"
}
}
for s.size() > 0 {
index := (*s.top()).(int)
if markdownBlockList[index].TabCount == markdownBlockList[ind+1].TabCount {
result += strings.Repeat(" ", markdownBlockList[index].TabCount+1) + markdownBlockList[index].GetSuffixForDatatype() + ",\n"
s.pop()
} else if markdownBlockList[index].TabCount > markdownBlockList[ind+1].TabCount {
result += strings.Repeat(" ", markdownBlockList[index].TabCount+1) + markdownBlockList[index].GetSuffixForDatatype() + "\n"
s.pop()
} else {
break
}
}
}
}
result += "}"
return result
}
func createMarkdownBlockList(lineDataList []string) ([]markdownBlock, error) {
var markdownBlockList []markdownBlock
for _, line := range lineDataList {
tabCount, lineWithoutTabs := removeTabsFromLines(line, tabSpacesValue)
key, val, err := parseLine(lineWithoutTabs)
if err != nil {
return []markdownBlock{}, err
}
dataType := getDatatypeFromVal(val)
markdownBlock := newMarkdownBlock(tabCount, key, dataType)
markdownBlockList = append(markdownBlockList, markdownBlock)
}
return markdownBlockList, nil
}