aboutsummaryrefslogtreecommitdiff
path: root/src/ast/container.rs
blob: d3f6f5a1a9bbfd5a2d8864081cd076bcd18ce9ec (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// This file is part of Germ <https://github.com/gemrest/germ>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

use super::Node;

/// An AST structure which contains an AST tree
///
/// # Example
///
/// ```rust
/// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#);
/// ```
#[derive(Clone)]
pub struct Ast {
  inner: Vec<Node>,
}

impl Ast {
  /// Build an AST tree from Gemtext
  ///
  /// # Example
  ///
  /// ```rust
  /// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#);
  /// ```
  #[must_use]
  pub fn from_owned(value: &(impl AsRef<str> + ?Sized)) -> Self {
    Self::from_value(value.as_ref())
  }

  /// Build an AST tree from Gemtext
  ///
  /// # Example
  ///
  /// ```rust
  /// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#);
  /// ```
  #[must_use]
  #[allow(clippy::needless_pass_by_value)]
  pub fn from_string(value: (impl Into<String> + ?Sized)) -> Self {
    Self::from_value(&value.into())
  }

  /// Build an AST tree from a value
  ///
  /// # Example
  ///
  /// ```rust
  /// let _ = germ::ast::Ast::from_value(r#"=> gemini://gem.rest/ GemRest"#);
  /// ```
  #[must_use]
  pub fn from_value(value: &(impl ToString + ?Sized)) -> Self {
    let mut ast = vec![];
    let mut in_preformatted = false;
    let mut in_list = false;
    let source = value.to_string();
    let mut lines = source.lines();

    // Iterate over all lines in the Gemtext `source`
    while let Some(line) = lines.next() {
      // Evaluate the Gemtext line and append its AST node to the `ast` tree
      ast.append(&mut Self::evaluate(
        line,
        &mut lines,
        &mut in_preformatted,
        &mut in_list,
      ));
    }

    Self { inner: ast }
  }

  #[must_use]
  pub fn to_gemtext(&self) -> String {
    let mut gemtext = String::new();

    for node in &self.inner {
      match node {
        Node::Text(text) => gemtext.push_str(&format!("{text}\n")),
        Node::Link { to, text } => gemtext.push_str(&format!(
          "=> {}{}\n",
          to,
          text.clone().map_or_else(String::new, |text| format!(" {text}")),
        )),
        Node::Heading { level, text } => gemtext.push_str(&format!(
          "{} {}\n",
          match level {
            1 => "#",
            2 => "##",
            3 => "###",
            _ => "",
          },
          text
        )),
        Node::List(items) => gemtext.push_str(&format!(
          "{}\n",
          items
            .iter()
            .map(|i| format!("* {i}"))
            .collect::<Vec<String>>()
            .join("\n"),
        )),
        Node::Blockquote(text) => gemtext.push_str(&format!("> {text}\n")),
        Node::PreformattedText { alt_text, text } =>
          gemtext.push_str(&format!(
            "```{}\n{}```\n",
            alt_text.clone().unwrap_or_default(),
            text
          )),
        Node::Whitespace => gemtext.push('\n'),
      }
    }

    gemtext
  }

  /// The actual AST of `Ast`
  ///
  /// # Example
  ///
  /// ```rust
  /// let _ =
  ///   germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#).inner();
  /// ```
  #[must_use]
  pub const fn inner(&self) -> &Vec<Node> { &self.inner }

  #[allow(clippy::too_many_lines)]
  fn evaluate(
    line: &str,
    lines: &mut std::str::Lines<'_>,
    in_preformatted: &mut bool,
    in_list: &mut bool,
  ) -> Vec<Node> {
    let mut preformatted = String::new();
    let mut alt_text = String::new();
    let mut nodes = vec![];
    let mut line = line;
    let mut list_items = vec![];

    // Enter a not-so-infinite loop as sometimes, we may need to stay in an
    // evaluation loop, e.g., multiline contexts: preformatted text, lists, etc.
    loop {
      // Match the first character of the Gemtext line to understand the line
      // type
      match line.get(0..1).unwrap_or("") {
        "=" => {
          // If the Gemtext line starts with an "=" ("=>"), it is a link line,
          // so splitting it up should be easy enough.
          let line = line.get(2..).unwrap();
          let mut split = line
            .split_whitespace()
            .map(String::from)
            .collect::<Vec<String>>()
            .into_iter();

          nodes.push(Node::Link {
            to:   split.next().expect("no location in link"),
            text: {
              let rest = split.collect::<Vec<String>>().join(" ");

              if rest.is_empty() { None } else { Some(rest) }
            },
          });

          break;
        }
        "#" => {
          // If the Gemtext line starts with an "#", it is a heading, so let's
          // find out how deep it goes.
          let level = line.get(0..3).map_or(0, |root| {
            if root.contains("###") {
              3
            } else if root.contains("##") {
              2
            } else {
              // Converting the boolean response of `contains` to an integer
              usize::from(root.contains('#'))
            }
          });

          nodes.push(Node::Heading {
            level,
            // Here, we are `get`ing the `&str` starting at the `level`-th
            // index, then trimming the start. These operations
            // effectively off the line identifier.
            text: line.get(level..).unwrap_or("").trim_start().to_string(),
          });

          break;
        }
        "*" => {
          // If the Gemtext line starts with an asterisk, it is a list item, so
          // let's enter a list context.
          if !*in_list {
            *in_list = true;
          }

          list_items.push(line.get(1..).unwrap_or("").trim_start().to_string());

          if let Some(next_line) = lines.next() {
            line = next_line;
          } else {
            break;
          }
        }
        ">" => {
          // If the Gemtext line starts with an ">", it is a blockquote, so
          // let's just clip off the line identifier.
          nodes.push(Node::Blockquote(
            line.get(1..).unwrap_or("").trim_start().to_string(),
          ));

          break;
        }
        "`" => {
          // If the Gemtext line starts with a backtick, it is a list item, so
          // let's enter a preformatted text context.
          *in_preformatted = !*in_preformatted;

          if *in_preformatted {
            alt_text = line.get(3..).unwrap_or("").to_string();

            if let Some(next_line) = lines.next() {
              line = next_line;
            } else {
              break;
            }
          } else {
            nodes.push(Node::PreformattedText {
              alt_text: if alt_text.is_empty() { None } else { Some(alt_text) },
              text:     preformatted,
            });

            break;
          }
        }
        "" if !*in_preformatted => {
          if line.is_empty() {
            // If the line has nothing on it, it is a whitespace line, as long
            // as we aren't in a preformatted line context.
            nodes.push(Node::Whitespace);
          } else {
            nodes.push(Node::Text(line.to_string()));
          }

          break;
        }
        // This as a catchall, it does a number of things.
        _ => {
          if *in_preformatted {
            // If we are in a preformatted line context, add the line to the
            // preformatted blocks content and increment the line.
            preformatted.push_str(&format!("{line}\n"));

            if let Some(next_line) = lines.next() {
              line = next_line;
            } else {
              break;
            }
          } else {
            // If we are in a list item and hit a catchall, that must mean that
            // we encountered a line which is not a list line, so
            // let's stop adding items to the list context.
            if *in_list {
              *in_list = false;

              nodes.push(Node::Text(line.to_string()));

              break;
            }

            nodes.push(Node::Text(line.to_string()));

            break;
          }
        }
      }
    }

    if !list_items.is_empty() {
      nodes.reverse();
      nodes.push(Node::List(list_items));
      nodes.reverse();
    }

    nodes
  }
}