Compare commits

..

No commits in common. "1b8614b9569d2d72a1781ff122ad27165ac9b801" and "67a509626701b30ce6d309176f3163e528364af0" have entirely different histories.

2 changed files with 64 additions and 55 deletions

View File

@ -8,4 +8,3 @@ colored = "2.2.0"
regex = "1.11.1" regex = "1.11.1"
url = "2.5.4" url = "2.5.4"
termion = "4.0.3" termion = "4.0.3"
open = "5.3.2"

View File

@ -21,7 +21,7 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
let mut parsed_page_content: String = "".to_string(); let mut parsed_page_content: String = "".to_string();
let mut hyperlink_number_counter: u64 = 0; let mut hyperlink_number_counter: u64 = 0;
let mut links: Vec<String> = Vec::new(); let mut links: Vec<String> = Vec::new();
let (screen_width, _screen_height) = termion::terminal_size().unwrap(); // So the horizontal line (<hr/>) spans the whole console let (screen_width, screen_height) = termion::terminal_size().unwrap(); // So the horizontal line (<hr/>) spans the whole console
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();
@ -140,7 +140,7 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
} }
fn fetch_page(host: &String, port: u16, path: &String) -> String { fn fetch_page(host: &String, port: u16, path: &String) -> String {
let full_url_formatted = format!("{}:{}{}", host, port, path); let full_url_formatted = format!("{}:{}/{}", host, port, path);
// Call curl using Com, mand // Call curl using Com, mand
let output = Command::new("curl") let output = Command::new("curl")
@ -163,19 +163,14 @@ fn render_page(host: String, port: u16, path: String) -> Vec<String> {
clear_screen(); clear_screen();
let mut content = fetch_page(&host, port, &path); let mut content = fetch_page(&host, port, &path);
let mut links = Vec::new(); let mut links = Vec::new();
let (screen_width, _screen_height) = termion::terminal_size().unwrap(); let (screen_width, screen_height) = termion::terminal_size().unwrap();
if &content[..13] == "<!DOCTYPE md>" { (content, links) = parse_markdown(content);
(content, links) = parse_markdown((&content[13..]).to_string());
}
else {
content += &format!("{}", &"Warning: This page is invalid markdown, it should contain <!DOCTYPE md> at the very start of the file, showing raw text".yellow());
}
for _i in 0..screen_width { for _i in 0..screen_width {
print!(""); print!("");
} }
print!("{}:{}{}\n", host, port, path); print!("{}:{}/{}\n", host, port, path);
for _i in 0..screen_width { for _i in 0..screen_width {
print!(""); print!("");
} }
@ -212,9 +207,11 @@ fn parse_url(user_input: String, previous_host: &String) -> Result<Url, u8> {
let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md 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 mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com
//let accept_this_as_mttp = Regex::new(r"^(.*?)\.(.*?)$").unwrap(); // 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 no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md
let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md
let http_regex = Regex::new(r"^http:\/\/(.*?)\/(.*?)$").unwrap(); // http://example.com/index.md
let http_regex_no_path = Regex::new(r"^http:\/\/(.*?)$").unwrap(); // http://example.com
if let Some(caps) = mttp_regex.captures(&user_input) { if let Some(caps) = mttp_regex.captures(&user_input) {
url.hostname = caps[1].to_string(); url.hostname = caps[1].to_string();
@ -233,26 +230,39 @@ fn parse_url(user_input: String, previous_host: &String) -> Result<Url, u8> {
else if let Some(caps) = path_change.captures(&user_input) { else if let Some(caps) = path_change.captures(&user_input) {
url.hostname = previous_host.to_string(); url.hostname = previous_host.to_string();
url.port = 3477; url.port = 3477;
url.path = format!("/{}", caps[1].to_string()); url.path = caps[1].to_string();
url.protocol = "mttp".to_string();
Ok(url)
}
else if let Some(caps) = no_protocol_but_path.captures(&user_input) {
url.hostname = caps[1].to_string();
url.port = 3477;
url.path = caps[2].to_string();
url.protocol = "mttp".to_string();
Ok(url)
}
else if let Some(caps) = http_regex.captures(&user_input) {
url.hostname = caps[1].to_string();
url.port = 80;
url.path = caps[2].to_string();
url.protocol = "http".to_string();
Ok(url)
}
else if let Some(caps) = http_regex_no_path.captures(&user_input) {
url.hostname = caps[1].to_string();
url.port = 80;
url.path = "/".to_string();
url.protocol = "http".to_string();
Ok(url)
}
else if let Some(caps) = accept_this_as_mttp.captures(&user_input) {
url.hostname = format!("{}{}{}",caps[1].to_string(),".",caps[2].to_string());
url.port = 3477;
url.path = "/".to_string();
url.protocol = "mttp".to_string(); url.protocol = "mttp".to_string();
Ok(url) Ok(url)
} }
//else if let Some(caps) = no_protocol_but_path.captures(&user_input) {
// url.hostname = caps[1].to_string();
//url.port = 3477;
//url.path = caps[2].to_string();
//url.protocol = "mttp".to_string();
//Ok(url)
//}
//else if let Some(caps) = accept_this_as_mttp.captures(&user_input) {
// url.hostname = format!("{}{}{}",caps[1].to_string(),".",caps[2].to_string());
//url.port = 3477;
// url.path = "/".to_string();
// url.protocol = "mttp".to_string();
//Ok(url)
//}
else { else {
open::that(user_input); // Fallback to open it in the web browser
Err(1) Err(1)
} }
} }
@ -287,10 +297,10 @@ fn main() {
if load_page { if load_page {
links = Vec::new(); links = Vec::new();
links = render_page(history[historical_position].hostname.clone(), history[historical_position].port.clone(), history[historical_position].path.clone()); links = render_page(history[historical_position].hostname.clone(), history[historical_position].port.clone(), history[historical_position].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\n{}",historical_position);
//for i in 0..history.len() { for i in 0..history.len() {
// println!("{}://{}:{}/{}",history[i].protocol,history[i].hostname, history[i].port, history[i].path); println!("{}://{}:{}/{}",history[i].protocol,history[i].hostname, history[i].port, history[i].path);
//} }
} }
load_page = false; load_page = false;
@ -346,7 +356,7 @@ fn main() {
load_page = true; load_page = true;
} }
else { else {
println!("Invalid url\nAttempting to open url in web browser"); println!("Invalid url");
} }
} else { } else {
println!("Invalid reference id"); println!("Invalid reference id");
@ -362,7 +372,7 @@ fn main() {
} }
} }
else { else {
println!("Invalid mttp url, try mttp:// at the start of your input."); println!("Invalid url");
} }
} }