1 // TAKEN FROM PAGE. Need to move to a common source.
2 pub fn parse(source: &str) -> String {
4 let lines = source.split("\n");
5 let mut is_preformatted = false;
7 let mut block_label: Option<String> = None;
8 let mut html: String = "".to_owned();
9 let mut current_line_type: Option<LineType> = None;
11 let mut heading_stack: Vec<u8> = Vec::new();
13 let mut line_type = LineType::Blank;
14 if line.char_indices().count() > 2 {
15 let mut end = line.len();
16 if line.char_indices().count() > 3 {
17 end = line.char_indices().map(|(i, _)| i).nth(3).unwrap();
19 line_type = identify_line(&line[..end], is_preformatted);
22 LineType::PreformattedToggle => {
23 is_preformatted = !is_preformatted;
24 if is_preformatted && line.char_indices().count() > 3 {
25 block_label = Some(get_partial_line_content(&line_type, line));
31 // Close previous block if needed
32 if let Some(line) = ¤t_line_type {
33 if line != &line_type && is_block(line) {
34 html.push_str(get_line_closer(line));
39 if is_block(&line_type) {
40 if let Some(line) = ¤t_line_type {
41 if line != &line_type {
42 html.push_str(&get_line_opener(&line_type, block_label.as_ref()));
45 html.push_str(&get_line_opener(&line_type, None));
48 let line_content = get_partial_line_content(&line_type, line);
49 html.push_str(&line_content);
51 html.push_str(&get_heading_wrapper(&mut heading_stack, &line_type));
52 html.push_str(&get_full_line_content(&line_type, line));
54 current_line_type = Some(line_type);
58 if let Some(line) = ¤t_line_type {
60 html.push_str(get_line_closer(line));
63 html.push_str(&close_heading_wrapper(&mut heading_stack));
67 fn is_block(line_type: &LineType) -> bool {
68 return match line_type {
69 LineType::PreformattedText | LineType::ListItem | LineType::Quote => true,
74 fn get_partial_line_content(line_type: &LineType, line: &str) -> String {
75 let encoded_line = line.replace("<", "<").replace(">", ">");
76 return match line_type {
77 LineType::ListItem => format!("<li>{}</li>", encoded_line[2..].trim()),
78 LineType::Quote => encoded_line[1..].trim().to_string(),
79 LineType::PreformattedText => format!("{}\n", encoded_line),
80 LineType::PreformattedToggle => encoded_line[3..].trim().to_string(),
85 fn get_full_line_content(line_type: &LineType, line: &str) -> String {
86 let encoded_line = line.replace("<", "<").replace(">", ">");
88 LineType::Text => format!("<p>{}</p>\n", encoded_line.trim()),
89 LineType::Blank => "<br>\n".to_string(),
91 let url = get_link_address(line);
92 if url.starts_with("gemini:") {
93 format!("<div><a href=\"{}\">{}</a></div>\n", url, get_link_content(line))
95 format!("<div><a href=\"{}\">{}</a></div>\n", url.replace(".gmi", ".html"), get_link_content(line))
98 LineType::Heading1 => format!("<h1>{}</h1>\n", encoded_line[1..].trim()),
99 LineType::Heading2 => format!("<h2>{}</h2>\n", encoded_line[2..].trim()),
100 LineType::Heading3 => format!("<h3>{}</h3>\n", encoded_line[3..].trim()),
105 fn get_heading_wrapper(heading_stack: &mut Vec<u8>, line_type: &LineType) -> String {
106 let mut string = String::new();
107 let current_heading: u8 = match line_type {
108 LineType::Heading1 => 1,
109 LineType::Heading2 => 2,
110 LineType::Heading3 => 3,
114 if current_heading < 255 {
115 while let Some(open_heading) = heading_stack.pop() {
116 // You just encountered a more important heading.
117 // Put it back. Desist.
118 if open_heading < current_heading {
119 heading_stack.push(open_heading);
123 string.push_str("</div>");
125 if open_heading == current_heading {
129 heading_stack.push(current_heading);
130 string.push_str(&format!("<div class=\"h{}\">", current_heading));
136 fn close_heading_wrapper(heading_stack: &mut Vec<u8>) -> String {
137 let mut string = String::new();
138 while let Some(_open_heading) = heading_stack.pop() {
139 string.push_str("</div>");
144 fn get_line_opener(line_type: &LineType, block_label: Option<&String>) -> String {
146 LineType::ListItem => "<ul>".to_string(),
147 LineType::Quote => "<blockquote>".to_string(),
148 LineType::PreformattedText => {
149 if let Some(label) = &block_label {
150 return format!("<pre role=\"img\" aria-label=\"{}\">", label);
152 return "<pre>".to_string();
159 fn get_line_closer(line_type: &LineType) -> &'static str {
161 LineType::ListItem => "</ul>\n",
162 LineType::Quote => "</blockquote>\n",
163 LineType::PreformattedText => "</pre>\n",
168 fn get_link_content(line: &str) -> &str {
169 let components: Vec<&str> = line[2..].trim().splitn(2, " ").collect();
170 if components.len() > 1 {
171 return components[1].trim()
176 fn get_link_address(line: &str) -> &str {
177 let components: Vec<&str> = line[2..].trim().splitn(2, " ").collect();
181 fn identify_line(line: &str, is_preformatted: bool) -> LineType {
182 if line.starts_with("```") {
183 return LineType::PreformattedToggle;
186 return LineType::PreformattedText;
189 return LineType::Blank;
191 if line.starts_with("=>") {
192 return LineType::Link;
194 if line.starts_with("* ") {
195 return LineType::ListItem;
197 if line.starts_with(">") {
198 return LineType::Quote;
200 if line.starts_with("###") {
201 return LineType::Heading3;
203 if line.starts_with("##") {
204 return LineType::Heading2;
206 if line.starts_with("#") {
207 return LineType::Heading1;
213 #[derive(PartialEq, Eq)]