Compare commits
No commits in common. "8e860b89e7490f0efe1171f0efc4f5db417a3be9" and "a433ad41b5883532daa538feb37e7a2e4dcd2646" have entirely different histories.
8e860b89e7
...
a433ad41b5
82
src/main.rs
82
src/main.rs
@ -19,15 +19,13 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
||||
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[4].bold().to_string()
|
||||
caps[1].bold().to_string()
|
||||
}).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()
|
||||
let bold_regex = Regex::new(r"__(.*?)__").unwrap();
|
||||
parsed_line = bold_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
||||
caps[1].bold().to_string()
|
||||
}).to_string();
|
||||
|
||||
// Horizontal lines
|
||||
@ -41,12 +39,6 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
||||
result
|
||||
}).to_string();
|
||||
|
||||
// html br tag support
|
||||
let br_regex = Regex::new(r"(.*?)<br/>(.*?)").unwrap();
|
||||
parsed_line = br_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
||||
format!("{}{}{}", &caps[1], "\n", &caps[2])
|
||||
}).to_string();
|
||||
|
||||
// Italics
|
||||
let italic_regex = Regex::new(r"\*(.*?)\*").unwrap();
|
||||
parsed_line = italic_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
||||
@ -72,7 +64,7 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
||||
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())
|
||||
}).to_string();
|
||||
@ -89,21 +81,10 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
||||
result
|
||||
} else {
|
||||
// If it's an image (starts with !), return the link as is
|
||||
let url = caps[3].to_string();
|
||||
links.push(url);
|
||||
hyperlink_number_counter += 1;
|
||||
format!("({})[{}]", &caps[2].green(), hyperlink_number_counter)
|
||||
format!("[{}]", &caps[2].green())
|
||||
}
|
||||
}).to_string();
|
||||
|
||||
let quick_hyperlink_regex = Regex::new(r"<(.*?)>").unwrap();
|
||||
parsed_line = quick_hyperlink_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
||||
hyperlink_number_counter += 1;
|
||||
let url = caps[1].to_string();
|
||||
links.push(url);
|
||||
format!("{}[{}]", &caps[1].blue().underline(), hyperlink_number_counter)
|
||||
}).to_string();
|
||||
|
||||
|
||||
parsed_page_content+=&(parsed_line + "\n");
|
||||
}
|
||||
@ -155,11 +136,7 @@ fn render_page(host: String, port: String, path: String) -> Vec<String> {
|
||||
let (screen_width, screen_height) = termion::terminal_size().unwrap();
|
||||
|
||||
(content, links) = parse_markdown(content);
|
||||
|
||||
for _i in 0..screen_width {
|
||||
print!("—");
|
||||
}
|
||||
print!("{}:{}/{}\n", host, port, path);
|
||||
print!("{}: {}\n", host, path);
|
||||
for _i in 0..screen_width {
|
||||
print!("—");
|
||||
}
|
||||
@ -168,8 +145,6 @@ fn render_page(host: String, port: String, path: String) -> Vec<String> {
|
||||
print!("—");
|
||||
}
|
||||
println!();
|
||||
|
||||
// Return links (you can add link parsing logic)
|
||||
return links;
|
||||
}
|
||||
|
||||
@ -191,11 +166,10 @@ 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(); // 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
|
||||
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();
|
||||
|
||||
if let Some(caps) = mttp_regex.captures(&url) {
|
||||
host = caps[1].to_string();
|
||||
@ -213,11 +187,6 @@ 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();
|
||||
@ -240,16 +209,13 @@ 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 mut links: Vec<String> = Vec::new();
|
||||
if load_page {
|
||||
links = render_page(host.clone(), port.clone(), path.clone());
|
||||
let links = render_page(host.clone(), port.clone(), path.clone());
|
||||
|
||||
println!("Enter reference number to follow, h for help, or q to quit");
|
||||
}
|
||||
load_page = true;
|
||||
println!("{}:{}/{}", host, port, path);
|
||||
|
||||
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;
|
||||
@ -258,20 +224,20 @@ fn main() {
|
||||
continue;
|
||||
}
|
||||
else if link_to_follow == "h" {
|
||||
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
|
||||
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
|
||||
");
|
||||
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::<usize>().unwrap();
|
||||
println!("{}",links[number]);
|
||||
load_page = false;
|
||||
}
|
||||
else {
|
||||
let number: usize = link_to_follow.parse::<usize>().unwrap();
|
||||
|
||||
(host, port, path) = parse_url(links[number].clone(), &host);
|
||||
println!("{}:{}/{}", host, port, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user