···2121}
22222323impl PageAssets {
2424+ /// Add an image to the page assets, causing the file to be created in the output directory.
2525+ ///
2626+ /// The image will not automatically be included in the page, but can be included through the `.url()` method on the returned `Image` object.
2727+ ///
2828+ /// 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.
2429 pub fn add_image<P>(&mut self, image_path: P) -> Image
2530 where
2631 P: Into<PathBuf>,
···3540 *image
3641 }
37424343+ /// Add a script to the page assets, causing the file to be created in the output directory.
4444+ ///
4545+ /// The script will not automatically be included in the page, but can be included through the `.url()` method on the returned `Script` object.
4646+ /// Alternatively, a script can be included automatically using the [PageAssets::include_script] method instead.
4747+ ///
4848+ /// 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.
3849 pub fn add_script<P>(&mut self, script_path: P) -> Script
3950 where
4051 P: Into<PathBuf>,
···4960 script
5061 }
51626363+ /// Include a script in the page
6464+ ///
6565+ /// 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.
6666+ ///
6767+ /// Subsequent calls to this function using the same path will result in the same script being included multiple times.
5268 pub fn include_script<P>(&mut self, script_path: P)
5369 where
5470 P: Into<PathBuf>,
···6278 self.included_scripts.push(script);
6379 }
64808181+ /// Add a style to the page assets, causing the file to be created in the output directory.
8282+ ///
8383+ /// The style will not automatically be included in the page, but can be included through the `.url()` method on the returned `Style` object.
8484+ ///
8585+ /// 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.
6586 pub fn add_style<P>(&mut self, style_path: P, tailwind: bool) -> Style
6687 where
6788 P: Into<PathBuf>,
···7899 style
79100 }
80101102102+ /// Include a style in the page
103103+ ///
104104+ /// 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.
105105+ ///
106106+ /// Subsequent calls to this function using the same path will result in the same style being included multiple times.
81107 pub fn include_style<P>(&mut self, style_path: P, tailwind: bool)
82108 where
83109 P: Into<PathBuf>,
···241267 [0; 8]
242268 }
243269}
270270+271271+#[cfg(test)]
272272+mod tests {
273273+ use super::*;
274274+275275+ #[test]
276276+ fn test_add_style() {
277277+ let mut page_assets = PageAssets {
278278+ assets_dir: PathBuf::from("assets"),
279279+ tailwind_path: PathBuf::from("tailwind"),
280280+ ..Default::default()
281281+ };
282282+283283+ page_assets.add_style("style.css", false);
284284+285285+ assert!(page_assets.styles.len() == 1);
286286+ }
287287+288288+ #[test]
289289+ fn test_include_style() {
290290+ let mut page_assets = PageAssets {
291291+ assets_dir: PathBuf::from("assets"),
292292+ tailwind_path: PathBuf::from("tailwind"),
293293+ ..Default::default()
294294+ };
295295+296296+ page_assets.include_style("style.css", false);
297297+298298+ assert!(page_assets.styles.len() == 1);
299299+ assert!(page_assets.included_styles.len() == 1);
300300+ }
301301+302302+ #[test]
303303+ fn test_add_script() {
304304+ let mut page_assets = PageAssets {
305305+ assets_dir: PathBuf::from("assets"),
306306+ tailwind_path: PathBuf::from("tailwind"),
307307+ ..Default::default()
308308+ };
309309+310310+ page_assets.add_script("script.js");
311311+ assert!(page_assets.scripts.len() == 1);
312312+ }
313313+314314+ #[test]
315315+ fn test_include_script() {
316316+ let mut page_assets = PageAssets {
317317+ assets_dir: PathBuf::from("assets"),
318318+ tailwind_path: PathBuf::from("tailwind"),
319319+ ..Default::default()
320320+ };
321321+322322+ page_assets.include_script("script.js");
323323+324324+ assert!(page_assets.scripts.len() == 1);
325325+ assert!(page_assets.included_scripts.len() == 1);
326326+ }
327327+328328+ #[test]
329329+ fn test_add_image() {
330330+ let mut page_assets = PageAssets {
331331+ assets_dir: PathBuf::from("assets"),
332332+ tailwind_path: PathBuf::from("tailwind"),
333333+ ..Default::default()
334334+ };
335335+336336+ page_assets.add_image("image.png");
337337+ assert!(page_assets.assets.len() == 1);
338338+ }
339339+340340+ #[test]
341341+ fn test_asset_has_leading_slash() {
342342+ let mut page_assets = PageAssets {
343343+ assets_dir: PathBuf::from("assets"),
344344+ tailwind_path: PathBuf::from("tailwind"),
345345+ ..Default::default()
346346+ };
347347+348348+ let image = page_assets.add_image("image.png");
349349+ assert_eq!(image.url().unwrap().chars().next(), Some('/'));
350350+351351+ let script = page_assets.add_script("script.js");
352352+ assert_eq!(script.url().unwrap().chars().next(), Some('/'));
353353+354354+ let style = page_assets.add_style("style.css", false);
355355+ assert_eq!(style.url().unwrap().chars().next(), Some('/'));
356356+ }
357357+}