1 pub fn parse(source: &str) -> String {
3 let lines = source.split("\n");
4 let mut is_preformatted = false;
6 let mut html:String = "".to_owned();
7 let mut current_line_type: Option<LineType> = None;
10 let mut line_type = LineType::Text;
11 if line.char_indices().count() > 2 {
12 let mut end = line.len();
13 if line.char_indices().count() > 3 {
14 end = line.char_indices().map(|(i, _)| i).nth(3).unwrap();
16 line_type = identify_line(&line[..end], is_preformatted);
19 LineType::PreformattedToggle => is_preformatted = !is_preformatted,
21 // Close previous block if needed
22 if let Some(line) = ¤t_line_type {
23 if line != &line_type && is_block(line) {
24 html.push_str(get_line_closer(line));
29 if is_block(&line_type) {
30 if let Some(line) = ¤t_line_type {
31 if line != &line_type {
32 html.push_str(get_line_opener(&line_type));
35 html.push_str(get_line_opener(&line_type));
38 let line_content = get_partial_line_content(&line_type, line);
39 html.push_str(&line_content);
41 let line_content = get_full_line_content(&line_type, line);
42 html.push_str(&line_content);
44 current_line_type = Some(line_type);
48 if let Some(line) = ¤t_line_type {
50 html.push_str(get_line_closer(line));
56 fn is_block(line_type: &LineType) -> bool {
57 return match line_type {
58 LineType::PreformattedText => true,
59 LineType::ListItem => true,
60 LineType::Quote => true,
65 fn get_partial_line_content(line_type: &LineType, line: &str) -> String {
66 return match line_type {
67 LineType::ListItem => format!("<li>{}</li>", line[2..].trim()),
68 LineType::Quote => line[1..].trim().to_string(),
69 LineType::PreformattedText => format!("{}\n", line),
74 fn get_full_line_content(line_type: &LineType, line: &str) -> String {
76 LineType::Text => format!("<p>{}</p>\n", line.trim()),
77 LineType::Blank => "<br/>\n".to_string(),
79 let url = get_link_address(line);
80 if url.starts_with("gemini:") {
81 format!("<div><a href=\"{}\">{}</a></div>\n", url, get_link_content(line))
83 format!("<div><a href=\"{}\">{}</a></div>\n", url.replace(".gmi", ".html"), get_link_content(line))
86 LineType::Heading1 => format!("<h1>{}</h1>\n", line[1..].trim()),
87 LineType::Heading2 => format!("<h2>{}</h2>\n", line[2..].trim()),
88 LineType::Heading3 => format!("<h3>{}</h3>\n", line[3..].trim()),
93 fn get_line_opener(line_type: &LineType) -> &'static str {
95 LineType::ListItem => "<ul>",
96 LineType::Quote => "<blockquote>",
97 LineType::PreformattedText => "<pre>",
102 fn get_line_closer(line_type: &LineType) -> &'static str {
104 LineType::ListItem => "</ul>\n",
105 LineType::Quote => "</blockquote>\n",
106 LineType::PreformattedText => "</pre>\n",
111 fn get_link_content(line: &str) -> &str {
112 let components: Vec<&str> = line[2..].trim().splitn(2, " ").collect();
113 if components.len() > 1 {
114 return components[1].trim()
119 fn get_link_address(line: &str) -> &str {
120 let components: Vec<&str> = line[2..].trim().splitn(2, " ").collect();
124 fn identify_line(line: &str, is_preformatted: bool) -> LineType {
125 if line.starts_with("```") {
126 return LineType::PreformattedToggle;
129 return LineType::PreformattedText;
132 return LineType::Blank;
134 if line.starts_with("=>") {
135 return LineType::Link;
137 if line.starts_with("* ") {
138 return LineType::ListItem;
140 if line.starts_with(">") {
141 return LineType::Quote;
143 if line.starts_with("###") {
144 return LineType::Heading3;
146 if line.starts_with("##") {
147 return LineType::Heading2;
149 if line.starts_with("#") {
150 return LineType::Heading1;
156 #[derive(PartialEq, Eq)]