view src/header/mod.rs @ 0:9875208e49a0 tip

First entry
author ferencd
date Thu, 16 Feb 2023 15:22:52 +0100
parents
children
line wrap: on
line source
use crate::interpret_line;

// returns true if the previous line is handled as a header, ie. this line contains only
// ---------- or ===========, in this case it will change the vector of lines and return true
pub fn handle_as_header(ip:&String, html_lines: &mut Vec<String>, ht:usize, bold_it: &mut bool, bold:&mut bool, italic:&mut bool) -> bool {
    let ch = ['-', '='];
    if only(&ip, ch[ht - 1]) {
        let last_line:String = html_lines.last().unwrap().to_owned();
        let header = format_header(last_line, ht, ht, bold_it, bold, italic);
        html_lines.pop();
        html_lines.push(header);
        return true;
    }
    return false;
}

// formats the header
pub fn format_header(s:String, c:usize, d:usize, bold_it:&mut bool, bold:&mut bool, italic:&mut bool) -> String {
    let m: String =  s[(c-d)..].to_string().trim().to_string();
    let m = interpret_line(&m, bold_it, bold, italic);
    let nr = String::from(c.to_string());
    let cp = ">\n";
    let h:String = "\n<h".to_string() + &nr + &cp + &m + &"\n</h".to_string() + &nr + &cp;
    return h;
}

// checks if the string contains only the given character
fn only(s:&String, c:char) -> bool
{
    if s.is_empty() {
        return false;
    }
    for sc in s.chars() {
        if sc != c {
            return false;
        }
    }
    return true;
}

// returns true if the line is a header line, ie. starts with a set of #'s
pub fn header(s:&str, c:usize) -> bool {
    if s.starts_with( &'#'.to_string().repeat(c)) {
        return true
    }
    return false
}