]> git.r.bdr.sh - rbdr/page/blobdiff - src/gemini_parser.rs
Relicense
[rbdr/page] / src / gemini_parser.rs
index d0715f102246aef12dbeade159c285b9d856ccd8..a7c752e7bf1d6a7e3b1c8c32c269c5acbaebb5e9 100644 (file)
@@ -1,13 +1,15 @@
-pub fn parse(source: &str) -> String {
+    pub fn parse(source: &str) -> String {
 
     let lines = source.split("\n");
     let mut is_preformatted = false;
 
 
     let lines = source.split("\n");
     let mut is_preformatted = false;
 
-    let mut html:String = "".to_owned();
+    let mut block_label: Option<String> = None;
+    let mut html: String = "".to_owned();
     let mut current_line_type: Option<LineType> = None;
 
     let mut current_line_type: Option<LineType> = None;
 
+    let mut heading_stack: Vec<u8> = Vec::new();
     for line in lines {
     for line in lines {
-        let mut line_type = LineType::Text;
+        let mut line_type = LineType::Blank;
         if line.char_indices().count() > 2 {
             let mut end = line.len();
             if line.char_indices().count() > 3 {
         if line.char_indices().count() > 2 {
             let mut end = line.len();
             if line.char_indices().count() > 3 {
@@ -16,7 +18,14 @@ pub fn parse(source: &str) -> String {
             line_type = identify_line(&line[..end], is_preformatted);
         }
         match line_type {
             line_type = identify_line(&line[..end], is_preformatted);
         }
         match line_type {
-            LineType::PreformattedToggle => is_preformatted = !is_preformatted,
+            LineType::PreformattedToggle => {
+                is_preformatted = !is_preformatted;
+                if is_preformatted && line.char_indices().count() > 3 {
+                    block_label = Some(get_partial_line_content(&line_type, line));
+                } else {
+                    block_label = None;
+                }
+            },
             _ => {
                 // Close previous block if needed
                 if let Some(line) = &current_line_type {
             _ => {
                 // Close previous block if needed
                 if let Some(line) = &current_line_type {
@@ -29,17 +38,17 @@ pub fn parse(source: &str) -> String {
                 if is_block(&line_type) {
                     if let Some(line) = &current_line_type {
                         if line != &line_type  {
                 if is_block(&line_type) {
                     if let Some(line) = &current_line_type {
                         if line != &line_type  {
-                            html.push_str(get_line_opener(&line_type));
+                            html.push_str(&get_line_opener(&line_type, block_label.as_ref()));
                         }
                     } else {
                         }
                     } else {
-                        html.push_str(get_line_opener(&line_type));
+                        html.push_str(&get_line_opener(&line_type, None));
                     }
 
                     let line_content = get_partial_line_content(&line_type, line);
                     html.push_str(&line_content);
                 } else {
                     }
 
                     let line_content = get_partial_line_content(&line_type, line);
                     html.push_str(&line_content);
                 } else {
-                    let line_content = get_full_line_content(&line_type, line);
-                    html.push_str(&line_content);
+                    html.push_str(&get_heading_wrapper(&mut heading_stack, &line_type));
+                    html.push_str(&get_full_line_content(&line_type, line));
                 }
                 current_line_type = Some(line_type);
             },
                 }
                 current_line_type = Some(line_type);
             },
@@ -50,31 +59,33 @@ pub fn parse(source: &str) -> String {
             html.push_str(get_line_closer(line));
         }
     }
             html.push_str(get_line_closer(line));
         }
     }
+    html.push_str(&close_heading_wrapper(&mut heading_stack));
     html
 }
 
 fn is_block(line_type: &LineType) -> bool {
     return match line_type {
     html
 }
 
 fn is_block(line_type: &LineType) -> bool {
     return match line_type {
-        LineType::PreformattedText => true,
-        LineType::ListItem => true,
-        LineType::Quote => true,
+        LineType::PreformattedText | LineType::ListItem | LineType::Quote => true,
         _ => false,
     }
 }
 
 fn get_partial_line_content(line_type: &LineType, line: &str) -> String {
         _ => false,
     }
 }
 
 fn get_partial_line_content(line_type: &LineType, line: &str) -> String {
+    let encoded_line = line.replace("<", "&lt;").replace(">", "&gt;");
     return match line_type {
     return match line_type {
-        LineType::ListItem => format!("<li>{}</li>", line[2..].trim()),
-        LineType::Quote => line[1..].trim().to_string(),
-        LineType::PreformattedText => format!("{}\n", line),
+        LineType::ListItem => format!("<li>{}</li>", encoded_line[2..].trim()),
+        LineType::Quote => encoded_line[1..].trim().to_string(),
+        LineType::PreformattedText => format!("{}\n", encoded_line),
+        LineType::PreformattedToggle => encoded_line[3..].trim().to_string(),
         _ => "".to_string(),
     }
 }
 
 fn get_full_line_content(line_type: &LineType, line: &str) -> String {
         _ => "".to_string(),
     }
 }
 
 fn get_full_line_content(line_type: &LineType, line: &str) -> String {
+    let encoded_line = line.replace("<", "&lt;").replace(">", "&gt;");
      match line_type {
      match line_type {
-        LineType::Text => format!("<p>{}</p>\n", line.trim()),
-        LineType::Blank => "<br/>\n".to_string(),
+        LineType::Text => format!("<p>{}</p>\n", encoded_line.trim()),
+        LineType::Blank => "<br>\n".to_string(),
         LineType::Link => {
             let url = get_link_address(line);
             if url.starts_with("gemini:") {
         LineType::Link => {
             let url = get_link_address(line);
             if url.starts_with("gemini:") {
@@ -83,19 +94,64 @@ fn get_full_line_content(line_type: &LineType, line: &str) -> String {
                 format!("<div><a href=\"{}\">{}</a></div>\n", url.replace(".gmi", ".html"), get_link_content(line))
             }
         },
                 format!("<div><a href=\"{}\">{}</a></div>\n", url.replace(".gmi", ".html"), get_link_content(line))
             }
         },
-        LineType::Heading1 => format!("<h1>{}</h1>\n", line[1..].trim()),
-        LineType::Heading2 => format!("<h2>{}</h2>\n", line[2..].trim()),
-        LineType::Heading3 => format!("<h3>{}</h3>\n", line[3..].trim()),
+        LineType::Heading1 => format!("<h1>{}</h1>\n", encoded_line[1..].trim()),
+        LineType::Heading2 => format!("<h2>{}</h2>\n", encoded_line[2..].trim()),
+        LineType::Heading3 => format!("<h3>{}</h3>\n", encoded_line[3..].trim()),
         _ => "".to_string(),
     }
 }
 
         _ => "".to_string(),
     }
 }
 
-fn get_line_opener(line_type: &LineType) -> &'static str {
+fn get_heading_wrapper(heading_stack: &mut Vec<u8>, line_type: &LineType) -> String {
+    let mut string = String::new();
+    let current_heading: u8 = match line_type {
+        LineType::Heading1 => 1,
+        LineType::Heading2 => 2,
+        LineType::Heading3 => 3,
+        _ => 255
+    };
+
+    if current_heading < 255 {
+        while let Some(open_heading) = heading_stack.pop() {
+            // You just encountered a more important heading.
+            // Put it back. Desist.
+            if open_heading < current_heading {
+                heading_stack.push(open_heading);
+                break;
+            }
+
+            string.push_str("</div>");
+
+            if open_heading == current_heading {
+                break;
+            }
+        }
+        heading_stack.push(current_heading);
+        string.push_str(&format!("<div class=\"h{}\">", current_heading));
+    }
+
+    return string;
+}
+
+fn close_heading_wrapper(heading_stack: &mut Vec<u8>) -> String {
+    let mut string = String::new();
+    while let Some(_open_heading) = heading_stack.pop() {
+        string.push_str("</div>");
+    }
+    return string;
+}
+
+fn get_line_opener(line_type: &LineType, block_label: Option<&String>) -> String {
     match line_type {
     match line_type {
-        LineType::ListItem => "<ul>",
-        LineType::Quote => "<blockquote>",
-        LineType::PreformattedText => "<pre>",
-        _ => "",
+        LineType::ListItem => "<ul>".to_string(),
+        LineType::Quote => "<blockquote>".to_string(),
+        LineType::PreformattedText => {
+            if let Some(label) = &block_label {
+                return format!("<pre role=\"img\" aria-label=\"{}\">", label);
+            } else {
+                return "<pre>".to_string();
+            }
+        },
+        _ => "".to_string(),
     }
 }
 
     }
 }