summaryrefslogtreecommitdiff
path: root/gemini/gemtext/parse_line.go
blob: faf457db438b15eead8b84f0ed94d7af2e1f7799 (plain)
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
package gemtext

import "bytes"

// ParseLine parses a single line (including the trailing \n) into a gemtext.Line.
func ParseLine(line []byte) Line {
	if len(line) == 0 {
		return nil
	}

	switch line[0] {
	case '=':
		if len(line) == 1 {
			break
		}
		if line[1] == '>' {
			return parseLinkLine(line)
		}
		if line[1] == ':' {
			return parsePromptLine(line)
		}
	case '`':
		if len(line) < 3 || line[1] != '`' || line[2] != '`' {
			break
		}
		return parsePreformatToggleLine(line)
	case '#':
		level := 1
		if len(line) > 1 && line[1] == '#' {
			level += 1
			if len(line) > 2 && line[2] == '#' {
				level += 1
			}
		}
		return parseHeadingLine(level, line)
	case '*':
		if len(line) == 1 || line[1] != ' ' {
			break
		}
		return parseListItemLine(line)
	case '>':
		return parseQuoteLine(line)
	}

	return TextLine{raw: line}
}

func parseLinkLine(raw []byte) LinkLine {
	line := LinkLine{raw: raw}

	// move past =>[<whitespace>]
	raw = bytes.TrimLeft(raw[2:], " \t")

	// find the next space or tab
	spIdx := bytes.IndexByte(raw, ' ')
	tbIdx := bytes.IndexByte(raw, '\t')
	idx := spIdx
	if idx == -1 {
		idx = tbIdx
	}
	if tbIdx >= 0 && tbIdx < idx {
		idx = tbIdx
	}

	if idx < 0 {
		line.url = bytes.TrimRight(raw, "\r\n")
		return line
	}

	line.url = raw[:idx]
	raw = raw[idx+1:]

	label := bytes.TrimRight(bytes.TrimLeft(raw, " \t"), "\r\n")
	if len(label) > 0 {
		line.label = label
	}

	return line
}

func parsePromptLine(raw []byte) PromptLine {
	line := PromptLine{raw: raw}

	// move past =:[<whitespace>]
	raw = bytes.TrimLeft(raw[2:], " \t")

	// find the next space or tab
	spIdx := bytes.IndexByte(raw, ' ')
	tbIdx := bytes.IndexByte(raw, '\t')
	idx := spIdx
	if idx == -1 {
		idx = tbIdx
	}
	if tbIdx >= 0 && tbIdx < idx {
		idx = tbIdx
	}

	if idx < 0 {
		line.url = bytes.TrimRight(raw, "\r\n")
		return line
	}

	line.url = raw[:idx]
	raw = raw[idx+1:]

	label := bytes.TrimRight(bytes.TrimLeft(raw, " \t"), "\r\n")
	if len(label) > 0 {
		line.label = label
	}

	return line
}

func parsePreformatToggleLine(raw []byte) PreformatToggleLine {
	line := PreformatToggleLine{raw: raw}

	raw = bytes.TrimRight(raw[3:], "\r\n")
	if len(raw) > 0 {
		line.altText = raw
	}

	return line
}

func parseHeadingLine(level int, raw []byte) HeadingLine {
	return HeadingLine{
		raw:      raw,
		lineType: LineTypeHeading1 - 1 + LineType(level),
		body:     bytes.TrimRight(bytes.TrimLeft(raw[level:], " \t"), "\r\n"),
	}
}

func parseListItemLine(raw []byte) ListItemLine {
	return ListItemLine{
		raw:  raw,
		body: bytes.TrimRight(raw[2:], "\r\n"),
	}
}

func parseQuoteLine(raw []byte) QuoteLine {
	return QuoteLine{
		raw:  raw,
		body: bytes.TrimRight(raw[1:], "\r\n"),
	}
}