]> git.r.bdr.sh - rbdr/page/commitdiff
Treat preformatted text with alt as an image
authorRuben Beltran del Rio <redacted>
Sun, 4 Feb 2024 16:53:02 +0000 (17:53 +0100)
committerRuben Beltran del Rio <redacted>
Sun, 4 Feb 2024 16:53:02 +0000 (17:53 +0100)
Cargo.lock
Cargo.toml
src/gemini_parser.rs

index 2a9b116404e78e904bea127f5aaddc73e028a636..83a98a73aae851ebc0e7bb780418c135cc8de35d 100644 (file)
@@ -4,4 +4,4 @@ version = 3
 
 [[package]]
 name = "page"
 
 [[package]]
 name = "page"
-version = "1.2.0"
+version = "1.3.0"
index 7ddba562adb74e2e0496c18dbbf7dbe35a9c5669..c148552f612d73e2c21ead86a3c74d29ad123146 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "page"
 [package]
 name = "page"
-version = "1.2.0"
+version = "1.3.0"
 edition = "2021"
 
 [dependencies]
 edition = "2021"
 
 [dependencies]
index e63ebd30344dccdfe22e06eaeba3f0cb4fefd2de..14d74c62b251b203a53ac111bcff65aadbda1748 100644 (file)
@@ -1,9 +1,10 @@
-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 heading_stack: Vec<u8> = Vec::new();
     let mut current_line_type: Option<LineType> = None;
 
     let mut heading_stack: Vec<u8> = Vec::new();
@@ -17,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 {
@@ -30,10 +38,10 @@ 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);
                     }
 
                     let line_content = get_partial_line_content(&line_type, line);
@@ -68,6 +76,7 @@ fn get_partial_line_content(line_type: &LineType, line: &str) -> String {
         LineType::ListItem => format!("<li>{}</li>", encoded_line[2..].trim()),
         LineType::Quote => encoded_line[1..].trim().to_string(),
         LineType::PreformattedText => format!("{}\n", encoded_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(),
     }
 }
         _ => "".to_string(),
     }
 }
@@ -131,12 +140,18 @@ fn close_heading_wrapper(heading_stack: &mut Vec<u8>) -> String {
     return string;
 }
 
     return string;
 }
 
-fn get_line_opener(line_type: &LineType) -> &'static str {
+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=\"h{}\">", label);
+            } else {
+                return "<pre>".to_string();
+            }
+        },
+        _ => "".to_string(),
     }
 }
 
     }
 }