Cleaned up code

+ Ran clippy
+ Cleaned up comments
+ Bumped rocket version
This commit is contained in:
Joey Hines 2020-09-17 20:21:17 -05:00
parent 6733076068
commit aa20bd9d51
5 changed files with 697 additions and 479 deletions

1138
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.2"
rocket = "0.4.5"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

View File

@ -77,10 +77,7 @@ fn index() -> Template {
let mut links: Vec<SiteFile> = Vec::new();
// Get the links to display on the main page
match get_pages("static/raw_rst", &mut links) {
Err(_) => (),
Ok(_) => (),
}
get_pages("static/raw_rst", &mut links).ok();
map.insert("links", links);
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`
///
/// # Arguments
///
/// * `path` - path to search in
/// * `page_name` - file to look for
fn get_page(path: &Path) -> PageResult<SiteFile> {
@ -153,7 +149,7 @@ fn get_page(path: &Path) -> PageResult<SiteFile> {
return Ok(SiteFile {
rank: 0,
file_name: file_name.clone(),
link_name: file_name.clone(),
link_name: file_name,
path: path.to_path_buf()
})
}
@ -182,13 +178,12 @@ fn get_page(path: &Path) -> PageResult<SiteFile> {
fn error_page(page: &str) -> Template {
let mut map = HashMap::new();
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
///
/// # Arguments
///
/// * `page` - path to page
#[get("/about/<page..>")]
fn rst_page(page: PathBuf) -> Template {
@ -220,7 +215,7 @@ fn rst_page(page: PathBuf) -> Template {
};
map.insert("page_data", page_data);
return Template::render("listing", &map);
Template::render("listing", &map)
} else {
// Else, render the RST page
let mut map = HashMap::new();
@ -249,7 +244,6 @@ fn rst_page(page: PathBuf) -> Template {
/// Catches 404 errors and displays an error message
///
/// #Arguments
///
/// * `req` - information on the original request
#[catch(404)]
fn not_found(req: &Request<'_>) -> Template {

View File

@ -1,4 +1,4 @@
use regex::Regex;
use regex::{Regex, Captures};
use std::collections::HashMap;
/// Renders RST links as HTML links
@ -7,28 +7,26 @@ use std::collections::HashMap;
///
/// * `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 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]));
}
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();
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]) {
None => String::from(""),
Some(link) => link.to_owned(),
};
output = output.replace(
&cap[0],
format!("<a class=\"link\" href=\"{}\">{}</a>", link, &cap[1]).as_str(),
);
}
format!("<a class=\"link\" href=\"{}\">{}</a>", link, &cap[1])
});
output
output.to_string()
}

View File

@ -1,5 +1,5 @@
#[cfg(test)]
use super::*;
use super::rst_parser::parse_links;
#[test]
fn test_link_parser() {