X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/6a8515fe6ed81369badb51496699a2ff25dbfa9f..45fbf824248215a737d5d0b52920b43b68941ad3:/src/gemini_parser.rs diff --git a/src/gemini_parser.rs b/src/gemini_parser.rs index e63ebd3..496dc0d 100644 --- a/src/gemini_parser.rs +++ b/src/gemini_parser.rs @@ -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 mut html:String = "".to_owned(); + let mut block_label: Option = None; + let mut html: String = "".to_owned(); let mut current_line_type: Option = None; let mut heading_stack: Vec = Vec::new(); @@ -17,7 +18,14 @@ pub fn parse(source: &str) -> String { 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) = ¤t_line_type { @@ -30,10 +38,10 @@ pub fn parse(source: &str) -> String { if is_block(&line_type) { if let Some(line) = ¤t_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 { - 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); @@ -68,6 +76,7 @@ fn get_partial_line_content(line_type: &LineType, line: &str) -> String { LineType::ListItem => format!("
  • {}
  • ", 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(), } } @@ -76,13 +85,13 @@ fn get_full_line_content(line_type: &LineType, line: &str) -> String { let encoded_line = line.replace("<", "<").replace(">", ">"); match line_type { LineType::Text => format!("

    {}

    \n", encoded_line.trim()), - LineType::Blank => "
    \n".to_string(), + LineType::Blank => "
    \n".to_string(), LineType::Link => { let url = get_link_address(line); if url.starts_with("gemini:") { - format!("
    {}
    \n", url, get_link_content(line)) + format!("

    {}

    \n", url, get_link_content(line)) } else { - format!("
    {}
    \n", url.replace(".gmi", ".html"), get_link_content(line)) + format!("

    {}

    \n", url.replace(".gmi", ".html"), get_link_content(line)) } }, LineType::Heading1 => format!("

    {}

    \n", encoded_line[1..].trim()), @@ -110,14 +119,14 @@ fn get_heading_wrapper(heading_stack: &mut Vec, line_type: &LineType) -> Str break; } - string.push_str(""); + string.push_str(""); if open_heading == current_heading { break; } } heading_stack.push(current_heading); - string.push_str(&format!("
    ", current_heading)); + string.push_str(&format!("
    ", current_heading)); } return string; @@ -126,17 +135,23 @@ fn get_heading_wrapper(heading_stack: &mut Vec, line_type: &LineType) -> Str fn close_heading_wrapper(heading_stack: &mut Vec) -> String { let mut string = String::new(); while let Some(_open_heading) = heading_stack.pop() { - string.push_str("
    "); + string.push_str(""); } 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 { - LineType::ListItem => "
      ", - LineType::Quote => "
      ", - LineType::PreformattedText => "
      ",
      -        _ => "",
      +        LineType::ListItem => "
        ".to_string(), + LineType::Quote => "
        ".to_string(), + LineType::PreformattedText => { + if let Some(label) = &block_label { + return format!("
        ", label);
        +            } else {
        +                return "
        ".to_string();
        +            }
        +        },
        +        _ => "".to_string(),
             }
         }