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