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
|
package gemtext_test
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tildegit.org/tjp/gus/gemini/gemtext"
)
func TestParse(t *testing.T) {
docBytes := []byte(`
# top-level header line
## subtitle
This is some non-blank regular text.
* an
* unordered
* list
=> gemini://google.com/ as if
=: spartan://foo.bar/baz this should be a spartan prompt
> this is a quote
> -tjp
`[1:] + "```pre-formatted code\ndoc := gemtext.Parse(req.Body)\n```ignored closing alt-text\n")
assertEmptyLine := func(t *testing.T, line gemtext.Line) {
assert.Equal(t, gemtext.LineTypeText, line.Type())
assert.Equal(t, "\n", string(line.Raw()))
}
doc, err := gemtext.Parse(bytes.NewBuffer(docBytes))
require.Nil(t, err)
require.Equal(t, 20, len(doc))
assert.Equal(t, gemtext.LineTypeHeading1, doc[0].Type())
assert.Equal(t, "# top-level header line\n", string(doc[0].Raw()))
assert.Equal(t, "top-level header line", doc[0].(gemtext.HeadingLine).Body())
assertEmptyLine(t, doc[1])
assert.Equal(t, gemtext.LineTypeHeading2, doc[2].Type())
assert.Equal(t, "## subtitle\n", string(doc[2].Raw()))
assert.Equal(t, "subtitle", doc[2].(gemtext.HeadingLine).Body())
assertEmptyLine(t, doc[3])
assert.Equal(t, gemtext.LineTypeText, doc[4].Type())
assert.Equal(t, "This is some non-blank regular text.\n", string(doc[4].Raw()))
assertEmptyLine(t, doc[5])
assert.Equal(t, gemtext.LineTypeListItem, doc[6].Type())
assert.Equal(t, "an", doc[6].(gemtext.ListItemLine).Body())
assert.Equal(t, gemtext.LineTypeListItem, doc[7].Type())
assert.Equal(t, "unordered", doc[7].(gemtext.ListItemLine).Body())
assert.Equal(t, gemtext.LineTypeListItem, doc[8].Type())
assert.Equal(t, "list", doc[8].(gemtext.ListItemLine).Body())
assertEmptyLine(t, doc[9])
assert.Equal(t, gemtext.LineTypeLink, doc[10].Type())
assert.Equal(t, "=> gemini://google.com/ as if\n", string(doc[10].Raw()))
assert.Equal(t, "gemini://google.com/", doc[10].(gemtext.LinkLine).URL())
assert.Equal(t, "as if", doc[10].(gemtext.LinkLine).Label())
assertEmptyLine(t, doc[11])
assert.Equal(t, gemtext.LineTypePrompt, doc[12].Type())
assert.Equal(t, "=: spartan://foo.bar/baz this should be a spartan prompt\n", string(doc[12].Raw()))
assert.Equal(t, "spartan://foo.bar/baz", doc[12].(gemtext.PromptLine).URL())
assert.Equal(t, "this should be a spartan prompt", doc[12].(gemtext.PromptLine).Label())
assertEmptyLine(t, doc[13])
assert.Equal(t, gemtext.LineTypeQuote, doc[14].Type())
assert.Equal(t, "> this is a quote\n", string(doc[14].Raw()))
assert.Equal(t, " this is a quote", doc[14].(gemtext.QuoteLine).Body())
assert.Equal(t, gemtext.LineTypeQuote, doc[15].Type())
assert.Equal(t, "> -tjp\n", string(doc[15].Raw()))
assert.Equal(t, " -tjp", doc[15].(gemtext.QuoteLine).Body())
assertEmptyLine(t, doc[16])
assert.Equal(t, gemtext.LineTypePreformatToggle, doc[17].Type())
assert.Equal(t, "```pre-formatted code\n", string(doc[17].Raw()))
assert.Equal(t, "pre-formatted code", doc[17].(gemtext.PreformatToggleLine).AltText())
assert.Equal(t, gemtext.LineTypePreformattedText, doc[18].Type())
assert.Equal(t, "doc := gemtext.Parse(req.Body)\n", string(doc[18].Raw()))
assert.Equal(t, gemtext.LineTypePreformatToggle, doc[19].Type())
assert.Equal(t, "```ignored closing alt-text\n", string(doc[19].Raw()))
assert.Equal(t, "", doc[19].(gemtext.PreformatToggleLine).AltText())
// ensure we can rebuild the original doc from all the line.Raw()s
buf := &bytes.Buffer{}
for _, line := range doc {
_, _ = buf.Write(line.Raw())
}
assert.Equal(t, string(docBytes), buf.String())
}
|