-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.go
executable file
·123 lines (107 loc) · 3.06 KB
/
grammar.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
package main
import (
"regexp"
)
const (
BlankChars string = " \r\n\t"
LineFeed string = "\n"
)
//meta characters
const (
EscapeChar string = "\\"
LeftBraceChar string = "{"
RightBraceChar string = "}"
FillerChar string = "#"
)
//supported grammar elements
const (
RawTextChar string = "r"
//inline element
EmphasisFormat = "e"
StrongFormat = "s"
HyperLink = "w"
InlineCode = "c"
AnchorBlock = "a"
ReferToBlock = "k"
ImageKeyword = "image"
CaptionKeyword = "caption"
InlineTex = "t"
CommentKeyword = "--" //it is able to comment out other grammar elements
//section
SectionHeader = "h"
SectionHeader1 = "h1"
SectionHeader2 = "h2"
SectionHeader3 = "h3"
SectionHeader4 = "h4"
SectionHeader5 = "h5"
SectionHeader6 = "h6"
//Sections that may have caption and may be shown in specific index
BlockTex = "tex"
BlockCode = "code"
OrderList = "ol"
BulletList = "ul"
ListItemMark = "-"
TableKeyword = "table"
//sub element of Table
TableCellDelimiterKeyword = "d"
//meta
TitleKeyword = "title"
SubTitleKeyword = "sub-title"
AuthorKeyword = "author"
CreateDateKeyword = "create-date"
ModifyDateKeyword = "modify-date"
KeywordsKeyword = "keywords"
//
IncludeKeyword string = "include" //to include other document
//index
SectionIndexKeyword = "toc"
ImageIndexKeyword = "image-index"
TableIndexKeyword = "table-index"
OrderListIndexKeyword = "order-list-index"
BulletListIndexKeyword = "bullet-list-index"
CodeIndexKeyword = "code-index"
MathIndexKeyword = "math-index"
)
var (
gTokenPattern = regexp.MustCompile(`[a-zA-Z-_\.][a-zA-Z0-9-_\.]*`)
gParagraphDivider = regexp.MustCompile(`(\n\s*\n)|(\r\n\s*\r\n)`)
)
var (
//MetaChars contains all meta characters in slice
MetaChars = []string{EscapeChar, LeftBraceChar, RightBraceChar, FillerChar}
//MetaCharMap contains all meta characters in map to be lookup
MetaCharMap = make(map[string]bool)
gInlineFormatMap = make(map[string]bool)
gInlineFormatList = []string{
EmphasisFormat, StrongFormat, HyperLink, InlineCode, CommentKeyword, AnchorBlock, ReferToBlock, InlineTex,
}
gChunkWithCaptionList = []string{
OrderList, BulletList, TableKeyword, BlockCode, ImageKeyword, BlockTex,
}
gChunkWithCaptionMap = make(map[string]bool)
gSectionLevel = map[string]int{SectionHeader: 1, SectionHeader1: 1, SectionHeader2: 2,
SectionHeader3: 3, SectionHeader4: 4, SectionHeader5: 5, SectionHeader6: 6}
gMetaInfoKeywordMap = make(map[string]bool)
gMetaInfoKeywordList = []string{
TitleKeyword,
SubTitleKeyword,
AuthorKeyword,
CreateDateKeyword,
ModifyDateKeyword,
KeywordsKeyword,
}
)
func init() {
for _, f := range gInlineFormatList {
gInlineFormatMap[f] = true
}
for _, f := range gChunkWithCaptionList {
gChunkWithCaptionMap[f] = true
}
for _, f := range gMetaInfoKeywordList {
gMetaInfoKeywordMap[f] = true
}
for _, c := range MetaChars {
MetaCharMap[c] = true
}
}