1 #[derive(PartialEq, Eq, Debug)]
3 /// Represents one of the six line types in gemtext.
7 /// use gema_texto::gemini_parser::GeminiLine;
8 /// use gema_texto::gemini_parser::parse;
10 /// let gemini_source = "\
12 /// This is some gemini text!
13 /// => https://test Go to the test page.
15 /// This text actually has more linkes
16 /// => https://test2 Go to the second test page.
17 /// => https://test3 Go to the third test page.
20 /// let gemini_lines = parse(&gemini_source);
21 /// // Count the number of links
22 /// let link_count = gemini_lines.iter()
23 /// .filter(|line| matches!(line, GeminiLine::Link(_, _)))
25 /// assert_eq!(link_count, 3);
28 /// Text line, with a bool that shows whether it's preformatted.
31 /// Preformatted toggle line with alt text.
33 /// The boolean indicates whether it turned preformatted mode on or off.
34 /// The string is the optional alt-text.
35 PreformattedToggle(bool, String),
37 /// Heading with a number between 1 and 3 inclusive indicating level.
40 /// Link with URL and text.
50 /// Parses gemtext source code into a vector of `GeminiLine` elements.
53 /// * `source` - A string slice that contains the gemtext
56 /// A `Vec<GeminiLine>` containing the rendered HTML.
61 /// use std::fs::read_to_string;
62 /// use gema_texto::gemini_parser::parse;
63 /// let gemini_source = "\
65 /// This is some gemini text!
66 /// => https://test Go to the test page.
69 /// let gemini_lines = parse(&gemini_source);
71 /// // Prints the different lines in the array.
72 /// for line in gemini_lines {
73 /// println!("{:#?}", line);
77 pub fn parse(source: &str) -> Vec<GeminiLine> {
80 .fold((Vec::new(), false), |(mut lines, is_preformatted), line| {
81 let parsed = if is_preformatted {
82 parse_preformatted_line(line)
87 let new_is_preformatted = match parsed {
88 GeminiLine::PreformattedToggle(x, _) => x,
93 (lines, new_is_preformatted)
98 fn parse_preformatted_line(line: &str) -> GeminiLine {
100 s if s.starts_with("```") => GeminiLine::PreformattedToggle(false, String::new()),
101 _ => GeminiLine::Text(line.to_string(), true),
105 fn parse_line(line: &str) -> GeminiLine {
107 s if s.starts_with("###") => GeminiLine::Heading(3, s[3..].to_string()),
108 s if s.starts_with("##") => GeminiLine::Heading(2, s[2..].to_string()),
109 s if s.starts_with('#') => GeminiLine::Heading(1, s[1..].to_string()),
110 s if s.starts_with("=>") => {
111 let content = s[2..].trim();
112 match content.split_once(char::is_whitespace) {
113 Some((url, text)) => {
114 GeminiLine::Link(url.trim().to_string(), text.trim().to_string())
116 None => GeminiLine::Link(content.trim().to_string(), String::new()),
119 s if s.starts_with("* ") => GeminiLine::ListItem(s[2..].to_string()),
120 s if s.starts_with('>') => GeminiLine::Quote(s[1..].to_string()),
121 s if s.starts_with("```") => GeminiLine::PreformattedToggle(true, s[3..].to_string()),
122 _ => GeminiLine::Text(line.to_string(), false),
133 parse_line("### Heading"),
134 GeminiLine::Heading(3, " Heading".to_string())
137 parse_line("## Heading"),
138 GeminiLine::Heading(2, " Heading".to_string())
141 parse_line("# Heading"),
142 GeminiLine::Heading(1, " Heading".to_string())
144 assert_eq!(parse_line("###"), GeminiLine::Heading(3, String::new()));
147 GeminiLine::Heading(3, "##".to_string())
149 assert_eq!(parse_line("# "), GeminiLine::Heading(1, " ".to_string()));
152 parse_preformatted_line("### Heading"),
153 GeminiLine::Text("### Heading".to_string(), true)
156 parse_preformatted_line("## Heading"),
157 GeminiLine::Text("## Heading".to_string(), true)
160 parse_preformatted_line("# Heading"),
161 GeminiLine::Text("# Heading".to_string(), true)
168 parse_line("=> https://example.com Link text"),
169 GeminiLine::Link("https://example.com".to_string(), "Link text".to_string())
172 parse_line("=> /local/path"),
173 GeminiLine::Link("/local/path".to_string(), String::new())
178 GeminiLine::Link(String::new(), String::new())
182 GeminiLine::Link(String::new(), String::new())
185 parse_line("=> multiple spaces in text"),
186 GeminiLine::Link("multiple".to_string(), "spaces in text".to_string())
190 parse_preformatted_line("=> https://example.com Link text"),
191 GeminiLine::Text("=> https://example.com Link text".to_string(), true)
196 fn test_list_items() {
198 parse_line("* List item"),
199 GeminiLine::ListItem("List item".to_string())
202 assert_eq!(parse_line("* "), GeminiLine::ListItem(String::new()));
203 assert_eq!(parse_line("*"), GeminiLine::Text("*".to_string(), false));
205 parse_line("*WithText"),
206 GeminiLine::Text("*WithText".to_string(), false)
209 parse_line("* Multiple spaces"),
210 GeminiLine::ListItem(" Multiple spaces".to_string())
217 parse_line(">Quote text"),
218 GeminiLine::Quote("Quote text".to_string())
221 assert_eq!(parse_line(">"), GeminiLine::Quote(String::new()));
222 assert_eq!(parse_line("> "), GeminiLine::Quote(" ".to_string()));
224 parse_line(">>Nested"),
225 GeminiLine::Quote(">Nested".to_string())
230 fn test_preformatted() {
232 parse_line("```alt-text"),
233 GeminiLine::PreformattedToggle(true, "alt-text".to_string())
238 GeminiLine::PreformattedToggle(true, String::new())
242 GeminiLine::PreformattedToggle(true, " ".to_string())
246 GeminiLine::PreformattedToggle(true, "`".to_string())
250 parse_preformatted_line("```alt-text"),
251 GeminiLine::PreformattedToggle(false, String::new())
254 parse_preformatted_line("```"),
255 GeminiLine::PreformattedToggle(false, String::new())
263 parse_line("Regular text"),
264 GeminiLine::Text("Regular text".to_string(), false)
268 assert_eq!(parse_line(""), GeminiLine::Text(String::new(), false));
269 assert_eq!(parse_line(" "), GeminiLine::Text(" ".to_string(), false));
270 assert_eq!(parse_line(" "), GeminiLine::Text(" ".to_string(), false));
274 fn test_malformed_input() {
276 parse_line("= >Not a link"),
277 GeminiLine::Text("= >Not a link".to_string(), false)
280 parse_line("``Not preformatted"),
281 GeminiLine::Text("``Not preformatted".to_string(), false)
284 parse_line("** Not a list"),
285 GeminiLine::Text("** Not a list".to_string(), false)
290 fn test_full_document() {
296 => https://example.com Link text
304 => https://example.com Link text
308 let result = parse(input);
312 GeminiLine::Heading(1, " Heading 1".to_string()),
313 GeminiLine::Heading(2, " Heading 2".to_string()),
314 GeminiLine::Heading(3, " Heading 3".to_string()),
315 GeminiLine::Text("Regular text".to_string(), false),
316 GeminiLine::Link("https://example.com".to_string(), "Link text".to_string()),
317 GeminiLine::ListItem("List item".to_string()),
318 GeminiLine::Quote("Quote".to_string()),
319 GeminiLine::PreformattedToggle(true, "alt".to_string()),
320 GeminiLine::Text("code".to_string(), true),
321 GeminiLine::Text("# Heading 1".to_string(), true),
322 GeminiLine::Text("## Heading 2".to_string(), true),
323 GeminiLine::Text("### Heading 3".to_string(), true),
324 GeminiLine::Text("=> https://example.com Link text".to_string(), true),
325 GeminiLine::Text("* List item".to_string(), true),
326 GeminiLine::Text(">Quote".to_string(), true),
327 GeminiLine::PreformattedToggle(false, String::new()),