-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
110 lines (91 loc) · 2.82 KB
/
parser.go
File metadata and controls
110 lines (91 loc) · 2.82 KB
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
// Package parser implements gemtext specification concepts by defining various
// structures and methods for manipulating gemtext.
//
// To represent a gemtext document which is line-oriented, flat, and
// nonhierarchical, this package uses an Abstract Syntax List (ASL) instead of
// the usual Abstract Syntax Tree (AST). Under the hood, ASLs are just slices.
//
// There are different ways to use this package. You can parse a gemtext
// document using [Parse] or [ParseFile]. From there, you can convert it to HTML
// with [ToHTML] or [ToHTMLFile], or transform it using the different [ASL]
// methods before generating a new gemtext document with [ToGemtext] or
// [ToGemtextFile]. You may even want to programmatically create a gemtext
// document by passing the different [Element] types, [Text], [Link], [Heading],
// [List], [Quote], and [PreformatToggle], to [NewASL].
package parser
import (
"os"
"strings"
"unicode"
)
// Parse parses s into an ASL.
func Parse(s string) ASL {
asl := NewASL()
isPreformattedMode := false
for line := range strings.Lines(s) {
if isPreformattedMode && !strings.HasPrefix(line, "```") {
asl.InsertLast(NewText(strings.TrimRight(line, "\r\n")))
continue
}
if strings.HasPrefix(line, "=>") {
line = line[2:]
line = strings.TrimSpace(line)
i := strings.IndexFunc(line, unicode.IsSpace)
var u string
var text string
if i == -1 {
u = line
} else {
u = line[:i]
text = strings.TrimSpace(line[i:])
}
asl.InsertLast(NewLink(text, u))
continue
}
if strings.HasPrefix(line, "#") {
var level headingLevel
switch true {
case strings.HasPrefix(line, "###"):
line = line[3:]
level = HeadingLevels.SubSubHeading()
case strings.HasPrefix(line, "##"):
line = line[2:]
level = HeadingLevels.SubHeading()
default:
line = line[1:]
level = HeadingLevels.Heading()
}
line = strings.TrimSpace(line)
asl.InsertLast(NewHeading(line, level))
continue
}
if strings.HasPrefix(line, "*") {
asl.InsertLast(NewList(strings.TrimSpace(line[1:])))
continue
}
if strings.HasPrefix(line, ">") {
asl.InsertLast(NewQuote(strings.TrimSpace(line[1:])))
continue
}
if strings.HasPrefix(line, "```") {
isPreformattedMode = !isPreformattedMode
asl.InsertLast(NewPreformatToggle(strings.TrimSpace(line[3:])))
continue
}
asl.InsertLast(NewText(strings.TrimRight(line, "\r\n")))
}
return asl
}
// ParseFile reads the named file, then parses it into an ASL. Returns an error
// if there's an error while reading the file.
func ParseFile(name string) (ASL, error) {
contents, err := os.ReadFile(name)
if err != nil {
return NewASL(), err
}
return Parse(string(contents)), nil
}
// Possible functions to add later:
// ParseBytes
// Could be more performant
// Prevents need for string conversion in ParseFile