This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
next_token.go
174 lines (155 loc) · 4.43 KB
/
next_token.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
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
package parser
import (
"bytes"
"github.com/bouncepaw/mycomarkup/blocks"
"github.com/bouncepaw/mycomarkup/mycocontext"
"html"
"strings"
)
func isPrefixedBy(ctx mycocontext.Context, s string) bool { // This function has bugs in it!
return bytes.HasPrefix(ctx.Input().Bytes(), []byte(s))
}
func nextLaunchPad(ctx mycocontext.Context) (blocks.LaunchPad, bool) {
var (
hyphaName = ctx.HyphaName()
launchPad = blocks.MakeLaunchPad()
line string
done bool
)
for isPrefixedBy(ctx, "=>") {
line, done = mycocontext.NextLine(ctx)
launchPad.AddRocket(blocks.MakeRocketLink(line, hyphaName))
}
return launchPad, done
}
func nextImg(ctx mycocontext.Context) (img blocks.Img, done bool) {
var r rune
line, done := mycocontext.NextLine(ctx)
img, imgDone := blocks.MakeImg(line, ctx.HyphaName())
if imgDone {
return img, done
}
for !imgDone && !done {
r, done = mycocontext.NextRune(ctx)
imgDone = img.ProcessRune(r)
}
defer mycocontext.NextLine(ctx) // Characters after the final } of img are ignored.
return img, done
}
func nextCodeBlock(ctx mycocontext.Context) (code blocks.CodeBlock, done bool) {
line, done := mycocontext.NextLine(ctx)
code = blocks.MakeCodeBlock(strings.TrimPrefix(line, "```"), "")
for {
line, done = mycocontext.NextLine(ctx)
switch {
case strings.HasPrefix(line, "```"):
return code, done
default:
code.AddLine(html.EscapeString(line))
}
if done {
return code, done
}
}
}
func linesForQuote(ctx mycocontext.Context) ([]string, bool) {
var (
line string
lines []string
done bool
)
for {
line, done = mycocontext.NextLine(ctx)
// Drop >, remove spaces, save this line
lines = append(lines, strings.TrimSpace(line[1:]))
// If the next line is not part of the same quote, we break.
if !isPrefixedBy(ctx, ">") {
break
}
}
return lines, done
}
func nextQuote(ctx mycocontext.Context) (blocks.Quote, bool) {
var (
quote = blocks.Quote{}
lines, done = linesForQuote(ctx)
innerText bytes.Buffer
)
for i, line := range lines {
if i > 0 {
innerText.WriteRune('\n')
}
innerText.WriteString(line)
}
parseSubdocumentForEachBlock(ctx, &innerText, func(block blocks.Block) {
quote.AddBlock(block)
})
return quote, done
}
func nextLineIsSomething(ctx mycocontext.Context) bool {
prefices := []string{"=>", "<=", "```", "* ", "*. ", "*v ", "*x ", "# ", "## ", "### ", "#### ", "##### ", "###### ", ">", "----"}
for _, prefix := range prefices {
if isPrefixedBy(ctx, prefix) {
return true
}
}
return emptyLine(ctx) || blocks.MatchesImg(ctx.Input().String()) || matchesTable(ctx)
}
func emptyLine(ctx mycocontext.Context) bool {
for _, b := range ctx.Input().Bytes() {
switch b {
case '\n':
return true
case '\t', ' ', '\r':
continue
default:
return false
}
}
return true
}
func nextToken(ctx mycocontext.Context) (blocks.Block, bool) {
switch {
case emptyLine(ctx):
_, done := mycocontext.NextLine(ctx)
return nil, done
case looksLikeList(ctx):
//case isPrefixedBy(ctx, "* "), isPrefixedBy(ctx, "*. "), isPrefixedBy(ctx, "*v "), isPrefixedBy(ctx, "*x "): — alternative way?
return nextList(ctx)
case isPrefixedBy(ctx, "```"):
return nextCodeBlock(ctx)
case isPrefixedBy(ctx, "=>"):
return nextLaunchPad(ctx)
case isPrefixedBy(ctx, ">"):
return nextQuote(ctx)
case isPrefixedBy(ctx, "<="):
line, done := mycocontext.NextLine(ctx)
return blocks.MakeTransclusion(line, ctx.HyphaName()), done
case isPrefixedBy(ctx, "----"):
line, done := mycocontext.NextLine(ctx)
return blocks.MakeHorizontalLine(line), done
case isPrefixedBy(ctx, "###### "):
line, done := mycocontext.NextLine(ctx)
return MakeHeading(line, ctx.HyphaName(), 6), done
case isPrefixedBy(ctx, "##### "):
line, done := mycocontext.NextLine(ctx)
return MakeHeading(line, ctx.HyphaName(), 5), done
case isPrefixedBy(ctx, "#### "):
line, done := mycocontext.NextLine(ctx)
return MakeHeading(line, ctx.HyphaName(), 4), done
case isPrefixedBy(ctx, "### "):
line, done := mycocontext.NextLine(ctx)
return MakeHeading(line, ctx.HyphaName(), 3), done
case isPrefixedBy(ctx, "## "):
line, done := mycocontext.NextLine(ctx)
return MakeHeading(line, ctx.HyphaName(), 2), done
case isPrefixedBy(ctx, "# "):
line, done := mycocontext.NextLine(ctx)
return MakeHeading(line, ctx.HyphaName(), 1), done
case blocks.MatchesImg(ctx.Input().String()):
return nextImg(ctx)
case matchesTable(ctx):
return nextTable(ctx)
}
return nextParagraph(ctx)
}