done for the day, got a proper url parser
This commit is contained in:
parent
e4b08b45bc
commit
7b07b6f051
@ -6,7 +6,6 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
colored = "2.2.0"
|
colored = "2.2.0"
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
url = "2.5.4"
|
|
||||||
termion = "4.0.3"
|
termion = "4.0.3"
|
||||||
open = "5.3.2"
|
open = "5.3.2"
|
||||||
url = "2.5.4"
|
url = "2.5.4"
|
||||||
|
127
src/main.rs
127
src/main.rs
@ -2,19 +2,12 @@ use std::process::{Command};
|
|||||||
use std::io::{stdin,stdout,Write};
|
use std::io::{stdin,stdout,Write};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use url::{Url, ParseError};
|
||||||
struct Url {
|
|
||||||
protocol: String,
|
|
||||||
hostname: String,
|
|
||||||
port: u16,
|
|
||||||
path: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn clear_screen() {
|
fn clear_screen() {
|
||||||
Command::new("clear")
|
Command::new("clear")
|
||||||
.status()
|
.status()
|
||||||
.expect("Failed to clear screen");
|
.expect("Failed to clear screen");
|
||||||
//println!("clearing");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
||||||
@ -139,8 +132,8 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
|||||||
return (parsed_page_content, links);
|
return (parsed_page_content, links);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_page(host: &String, port: u16, path: &String) -> String {
|
fn fetch_page(url: &Url) -> String {
|
||||||
let full_url_formatted = format!("{}:{}{}", host, port, path);
|
let full_url_formatted = format!("{}", url);
|
||||||
|
|
||||||
// Call curl using Com, mand
|
// Call curl using Com, mand
|
||||||
let output = Command::new("curl")
|
let output = Command::new("curl")
|
||||||
@ -159,9 +152,9 @@ fn fetch_page(host: &String, port: u16, path: &String) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_page(host: String, port: u16, path: String) -> Vec<String> {
|
fn render_page(url: Url) -> Vec<String> {
|
||||||
clear_screen();
|
clear_screen();
|
||||||
let mut content = fetch_page(&host, port, &path);
|
let mut content = fetch_page(&url);
|
||||||
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();
|
||||||
|
|
||||||
@ -175,7 +168,7 @@ fn render_page(host: String, port: u16, path: String) -> Vec<String> {
|
|||||||
for _i in 0..screen_width {
|
for _i in 0..screen_width {
|
||||||
print!("—");
|
print!("—");
|
||||||
}
|
}
|
||||||
print!("{}:{}{}\n", host, port, path);
|
print!("{}\n", url);
|
||||||
for _i in 0..screen_width {
|
for _i in 0..screen_width {
|
||||||
print!("—");
|
print!("—");
|
||||||
}
|
}
|
||||||
@ -202,59 +195,28 @@ fn input() -> String{
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_url(user_input: String, previous_host: &String) -> Result<Url, u8> {
|
fn parse_url(user_input: String, previous_url: String) -> Result<Url, ParseError> {
|
||||||
let mut url = Url {
|
let user_input = if user_input.contains("http://") || user_input.contains("https://") {
|
||||||
protocol: "internal".to_string(),
|
println!("Opening http page in web browser");
|
||||||
hostname: "home".to_string(),
|
open::that(user_input);
|
||||||
port: 0,
|
previous_url
|
||||||
path: "/".to_string(),
|
}
|
||||||
|
else if user_input.contains("://") {
|
||||||
|
println!("Contains different scheme");
|
||||||
|
user_input
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
format!("http://{}", user_input) // Prepend 'mttp://' if no scheme is found
|
||||||
};
|
};
|
||||||
|
|
||||||
let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md
|
let mut url: Url = Url::parse(&user_input).unwrap();
|
||||||
let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com
|
if url.port() == None {
|
||||||
//let accept_this_as_mttp = Regex::new(r"^(.*?)\.(.*?)$").unwrap(); // example.com
|
url.set_port(Some(3477));
|
||||||
//let no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md
|
}
|
||||||
let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md
|
println!("{:?}",url);
|
||||||
|
println!("{}",url.as_str());
|
||||||
|
return Ok(url);
|
||||||
|
|
||||||
if let Some(caps) = mttp_regex.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) = mttp_regex_no_path.captures(&user_input) {
|
|
||||||
url.hostname = caps[1].to_string();
|
|
||||||
url.port = 3477;
|
|
||||||
url.path = "/".to_string();
|
|
||||||
url.protocol = "mttp".to_string();
|
|
||||||
Ok(url)
|
|
||||||
}
|
|
||||||
else if let Some(caps) = path_change.captures(&user_input) {
|
|
||||||
url.hostname = previous_host.to_string();
|
|
||||||
url.port = 3477;
|
|
||||||
url.path = format!("/{}", 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) = 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 {
|
|
||||||
open::that(user_input); // Fallback to open it in the web browser
|
|
||||||
Err(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -269,28 +231,12 @@ fn main() {
|
|||||||
let mut history: Vec<Url> = Vec::new();
|
let mut history: Vec<Url> = Vec::new();
|
||||||
let mut historical_position: usize = 0;
|
let mut historical_position: usize = 0;
|
||||||
let mut links: Vec<String> = Vec::new();
|
let mut links: Vec<String> = Vec::new();
|
||||||
let mut url = Url {
|
if let Ok(mut url) = parse_url(user_input, "http://deadvey.com".to_string()) { // Change this and make internal pages ;)
|
||||||
protocol: "internal".to_string(),
|
|
||||||
hostname: "home".to_string(),
|
|
||||||
port: 0,
|
|
||||||
path: "/".to_string(),
|
|
||||||
};
|
|
||||||
if let Ok(parsed_value) = parse_url(user_input, &"example.com".to_string()) {
|
|
||||||
url = parsed_value;
|
|
||||||
history.push(Url {
|
|
||||||
protocol: url.protocol.clone(),
|
|
||||||
hostname: url.hostname.clone(),
|
|
||||||
port: url.port.clone(),
|
|
||||||
path: url.path.clone(),
|
|
||||||
});
|
|
||||||
'mainloop: loop {
|
'mainloop: loop {
|
||||||
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(url.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");
|
||||||
//for i in 0..history.len() {
|
|
||||||
// println!("{}://{}:{}/{}",history[i].protocol,history[i].hostname, history[i].port, history[i].path);
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
load_page = false;
|
load_page = false;
|
||||||
|
|
||||||
@ -301,7 +247,7 @@ fn main() {
|
|||||||
else if user_input == "r" {
|
else if user_input == "r" {
|
||||||
load_page = true;
|
load_page = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}/*
|
||||||
else if user_input == "i" {
|
else if user_input == "i" {
|
||||||
url.path = "/".to_string();
|
url.path = "/".to_string();
|
||||||
load_page = true;
|
load_page = true;
|
||||||
@ -317,7 +263,7 @@ fn main() {
|
|||||||
println!("Invalid url");
|
println!("Invalid url");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
else if user_input == "h" {
|
else if user_input == "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\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");
|
||||||
}
|
}
|
||||||
@ -331,18 +277,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
else if let Ok(number) = user_input.parse::<usize>() {
|
else if let Ok(number) = user_input.parse::<usize>() {
|
||||||
if number < links.len() {
|
if number < links.len() {
|
||||||
if let Ok(parsed_value) = parse_url(links[number].clone(), &url.hostname) {
|
if let Ok(parsed_value) = parse_url(links[number].clone(), url.as_str().to_string()) {
|
||||||
url = parsed_value;
|
url = parsed_value;
|
||||||
history.insert(historical_position+1, Url {
|
/*
|
||||||
protocol: url.protocol.clone(),
|
|
||||||
hostname: url.hostname.clone(),
|
|
||||||
port: url.port.clone(),
|
|
||||||
path: url.path.clone(),
|
|
||||||
});
|
|
||||||
for i in historical_position+1..history.len()-1 {
|
for i in historical_position+1..history.len()-1 {
|
||||||
history.remove(i);
|
history.remove(i);
|
||||||
}
|
}
|
||||||
historical_position += 1;
|
historical_position += 1;*/
|
||||||
load_page = true;
|
load_page = true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -351,11 +292,11 @@ fn main() {
|
|||||||
} else {
|
} else {
|
||||||
println!("Invalid reference id");
|
println!("Invalid reference id");
|
||||||
}
|
}
|
||||||
}
|
}/*
|
||||||
else if let Ok(parsed_value) = parse_url(user_input, &url.hostname) {
|
else if let Ok(parsed_value) = parse_url(user_input, &url.hostname) {
|
||||||
url = parsed_value;
|
url = parsed_value;
|
||||||
load_page = true;
|
load_page = true;
|
||||||
}
|
}*/
|
||||||
else {
|
else {
|
||||||
println!("Invalid input");
|
println!("Invalid input");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user