Mercurial > md2html
diff src/list/mod.rs @ 0:9875208e49a0 tip
First entry
| author | ferencd |
|---|---|
| date | Thu, 16 Feb 2023 15:22:52 +0100 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/list/mod.rs Thu Feb 16 15:22:52 2023 +0100 @@ -0,0 +1,113 @@ +// deals with increasing the <ol> tags, by trying to identify lines starting with spaces/tabs +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) { + let mut start = "\t"; + if orig_ip.starts_with(" ") { + start = " "; + } + let mut current_space_count:i8 = i; + let mut oip_cp = &orig_ip[start.len() ..]; + + while oip_cp.starts_with(start) { + oip_cp = &oip_cp[start.len() ..]; + current_space_count += 1; + } + + if *olist_space_count != current_space_count { + if *olist_space_count < current_space_count { + *olist_count += 1; + html_lines.push(open_tag.to_string()); + } else { + *olist_count -= 1; + html_lines.push(close_tag.to_string()); + } + *olist_space_count = current_space_count; + } +} + +// closes the opened lists with the given close tag +pub fn close_opened_lists(html_lines: &mut Vec<String>, list_nest_count: &mut i8, list_space_count: &mut i8, close_tag: &str) { + println!("For {} nestcount={}",close_tag, list_nest_count); + while *list_nest_count > 0 { + html_lines.push(close_tag.to_string()); + *list_nest_count -= 1; + } + *list_space_count = 0; +} + +// deals with some list, checks lines that start with the given start_checker +pub fn deal_with_list<C>(mut html_lines: &mut Vec<String>, mut list_nest_count: &mut i8, + mut list_space_count: &mut i8, orig_ip: &String, + tip:&str, list_start_checker: C, + open_tag: &str, close_tag: &str, list_delim_char: char) +where C: Fn(&str, isize) -> bool +{ + let mut cidxc:isize = 0; + if list_start_checker(tip, cidxc) { + println!("list start: {}", tip); + while list_start_checker(tip, cidxc) { + println!("list start: {}, {}", tip, cidxc); + cidxc = cidxc + 1; + } + if tip.chars().nth(cidxc.try_into().unwrap()).unwrap() == list_delim_char { + // this is indeed starting a list + if list_delim_char == '.' { // ordered list + let nr = tip[..cidxc.try_into().unwrap()].to_string().parse::<i32>().unwrap(); + if nr == 1 { + if *list_nest_count == 0 { // the very first list + *list_nest_count = 1; + html_lines.push(open_tag.to_string()); + } else { // see if we start with a tab or not + resolve_list_entry(&mut html_lines, &mut list_nest_count, &mut list_space_count, orig_ip, open_tag, close_tag) + } + } else { + // the number is not one. try to see if it is a different list or continues + resolve_list_entry(&mut html_lines, &mut list_nest_count, &mut list_space_count, orig_ip, open_tag, close_tag) + } + } else { // unordered list + if *list_nest_count == 0 { // the very first list + *list_nest_count = 1; + html_lines.push(open_tag.to_string()); + } else { + resolve_list_entry(&mut html_lines, &mut list_nest_count, &mut list_space_count, orig_ip, open_tag, close_tag) + } + } + } else { + close_opened_lists(&mut html_lines, &mut list_nest_count, &mut list_space_count, close_tag); + } + } else { + close_opened_lists(&mut html_lines, &mut list_nest_count, &mut list_space_count, close_tag); + } +} + +// closes the <ol> tags opened +pub fn insert_list_entry<C>(list_counter: &mut i8, tip: &str, inted: &mut String, list_start_checker:C, sep_char: char) +where C: Fn(&str, isize) -> bool +{ + if *list_counter != 0 { + let mut cidxc = 0; + if list_start_checker(inted, cidxc) { + while list_start_checker(tip, cidxc) { + cidxc = cidxc + 1; + } + if tip.chars().nth(cidxc.try_into().unwrap()).unwrap() == sep_char { + cidxc += 1; + } + *inted = "<li>".to_owned() + &inted[cidxc.try_into().unwrap()..].trim(); + } else { + *inted = "<li>".to_owned() + &inted; + } + } +} + +// resolves a list entry +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) +{ + if orig_ip.starts_with("\t") || orig_ip.starts_with(" ") { + deal_with_list_nesting(orig_ip, &mut olist_space_count, &mut olist_count, &mut html_lines, 1, open_tag, close_tag); + } else { // maybe we slipped back one ol + while *olist_count > 1 { + *olist_count -= 1; + html_lines.push(close_tag.to_string()); + } + } +}
