diff --git a/components/content/src/library.rs b/components/content/src/library.rs index 9b0dac00..83d1a523 100644 --- a/components/content/src/library.rs +++ b/components/content/src/library.rs @@ -9,7 +9,7 @@ use crate::sorting::sort_pages; use crate::taxonomies::{find_taxonomies, Taxonomy}; use crate::{Page, Section, SortBy}; -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Library { pub pages: AHashMap, pub sections: AHashMap, @@ -21,13 +21,7 @@ pub struct Library { impl Library { pub fn new() -> Self { - Self { - pages: AHashMap::new(), - sections: AHashMap::new(), - taxonomies: Vec::new(), - reverse_aliases: AHashMap::new(), - translations: AHashMap::new(), - } + Self::default() } fn insert_reverse_aliases(&mut self, file_path: &Path, entries: Vec) { diff --git a/components/content/src/section.rs b/components/content/src/section.rs index 0d422ca2..4dadfc20 100644 --- a/components/content/src/section.rs +++ b/components/content/src/section.rs @@ -182,7 +182,7 @@ impl Section { context.insert("config", &config.serialize(&self.lang)); context.insert("current_url", &self.permalink); context.insert("current_path", &self.path); - context.insert("section", &SerializingSection::new(&self, SectionSerMode::Full(library))); + context.insert("section", &SerializingSection::new(self, SectionSerMode::Full(library))); context.insert("lang", &self.lang); render_template(tpl_name, tera, context, &config.theme) diff --git a/components/markdown/benches/all.rs b/components/markdown/benches/all.rs index fc3e08fe..57fc1eaa 100644 --- a/components/markdown/benches/all.rs +++ b/components/markdown/benches/all.rs @@ -4,9 +4,9 @@ extern crate test; use std::collections::HashMap; use config::Config; -use front_matter::InsertAnchor; +use utils::types::InsertAnchor; use libs::tera::Tera; -use rendering::{render_content, RenderContext}; +use markdown::{render_content, RenderContext}; static CONTENT: &str = r#" # Modus cognitius profanam ne duae virtutis mundi @@ -85,10 +85,10 @@ fn bench_render_content_with_highlighting(b: &mut test::Bencher) { let mut tera = Tera::default(); tera.add_raw_template("shortcodes/youtube.html", "{{id}}").unwrap(); let permalinks_ctx = HashMap::new(); - let mut config = Config::default(); + let mut config = Config::default_for_test(); config.markdown.highlight_code = true; let current_page_permalink = ""; - let context = RenderContext::new( + let mut context = RenderContext::new( &tera, &config, &config.default_language, @@ -96,6 +96,8 @@ fn bench_render_content_with_highlighting(b: &mut test::Bencher) { &permalinks_ctx, InsertAnchor::None, ); + let shortcode_def = utils::templates::get_shortcodes(&tera); + context.set_shortcode_definitions(&shortcode_def); b.iter(|| render_content(CONTENT, &context).unwrap()); } @@ -104,10 +106,10 @@ fn bench_render_content_without_highlighting(b: &mut test::Bencher) { let mut tera = Tera::default(); tera.add_raw_template("shortcodes/youtube.html", "{{id}}").unwrap(); let permalinks_ctx = HashMap::new(); - let mut config = Config::default(); + let mut config = Config::default_for_test(); config.markdown.highlight_code = false; let current_page_permalink = ""; - let context = RenderContext::new( + let mut context = RenderContext::new( &tera, &config, &config.default_language, @@ -115,6 +117,8 @@ fn bench_render_content_without_highlighting(b: &mut test::Bencher) { &permalinks_ctx, InsertAnchor::None, ); + let shortcode_def = utils::templates::get_shortcodes(&tera); + context.set_shortcode_definitions(&shortcode_def); b.iter(|| render_content(CONTENT, &context).unwrap()); } @@ -122,7 +126,7 @@ fn bench_render_content_without_highlighting(b: &mut test::Bencher) { fn bench_render_content_no_shortcode(b: &mut test::Bencher) { let tera = Tera::default(); let content2 = CONTENT.replace(r#"{{ youtube(id="my_youtube_id") }}"#, ""); - let mut config = Config::default(); + let mut config = Config::default_for_test(); config.markdown.highlight_code = false; let permalinks_ctx = HashMap::new(); let current_page_permalink = ""; @@ -142,7 +146,7 @@ fn bench_render_content_no_shortcode(b: &mut test::Bencher) { fn bench_render_content_with_emoji(b: &mut test::Bencher) { let tera = Tera::default(); let content2 = CONTENT.replace(r#"{{ youtube(id="my_youtube_id") }}"#, ""); - let mut config = Config::default(); + let mut config = Config::default_for_test(); config.markdown.highlight_code = false; config.markdown.render_emoji = true; let permalinks_ctx = HashMap::new(); diff --git a/components/markdown/src/markdown.rs b/components/markdown/src/markdown.rs index 78ecebca..b602c49f 100644 --- a/components/markdown/src/markdown.rs +++ b/components/markdown/src/markdown.rs @@ -12,7 +12,6 @@ use utils::site::resolve_internal_link; use utils::slugs::slugify_anchors; use utils::table_of_contents::{make_table_of_contents, Heading}; use utils::types::InsertAnchor; -use utils::vec::InsertMany; use self::cmark::{Event, LinkType, Options, Parser, Tag}; use crate::codeblock::{CodeBlock, FenceSettings}; @@ -22,6 +21,27 @@ const CONTINUE_READING: &str = ""; const ANCHOR_LINK_TEMPLATE: &str = "anchor-link.html"; static EMOJI_REPLACER: Lazy = Lazy::new(EmojiReplacer::new); + +/// Efficiently insert multiple element in their specified index. +/// The elements should sorted in ascending order by their index. +/// +/// This is done in O(n) time. +fn insert_many(input: &mut Vec, elem_to_insert: Vec<(usize, T)>) { + let mut inserted = vec![]; + let mut last_idx = 0; + + for (idx, elem) in elem_to_insert.into_iter() { + let head_len = idx - last_idx; + inserted.extend(input.splice(0..head_len, std::iter::empty())); + inserted.push(elem); + last_idx = idx; + } + let len = input.len(); + inserted.extend(input.drain(0..len)); + + *input = inserted; +} + #[derive(Debug)] pub struct Rendered { pub body: String, @@ -490,7 +510,7 @@ pub fn markdown_to_html( } if context.insert_anchor != InsertAnchor::None { - events.insert_many(anchors_to_insert); + insert_many(&mut events, anchors_to_insert); } cmark::html::push_html(&mut html, events.into_iter()); @@ -512,6 +532,17 @@ pub fn markdown_to_html( #[cfg(test)] mod tests { use super::*; + #[test] + + fn insert_many_works() { + let mut v = vec![1, 2, 3, 4, 5]; + insert_many(&mut v, vec![(0, 0), (2, -1), (5, 6)]); + assert_eq!(v, &[0, 1, 2, -1, 3, 4, 5, 6]); + + let mut v2 = vec![1, 2, 3, 4, 5]; + insert_many(&mut v2, vec![(0, 0), (2, -1)]); + assert_eq!(v2, &[0, 1, 2, -1, 3, 4, 5]); + } #[test] fn test_is_external_link() { diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index c1d6dad9..97ddfe58 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -3,6 +3,7 @@ pub mod link_checking; pub mod sass; pub mod sitemap; pub mod tpls; +mod minify; use std::collections::HashMap; use std::fs::remove_dir_all; @@ -23,7 +24,6 @@ use templates::{load_tera, render_redirect_template}; use utils::fs::{ copy_directory, copy_file_if_needed, create_directory, create_file, ensure_directory_exists, }; -use utils::minify; use utils::net::get_available_port; use utils::templates::{render_template, ShortcodeDefinition}; use utils::types::InsertAnchor; diff --git a/components/site/src/link_checking.rs b/components/site/src/link_checking.rs index d9157b12..070f699f 100644 --- a/components/site/src/link_checking.rs +++ b/components/site/src/link_checking.rs @@ -96,7 +96,7 @@ pub fn check_internal_links_with_anchors(site: &Site) -> Result<()> { } } -fn should_skip_by_prefix(link: &String, skip_prefixes: &Vec) -> bool { +fn should_skip_by_prefix(link: &str, skip_prefixes: &[String]) -> bool { skip_prefixes.iter().any(|prefix| link.starts_with(prefix)) } @@ -161,7 +161,7 @@ pub fn check_external_links(site: &Site) -> Result<()> { for link in checked_links.iter() { links_by_domain.entry(link.domain.to_string()).or_default(); // Insert content path and link under the domain key - links_by_domain.get_mut(&link.domain).unwrap().push(&link); + links_by_domain.get_mut(&link.domain).unwrap().push(link); } if checked_links.is_empty() { diff --git a/components/utils/src/minify.rs b/components/site/src/minify.rs similarity index 100% rename from components/utils/src/minify.rs rename to components/site/src/minify.rs diff --git a/components/utils/src/lib.rs b/components/utils/src/lib.rs index 44f24e5a..974bf043 100644 --- a/components/utils/src/lib.rs +++ b/components/utils/src/lib.rs @@ -1,11 +1,9 @@ pub mod de; pub mod fs; pub mod links; -pub mod minify; pub mod net; pub mod site; pub mod slugs; pub mod table_of_contents; pub mod templates; pub mod types; -pub mod vec; diff --git a/components/utils/src/site.rs b/components/utils/src/site.rs index d09e8833..21f26b3b 100644 --- a/components/utils/src/site.rs +++ b/components/utils/src/site.rs @@ -1,6 +1,5 @@ use libs::percent_encoding::percent_decode; use std::collections::HashMap; -use std::hash::BuildHasher; use errors::{anyhow, Result}; @@ -19,9 +18,9 @@ pub struct ResolvedInternalLink { /// Resolves an internal link (of the `@/posts/something.md#hey` sort) to its absolute link and /// returns the path + anchor as well -pub fn resolve_internal_link( +pub fn resolve_internal_link( link: &str, - permalinks: &HashMap, + permalinks: &HashMap, ) -> Result { // First we remove the ./ since that's zola specific let clean_link = link.replacen("@/", "", 1); diff --git a/components/utils/src/vec.rs b/components/utils/src/vec.rs deleted file mode 100644 index 346769c1..00000000 --- a/components/utils/src/vec.rs +++ /dev/null @@ -1,44 +0,0 @@ -pub trait InsertMany { - type Element; - fn insert_many(&mut self, elem_to_insert: Vec<(usize, Self::Element)>); -} - -impl InsertMany for Vec { - type Element = T; - - /// Efficiently insert multiple element in their specified index. - /// The elements should sorted in ascending order by their index. - /// - /// This is done in O(n) time. - fn insert_many(&mut self, elem_to_insert: Vec<(usize, T)>) { - let mut inserted = vec![]; - let mut last_idx = 0; - - for (idx, elem) in elem_to_insert.into_iter() { - let head_len = idx - last_idx; - inserted.extend(self.splice(0..head_len, std::iter::empty())); - inserted.push(elem); - last_idx = idx; - } - let len = self.len(); - inserted.extend(self.drain(0..len)); - - *self = inserted; - } -} - -#[cfg(test)] -mod test { - use super::InsertMany; - - #[test] - fn insert_many_works() { - let mut v = vec![1, 2, 3, 4, 5]; - v.insert_many(vec![(0, 0), (2, -1), (5, 6)]); - assert_eq!(v, &[0, 1, 2, -1, 3, 4, 5, 6]); - - let mut v2 = vec![1, 2, 3, 4, 5]; - v2.insert_many(vec![(0, 0), (2, -1)]); - assert_eq!(v2, &[0, 1, 2, -1, 3, 4, 5]); - } -} diff --git a/src/main.rs b/src/main.rs index 6cc26956..ec3bcaa5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod cmd; mod console; mod prompt; -fn get_config_file_path(dir: &PathBuf, config_path: &Path) -> (PathBuf, PathBuf) { +fn get_config_file_path(dir: &Path, config_path: &Path) -> (PathBuf, PathBuf) { let root_dir = dir .ancestors() .find_map(|a| if a.join(&config_path).exists() { Some(a) } else { None })