Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: tests

+162 -18
+114
crates/framework/src/assets.rs
··· 21 21 } 22 22 23 23 impl PageAssets { 24 + /// Add an image to the page assets, causing the file to be created in the output directory. 25 + /// 26 + /// The image will not automatically be included in the page, but can be included through the `.url()` method on the returned `Image` object. 27 + /// 28 + /// Subsequent calls to this function using the same path will return the same image, as such, the value returned by this function can be cloned and used multiple times without issue. 24 29 pub fn add_image<P>(&mut self, image_path: P) -> Image 25 30 where 26 31 P: Into<PathBuf>, ··· 35 40 *image 36 41 } 37 42 43 + /// Add a script to the page assets, causing the file to be created in the output directory. 44 + /// 45 + /// The script will not automatically be included in the page, but can be included through the `.url()` method on the returned `Script` object. 46 + /// Alternatively, a script can be included automatically using the [PageAssets::include_script] method instead. 47 + /// 48 + /// Subsequent calls to this function using the same path will return the same script, as such, the value returned by this function can be cloned and used multiple times without issue. 38 49 pub fn add_script<P>(&mut self, script_path: P) -> Script 39 50 where 40 51 P: Into<PathBuf>, ··· 49 60 script 50 61 } 51 62 63 + /// Include a script in the page 64 + /// 65 + /// This method will automatically include the script in the `<head>` of the page, if it exists. If the page does not include a `<head>` tag, at this time this method will silently fail. 66 + /// 67 + /// Subsequent calls to this function using the same path will result in the same script being included multiple times. 52 68 pub fn include_script<P>(&mut self, script_path: P) 53 69 where 54 70 P: Into<PathBuf>, ··· 62 78 self.included_scripts.push(script); 63 79 } 64 80 81 + /// Add a style to the page assets, causing the file to be created in the output directory. 82 + /// 83 + /// The style will not automatically be included in the page, but can be included through the `.url()` method on the returned `Style` object. 84 + /// 85 + /// Subsequent calls to this function using the same path will return the same style, as such, the value returned by this function can be cloned and used multiple times without issue. 65 86 pub fn add_style<P>(&mut self, style_path: P, tailwind: bool) -> Style 66 87 where 67 88 P: Into<PathBuf>, ··· 78 99 style 79 100 } 80 101 102 + /// Include a style in the page 103 + /// 104 + /// This method will automatically include the style in the `<head>` of the page, if it exists. If the page does not include a `<head>` tag, at this time this method will silently fail. 105 + /// 106 + /// Subsequent calls to this function using the same path will result in the same style being included multiple times. 81 107 pub fn include_style<P>(&mut self, style_path: P, tailwind: bool) 82 108 where 83 109 P: Into<PathBuf>, ··· 241 267 [0; 8] 242 268 } 243 269 } 270 + 271 + #[cfg(test)] 272 + mod tests { 273 + use super::*; 274 + 275 + #[test] 276 + fn test_add_style() { 277 + let mut page_assets = PageAssets { 278 + assets_dir: PathBuf::from("assets"), 279 + tailwind_path: PathBuf::from("tailwind"), 280 + ..Default::default() 281 + }; 282 + 283 + page_assets.add_style("style.css", false); 284 + 285 + assert!(page_assets.styles.len() == 1); 286 + } 287 + 288 + #[test] 289 + fn test_include_style() { 290 + let mut page_assets = PageAssets { 291 + assets_dir: PathBuf::from("assets"), 292 + tailwind_path: PathBuf::from("tailwind"), 293 + ..Default::default() 294 + }; 295 + 296 + page_assets.include_style("style.css", false); 297 + 298 + assert!(page_assets.styles.len() == 1); 299 + assert!(page_assets.included_styles.len() == 1); 300 + } 301 + 302 + #[test] 303 + fn test_add_script() { 304 + let mut page_assets = PageAssets { 305 + assets_dir: PathBuf::from("assets"), 306 + tailwind_path: PathBuf::from("tailwind"), 307 + ..Default::default() 308 + }; 309 + 310 + page_assets.add_script("script.js"); 311 + assert!(page_assets.scripts.len() == 1); 312 + } 313 + 314 + #[test] 315 + fn test_include_script() { 316 + let mut page_assets = PageAssets { 317 + assets_dir: PathBuf::from("assets"), 318 + tailwind_path: PathBuf::from("tailwind"), 319 + ..Default::default() 320 + }; 321 + 322 + page_assets.include_script("script.js"); 323 + 324 + assert!(page_assets.scripts.len() == 1); 325 + assert!(page_assets.included_scripts.len() == 1); 326 + } 327 + 328 + #[test] 329 + fn test_add_image() { 330 + let mut page_assets = PageAssets { 331 + assets_dir: PathBuf::from("assets"), 332 + tailwind_path: PathBuf::from("tailwind"), 333 + ..Default::default() 334 + }; 335 + 336 + page_assets.add_image("image.png"); 337 + assert!(page_assets.assets.len() == 1); 338 + } 339 + 340 + #[test] 341 + fn test_asset_has_leading_slash() { 342 + let mut page_assets = PageAssets { 343 + assets_dir: PathBuf::from("assets"), 344 + tailwind_path: PathBuf::from("tailwind"), 345 + ..Default::default() 346 + }; 347 + 348 + let image = page_assets.add_image("image.png"); 349 + assert_eq!(image.url().unwrap().chars().next(), Some('/')); 350 + 351 + let script = page_assets.add_script("script.js"); 352 + assert_eq!(script.url().unwrap().chars().next(), Some('/')); 353 + 354 + let style = page_assets.add_style("style.css", false); 355 + assert_eq!(style.url().unwrap().chars().next(), Some('/')); 356 + } 357 + }
+2
crates/framework/src/content.rs
··· 37 37 /// 38 38 /// ## Expand 39 39 /// ```rust 40 + /// use maudit::content::{markdown_entry}; 41 + /// 40 42 /// #[markdown_entry] 41 43 /// pub struct Article { 42 44 /// pub title: String,
+46 -18
crates/framework/src/lib.rs
··· 65 65 /// ```rust 66 66 /// use maudit::{ 67 67 /// content_sources, coronate, routes, BuildOptions, BuildOutput, 68 - /// } 69 - /// use crate::pages::{Index, Article}; 68 + /// }; 69 + /// 70 + /// # mod pages { 71 + /// # use maudit::page::prelude::*; 72 + /// # 73 + /// # #[route("/")] 74 + /// # pub struct Index; 75 + /// # impl Page<String> for Index { 76 + /// # fn render(&self, _ctx: &mut RouteContext) -> String { 77 + /// # "Hello, world!".to_string() 78 + /// # } 79 + /// # } 80 + /// # #[route("/article")] 81 + /// # pub struct Article; 82 + /// # 83 + /// # impl Page<String> for Article { 84 + /// # fn render(&self, _ctx: &mut RouteContext) -> String { 85 + /// # "Hello, world!".to_string() 86 + /// # } 87 + /// # } 88 + /// # } 70 89 /// 71 90 /// fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> { 72 91 /// coronate( ··· 77 96 /// } 78 97 /// ``` 79 98 /// 80 - /// ## Expand 81 - /// ```rust 82 - /// routes![pages::Index, pages::Article] 83 - /// ``` 84 - /// expands to 85 - /// ```rust 86 - /// &[ 87 - /// &pages::Index, 88 - /// &pages::Article, 89 - /// ] 90 - /// ``` 91 - /// 92 99 macro_rules! routes { 93 100 [$($route:path),*] => { 94 101 &[$(&$route),*] ··· 101 108 /// ```rust 102 109 /// use maudit::{ 103 110 /// content_sources, coronate, routes, BuildOptions, BuildOutput, 111 + /// }; 112 + /// use maudit::content::{glob_markdown, markdown_entry}; 113 + /// 114 + /// #[markdown_entry] 115 + /// pub struct ArticleContent { 116 + /// pub title: String, 117 + /// pub description: String, 104 118 /// } 105 - /// use crate::content::ArticleContent; 106 119 /// 107 120 /// fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> { 108 121 /// coronate( ··· 117 130 /// 118 131 /// ## Expand 119 132 /// ```rust 133 + /// # use maudit::{content_sources}; 134 + /// # use maudit::content::{glob_markdown, markdown_entry}; 135 + /// # #[markdown_entry] 136 + /// # pub struct ArticleContent { 137 + /// # pub title: String, 138 + /// # pub description: String, 139 + /// # } 140 + /// 120 141 /// content_sources![ 121 142 /// "articles" => glob_markdown::<ArticleContent>("content/articles/*.md") 122 - /// ] 143 + /// ]; 123 144 /// ``` 124 145 /// expands to 125 146 /// ```rust 147 + /// # use maudit::content::{glob_markdown, markdown_entry}; 148 + /// # #[markdown_entry] 149 + /// # pub struct ArticleContent { 150 + /// # pub title: String, 151 + /// # pub description: String, 152 + /// # } 153 + /// 126 154 /// maudit::content::ContentSources(vec![ 127 155 /// Box::new(maudit::content::ContentSource::new("articles", Box::new(move || glob_markdown::<ArticleContent>("content/articles/*.md")))) 128 - /// ]) 156 + /// ]); 129 157 #[macro_export] 130 158 macro_rules! content_sources { 131 159 ($($name:expr => $entries:expr),*) => { ··· 144 172 /// ```rust 145 173 /// use maudit::{ 146 174 /// content_sources, coronate, routes, BuildOptions, BuildOutput, 147 - /// } 175 + /// }; 148 176 /// 149 177 /// fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> { 150 178 /// coronate(