Mercurial > md2html
comparison src/list/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 // deals with increasing the <ol> tags, by trying to identify lines starting with spaces/tabs | |
| 2 pub fn deal_with_list_nesting(orig_ip: &String, olist_space_count: &mut i8, olist_count: &mut i8, html_lines: &mut Vec<String>, i: i8, open_tag: &str, close_tag: &str) { | |
| 3 let mut start = "\t"; | |
| 4 if orig_ip.starts_with(" ") { | |
| 5 start = " "; | |
| 6 } | |
| 7 let mut current_space_count:i8 = i; | |
| 8 let mut oip_cp = &orig_ip[start.len() ..]; | |
| 9 | |
| 10 while oip_cp.starts_with(start) { | |
| 11 oip_cp = &oip_cp[start.len() ..]; | |
| 12 current_space_count += 1; | |
| 13 } | |
| 14 | |
| 15 if *olist_space_count != current_space_count { | |
| 16 if *olist_space_count < current_space_count { | |
| 17 *olist_count += 1; | |
| 18 html_lines.push(open_tag.to_string()); | |
| 19 } else { | |
| 20 *olist_count -= 1; | |
| 21 html_lines.push(close_tag.to_string()); | |
| 22 } | |
| 23 *olist_space_count = current_space_count; | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 // closes the opened lists with the given close tag | |
| 28 pub fn close_opened_lists(html_lines: &mut Vec<String>, list_nest_count: &mut i8, list_space_count: &mut i8, close_tag: &str) { | |
| 29 println!("For {} nestcount={}",close_tag, list_nest_count); | |
| 30 while *list_nest_count > 0 { | |
| 31 html_lines.push(close_tag.to_string()); | |
| 32 *list_nest_count -= 1; | |
| 33 } | |
| 34 *list_space_count = 0; | |
| 35 } | |
| 36 | |
| 37 // deals with some list, checks lines that start with the given start_checker | |
| 38 pub fn deal_with_list<C>(mut html_lines: &mut Vec<String>, mut list_nest_count: &mut i8, | |
| 39 mut list_space_count: &mut i8, orig_ip: &String, | |
| 40 tip:&str, list_start_checker: C, | |
| 41 open_tag: &str, close_tag: &str, list_delim_char: char) | |
| 42 where C: Fn(&str, isize) -> bool | |
| 43 { | |
| 44 let mut cidxc:isize = 0; | |
| 45 if list_start_checker(tip, cidxc) { | |
| 46 println!("list start: {}", tip); | |
| 47 while list_start_checker(tip, cidxc) { | |
| 48 println!("list start: {}, {}", tip, cidxc); | |
| 49 cidxc = cidxc + 1; | |
| 50 } | |
| 51 if tip.chars().nth(cidxc.try_into().unwrap()).unwrap() == list_delim_char { | |
| 52 // this is indeed starting a list | |
| 53 if list_delim_char == '.' { // ordered list | |
| 54 let nr = tip[..cidxc.try_into().unwrap()].to_string().parse::<i32>().unwrap(); | |
| 55 if nr == 1 { | |
| 56 if *list_nest_count == 0 { // the very first list | |
| 57 *list_nest_count = 1; | |
| 58 html_lines.push(open_tag.to_string()); | |
| 59 } else { // see if we start with a tab or not | |
| 60 resolve_list_entry(&mut html_lines, &mut list_nest_count, &mut list_space_count, orig_ip, open_tag, close_tag) | |
| 61 } | |
| 62 } else { | |
| 63 // the number is not one. try to see if it is a different list or continues | |
| 64 resolve_list_entry(&mut html_lines, &mut list_nest_count, &mut list_space_count, orig_ip, open_tag, close_tag) | |
| 65 } | |
| 66 } else { // unordered list | |
| 67 if *list_nest_count == 0 { // the very first list | |
| 68 *list_nest_count = 1; | |
| 69 html_lines.push(open_tag.to_string()); | |
| 70 } else { | |
| 71 resolve_list_entry(&mut html_lines, &mut list_nest_count, &mut list_space_count, orig_ip, open_tag, close_tag) | |
| 72 } | |
| 73 } | |
| 74 } else { | |
| 75 close_opened_lists(&mut html_lines, &mut list_nest_count, &mut list_space_count, close_tag); | |
| 76 } | |
| 77 } else { | |
| 78 close_opened_lists(&mut html_lines, &mut list_nest_count, &mut list_space_count, close_tag); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 // closes the <ol> tags opened | |
| 83 pub fn insert_list_entry<C>(list_counter: &mut i8, tip: &str, inted: &mut String, list_start_checker:C, sep_char: char) | |
| 84 where C: Fn(&str, isize) -> bool | |
| 85 { | |
| 86 if *list_counter != 0 { | |
| 87 let mut cidxc = 0; | |
| 88 if list_start_checker(inted, cidxc) { | |
| 89 while list_start_checker(tip, cidxc) { | |
| 90 cidxc = cidxc + 1; | |
| 91 } | |
| 92 if tip.chars().nth(cidxc.try_into().unwrap()).unwrap() == sep_char { | |
| 93 cidxc += 1; | |
| 94 } | |
| 95 *inted = "<li>".to_owned() + &inted[cidxc.try_into().unwrap()..].trim(); | |
| 96 } else { | |
| 97 *inted = "<li>".to_owned() + &inted; | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // resolves a list entry | |
| 103 pub fn resolve_list_entry(mut html_lines: &mut Vec<String>, mut olist_count: &mut i8, mut olist_space_count: &mut i8, orig_ip: &String, open_tag: &str, close_tag: &str) | |
| 104 { | |
| 105 if orig_ip.starts_with("\t") || orig_ip.starts_with(" ") { | |
| 106 deal_with_list_nesting(orig_ip, &mut olist_space_count, &mut olist_count, &mut html_lines, 1, open_tag, close_tag); | |
| 107 } else { // maybe we slipped back one ol | |
| 108 while *olist_count > 1 { | |
| 109 *olist_count -= 1; | |
| 110 html_lines.push(close_tag.to_string()); | |
| 111 } | |
| 112 } | |
| 113 } |
