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