url parsing works (FINALLY)
This commit is contained in:
parent
7b07b6f051
commit
ced5648c01
37
src/main.rs
37
src/main.rs
@ -5,6 +5,7 @@ use regex::Regex;
|
||||
use url::{Url, ParseError};
|
||||
|
||||
fn clear_screen() {
|
||||
println!("clearing");
|
||||
Command::new("clear")
|
||||
.status()
|
||||
.expect("Failed to clear screen");
|
||||
@ -100,7 +101,7 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
||||
}
|
||||
}).to_string();
|
||||
|
||||
let quick_hyperlink_regex = Regex::new(r"<(.*?)>").unwrap();
|
||||
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();
|
||||
@ -195,27 +196,37 @@ fn input() -> String{
|
||||
return s;
|
||||
}
|
||||
|
||||
fn parse_url(user_input: String, previous_url: String) -> Result<Url, ParseError> {
|
||||
let user_input = if user_input.contains("http://") || user_input.contains("https://") {
|
||||
println!("Opening http page in web browser");
|
||||
open::that(user_input);
|
||||
previous_url
|
||||
}
|
||||
else if user_input.contains("://") {
|
||||
println!("Contains different scheme");
|
||||
fn parse_url(user_input: String, previous_url: &Url) -> Result<Url, ParseError> {
|
||||
let mut relative = false;
|
||||
let mut relative_path: &str = "/";
|
||||
println!("user input: {}",user_input);
|
||||
println!("previous url: {:?}",previous_url);
|
||||
let to_parse = if user_input.contains("://") {
|
||||
println!("Contains different scheme or is a path");
|
||||
user_input
|
||||
}
|
||||
else if user_input[..1] == *"/" {
|
||||
relative = true;
|
||||
format!("http://{}/{}",Url::host_str(previous_url).expect("ivalid").to_string(), user_input)
|
||||
}
|
||||
else {
|
||||
println!("prepending scheme to user input");
|
||||
format!("http://{}", user_input) // Prepend 'mttp://' if no scheme is found
|
||||
};
|
||||
|
||||
let mut url: Url = Url::parse(&user_input).unwrap();
|
||||
println!("Parsing: {}", to_parse);
|
||||
if let Ok(mut url) = Url::parse(&to_parse) {
|
||||
if url.port() == None {
|
||||
url.set_port(Some(3477));
|
||||
}
|
||||
println!("{:?}",url);
|
||||
println!("{}",url.as_str());
|
||||
return Ok(url);
|
||||
println!("parsed successfully");
|
||||
return Ok(url)
|
||||
}
|
||||
else {
|
||||
return Err(ParseError::InvalidDomainCharacter)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -231,7 +242,7 @@ fn main() {
|
||||
let mut history: Vec<Url> = Vec::new();
|
||||
let mut historical_position: usize = 0;
|
||||
let mut links: Vec<String> = Vec::new();
|
||||
if let Ok(mut url) = parse_url(user_input, "http://deadvey.com".to_string()) { // Change this and make internal pages ;)
|
||||
if let Ok(mut url) = parse_url(user_input, &Url::parse(&"http://deadvey.com").unwrap()) { // Change this and make internal pages ;)
|
||||
'mainloop: loop {
|
||||
if load_page {
|
||||
links = Vec::new();
|
||||
@ -277,7 +288,7 @@ fn main() {
|
||||
}
|
||||
else if let Ok(number) = user_input.parse::<usize>() {
|
||||
if number < links.len() {
|
||||
if let Ok(parsed_value) = parse_url(links[number].clone(), url.as_str().to_string()) {
|
||||
if let Ok(parsed_value) = parse_url(links[number].clone(), &url) {
|
||||
url = parsed_value;
|
||||
/*
|
||||
for i in historical_position+1..history.len()-1 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user