Cleaned up code
+ Ran clippy + Cleaned up comments + Bumped rocket version
This commit is contained in:
parent
6733076068
commit
aa20bd9d51
1138
Cargo.lock
generated
1138
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -7,7 +7,7 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = "0.4.2"
|
rocket = "0.4.5"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
14
src/main.rs
14
src/main.rs
@ -77,10 +77,7 @@ fn index() -> Template {
|
|||||||
let mut links: Vec<SiteFile> = Vec::new();
|
let mut links: Vec<SiteFile> = Vec::new();
|
||||||
|
|
||||||
// Get the links to display on the main page
|
// Get the links to display on the main page
|
||||||
match get_pages("static/raw_rst", &mut links) {
|
get_pages("static/raw_rst", &mut links).ok();
|
||||||
Err(_) => (),
|
|
||||||
Ok(_) => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
map.insert("links", links);
|
map.insert("links", links);
|
||||||
Template::render("index", &map)
|
Template::render("index", &map)
|
||||||
@ -143,7 +140,6 @@ fn get_pages(path: &str, pages: &mut Vec<SiteFile>) -> io::Result<()> {
|
|||||||
/// Gets a page matching `page_name` in directory `path`
|
/// Gets a page matching `page_name` in directory `path`
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
|
||||||
/// * `path` - path to search in
|
/// * `path` - path to search in
|
||||||
/// * `page_name` - file to look for
|
/// * `page_name` - file to look for
|
||||||
fn get_page(path: &Path) -> PageResult<SiteFile> {
|
fn get_page(path: &Path) -> PageResult<SiteFile> {
|
||||||
@ -153,7 +149,7 @@ fn get_page(path: &Path) -> PageResult<SiteFile> {
|
|||||||
return Ok(SiteFile {
|
return Ok(SiteFile {
|
||||||
rank: 0,
|
rank: 0,
|
||||||
file_name: file_name.clone(),
|
file_name: file_name.clone(),
|
||||||
link_name: file_name.clone(),
|
link_name: file_name,
|
||||||
path: path.to_path_buf()
|
path: path.to_path_buf()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -182,13 +178,12 @@ fn get_page(path: &Path) -> PageResult<SiteFile> {
|
|||||||
fn error_page(page: &str) -> Template {
|
fn error_page(page: &str) -> Template {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert("error_page", page);
|
map.insert("error_page", page);
|
||||||
return Template::render("404", map);
|
Template::render("404", map)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a rendered template of a raw rst page if it exists
|
/// Returns a rendered template of a raw rst page if it exists
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
|
||||||
/// * `page` - path to page
|
/// * `page` - path to page
|
||||||
#[get("/about/<page..>")]
|
#[get("/about/<page..>")]
|
||||||
fn rst_page(page: PathBuf) -> Template {
|
fn rst_page(page: PathBuf) -> Template {
|
||||||
@ -220,7 +215,7 @@ fn rst_page(page: PathBuf) -> Template {
|
|||||||
};
|
};
|
||||||
|
|
||||||
map.insert("page_data", page_data);
|
map.insert("page_data", page_data);
|
||||||
return Template::render("listing", &map);
|
Template::render("listing", &map)
|
||||||
} else {
|
} else {
|
||||||
// Else, render the RST page
|
// Else, render the RST page
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
@ -249,7 +244,6 @@ fn rst_page(page: PathBuf) -> Template {
|
|||||||
/// Catches 404 errors and displays an error message
|
/// Catches 404 errors and displays an error message
|
||||||
///
|
///
|
||||||
/// #Arguments
|
/// #Arguments
|
||||||
///
|
|
||||||
/// * `req` - information on the original request
|
/// * `req` - information on the original request
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found(req: &Request<'_>) -> Template {
|
fn not_found(req: &Request<'_>) -> Template {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use regex::Regex;
|
use regex::{Regex, Captures};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// Renders RST links as HTML links
|
/// Renders RST links as HTML links
|
||||||
@ -7,28 +7,26 @@ use std::collections::HashMap;
|
|||||||
///
|
///
|
||||||
/// * `string` - input RST string
|
/// * `string` - input RST string
|
||||||
///
|
///
|
||||||
pub fn parse_links(string: &String) -> String {
|
pub fn parse_links(string: &str) -> String {
|
||||||
let re_link_ref = Regex::new(r"\n?.. _(.*): (.*)\n").unwrap();
|
let re_link_ref = Regex::new(r"\n?.. _(.*): (.*)\n").unwrap();
|
||||||
let mut link_map: HashMap<String, String> = HashMap::new();
|
let mut link_map: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
for cap in re_link_ref.captures_iter(string.as_str()) {
|
for cap in re_link_ref.captures_iter(string) {
|
||||||
link_map.insert(String::from(&cap[1]), String::from(&cap[2]));
|
link_map.insert(String::from(&cap[1]), String::from(&cap[2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut output: String = re_link_ref.replace_all(string, "").to_string();
|
let text: String = re_link_ref.replace_all(string, "").to_string();
|
||||||
|
|
||||||
let re_link = Regex::new(r"`(.*)`_").unwrap();
|
let re_link = Regex::new(r"`(.*)`_").unwrap();
|
||||||
for cap in re_link.captures_iter(output.clone().as_ref()) {
|
|
||||||
|
let output = re_link.replace_all(&text, |cap: &Captures| {
|
||||||
let link = match link_map.get(&cap[1]) {
|
let link = match link_map.get(&cap[1]) {
|
||||||
None => String::from(""),
|
None => String::from(""),
|
||||||
Some(link) => link.to_owned(),
|
Some(link) => link.to_owned(),
|
||||||
};
|
};
|
||||||
|
|
||||||
output = output.replace(
|
format!("<a class=\"link\" href=\"{}\">{}</a>", link, &cap[1])
|
||||||
&cap[0],
|
});
|
||||||
format!("<a class=\"link\" href=\"{}\">{}</a>", link, &cap[1]).as_str(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
output
|
output.to_string()
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use super::*;
|
use super::rst_parser::parse_links;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_link_parser() {
|
fn test_link_parser() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user