Compare commits
2 Commits
a433ad41b5
...
8e860b89e7
Author | SHA1 | Date | |
---|---|---|---|
|
8e860b89e7 | ||
|
590cd001be |
82
src/main.rs
82
src/main.rs
@ -19,13 +19,15 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
|||||||
for line in page_content.lines() {
|
for line in page_content.lines() {
|
||||||
let mut parsed_line: String = line.to_string();
|
let mut parsed_line: String = line.to_string();
|
||||||
// Bold
|
// 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| {
|
parsed_line = bold_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
||||||
caps[1].bold().to_string()
|
caps[4].bold().to_string()
|
||||||
}).to_string();
|
}).to_string();
|
||||||
let bold_regex = Regex::new(r"__(.*?)__").unwrap();
|
|
||||||
parsed_line = bold_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
// Strikethrough
|
||||||
caps[1].bold().to_string()
|
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();
|
}).to_string();
|
||||||
|
|
||||||
// Horizontal lines
|
// Horizontal lines
|
||||||
@ -39,6 +41,12 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
|||||||
result
|
result
|
||||||
}).to_string();
|
}).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
|
// Italics
|
||||||
let italic_regex = Regex::new(r"\*(.*?)\*").unwrap();
|
let italic_regex = Regex::new(r"\*(.*?)\*").unwrap();
|
||||||
parsed_line = italic_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
parsed_line = italic_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
||||||
@ -64,7 +72,7 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
|||||||
parsed_line = unordered_list_regex.replace_all(&parsed_line, " • $2").to_string();
|
parsed_line = unordered_list_regex.replace_all(&parsed_line, " • $2").to_string();
|
||||||
|
|
||||||
// Inline code
|
// 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| {
|
parsed_line = inline_code_regex.replace_all(&parsed_line, |caps: ®ex::Captures| {
|
||||||
format!("{}", &caps[1].magenta())
|
format!("{}", &caps[1].magenta())
|
||||||
}).to_string();
|
}).to_string();
|
||||||
@ -81,10 +89,21 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
|||||||
result
|
result
|
||||||
} else {
|
} else {
|
||||||
// If it's an image (starts with !), return the link as is
|
// 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();
|
}).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");
|
parsed_page_content+=&(parsed_line + "\n");
|
||||||
}
|
}
|
||||||
@ -136,7 +155,11 @@ fn render_page(host: String, port: String, path: String) -> Vec<String> {
|
|||||||
let (screen_width, screen_height) = termion::terminal_size().unwrap();
|
let (screen_width, screen_height) = termion::terminal_size().unwrap();
|
||||||
|
|
||||||
(content, links) = parse_markdown(content);
|
(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 {
|
for _i in 0..screen_width {
|
||||||
print!("—");
|
print!("—");
|
||||||
}
|
}
|
||||||
@ -145,6 +168,8 @@ fn render_page(host: String, port: String, path: String) -> Vec<String> {
|
|||||||
print!("—");
|
print!("—");
|
||||||
}
|
}
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
|
// Return links (you can add link parsing logic)
|
||||||
return links;
|
return links;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,10 +191,11 @@ fn parse_url(url: String, previous_host: &String) -> (String, String, String) {
|
|||||||
let mut port: String = "3477".to_string();
|
let mut port: String = "3477".to_string();
|
||||||
let mut path: String = "/".to_string();
|
let mut path: String = "/".to_string();
|
||||||
|
|
||||||
let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap();
|
let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md
|
||||||
let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap();
|
let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com
|
||||||
let accept_this_as_mttp = Regex::new(r"^(.*?)$").unwrap();
|
let accept_this_as_mttp = Regex::new(r"^(.*?)$").unwrap(); // example.com
|
||||||
let path_change = Regex::new(r"^/(.*?)$").unwrap();
|
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) {
|
if let Some(caps) = mttp_regex.captures(&url) {
|
||||||
host = caps[1].to_string();
|
host = caps[1].to_string();
|
||||||
@ -187,6 +213,11 @@ fn parse_url(url: String, previous_host: &String) -> (String, String, String) {
|
|||||||
port = "3477".to_string();
|
port = "3477".to_string();
|
||||||
path = caps[1].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) {
|
else if let Some(caps) = accept_this_as_mttp.captures(&url) {
|
||||||
host = caps[1].to_string();
|
host = caps[1].to_string();
|
||||||
port = "3477".to_string();
|
port = "3477".to_string();
|
||||||
@ -209,13 +240,16 @@ fn main() {
|
|||||||
let (mut host, mut port, mut path) = parse_url(url, &"example.com".to_string()); // Must pass
|
let (mut host, mut port, mut path) = parse_url(url, &"example.com".to_string()); // Must pass
|
||||||
// 'previous
|
// 'previous
|
||||||
// host'
|
// host'
|
||||||
|
let mut load_page: bool = true;
|
||||||
'mainloop: loop {
|
'mainloop: loop {
|
||||||
let links = render_page(host.clone(), port.clone(), path.clone());
|
let mut links: Vec<String> = Vec::new();
|
||||||
|
if load_page {
|
||||||
println!("{}:{}/{}", host, port, path);
|
links = render_page(host.clone(), port.clone(), path.clone());
|
||||||
|
|
||||||
println!("Enter reference number to follow, h for help, or q to quit");
|
println!("Enter reference number to follow, h for help, or q to quit");
|
||||||
|
}
|
||||||
|
load_page = true;
|
||||||
|
|
||||||
let link_to_follow = input();
|
let link_to_follow = input();
|
||||||
if link_to_follow == "q" {
|
if link_to_follow == "q" {
|
||||||
break 'mainloop;
|
break 'mainloop;
|
||||||
@ -224,20 +258,20 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if link_to_follow == "h" {
|
else if link_to_follow == "h" {
|
||||||
println!("
|
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
|
||||||
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 {
|
else {
|
||||||
let number: usize = link_to_follow.parse::<usize>().unwrap();
|
let number: usize = link_to_follow.parse::<usize>().unwrap();
|
||||||
|
|
||||||
(host, port, path) = parse_url(links[number].clone(), &host);
|
(host, port, path) = parse_url(links[number].clone(), &host);
|
||||||
println!("{}:{}/{}", host, port, path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user