-
Notifications
You must be signed in to change notification settings - Fork 0
/
glemtext.gleam
213 lines (175 loc) · 4.44 KB
/
glemtext.gleam
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import gleam/option.{type Option, None, Some}
import gleam/string
import gleam/list
/// A `Document` is a list of Gemtext elements. As Gemtext "elements"
/// are not nested, there is no need for a nested structure here, either.
///
/// As such, `Document` is a type alias to `List(GemElement)`
pub type Document =
List(GemElement)
/// `GemElement` is an element, or possible line type for Gemtext.
pub type GemElement {
/// `Text` is any text that doesn't have specific syntax for creating
/// an element. Note that empty lines are `Text("")`
Text(String)
/// A link to another document, both Gemini and non-Gemini,
/// and optionally, a label.
Link(to: String, label: Option(String))
Heading(level: Int, text: String)
ListItem(String)
Blockquote(String)
Preformatted(alt: Option(String), text: String)
}
/// `parse` parses a string into a Gemtext Document.
/// It will always return a valid document, and does not panic.
pub fn parse(text: String) -> Document {
text
|> string.replace("\r\n", "\n")
|> string.to_graphemes
|> parse_document([])
|> list.reverse
}
fn parse_document(in: Chars, ast: Document) -> Document {
case in {
["=", ">", ..rest] -> {
let #(link, in) = parse_link(rest)
parse_document(in, [link, ..ast])
}
["*", " ", ..rest] -> {
let #(item, in) = parse_list(rest)
parse_document(in, [item, ..ast])
}
[">", ..rest] -> {
let #(quote, in) = parse_blockquote(rest)
parse_document(in, [quote, ..ast])
}
["#", " ", ..rest] -> {
let #(heading, in) = parse_heading(1, rest)
parse_document(in, [heading, ..ast])
}
["#", "#", " ", ..rest] -> {
let #(heading, in) = parse_heading(2, rest)
parse_document(in, [heading, ..ast])
}
["#", "#", "#", " ", ..rest] -> {
let #(heading, in) = parse_heading(3, rest)
parse_document(in, [heading, ..ast])
}
["`", "`", "`", ..rest] -> {
let #(preformatted, in) = parse_preformatted(rest)
parse_document(in, [preformatted, ..ast])
}
[] -> ast
_ -> {
let #(text, in) = parse_text(in)
parse_document(in, [text, ..ast])
}
}
}
fn parse_blockquote(in: Chars) -> #(GemElement, Chars) {
let #(line, rest) = get_line(in)
let line =
line
|> drop_ws
|> string.join("")
#(Blockquote(line), rest)
}
fn parse_text(in: Chars) -> #(GemElement, Chars) {
let #(line, rest) = get_line(in)
let line = string.join(line, "")
#(Text(line), rest)
}
fn parse_preformatted(in: Chars) -> #(GemElement, Chars) {
let #(line, rest) = get_line(in)
let alt =
line
|> drop_ws
|> string.join("")
let alt = case alt {
"" -> None
_ -> Some(alt)
}
let #(pre_lines, in) = take_preformatted_lines(rest)
let pre_lines =
pre_lines
|> string.join("\n")
#(Preformatted(alt: alt, text: pre_lines), in)
}
fn take_preformatted_lines(in: Chars) -> #(List(String), Chars) {
let #(line, rest) = get_line(in)
case line {
["`", "`", "`", ..rest] -> {
let #(_, rest) = get_line(rest)
#([], rest)
}
[] -> #([], [])
_ -> {
let #(next, in) = take_preformatted_lines(rest)
let line =
line
|> string.join("")
#([line, ..next], in)
}
}
}
fn parse_heading(level: Int, in: Chars) -> #(GemElement, Chars) {
let #(line, rest) = get_line(in)
let line =
line
|> drop_ws
|> string.join("")
#(Heading(level, line), rest)
}
fn parse_link(in: Chars) -> #(GemElement, Chars) {
let #(line, rest) = get_line(in)
let #(url, linerest) =
line
|> drop_ws
|> list.split_while(fn(c) { !{ c == " " || c == "\t" } })
let url = string.join(url, "")
let label =
linerest
|> drop_ws
|> string.join("")
let label = case label {
"" -> None
_ -> Some(label)
}
#(Link(to: url, label: label), rest)
}
fn parse_list(in: Chars) -> #(GemElement, Chars) {
let #(line, rest) = get_line(in)
let line =
line
|> drop_ws
|> string.join("")
#(ListItem(line), rest)
}
fn get_line(in: Chars) -> #(Chars, Chars) {
let line =
in
|> list.take_while(until_nl)
let rest =
in
|> list.drop_while(until_nl)
|> list.drop(1)
#(line, rest)
}
fn until_nl(c) {
c != "\n"
}
fn drop_lines(in: Chars) {
case in {
["\n", ..rest] -> drop_lines(rest)
_ -> in
}
}
fn drop_ws(in: Chars) {
case in {
[" ", ..rest] -> drop_ws(rest)
["\t", ..rest] -> drop_ws(rest)
_ -> in
}
}
type Chars =
List(String)