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
168 lines (152 loc) · 4.55 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
package parser
import (
"bytes"
"github.com/bouncepaw/mycomarkup/mycocontext"
"html"
"strings"
"github.com/bouncepaw/mycomarkup/blocks"
)
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 b byte
line, done := mycocontext.NextLine(ctx)
img, imgDone := blocks.MakeImg(line, ctx.HyphaName())
if imgDone {
return img, done
}
for !imgDone {
b, done = mycocontext.NextByte(ctx)
imgDone = img.ProcessRune(rune(b))
}
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 nextTable(ctx mycocontext.Context) (t blocks.Table, done bool) {
line, done := mycocontext.NextLine(ctx)
t = blocks.TableFromFirstLine(line, ctx.HyphaName())
for {
line, done = mycocontext.NextLine(ctx)
if t.ProcessLine(line) {
break
}
}
return t, done
}
func nextParagraph(ctx mycocontext.Context) (p blocks.Paragraph, done bool) {
line, done := mycocontext.NextLine(ctx)
p = blocks.Paragraph{blocks.MakeFormatted(line, ctx.HyphaName())}
if nextLineIsSomething(ctx) {
return
}
for {
line, done = mycocontext.NextLine(ctx)
if done && line == "" {
break
}
p.AddLine(line)
if nextLineIsSomething(ctx) {
break
}
}
return
}
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()) || blocks.MatchesTable(ctx.Input().String())
}
func emptyLine(ctx mycocontext.Context) bool {
for _, b := range ctx.Input().Bytes() {
switch b {
case '\n':
return true
case '\t', ' ':
continue
default:
return false
}
}
return false
}
// Lex `line` in markup and maybe return a token.
func nextToken(ctx mycocontext.Context) (interface{}, 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, "<="):
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 blocks.MakeHeading(line, ctx.HyphaName(), 6), done
case isPrefixedBy(ctx, "##### "):
line, done := mycocontext.NextLine(ctx)
return blocks.MakeHeading(line, ctx.HyphaName(), 5), done
case isPrefixedBy(ctx, "#### "):
line, done := mycocontext.NextLine(ctx)
return blocks.MakeHeading(line, ctx.HyphaName(), 4), done
case isPrefixedBy(ctx, "### "):
line, done := mycocontext.NextLine(ctx)
return blocks.MakeHeading(line, ctx.HyphaName(), 3), done
case isPrefixedBy(ctx, "## "):
line, done := mycocontext.NextLine(ctx)
return blocks.MakeHeading(line, ctx.HyphaName(), 2), done
case isPrefixedBy(ctx, "# "):
line, done := mycocontext.NextLine(ctx)
return blocks.MakeHeading(line, ctx.HyphaName(), 1), done
case isPrefixedBy(ctx, ">"): // TODO: implement proper fractal quotes
line, done := mycocontext.NextLine(ctx)
return blocks.MakeQuote(line, ctx.HyphaName()), done
case blocks.MatchesImg(ctx.Input().String()):
return nextImg(ctx)
case blocks.MatchesTable(ctx.Input().String()):
return nextTable(ctx)
}
return nextParagraph(ctx)
}