aboutsummaryrefslogtreecommitdiff
path: root/markdown/lexer.go
blob: 738684406e6b7d54a36f20fb65ae8f9d425b5046 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package markdown

import (
	"fmt"
	"strings"
)

type lexerType string

const (
	lexerBreak lexerType = "break"

	lexerModifier lexerType = "modifier"

	lexerCode lexerType = "code"

	lexerHeading lexerType = "header"
	lexerQuote   lexerType = "quote"
	lexerList    lexerType = "list"

	lexerExternal lexerType = "external"
	lexerCallout  lexerType = "callout"

	lexerLiteral lexerType = "literal"
	lexerReplace lexerType = "replace"
)

type lexer struct {
	Type  lexerType
	Value string
}

func (l lexer) String() string {
	return fmt.Sprintf("%s(%s)", l.Type, strings.ReplaceAll(l.Value, "\n", `{\n}`))
}

type lexers struct {
	current int
	lexers  []lexer
}

func (l *lexers) Next() bool {
	l.current++
	return !l.Finished()
}

func (l *lexers) Current() lexer {
	return l.lexers[l.current]
}

func (l *lexers) Finished() bool {
	return l.current >= len(l.lexers)
}

func (l *lexers) Before() bool {
	l.current--
	return l.current >= 0 && !l.Finished()
}

func (l *lexers) String() string {
	var sb strings.Builder
	// 8 for "Lexers[" and "]"
	sb.Grow(8 + len(l.lexers)*4)
	_, err := sb.WriteString("Lexers[")
	if err != nil {
		panic(err)
	}
	for _, l := range l.lexers {
		_, err = sb.WriteString(l.String())
		if err == nil {
			_, err = sb.WriteString(" ")
		}
		if err != nil {
			panic(err)
		}
	}
	_, err = sb.WriteString("]")
	if err != nil {
		panic(err)
	}
	return sb.String()
}

func lex(s string, opt *Option) *lexers {
	lxs := &lexers{current: -1}
	var lexs []lexer
	var currentType lexerType
	var previous string
	fn := func(c rune, t lexerType, validate func(rune) bool) {
		if validate == nil {
			validate = func(r rune) bool { return true }
		}
		if (currentType != t || !validate(c)) && len(previous) > 0 {
			lexs = append(lexs, lexer{Type: currentType, Value: previous})
			previous = ""
		}
		currentType = t
		previous += string(c)
	}
	newLine := true
	literalNext := false
	runes := []rune(s)
	for i, c := range runes {
		if literalNext {
			fn(c, lexerLiteral, nil)
			literalNext = false
			continue
		}
		if c == '\\' {
			literalNext = true
			continue
		}
		switch c {
		case '*':
			if newLine && i < len(runes)-1 && runes[i+1] == ' ' {
				fn(c, lexerList, nil)
				newLine = false
				continue
			}
			fallthrough
		case '_':
			if (currentType != lexerModifier && len(previous) > 0) ||
				(len(previous) > 0 && []rune(previous)[0] != c) ||
				len(previous) >= 3 {
				lexs = append(lexs, lexer{Type: currentType, Value: previous})
				previous = ""
			}
			currentType = lexerModifier
			previous += string(c)
		case '`':
			fn(c, lexerCode, nil)
		case '\n':
			fn(c, lexerBreak, nil)
		case '#':
			fn(c, lexerHeading, nil)
		case '>':
			fn(c, lexerQuote, nil)
		case '[', ']', '!':
			if !newLine && i < len(runes)-1 {
				next := runes[i+1]
				runes := []rune(previous)
				if (c == '[' && next == '!') ||
					(c == '!' && len(runes) > 0 && previous[len(previous)-1] == '[') ||
					(c == ']' && next != '(') {
					allSpace := true
					for i := 0; allSpace && i < len(runes); i++ {
						if runes[i] != ' ' {
							allSpace = false
						}
					}
					if allSpace {
						previous = ""
					}
					fn(c, lexerCallout, nil)
					continue
				}
			}
			fallthrough
		case '(', ')':
			fn(c, lexerExternal, func(c rune) bool { return validExternal(previous + string(c)) })
		case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
			fn(c, lexerList, nil)
		default:
			if _, ok := opt.Replaces[c]; ok {
				fn(c, lexerReplace, func(c rune) bool { return false })
			} else {
				fn(c, lexerLiteral, nil)
			}
		}
		newLine = c == '\n'
	}
	if len(previous) > 0 {
		lexs = append(lexs, lexer{Type: currentType, Value: previous})
	}
	lxs.lexers = lexs
	return lxs
}

func validExternal(s string) bool {
	switch s {
	// start
	case "![", "[":
		return true
	// mid
	case "](":
		return true
	// end
	case ")":
		return true
	default:
		return false
	}
}