From 590cd001bee897e7a6acd7fe5a60eb918c71f9f4 Mon Sep 17 00:00:00 2001 From: deadvey Date: Wed, 8 Jan 2025 20:35:59 +0000 Subject: [PATCH] few rendering changes --- src/main.rs | 74 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index ce40c02..049024d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,15 @@ fn parse_markdown(page_content: String) -> (String, Vec) { for line in page_content.lines() { let mut parsed_line: String = line.to_string(); // Bold - let bold_regex = Regex::new(r"\*\*(.*?)\*\*").unwrap(); + let bold_regex = Regex::new(r"((\*\*)|(__))(.*?)((\*\*)|(__))").unwrap(); parsed_line = bold_regex.replace_all(&parsed_line, |caps: ®ex::Captures| { - caps[1].bold().to_string() + caps[4].bold().to_string() }).to_string(); - let bold_regex = Regex::new(r"__(.*?)__").unwrap(); - parsed_line = bold_regex.replace_all(&parsed_line, |caps: ®ex::Captures| { - caps[1].bold().to_string() + + // Strikethrough + let strikethrough_regex = Regex::new(r"~~(.*?)~~").unwrap(); + parsed_line = strikethrough_regex.replace_all(&parsed_line, |caps: ®ex::Captures| { + caps[1].strikethrough().to_string() }).to_string(); // Horizontal lines @@ -64,9 +66,9 @@ fn parse_markdown(page_content: String) -> (String, Vec) { parsed_line = unordered_list_regex.replace_all(&parsed_line, " • $2").to_string(); // Inline code - let inline_code_regex = Regex::new(r"`(.*)`").unwrap(); + let inline_code_regex = Regex::new(r"`(.*?)`").unwrap(); parsed_line = inline_code_regex.replace_all(&parsed_line, |caps: ®ex::Captures| { - format!("{}", &caps[1].magenta()) + format!("{}", &caps[1].red().on_white()) }).to_string(); // HyperLink @@ -81,7 +83,10 @@ fn parse_markdown(page_content: String) -> (String, Vec) { result } else { // If it's an image (starts with !), return the link as is - format!("[{}]", &caps[2].green()) + let url = caps[3].to_string(); + links.push(url); + hyperlink_number_counter += 1; + format!("({})[{}]", &caps[2].green(), hyperlink_number_counter) } }).to_string(); @@ -103,7 +108,7 @@ fn parse_markdown(page_content: String) -> (String, Vec) { .join("\n"); // Return the formatted block with magenta color - format!("{}", indented_code.magenta()) + format!("{}", indented_code.red().on_white()) }).to_string(); return (parsed_page_content, links); @@ -136,7 +141,11 @@ fn render_page(host: String, port: String, path: String) -> Vec { let (screen_width, screen_height) = termion::terminal_size().unwrap(); (content, links) = parse_markdown(content); - print!("{}: {}\n", host, path); + + for _i in 0..screen_width { + print!("—"); + } + print!("{}:{}/{}\n", host, port, path); for _i in 0..screen_width { print!("—"); } @@ -145,7 +154,9 @@ fn render_page(host: String, port: String, path: String) -> Vec { print!("—"); } println!(); - return links; + + // Return links (you can add link parsing logic) + return links; } fn input() -> String{ @@ -166,10 +177,11 @@ fn parse_url(url: String, previous_host: &String) -> (String, String, String) { let mut port: String = "3477".to_string(); let mut path: String = "/".to_string(); - let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); - let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); - let accept_this_as_mttp = Regex::new(r"^(.*?)$").unwrap(); - let path_change = Regex::new(r"^/(.*?)$").unwrap(); + let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md + let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com + let accept_this_as_mttp = Regex::new(r"^(.*?)$").unwrap(); // example.com + let no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md + let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md if let Some(caps) = mttp_regex.captures(&url) { host = caps[1].to_string(); @@ -187,6 +199,11 @@ fn parse_url(url: String, previous_host: &String) -> (String, String, String) { port = "3477".to_string(); path = caps[1].to_string(); } + else if let Some(caps) = no_protocol_but_path.captures(&url) { + host = caps[1].to_string(); + port = "3477".to_string(); + path = caps[2].to_string(); + } else if let Some(caps) = accept_this_as_mttp.captures(&url) { host = caps[1].to_string(); port = "3477".to_string(); @@ -209,13 +226,16 @@ fn main() { let (mut host, mut port, mut path) = parse_url(url, &"example.com".to_string()); // Must pass // 'previous // host' - + let mut load_page: bool = true; 'mainloop: loop { - let links = render_page(host.clone(), port.clone(), path.clone()); + let mut links: Vec = Vec::new(); + if load_page { + links = render_page(host.clone(), port.clone(), path.clone()); - println!("{}:{}/{}", host, port, path); + println!("Enter reference number to follow, h for help, or q to quit"); + } + load_page = true; - println!("Enter reference number to follow, h for help, or q to quit"); let link_to_follow = input(); if link_to_follow == "q" { break 'mainloop; @@ -224,20 +244,20 @@ fn main() { continue; } else if link_to_follow == "h" { - println!(" - Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\n - q: quit - h: help - r: reload - i: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com - b: go back in history + println!("Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\nq: quit\nh: help\nr: reload\ni: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com\nb: go back in history\nox: print the hyprlink of reference x eg: o5 or o24 "); + load_page = false; + } + else if link_to_follow.chars().nth(0).unwrap() == 'o' { + let number_str = &link_to_follow[1..]; + let number: usize = number_str.parse::().unwrap(); + println!("{}",links[number]); + load_page = false; } else { let number: usize = link_to_follow.parse::().unwrap(); (host, port, path) = parse_url(links[number].clone(), &host); - println!("{}:{}/{}", host, port, path); } } }