馃 The Definitive Gemini Protocol Toolkit
gemini
gemini-protocol
gemtext
parser
zero-dependency
toolkit
ast
converter
html
markdown
cli
networking
1/// A Gemtext AST node.
2///
3/// Each Gemtext line is a `Node`, and some lines can even be grouped together,
4/// such as the `Node::List` `Node`!
5///
6/// # Gemtext Resources
7///
8/// - [Gemtext Documentation](https://gemini.circumlunar.space/docs/gemtext.gmi)
9/// - [Gemtext Cheatsheet](https://gemini.circumlunar.space/docs/cheatsheet.gmi).
10/// - [Gemini Specification](https://gemini.circumlunar.space/docs/specification.gmi).
11#[derive(Debug, PartialEq, Clone, Eq)]
12pub enum Node {
13 /// A text line
14 ///
15 /// # Example
16 ///
17 /// ```gemini
18 /// This is a text line
19 /// ```
20 Text(String),
21 /// A link line
22 ///
23 /// # Examples
24 ///
25 /// ```gemini
26 /// => /this-is-the-to This is the text
27 ///
28 /// => gemini://to.somewhere.link
29 /// ```
30 Link {
31 /// The location that a link line is pointing to
32 ///
33 /// # Examples
34 ///
35 /// ```gemini
36 /// => /this-is-the-to This is the text
37 ///
38 /// => gemini://to.somewhere.link
39 /// ```
40 to: String,
41 /// The text a link line *may* have
42 ///
43 /// # Examples
44 ///
45 /// ```gemini
46 /// => /this-is-the-to This line has text, unlike the next one.
47 ///
48 /// => gemini://to.somewhere.link
49 /// ```
50 text: Option<String>,
51 },
52 /// A heading line
53 ///
54 /// # Examples
55 ///
56 /// ```gemini
57 /// # This is a heading
58 ///
59 /// ## This is a sub-heading
60 ///
61 /// ### This is a sub-sub-heading
62 /// ```
63 Heading {
64 /// The level of a heading
65 ///
66 /// # Examples
67 ///
68 /// ```gemini
69 /// # This is a level 1 heading
70 ///
71 /// ## This is a level 2 sub-heading
72 ///
73 /// ### This is a level 3 sub-sub-heading
74 /// ```
75 level: usize,
76 /// The text of a heading
77 ///
78 /// # Examples
79 ///
80 /// ```gemini
81 /// # This is the headings text
82 ///
83 /// # This is also the headings text
84 /// ```
85 text: String,
86 },
87 /// A collection of sequential list item lines
88 ///
89 /// # Examples
90 ///
91 /// ```gemini
92 /// * These are
93 /// * sequential list
94 /// * items.
95 /// ```
96 List(Vec<String>),
97 /// A blockquote line
98 ///
99 /// # Examples
100 ///
101 /// ```gemini
102 /// > This is a blockquote line
103 ///
104 /// > This is also a blockquote line
105 /// ```
106 Blockquote(String),
107 /// A preformatted block
108 ///
109 /// # Examples
110 ///
111 /// Try to ignore the leading backslash in-front of the triple backticks,
112 /// they are there to not confuse the Markdown engine.
113 ///
114 /// ```gemini
115 /// \```This is the alt-text
116 /// This is the preformatted block
117 ///
118 /// This is the rest of the preformatted block
119 /// \```
120 /// ```
121 PreformattedText {
122 /// A preformatted blocks alt-text
123 ///
124 /// # Examples
125 ///
126 /// Try to ignore the leading backslash in-front of the triple backticks,
127 /// they are there to not confuse the Markdown engine.
128 ///
129 /// ```gemini
130 /// \```This is the alt-text
131 /// This is the preformatted block
132 ///
133 /// This is the rest of the preformatted block
134 /// \```
135 /// ```
136 alt_text: Option<String>,
137 /// A preformatted blocks content
138 ///
139 /// # Examples
140 ///
141 /// Try to ignore the leading backslash in-front of the triple backticks,
142 /// they are there to not confuse the Markdown engine.
143 ///
144 /// ```gemini
145 /// \```This is the alt-text
146 /// This is the preformatted blocks content
147 ///
148 /// This is the rest of the preformatted blocks content
149 /// \```
150 /// ```
151 text: String,
152 },
153 /// A whitespace line, a line which contains nothing but whitespace.
154 Whitespace,
155}
156
157impl Node {
158 /// Convert a single [`Node`] of any node type to a Gemtext [`String`]
159 #[must_use]
160 pub fn to_gemtext(&self) -> String {
161 super::Ast::from_nodes(vec![self.to_owned()]).to_gemtext()
162 }
163}