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

First entry
author ferencd
date Thu, 16 Feb 2023 15:22:52 +0100
parents
children
rev   line source
ferencd@0 1 use crate::interpret_line;
ferencd@0 2
ferencd@0 3 // returns true if the previous line is handled as a header, ie. this line contains only
ferencd@0 4 // ---------- or ===========, in this case it will change the vector of lines and return true
ferencd@0 5 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 {
ferencd@0 6 let ch = ['-', '='];
ferencd@0 7 if only(&ip, ch[ht - 1]) {
ferencd@0 8 let last_line:String = html_lines.last().unwrap().to_owned();
ferencd@0 9 let header = format_header(last_line, ht, ht, bold_it, bold, italic);
ferencd@0 10 html_lines.pop();
ferencd@0 11 html_lines.push(header);
ferencd@0 12 return true;
ferencd@0 13 }
ferencd@0 14 return false;
ferencd@0 15 }
ferencd@0 16
ferencd@0 17 // formats the header
ferencd@0 18 pub fn format_header(s:String, c:usize, d:usize, bold_it:&mut bool, bold:&mut bool, italic:&mut bool) -> String {
ferencd@0 19 let m: String = s[(c-d)..].to_string().trim().to_string();
ferencd@0 20 let m = interpret_line(&m, bold_it, bold, italic);
ferencd@0 21 let nr = String::from(c.to_string());
ferencd@0 22 let cp = ">\n";
ferencd@0 23 let h:String = "\n<h".to_string() + &nr + &cp + &m + &"\n</h".to_string() + &nr + &cp;
ferencd@0 24 return h;
ferencd@0 25 }
ferencd@0 26
ferencd@0 27 // checks if the string contains only the given character
ferencd@0 28 fn only(s:&String, c:char) -> bool
ferencd@0 29 {
ferencd@0 30 if s.is_empty() {
ferencd@0 31 return false;
ferencd@0 32 }
ferencd@0 33 for sc in s.chars() {
ferencd@0 34 if sc != c {
ferencd@0 35 return false;
ferencd@0 36 }
ferencd@0 37 }
ferencd@0 38 return true;
ferencd@0 39 }
ferencd@0 40
ferencd@0 41 // returns true if the line is a header line, ie. starts with a set of #'s
ferencd@0 42 pub fn header(s:&str, c:usize) -> bool {
ferencd@0 43 if s.starts_with( &'#'.to_string().repeat(c)) {
ferencd@0 44 return true
ferencd@0 45 }
ferencd@0 46 return false
ferencd@0 47 }