feat: add pretty links

master
ana 11 months ago
parent 2d4ff4481f
commit 8c5217f123
  1. 1
      fixtures/pretty_links/hi.html
  2. 1
      fixtures/pretty_links/subfolder/hello.html
  3. 6
      shtola/CHANGELOG.md
  4. 2
      shtola/src/plugins/mod.rs
  5. 46
      shtola/src/plugins/pretty_links.rs

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.4.0] - 2021-11-10
### Added
- Added a plugin to rewrite HTML files into `<filename>/index.html` paths.
## [0.3.0] - 2021-11-09
### Added

@ -3,3 +3,5 @@ pub mod markdown;
#[cfg(feature = "tera_layouts")]
pub mod tera_layouts;
pub mod pretty_links;

@ -0,0 +1,46 @@
use std::path::Path;
use crate::log::{debug, info};
use crate::{Plugin, RefIR};
pub fn plugin() -> Plugin {
Box::new(|mut ir: RefIR| {
info!("Prettifying links...");
let files = ir.files.clone();
let html_files = files.iter().filter(|(p, _)| match p.extension() {
Some(ext) => ext == "html",
None => false,
});
for (path, file) in html_files {
let basename = path.file_stem().unwrap();
let mut new_path = match path.parent() {
Some(parent) => parent,
None => Path::new(""),
}
.to_path_buf();
new_path.push(basename);
new_path.push(path.file_name().unwrap());
new_path.set_file_name("index.html");
debug!("{:?} -> {:?}", path, new_path);
ir.files.remove(&path.to_path_buf());
ir.files.insert(new_path, file.clone());
}
})
}
#[test]
fn it_works() {
use crate::Shtola;
use std::path::PathBuf;
let mut s = Shtola::new();
s.source("../fixtures/pretty_links");
s.destination("../fixtures/dest_pretty_links");
s.clean(true);
s.register(plugin());
let r = s.build().unwrap();
assert!(r.files.contains_key(&PathBuf::from("hi/index.html")));
assert!(r
.files
.contains_key(&PathBuf::from("subfolder/hello/index.html")));
}
Loading…
Cancel
Save