1 #[derive(PartialEq, Eq, Debug)]
4 PreformattedToggle(bool, String),
11 /// Parses gemtext source code into a vector of `GeminiLine` elements.
14 /// * `source` - A string slice that contains the gemtext
17 /// A `Vec<GeminiLine>` containing the rendered HTML.
18 pub fn parse(source: &str) -> Vec<GeminiLine> {
21 .fold((Vec::new(), false), |(mut lines, is_preformatted), line| {
22 let parsed = if is_preformatted {
23 parse_preformatted_line(line)
28 let new_is_preformatted = match parsed {
29 GeminiLine::PreformattedToggle(x, _) => x,
34 (lines, new_is_preformatted)
39 fn parse_preformatted_line(line: &str) -> GeminiLine {
41 s if s.starts_with("```") => GeminiLine::PreformattedToggle(false, String::new()),
42 _ => GeminiLine::Text(line.to_string(), true),
46 fn parse_line(line: &str) -> GeminiLine {
48 s if s.starts_with("###") => GeminiLine::Heading(3, s[3..].to_string()),
49 s if s.starts_with("##") => GeminiLine::Heading(2, s[2..].to_string()),
50 s if s.starts_with('#') => GeminiLine::Heading(1, s[1..].to_string()),
51 s if s.starts_with("=>") => {
52 let content = s[2..].trim();
53 match content.split_once(char::is_whitespace) {
54 Some((url, text)) => {
55 GeminiLine::Link(url.trim().to_string(), text.trim().to_string())
57 None => GeminiLine::Link(content.trim().to_string(), String::new()),
60 s if s.starts_with("* ") => GeminiLine::ListItem(s[2..].to_string()),
61 s if s.starts_with('>') => GeminiLine::Quote(s[1..].to_string()),
62 s if s.starts_with("```") => GeminiLine::PreformattedToggle(true, s[3..].to_string()),
63 _ => GeminiLine::Text(line.to_string(), false),
74 parse_line("### Heading"),
75 GeminiLine::Heading(3, " Heading".to_string())
78 parse_line("## Heading"),
79 GeminiLine::Heading(2, " Heading".to_string())
82 parse_line("# Heading"),
83 GeminiLine::Heading(1, " Heading".to_string())
85 assert_eq!(parse_line("###"), GeminiLine::Heading(3, "".to_string()));
88 GeminiLine::Heading(3, "##".to_string())
90 assert_eq!(parse_line("# "), GeminiLine::Heading(1, " ".to_string()));
93 parse_preformatted_line("### Heading"),
94 GeminiLine::Text("### Heading".to_string(), true)
97 parse_preformatted_line("## Heading"),
98 GeminiLine::Text("## Heading".to_string(), true)
101 parse_preformatted_line("# Heading"),
102 GeminiLine::Text("# Heading".to_string(), true)
109 parse_line("=> https://example.com Link text"),
110 GeminiLine::Link("https://example.com".to_string(), "Link text".to_string())
113 parse_line("=> /local/path"),
114 GeminiLine::Link("/local/path".to_string(), "".to_string())
119 GeminiLine::Link("".to_string(), "".to_string())
123 GeminiLine::Link("".to_string(), "".to_string())
126 parse_line("=> multiple spaces in text"),
127 GeminiLine::Link("multiple".to_string(), "spaces in text".to_string())
131 parse_preformatted_line("=> https://example.com Link text"),
132 GeminiLine::Text("=> https://example.com Link text".to_string(), true)
137 fn test_list_items() {
139 parse_line("* List item"),
140 GeminiLine::ListItem("List item".to_string())
143 assert_eq!(parse_line("* "), GeminiLine::ListItem("".to_string()));
144 assert_eq!(parse_line("*"), GeminiLine::Text("*".to_string(), false));
146 parse_line("*WithText"),
147 GeminiLine::Text("*WithText".to_string(), false)
150 parse_line("* Multiple spaces"),
151 GeminiLine::ListItem(" Multiple spaces".to_string())
158 parse_line(">Quote text"),
159 GeminiLine::Quote("Quote text".to_string())
162 assert_eq!(parse_line(">"), GeminiLine::Quote("".to_string()));
163 assert_eq!(parse_line("> "), GeminiLine::Quote(" ".to_string()));
165 parse_line(">>Nested"),
166 GeminiLine::Quote(">Nested".to_string())
171 fn test_preformatted() {
173 parse_line("```alt-text"),
174 GeminiLine::PreformattedToggle(true, "alt-text".to_string())
179 GeminiLine::PreformattedToggle(true, "".to_string())
183 GeminiLine::PreformattedToggle(true, " ".to_string())
187 GeminiLine::PreformattedToggle(true, "`".to_string())
191 parse_preformatted_line("```alt-text"),
192 GeminiLine::PreformattedToggle(false, "".to_string())
195 parse_preformatted_line("```"),
196 GeminiLine::PreformattedToggle(false, "".to_string())
204 parse_line("Regular text"),
205 GeminiLine::Text("Regular text".to_string(), false)
209 assert_eq!(parse_line(""), GeminiLine::Text("".to_string(), false));
210 assert_eq!(parse_line(" "), GeminiLine::Text(" ".to_string(), false));
211 assert_eq!(parse_line(" "), GeminiLine::Text(" ".to_string(), false));
215 fn test_malformed_input() {
217 parse_line("= >Not a link"),
218 GeminiLine::Text("= >Not a link".to_string(), false)
221 parse_line("``Not preformatted"),
222 GeminiLine::Text("``Not preformatted".to_string(), false)
225 parse_line("** Not a list"),
226 GeminiLine::Text("** Not a list".to_string(), false)
231 fn test_full_document() {
237 => https://example.com Link text
245 => https://example.com Link text
249 let result = parse(input);
253 GeminiLine::Heading(1, " Heading 1".to_string()),
254 GeminiLine::Heading(2, " Heading 2".to_string()),
255 GeminiLine::Heading(3, " Heading 3".to_string()),
256 GeminiLine::Text("Regular text".to_string(), false),
257 GeminiLine::Link("https://example.com".to_string(), "Link text".to_string()),
258 GeminiLine::ListItem("List item".to_string()),
259 GeminiLine::Quote("Quote".to_string()),
260 GeminiLine::PreformattedToggle(true, "alt".to_string()),
261 GeminiLine::Text("code".to_string(), true),
262 GeminiLine::Text("# Heading 1".to_string(), true),
263 GeminiLine::Text("## Heading 2".to_string(), true),
264 GeminiLine::Text("### Heading 3".to_string(), true),
265 GeminiLine::Text("=> https://example.com Link text".to_string(), true),
266 GeminiLine::Text("* List item".to_string(), true),
267 GeminiLine::Text(">Quote".to_string(), true),
268 GeminiLine::PreformattedToggle(false, "".to_string()),