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]
|
||||
colored = "2.2.0"
|
||||
regex = "1.11.1"
|
||||
url = "2.5.4"
|
||||
termion = "4.0.3"
|
||||
open = "5.3.2"
|
||||
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 colored::Colorize;
|
||||
use regex::Regex;
|
||||
|
||||
struct Url {
|
||||
protocol: String,
|
||||
hostname: String,
|
||||
port: u16,
|
||||
path: String,
|
||||
}
|
||||
use url::{Url, ParseError};
|
||||
|
||||
fn clear_screen() {
|
||||
Command::new("clear")
|
||||
.status()
|
||||
.expect("Failed to clear screen");
|
||||
//println!("clearing");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
fn fetch_page(host: &String, port: u16, path: &String) -> String {
|
||||
let full_url_formatted = format!("{}:{}{}", host, port, path);
|
||||
fn fetch_page(url: &Url) -> String {
|
||||
let full_url_formatted = format!("{}", url);
|
||||
|
||||
// Call curl using Com, mand
|
||||
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();
|
||||
let mut content = fetch_page(&host, port, &path);
|
||||
let mut content = fetch_page(&url);
|
||||
let mut links = Vec::new();
|
||||
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 {
|
||||
print!("—");
|
||||
}
|
||||
print!("{}:{}{}\n", host, port, path);
|
||||
print!("{}\n", url);
|
||||
for _i in 0..screen_width {
|
||||
print!("—");
|
||||
}
|
||||
@ -202,59 +195,28 @@ fn input() -> String{
|
||||
return s;
|
||||
}
|
||||
|
||||
fn parse_url(user_input: String, previous_host: &String) -> Result<Url, u8> {
|
||||
let mut url = Url {
|
||||
protocol: "internal".to_string(),
|
||||
hostname: "home".to_string(),
|
||||
port: 0,
|
||||
path: "/".to_string(),
|
||||
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");
|
||||
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 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 mut url: Url = Url::parse(&user_input).unwrap();
|
||||
if url.port() == None {
|
||||
url.set_port(Some(3477));
|
||||
}
|
||||
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() {
|
||||
@ -269,28 +231,12 @@ fn main() {
|
||||
let mut history: Vec<Url> = Vec::new();
|
||||
let mut historical_position: usize = 0;
|
||||
let mut links: Vec<String> = Vec::new();
|
||||
let mut url = Url {
|
||||
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(),
|
||||
});
|
||||
if let Ok(mut url) = parse_url(user_input, "http://deadvey.com".to_string()) { // Change this and make internal pages ;)
|
||||
'mainloop: loop {
|
||||
if load_page {
|
||||
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");
|
||||
//for i in 0..history.len() {
|
||||
// println!("{}://{}:{}/{}",history[i].protocol,history[i].hostname, history[i].port, history[i].path);
|
||||
//}
|
||||
}
|
||||
load_page = false;
|
||||
|
||||
@ -301,7 +247,7 @@ fn main() {
|
||||
else if user_input == "r" {
|
||||
load_page = true;
|
||||
continue;
|
||||
}
|
||||
}/*
|
||||
else if user_input == "i" {
|
||||
url.path = "/".to_string();
|
||||
load_page = true;
|
||||
@ -317,7 +263,7 @@ fn main() {
|
||||
println!("Invalid url");
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
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");
|
||||
}
|
||||
@ -331,18 +277,13 @@ 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.hostname) {
|
||||
if let Ok(parsed_value) = parse_url(links[number].clone(), url.as_str().to_string()) {
|
||||
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 {
|
||||
history.remove(i);
|
||||
}
|
||||
historical_position += 1;
|
||||
historical_position += 1;*/
|
||||
load_page = true;
|
||||
}
|
||||
else {
|
||||
@ -351,11 +292,11 @@ fn main() {
|
||||
} else {
|
||||
println!("Invalid reference id");
|
||||
}
|
||||
}
|
||||
}/*
|
||||
else if let Ok(parsed_value) = parse_url(user_input, &url.hostname) {
|
||||
url = parsed_value;
|
||||
load_page = true;
|
||||
}
|
||||
}*/
|
||||
else {
|
||||
println!("Invalid input");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user