2017-05-03 14:16:09 +00:00
|
|
|
use std::collections::HashMap;
|
2020-06-18 19:11:22 +00:00
|
|
|
use std::ffi::OsStr;
|
2019-01-27 17:57:07 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2020-05-23 09:46:50 +00:00
|
|
|
use std::{fs, io, result};
|
2018-10-18 15:32:30 +00:00
|
|
|
|
2020-06-09 20:38:29 +00:00
|
|
|
use sha2::{Digest, Sha256, Sha384, Sha512};
|
2020-06-18 09:36:11 +00:00
|
|
|
use svg_metadata as svg;
|
2020-06-18 19:11:22 +00:00
|
|
|
use tera::{from_value, to_value, Error, Function as TeraFn, Result, Value};
|
2017-05-03 14:16:09 +00:00
|
|
|
|
2017-08-07 11:38:13 +00:00
|
|
|
use config::Config;
|
2019-05-30 18:06:24 +00:00
|
|
|
use image::GenericImageView;
|
2019-06-02 18:21:06 +00:00
|
|
|
use library::{Library, Taxonomy};
|
|
|
|
use utils::site::resolve_internal_link;
|
2020-09-02 09:40:06 +00:00
|
|
|
use utils::slugs::{slugify_paths, SlugifyStrategy};
|
2018-10-29 19:13:09 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
2018-02-02 20:35:04 +00:00
|
|
|
|
2018-10-29 19:13:09 +00:00
|
|
|
mod load_data;
|
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
pub use self::load_data::LoadData;
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Trans {
|
|
|
|
config: Config,
|
|
|
|
}
|
|
|
|
impl Trans {
|
|
|
|
pub fn new(config: Config) -> Self {
|
2019-02-09 18:54:46 +00:00
|
|
|
Self { config }
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl TeraFn for Trans {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
2018-02-02 20:35:04 +00:00
|
|
|
let key = required_arg!(String, args.get("key"), "`trans` requires a `key` argument.");
|
2018-10-31 07:18:57 +00:00
|
|
|
let lang = optional_arg!(String, args.get("lang"), "`trans`: `lang` must be a string.")
|
2019-01-23 18:20:02 +00:00
|
|
|
.unwrap_or_else(|| self.config.default_language.clone());
|
2019-09-03 08:51:41 +00:00
|
|
|
|
2019-10-14 16:31:03 +00:00
|
|
|
let term = self
|
|
|
|
.config
|
|
|
|
.get_translation(lang, key)
|
|
|
|
.map_err(|e| Error::chain("Failed to retreive term translation", e))?;
|
2019-09-03 08:51:41 +00:00
|
|
|
|
|
|
|
Ok(to_value(term).unwrap())
|
2017-05-03 14:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 10:04:26 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetUrl {
|
|
|
|
config: Config,
|
|
|
|
permalinks: HashMap<String, String>,
|
2020-06-09 20:38:29 +00:00
|
|
|
search_paths: Vec<PathBuf>,
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
impl GetUrl {
|
2020-06-18 19:11:22 +00:00
|
|
|
pub fn new(
|
|
|
|
config: Config,
|
|
|
|
permalinks: HashMap<String, String>,
|
|
|
|
search_paths: Vec<PathBuf>,
|
|
|
|
) -> Self {
|
2020-06-09 20:38:29 +00:00
|
|
|
Self { config, permalinks, search_paths }
|
2017-05-23 11:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 17:23:17 +00:00
|
|
|
|
|
|
|
fn make_path_with_lang(path: String, lang: &str, config: &Config) -> Result<String> {
|
2020-06-18 19:11:22 +00:00
|
|
|
if lang == config.default_language {
|
2020-04-12 17:23:17 +00:00
|
|
|
return Ok(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.languages.iter().any(|x| x.code == lang) {
|
2020-04-22 08:07:17 +00:00
|
|
|
return Err(
|
|
|
|
format!("`{}` is not an authorized language (check config.languages).", lang).into()
|
|
|
|
);
|
2020-04-12 17:23:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 19:11:22 +00:00
|
|
|
let mut splitted_path: Vec<String> = path.split('.').map(String::from).collect();
|
2020-04-12 17:23:17 +00:00
|
|
|
let ilast = splitted_path.len() - 1;
|
|
|
|
splitted_path[ilast] = format!("{}.{}", lang, splitted_path[ilast]);
|
|
|
|
Ok(splitted_path.join("."))
|
|
|
|
}
|
|
|
|
|
2020-06-18 19:11:22 +00:00
|
|
|
fn open_file(search_paths: &[PathBuf], url: &str) -> result::Result<fs::File, io::Error> {
|
|
|
|
let cleaned_url = url.trim_start_matches("@/").trim_start_matches('/');
|
2020-06-09 20:38:29 +00:00
|
|
|
for base_path in search_paths {
|
|
|
|
match fs::File::open(base_path.join(cleaned_url)) {
|
|
|
|
Ok(f) => return Ok(f),
|
2020-06-18 19:11:22 +00:00
|
|
|
Err(_) => continue,
|
2020-06-09 20:38:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
Err(io::Error::from(io::ErrorKind::NotFound))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_file_sha256(mut file: fs::File) -> result::Result<String, io::Error> {
|
2020-05-23 09:46:50 +00:00
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
io::copy(&mut file, &mut hasher)?;
|
2020-06-18 19:11:22 +00:00
|
|
|
Ok(format!("{:x}", hasher.finalize()))
|
2020-05-23 09:46:50 +00:00
|
|
|
}
|
2020-06-18 19:11:22 +00:00
|
|
|
|
2020-06-09 20:38:29 +00:00
|
|
|
fn compute_file_sha384(mut file: fs::File) -> result::Result<String, io::Error> {
|
|
|
|
let mut hasher = Sha384::new();
|
|
|
|
io::copy(&mut file, &mut hasher)?;
|
2020-06-18 19:11:22 +00:00
|
|
|
Ok(format!("{:x}", hasher.finalize()))
|
2020-06-09 20:38:29 +00:00
|
|
|
}
|
2020-06-18 19:11:22 +00:00
|
|
|
|
2020-06-09 20:38:29 +00:00
|
|
|
fn compute_file_sha512(mut file: fs::File) -> result::Result<String, io::Error> {
|
|
|
|
let mut hasher = Sha512::new();
|
|
|
|
io::copy(&mut file, &mut hasher)?;
|
2020-06-18 19:11:22 +00:00
|
|
|
Ok(format!("{:x}", hasher.finalize()))
|
2020-06-09 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 19:11:22 +00:00
|
|
|
fn file_not_found_err(search_paths: &[PathBuf], url: &str) -> Result<Value> {
|
|
|
|
Err(format!(
|
|
|
|
"file `{}` not found; searched in{}",
|
|
|
|
url,
|
|
|
|
search_paths.iter().fold(String::new(), |acc, arg| acc + " " + arg.to_str().unwrap())
|
|
|
|
)
|
|
|
|
.into())
|
2020-06-09 20:38:29 +00:00
|
|
|
}
|
2020-05-23 09:46:50 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
impl TeraFn for GetUrl {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
2018-10-31 07:18:57 +00:00
|
|
|
let cachebust =
|
|
|
|
args.get("cachebust").map_or(false, |c| from_value::<bool>(c.clone()).unwrap_or(false));
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2017-11-10 16:46:14 +00:00
|
|
|
let trailing_slash = args
|
|
|
|
.get("trailing_slash")
|
2018-10-31 07:18:57 +00:00
|
|
|
.map_or(false, |c| from_value::<bool>(c.clone()).unwrap_or(false));
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2018-06-25 17:13:21 +00:00
|
|
|
let path = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("path"),
|
|
|
|
"`get_url` requires a `path` argument with a string value"
|
|
|
|
);
|
2020-04-12 17:23:17 +00:00
|
|
|
|
|
|
|
let lang = optional_arg!(String, args.get("lang"), "`get_url`: `lang` must be a string.")
|
|
|
|
.unwrap_or_else(|| self.config.default_language.clone());
|
|
|
|
|
2019-05-27 12:35:14 +00:00
|
|
|
if path.starts_with("@/") {
|
2020-04-12 17:23:17 +00:00
|
|
|
let path_with_lang = match make_path_with_lang(path, &lang, &self.config) {
|
|
|
|
Ok(x) => x,
|
2020-04-22 08:07:17 +00:00
|
|
|
Err(e) => return Err(e),
|
2020-04-12 17:23:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match resolve_internal_link(&path_with_lang, &self.permalinks) {
|
2019-06-06 17:49:31 +00:00
|
|
|
Ok(resolved) => Ok(to_value(resolved.permalink).unwrap()),
|
2018-10-31 07:18:57 +00:00
|
|
|
Err(_) => {
|
2020-04-22 08:07:17 +00:00
|
|
|
Err(format!("Could not resolve URL for link `{}` not found.", path_with_lang)
|
|
|
|
.into())
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2017-11-16 17:08:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// anything else
|
2019-01-23 18:20:02 +00:00
|
|
|
let mut permalink = self.config.make_permalink(&path);
|
2018-09-30 19:15:09 +00:00
|
|
|
if !trailing_slash && permalink.ends_with('/') {
|
2017-11-16 17:08:06 +00:00
|
|
|
permalink.pop(); // Removes the slash
|
|
|
|
}
|
|
|
|
|
|
|
|
if cachebust {
|
2020-06-09 20:38:29 +00:00
|
|
|
match open_file(&self.search_paths, &path).and_then(compute_file_sha256) {
|
|
|
|
Ok(hash) => {
|
|
|
|
permalink = format!("{}?h={}", permalink, hash);
|
2020-06-18 19:11:22 +00:00
|
|
|
}
|
|
|
|
Err(_) => return file_not_found_err(&self.search_paths, &path),
|
2020-05-23 09:46:50 +00:00
|
|
|
};
|
2017-11-16 17:08:06 +00:00
|
|
|
}
|
|
|
|
Ok(to_value(permalink).unwrap())
|
|
|
|
}
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2017-11-16 17:08:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 20:38:29 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetFileHash {
|
|
|
|
search_paths: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
impl GetFileHash {
|
|
|
|
pub fn new(search_paths: Vec<PathBuf>) -> Self {
|
|
|
|
Self { search_paths }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_SHA_TYPE: u16 = 384;
|
|
|
|
|
|
|
|
impl TeraFn for GetFileHash {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
|
|
|
let path = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("path"),
|
|
|
|
"`get_file_hash` requires a `path` argument with a string value"
|
|
|
|
);
|
|
|
|
let sha_type = optional_arg!(
|
|
|
|
u16,
|
|
|
|
args.get("sha_type"),
|
|
|
|
"`get_file_hash`: `sha_type` must be 256, 384 or 512"
|
2020-06-18 19:11:22 +00:00
|
|
|
)
|
|
|
|
.unwrap_or(DEFAULT_SHA_TYPE);
|
2020-06-09 20:38:29 +00:00
|
|
|
|
|
|
|
let compute_hash_fn = match sha_type {
|
|
|
|
256 => compute_file_sha256,
|
|
|
|
384 => compute_file_sha384,
|
|
|
|
512 => compute_file_sha512,
|
2020-06-18 19:11:22 +00:00
|
|
|
_ => return Err("`get_file_hash`: `sha_type` must be 256, 384 or 512".into()),
|
2020-06-09 20:38:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let hash = open_file(&self.search_paths, &path).and_then(compute_hash_fn);
|
|
|
|
|
|
|
|
match hash {
|
|
|
|
Ok(digest) => Ok(to_value(digest).unwrap()),
|
2020-06-18 19:11:22 +00:00
|
|
|
Err(_) => file_not_found_err(&self.search_paths, &path),
|
2020-06-09 20:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ResizeImage {
|
|
|
|
imageproc: Arc<Mutex<imageproc::Processor>>,
|
|
|
|
}
|
|
|
|
impl ResizeImage {
|
|
|
|
pub fn new(imageproc: Arc<Mutex<imageproc::Processor>>) -> Self {
|
2019-02-09 18:54:46 +00:00
|
|
|
Self { imageproc }
|
2018-07-16 08:54:05 +00:00
|
|
|
}
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2018-07-31 14:35:16 +00:00
|
|
|
|
2019-08-24 20:23:08 +00:00
|
|
|
static DEFAULT_OP: &str = "fill";
|
|
|
|
static DEFAULT_FMT: &str = "auto";
|
2019-01-23 18:20:02 +00:00
|
|
|
const DEFAULT_Q: u8 = 75;
|
|
|
|
|
|
|
|
impl TeraFn for ResizeImage {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
|
|
|
let path = required_arg!(
|
2018-06-25 17:13:21 +00:00
|
|
|
String,
|
2019-01-23 18:20:02 +00:00
|
|
|
args.get("path"),
|
|
|
|
"`resize_image` requires a `path` argument with a string value"
|
2018-06-25 17:13:21 +00:00
|
|
|
);
|
2019-01-23 18:20:02 +00:00
|
|
|
let width = optional_arg!(
|
|
|
|
u32,
|
|
|
|
args.get("width"),
|
|
|
|
"`resize_image`: `width` must be a non-negative integer"
|
|
|
|
);
|
|
|
|
let height = optional_arg!(
|
|
|
|
u32,
|
|
|
|
args.get("height"),
|
|
|
|
"`resize_image`: `height` must be a non-negative integer"
|
|
|
|
);
|
|
|
|
let op = optional_arg!(String, args.get("op"), "`resize_image`: `op` must be a string")
|
|
|
|
.unwrap_or_else(|| DEFAULT_OP.to_string());
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
let format =
|
|
|
|
optional_arg!(String, args.get("format"), "`resize_image`: `format` must be a string")
|
|
|
|
.unwrap_or_else(|| DEFAULT_FMT.to_string());
|
2017-08-07 11:38:13 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
let quality =
|
|
|
|
optional_arg!(u8, args.get("quality"), "`resize_image`: `quality` must be a number")
|
|
|
|
.unwrap_or(DEFAULT_Q);
|
|
|
|
if quality == 0 || quality > 100 {
|
|
|
|
return Err("`resize_image`: `quality` must be in range 1-100".to_string().into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut imageproc = self.imageproc.lock().unwrap();
|
|
|
|
if !imageproc.source_exists(&path) {
|
|
|
|
return Err(format!("`resize_image`: Cannot find path: {}", path).into());
|
2018-10-02 14:42:34 +00:00
|
|
|
}
|
2019-01-23 18:20:02 +00:00
|
|
|
|
|
|
|
let imageop = imageproc::ImageOp::from_args(path, &op, width, height, &format, quality)
|
|
|
|
.map_err(|e| format!("`resize_image`: {}", e))?;
|
|
|
|
let url = imageproc.insert(imageop);
|
|
|
|
|
|
|
|
to_value(url).map_err(|err| err.into())
|
2018-07-31 14:35:16 +00:00
|
|
|
}
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2018-07-31 14:35:16 +00:00
|
|
|
|
2019-05-30 18:06:24 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetImageMeta {
|
2019-06-02 18:21:06 +00:00
|
|
|
content_path: PathBuf,
|
2019-05-30 18:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GetImageMeta {
|
|
|
|
pub fn new(content_path: PathBuf) -> Self {
|
|
|
|
Self { content_path }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TeraFn for GetImageMeta {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
|
|
|
let path = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("path"),
|
2020-01-26 01:55:37 +00:00
|
|
|
"`get_image_metadata` requires a `path` argument with a string value"
|
2019-05-30 18:06:24 +00:00
|
|
|
);
|
|
|
|
let src_path = self.content_path.join(&path);
|
2019-06-02 18:21:06 +00:00
|
|
|
if !src_path.exists() {
|
2020-01-26 01:55:37 +00:00
|
|
|
return Err(format!("`get_image_metadata`: Cannot find path: {}", path).into());
|
2019-05-30 18:06:24 +00:00
|
|
|
}
|
2020-06-18 09:36:11 +00:00
|
|
|
let (height, width) = image_dimensions(&src_path)?;
|
2019-05-30 18:06:24 +00:00
|
|
|
let mut map = tera::Map::new();
|
2020-06-18 09:36:11 +00:00
|
|
|
map.insert(String::from("height"), Value::Number(tera::Number::from(height)));
|
|
|
|
map.insert(String::from("width"), Value::Number(tera::Number::from(width)));
|
2019-05-30 18:06:24 +00:00
|
|
|
Ok(Value::Object(map))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 09:36:11 +00:00
|
|
|
// Try to read the image dimensions for a given image
|
|
|
|
fn image_dimensions(path: &PathBuf) -> Result<(u32, u32)> {
|
|
|
|
if let Some("svg") = path.extension().and_then(OsStr::to_str) {
|
|
|
|
let img = svg::Metadata::parse_file(&path)
|
|
|
|
.map_err(|e| Error::chain(format!("Failed to process SVG: {}", path.display()), e))?;
|
|
|
|
match (img.height(), img.width(), img.view_box()) {
|
|
|
|
(Some(h), Some(w), _) => Ok((h as u32, w as u32)),
|
|
|
|
(_, _, Some(view_box)) => Ok((view_box.height as u32, view_box.width as u32)),
|
2020-06-18 19:11:22 +00:00
|
|
|
_ => Err("Invalid dimensions: SVG width/height and viewbox not set.".into()),
|
2020-06-18 09:36:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let img = image::open(&path)
|
|
|
|
.map_err(|e| Error::chain(format!("Failed to process image: {}", path.display()), e))?;
|
|
|
|
Ok((img.height(), img.width()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetTaxonomyUrl {
|
|
|
|
taxonomies: HashMap<String, HashMap<String, String>>,
|
2019-08-15 20:14:53 +00:00
|
|
|
default_lang: String,
|
2020-09-02 09:40:06 +00:00
|
|
|
slugify: SlugifyStrategy,
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2020-09-02 09:40:06 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
impl GetTaxonomyUrl {
|
2020-09-02 09:40:06 +00:00
|
|
|
pub fn new(default_lang: &str, all_taxonomies: &[Taxonomy], slugify: SlugifyStrategy) -> Self {
|
2019-01-23 18:20:02 +00:00
|
|
|
let mut taxonomies = HashMap::new();
|
2019-08-15 20:14:53 +00:00
|
|
|
for taxo in all_taxonomies {
|
2019-01-23 18:20:02 +00:00
|
|
|
let mut items = HashMap::new();
|
2019-08-15 20:14:53 +00:00
|
|
|
for item in &taxo.items {
|
2020-09-02 09:40:06 +00:00
|
|
|
items.insert(slugify_paths(&item.name.clone(), slugify), item.permalink.clone());
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2019-08-15 20:14:53 +00:00
|
|
|
taxonomies.insert(format!("{}-{}", taxo.kind.name, taxo.kind.lang), items);
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2020-09-02 09:40:06 +00:00
|
|
|
Self { taxonomies, default_lang: default_lang.to_string(), slugify: slugify }
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl TeraFn for GetTaxonomyUrl {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
2018-07-31 14:35:16 +00:00
|
|
|
let kind = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("kind"),
|
|
|
|
"`get_taxonomy_url` requires a `kind` argument with a string value"
|
|
|
|
);
|
|
|
|
let name = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("name"),
|
|
|
|
"`get_taxonomy_url` requires a `name` argument with a string value"
|
|
|
|
);
|
2019-08-24 20:23:08 +00:00
|
|
|
let lang =
|
|
|
|
optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string")
|
|
|
|
.unwrap_or_else(|| self.default_lang.clone());
|
2019-08-15 20:14:53 +00:00
|
|
|
|
|
|
|
let container = match self.taxonomies.get(&format!("{}-{}", kind, lang)) {
|
2018-07-31 14:35:16 +00:00
|
|
|
Some(c) => c,
|
2018-10-31 07:18:57 +00:00
|
|
|
None => {
|
|
|
|
return Err(format!(
|
|
|
|
"`get_taxonomy_url` received an unknown taxonomy as kind: {}",
|
|
|
|
kind
|
|
|
|
)
|
2018-12-10 17:21:08 +00:00
|
|
|
.into());
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2018-07-31 14:35:16 +00:00
|
|
|
};
|
|
|
|
|
2020-09-02 09:40:06 +00:00
|
|
|
if let Some(permalink) = container.get(&slugify_paths(&name, self.slugify)) {
|
2018-11-19 14:04:22 +00:00
|
|
|
return Ok(to_value(permalink).unwrap());
|
2018-07-31 14:35:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
Err(format!("`get_taxonomy_url`: couldn't find `{}` in `{}` taxonomy", name, kind).into())
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2018-07-31 14:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetPage {
|
2019-01-27 17:57:07 +00:00
|
|
|
base_path: PathBuf,
|
|
|
|
library: Arc<RwLock<Library>>,
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
impl GetPage {
|
2019-01-27 17:57:07 +00:00
|
|
|
pub fn new(base_path: PathBuf, library: Arc<RwLock<Library>>) -> Self {
|
2019-02-09 18:54:46 +00:00
|
|
|
Self { base_path: base_path.join("content"), library }
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl TeraFn for GetPage {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
2018-06-25 17:13:21 +00:00
|
|
|
let path = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("path"),
|
2019-01-23 18:20:02 +00:00
|
|
|
"`get_page` requires a `path` argument with a string value"
|
2018-06-25 17:13:21 +00:00
|
|
|
);
|
2019-01-27 17:57:07 +00:00
|
|
|
let full_path = self.base_path.join(&path);
|
|
|
|
let library = self.library.read().unwrap();
|
|
|
|
match library.get_page(&full_path) {
|
2019-02-09 18:54:46 +00:00
|
|
|
Some(p) => Ok(to_value(p.to_serialized(&library)).unwrap()),
|
2019-01-23 18:20:02 +00:00
|
|
|
None => Err(format!("Page `{}` not found.", path).into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetSection {
|
2019-01-27 17:57:07 +00:00
|
|
|
base_path: PathBuf,
|
|
|
|
library: Arc<RwLock<Library>>,
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
impl GetSection {
|
2019-01-27 17:57:07 +00:00
|
|
|
pub fn new(base_path: PathBuf, library: Arc<RwLock<Library>>) -> Self {
|
2019-02-09 18:54:46 +00:00
|
|
|
Self { base_path: base_path.join("content"), library }
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl TeraFn for GetSection {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
|
|
|
let path = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("path"),
|
|
|
|
"`get_section` requires a `path` argument with a string value"
|
2018-06-25 17:13:21 +00:00
|
|
|
);
|
2018-12-27 23:54:06 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
let metadata_only = args
|
|
|
|
.get("metadata_only")
|
|
|
|
.map_or(false, |c| from_value::<bool>(c.clone()).unwrap_or(false));
|
2018-12-27 23:54:06 +00:00
|
|
|
|
2019-01-27 17:57:07 +00:00
|
|
|
let full_path = self.base_path.join(&path);
|
|
|
|
let library = self.library.read().unwrap();
|
2018-02-02 20:35:04 +00:00
|
|
|
|
2019-01-27 17:57:07 +00:00
|
|
|
match library.get_section(&full_path) {
|
|
|
|
Some(s) => {
|
|
|
|
if metadata_only {
|
|
|
|
Ok(to_value(s.to_serialized_basic(&library)).unwrap())
|
|
|
|
} else {
|
|
|
|
Ok(to_value(s.to_serialized(&library)).unwrap())
|
|
|
|
}
|
2019-02-09 18:54:46 +00:00
|
|
|
}
|
2019-01-23 18:20:02 +00:00
|
|
|
None => Err(format!("Section `{}` not found.", path).into()),
|
2018-02-02 20:35:04 +00:00
|
|
|
}
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-02 20:35:04 +00:00
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetTaxonomy {
|
2019-01-27 17:57:07 +00:00
|
|
|
library: Arc<RwLock<Library>>,
|
|
|
|
taxonomies: HashMap<String, Taxonomy>,
|
2019-08-15 20:14:53 +00:00
|
|
|
default_lang: String,
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
impl GetTaxonomy {
|
2019-08-24 20:23:08 +00:00
|
|
|
pub fn new(
|
|
|
|
default_lang: &str,
|
|
|
|
all_taxonomies: Vec<Taxonomy>,
|
|
|
|
library: Arc<RwLock<Library>>,
|
|
|
|
) -> Self {
|
2019-01-23 18:20:02 +00:00
|
|
|
let mut taxonomies = HashMap::new();
|
2019-01-27 17:57:07 +00:00
|
|
|
for taxo in all_taxonomies {
|
2019-08-15 20:14:53 +00:00
|
|
|
taxonomies.insert(format!("{}-{}", taxo.kind.name, taxo.kind.lang), taxo);
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2019-08-15 20:14:53 +00:00
|
|
|
Self { taxonomies, library, default_lang: default_lang.to_string() }
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl TeraFn for GetTaxonomy {
|
|
|
|
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
|
|
|
|
let kind = required_arg!(
|
|
|
|
String,
|
|
|
|
args.get("kind"),
|
|
|
|
"`get_taxonomy` requires a `kind` argument with a string value"
|
|
|
|
);
|
2019-01-27 17:57:07 +00:00
|
|
|
|
2019-08-24 20:23:08 +00:00
|
|
|
let lang =
|
|
|
|
optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string")
|
|
|
|
.unwrap_or_else(|| self.default_lang.clone());
|
2019-08-15 20:14:53 +00:00
|
|
|
|
|
|
|
match self.taxonomies.get(&format!("{}-{}", kind, lang)) {
|
2019-02-09 18:54:46 +00:00
|
|
|
Some(t) => Ok(to_value(t.to_serialized(&self.library.read().unwrap())).unwrap()),
|
2019-01-23 18:20:02 +00:00
|
|
|
None => {
|
2019-02-09 18:54:46 +00:00
|
|
|
Err(format!("`get_taxonomy` received an unknown taxonomy as kind: {}", kind).into())
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2019-01-27 17:57:07 +00:00
|
|
|
}
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2018-02-02 20:35:04 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 11:38:13 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-06-18 19:11:22 +00:00
|
|
|
use super::{GetFileHash, GetTaxonomy, GetTaxonomyUrl, GetUrl, Trans};
|
2017-08-07 11:38:13 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2020-05-23 09:46:50 +00:00
|
|
|
use std::env::temp_dir;
|
|
|
|
use std::fs::remove_dir_all;
|
|
|
|
use std::path::PathBuf;
|
2019-02-09 18:54:46 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2017-08-07 11:38:13 +00:00
|
|
|
|
2020-05-23 09:46:50 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
use tera::{to_value, Function, Value};
|
2017-08-07 11:38:13 +00:00
|
|
|
|
2018-07-16 08:54:05 +00:00
|
|
|
use config::{Config, Taxonomy as TaxonomyConfig};
|
2018-10-31 07:18:57 +00:00
|
|
|
use library::{Library, Taxonomy, TaxonomyItem};
|
2020-05-23 09:46:50 +00:00
|
|
|
use utils::fs::{create_directory, create_file};
|
2020-02-05 08:13:14 +00:00
|
|
|
use utils::slugs::SlugifyStrategy;
|
2017-08-07 11:38:13 +00:00
|
|
|
|
2020-05-23 09:46:50 +00:00
|
|
|
struct TestContext {
|
2020-06-09 20:38:29 +00:00
|
|
|
static_path: PathBuf,
|
2020-05-23 09:46:50 +00:00
|
|
|
}
|
|
|
|
impl TestContext {
|
|
|
|
fn setup() -> Self {
|
2020-06-09 20:38:29 +00:00
|
|
|
let dir = temp_dir().join("static");
|
2020-05-23 09:46:50 +00:00
|
|
|
create_directory(&dir).expect("Could not create test directory");
|
|
|
|
create_file(&dir.join("app.css"), "// Hello world!")
|
|
|
|
.expect("Could not create test content (app.css)");
|
2020-06-09 20:38:29 +00:00
|
|
|
Self { static_path: dir }
|
2020-05-23 09:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Drop for TestContext {
|
|
|
|
fn drop(&mut self) {
|
2020-06-09 20:38:29 +00:00
|
|
|
remove_dir_all(&self.static_path).expect("Could not free test directory");
|
2020-05-23 09:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref TEST_CONTEXT: TestContext = TestContext::setup();
|
|
|
|
}
|
|
|
|
|
2017-08-07 11:38:13 +00:00
|
|
|
#[test]
|
2017-08-07 14:29:58 +00:00
|
|
|
fn can_add_cachebust_to_url() {
|
2017-08-07 11:38:13 +00:00
|
|
|
let config = Config::default();
|
2020-06-09 20:38:29 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new(), vec![TEST_CONTEXT.static_path.clone()]);
|
2017-08-07 11:38:13 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
2017-08-07 14:29:58 +00:00
|
|
|
args.insert("cachebust".to_string(), to_value(true).unwrap());
|
2020-05-23 09:46:50 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "http://a-website.com/app.css?h=572e691dc68c3fcd653ae463261bdb38f35dc6f01715d9ce68799319dd158840");
|
2017-08-07 11:38:13 +00:00
|
|
|
}
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2017-11-10 16:46:14 +00:00
|
|
|
#[test]
|
2018-09-10 18:13:44 +00:00
|
|
|
fn can_add_trailing_slashes() {
|
2017-11-10 16:46:14 +00:00
|
|
|
let config = Config::default();
|
2020-06-09 20:38:29 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new(), vec![TEST_CONTEXT.static_path.clone()]);
|
2017-11-10 16:46:14 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
2018-09-10 18:13:44 +00:00
|
|
|
args.insert("trailing_slash".to_string(), to_value(true).unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "http://a-website.com/app.css/");
|
2017-11-10 16:46:14 +00:00
|
|
|
}
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2017-11-10 16:46:14 +00:00
|
|
|
#[test]
|
2018-09-10 18:13:44 +00:00
|
|
|
fn can_add_slashes_and_cachebust() {
|
2017-11-10 16:46:14 +00:00
|
|
|
let config = Config::default();
|
2020-06-09 20:38:29 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new(), vec![TEST_CONTEXT.static_path.clone()]);
|
2017-11-10 16:46:14 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
2018-09-10 18:13:44 +00:00
|
|
|
args.insert("trailing_slash".to_string(), to_value(true).unwrap());
|
2017-11-10 16:46:14 +00:00
|
|
|
args.insert("cachebust".to_string(), to_value(true).unwrap());
|
2020-05-23 09:46:50 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "http://a-website.com/app.css/?h=572e691dc68c3fcd653ae463261bdb38f35dc6f01715d9ce68799319dd158840");
|
2017-11-10 16:46:14 +00:00
|
|
|
}
|
2017-08-07 11:38:13 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-08-07 14:29:58 +00:00
|
|
|
fn can_link_to_some_static_file() {
|
2017-08-07 11:38:13 +00:00
|
|
|
let config = Config::default();
|
2020-06-09 20:38:29 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new(), vec![TEST_CONTEXT.static_path.clone()]);
|
2017-08-07 11:38:13 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "http://a-website.com/app.css");
|
2017-08-07 11:38:13 +00:00
|
|
|
}
|
2017-11-16 17:08:06 +00:00
|
|
|
|
|
|
|
#[test]
|
2018-07-16 08:54:05 +00:00
|
|
|
fn can_get_taxonomy() {
|
2019-12-21 09:44:13 +00:00
|
|
|
let mut config = Config::default();
|
2020-02-05 08:13:14 +00:00
|
|
|
config.slugify.taxonomies = SlugifyStrategy::On;
|
2019-02-09 18:54:46 +00:00
|
|
|
let taxo_config = TaxonomyConfig {
|
|
|
|
name: "tags".to_string(),
|
|
|
|
lang: config.default_language.clone(),
|
|
|
|
..TaxonomyConfig::default()
|
|
|
|
};
|
2019-08-15 20:14:53 +00:00
|
|
|
let taxo_config_fr = TaxonomyConfig {
|
|
|
|
name: "tags".to_string(),
|
|
|
|
lang: "fr".to_string(),
|
|
|
|
..TaxonomyConfig::default()
|
|
|
|
};
|
2019-01-27 17:57:07 +00:00
|
|
|
let library = Arc::new(RwLock::new(Library::new(0, 0, false)));
|
2019-02-09 18:54:46 +00:00
|
|
|
let tag = TaxonomyItem::new(
|
|
|
|
"Programming",
|
|
|
|
&taxo_config,
|
|
|
|
&config,
|
|
|
|
vec![],
|
|
|
|
&library.read().unwrap(),
|
|
|
|
);
|
2019-08-15 20:14:53 +00:00
|
|
|
let tag_fr = TaxonomyItem::new(
|
|
|
|
"Programmation",
|
|
|
|
&taxo_config_fr,
|
|
|
|
&config,
|
|
|
|
vec![],
|
|
|
|
&library.read().unwrap(),
|
|
|
|
);
|
2018-10-31 07:18:57 +00:00
|
|
|
let tags = Taxonomy { kind: taxo_config, items: vec![tag] };
|
2019-08-15 20:14:53 +00:00
|
|
|
let tags_fr = Taxonomy { kind: taxo_config_fr, items: vec![tag_fr] };
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2019-08-15 20:14:53 +00:00
|
|
|
let taxonomies = vec![tags.clone(), tags_fr.clone()];
|
2019-08-24 20:23:08 +00:00
|
|
|
let static_fn =
|
|
|
|
GetTaxonomy::new(&config.default_language, taxonomies.clone(), library.clone());
|
2017-11-16 17:08:06 +00:00
|
|
|
// can find it correctly
|
|
|
|
let mut args = HashMap::new();
|
2018-07-16 08:54:05 +00:00
|
|
|
args.insert("kind".to_string(), to_value("tags").unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
let res = static_fn.call(&args).unwrap();
|
2018-10-02 14:42:34 +00:00
|
|
|
let res_obj = res.as_object().unwrap();
|
|
|
|
assert_eq!(res_obj["kind"], to_value(tags.kind).unwrap());
|
|
|
|
assert_eq!(res_obj["items"].clone().as_array().unwrap().len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["name"],
|
|
|
|
Value::String("Programming".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["slug"],
|
|
|
|
Value::String("programming".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-10-31 07:18:57 +00:00
|
|
|
res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()
|
|
|
|
["permalink"],
|
2018-10-02 14:42:34 +00:00
|
|
|
Value::String("http://a-website.com/tags/programming/".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["pages"],
|
|
|
|
Value::Array(vec![])
|
|
|
|
);
|
2019-08-15 20:14:53 +00:00
|
|
|
// Works with other languages as well
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("kind".to_string(), to_value("tags").unwrap());
|
|
|
|
args.insert("lang".to_string(), to_value("fr").unwrap());
|
|
|
|
let res = static_fn.call(&args).unwrap();
|
|
|
|
let res_obj = res.as_object().unwrap();
|
|
|
|
assert_eq!(res_obj["kind"], to_value(tags_fr.kind).unwrap());
|
|
|
|
assert_eq!(res_obj["items"].clone().as_array().unwrap().len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["name"],
|
|
|
|
Value::String("Programmation".to_string())
|
|
|
|
);
|
|
|
|
|
2017-11-16 17:08:06 +00:00
|
|
|
// and errors if it can't find it
|
|
|
|
let mut args = HashMap::new();
|
2018-07-16 08:54:05 +00:00
|
|
|
args.insert("kind".to_string(), to_value("something-else").unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert!(static_fn.call(&args).is_err());
|
2017-11-16 17:08:06 +00:00
|
|
|
}
|
2018-01-12 23:10:19 +00:00
|
|
|
|
2018-07-31 14:35:16 +00:00
|
|
|
#[test]
|
|
|
|
fn can_get_taxonomy_url() {
|
2019-12-21 09:44:13 +00:00
|
|
|
let mut config = Config::default();
|
2020-02-05 08:13:14 +00:00
|
|
|
config.slugify.taxonomies = SlugifyStrategy::On;
|
2019-02-09 18:54:46 +00:00
|
|
|
let taxo_config = TaxonomyConfig {
|
|
|
|
name: "tags".to_string(),
|
|
|
|
lang: config.default_language.clone(),
|
|
|
|
..TaxonomyConfig::default()
|
|
|
|
};
|
2019-08-15 20:14:53 +00:00
|
|
|
let taxo_config_fr = TaxonomyConfig {
|
|
|
|
name: "tags".to_string(),
|
|
|
|
lang: "fr".to_string(),
|
|
|
|
..TaxonomyConfig::default()
|
|
|
|
};
|
2019-01-04 19:31:31 +00:00
|
|
|
let library = Library::new(0, 0, false);
|
2019-01-29 18:20:03 +00:00
|
|
|
let tag = TaxonomyItem::new("Programming", &taxo_config, &config, vec![], &library);
|
2019-08-15 20:14:53 +00:00
|
|
|
let tag_fr = TaxonomyItem::new("Programmation", &taxo_config_fr, &config, vec![], &library);
|
2018-10-31 07:18:57 +00:00
|
|
|
let tags = Taxonomy { kind: taxo_config, items: vec![tag] };
|
2019-08-15 20:14:53 +00:00
|
|
|
let tags_fr = Taxonomy { kind: taxo_config_fr, items: vec![tag_fr] };
|
2018-07-31 14:35:16 +00:00
|
|
|
|
2019-08-15 20:14:53 +00:00
|
|
|
let taxonomies = vec![tags.clone(), tags_fr.clone()];
|
2020-09-02 09:40:06 +00:00
|
|
|
let static_fn =
|
|
|
|
GetTaxonomyUrl::new(&config.default_language, &taxonomies, config.slugify.taxonomies);
|
|
|
|
|
2018-07-31 14:35:16 +00:00
|
|
|
// can find it correctly
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("kind".to_string(), to_value("tags").unwrap());
|
|
|
|
args.insert("name".to_string(), to_value("Programming").unwrap());
|
2018-10-31 07:18:57 +00:00
|
|
|
assert_eq!(
|
2019-01-23 18:20:02 +00:00
|
|
|
static_fn.call(&args).unwrap(),
|
2018-10-31 07:18:57 +00:00
|
|
|
to_value("http://a-website.com/tags/programming/").unwrap()
|
|
|
|
);
|
2020-09-02 09:40:06 +00:00
|
|
|
|
|
|
|
// can find it correctly with inconsistent capitalisation
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("kind".to_string(), to_value("tags").unwrap());
|
|
|
|
args.insert("name".to_string(), to_value("programming").unwrap());
|
|
|
|
assert_eq!(
|
|
|
|
static_fn.call(&args).unwrap(),
|
|
|
|
to_value("http://a-website.com/tags/programming/").unwrap()
|
|
|
|
);
|
|
|
|
|
2019-08-15 20:14:53 +00:00
|
|
|
// works with other languages
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("kind".to_string(), to_value("tags").unwrap());
|
|
|
|
args.insert("name".to_string(), to_value("Programmation").unwrap());
|
|
|
|
args.insert("lang".to_string(), to_value("fr").unwrap());
|
|
|
|
assert_eq!(
|
|
|
|
static_fn.call(&args).unwrap(),
|
|
|
|
to_value("http://a-website.com/fr/tags/programmation/").unwrap()
|
|
|
|
);
|
|
|
|
|
2018-07-31 14:35:16 +00:00
|
|
|
// and errors if it can't find it
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("kind".to_string(), to_value("tags").unwrap());
|
|
|
|
args.insert("name".to_string(), to_value("random").unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert!(static_fn.call(&args).is_err());
|
2018-07-31 14:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 08:51:41 +00:00
|
|
|
const TRANS_CONFIG: &str = r#"
|
2018-01-12 23:10:19 +00:00
|
|
|
base_url = "https://remplace-par-ton-url.fr"
|
|
|
|
default_language = "fr"
|
2020-04-12 17:23:17 +00:00
|
|
|
languages = [
|
|
|
|
{ code = "en" },
|
|
|
|
]
|
2018-01-12 23:10:19 +00:00
|
|
|
|
|
|
|
[translations]
|
|
|
|
[translations.fr]
|
|
|
|
title = "Un titre"
|
|
|
|
|
|
|
|
[translations.en]
|
|
|
|
title = "A title"
|
|
|
|
"#;
|
|
|
|
|
2019-09-03 08:51:41 +00:00
|
|
|
#[test]
|
|
|
|
fn can_translate_a_string() {
|
|
|
|
let config = Config::parse(TRANS_CONFIG).unwrap();
|
2019-01-23 18:20:02 +00:00
|
|
|
let static_fn = Trans::new(config);
|
2018-01-12 23:10:19 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
|
|
|
|
args.insert("key".to_string(), to_value("title").unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "Un titre");
|
2018-01-12 23:10:19 +00:00
|
|
|
|
|
|
|
args.insert("lang".to_string(), to_value("en").unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "A title");
|
2018-01-12 23:10:19 +00:00
|
|
|
|
|
|
|
args.insert("lang".to_string(), to_value("fr").unwrap());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "Un titre");
|
2018-01-12 23:10:19 +00:00
|
|
|
}
|
2019-09-03 08:51:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_on_absent_translation_lang() {
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("lang".to_string(), to_value("absent").unwrap());
|
|
|
|
args.insert("key".to_string(), to_value("title").unwrap());
|
|
|
|
|
|
|
|
let config = Config::parse(TRANS_CONFIG).unwrap();
|
|
|
|
let error = Trans::new(config).call(&args).unwrap_err();
|
|
|
|
assert_eq!("Failed to retreive term translation", format!("{}", error));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_on_absent_translation_key() {
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("lang".to_string(), to_value("en").unwrap());
|
|
|
|
args.insert("key".to_string(), to_value("absent").unwrap());
|
|
|
|
|
|
|
|
let config = Config::parse(TRANS_CONFIG).unwrap();
|
|
|
|
let error = Trans::new(config).call(&args).unwrap_err();
|
|
|
|
assert_eq!("Failed to retreive term translation", format!("{}", error));
|
|
|
|
}
|
2020-04-12 17:23:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_when_language_not_available() {
|
|
|
|
let config = Config::parse(TRANS_CONFIG).unwrap();
|
2020-06-09 20:38:29 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new(), vec![TEST_CONTEXT.static_path.clone()]);
|
2020-04-12 17:23:17 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("@/a_section/a_page.md").unwrap());
|
|
|
|
args.insert("lang".to_string(), to_value("it").unwrap());
|
|
|
|
let err = static_fn.call(&args).unwrap_err();
|
2020-04-22 08:07:17 +00:00
|
|
|
assert_eq!(
|
|
|
|
"`it` is not an authorized language (check config.languages).",
|
|
|
|
format!("{}", err)
|
|
|
|
);
|
2020-04-12 17:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_url_with_default_language() {
|
|
|
|
let config = Config::parse(TRANS_CONFIG).unwrap();
|
|
|
|
let mut permalinks = HashMap::new();
|
|
|
|
permalinks.insert(
|
|
|
|
"a_section/a_page.md".to_string(),
|
2020-04-22 08:07:17 +00:00
|
|
|
"https://remplace-par-ton-url.fr/a_section/a_page/".to_string(),
|
2020-04-12 17:23:17 +00:00
|
|
|
);
|
|
|
|
permalinks.insert(
|
|
|
|
"a_section/a_page.en.md".to_string(),
|
2020-04-22 08:07:17 +00:00
|
|
|
"https://remplace-par-ton-url.fr/en/a_section/a_page/".to_string(),
|
2020-04-12 17:23:17 +00:00
|
|
|
);
|
2020-06-09 20:38:29 +00:00
|
|
|
let static_fn = GetUrl::new(config, permalinks, vec![TEST_CONTEXT.static_path.clone()]);
|
2020-04-12 17:23:17 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("@/a_section/a_page.md").unwrap());
|
|
|
|
args.insert("lang".to_string(), to_value("fr").unwrap());
|
2020-04-22 08:07:17 +00:00
|
|
|
assert_eq!(
|
|
|
|
static_fn.call(&args).unwrap(),
|
|
|
|
"https://remplace-par-ton-url.fr/a_section/a_page/"
|
|
|
|
);
|
2020-04-12 17:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_url_with_other_language() {
|
|
|
|
let config = Config::parse(TRANS_CONFIG).unwrap();
|
|
|
|
let mut permalinks = HashMap::new();
|
|
|
|
permalinks.insert(
|
|
|
|
"a_section/a_page.md".to_string(),
|
2020-04-22 08:07:17 +00:00
|
|
|
"https://remplace-par-ton-url.fr/a_section/a_page/".to_string(),
|
2020-04-12 17:23:17 +00:00
|
|
|
);
|
|
|
|
permalinks.insert(
|
|
|
|
"a_section/a_page.en.md".to_string(),
|
2020-04-22 08:07:17 +00:00
|
|
|
"https://remplace-par-ton-url.fr/en/a_section/a_page/".to_string(),
|
2020-04-12 17:23:17 +00:00
|
|
|
);
|
2020-06-09 20:38:29 +00:00
|
|
|
let static_fn = GetUrl::new(config, permalinks, vec![TEST_CONTEXT.static_path.clone()]);
|
2020-04-12 17:23:17 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("@/a_section/a_page.md").unwrap());
|
|
|
|
args.insert("lang".to_string(), to_value("en").unwrap());
|
2020-04-22 08:07:17 +00:00
|
|
|
assert_eq!(
|
|
|
|
static_fn.call(&args).unwrap(),
|
|
|
|
"https://remplace-par-ton-url.fr/en/a_section/a_page/"
|
|
|
|
);
|
2020-04-12 17:23:17 +00:00
|
|
|
}
|
2020-06-09 20:38:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_file_hash_sha256() {
|
|
|
|
let static_fn = GetFileHash::new(vec![TEST_CONTEXT.static_path.clone()]);
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
|
|
|
args.insert("sha_type".to_string(), to_value(256).unwrap());
|
2020-06-18 19:11:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
static_fn.call(&args).unwrap(),
|
|
|
|
"572e691dc68c3fcd653ae463261bdb38f35dc6f01715d9ce68799319dd158840"
|
|
|
|
);
|
2020-06-09 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_file_hash_sha384() {
|
|
|
|
let static_fn = GetFileHash::new(vec![TEST_CONTEXT.static_path.clone()]);
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "141c09bd28899773b772bbe064d8b718fa1d6f2852b7eafd5ed6689d26b74883b79e2e814cd69d5b52ab476aa284c414");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_file_hash_sha512() {
|
|
|
|
let static_fn = GetFileHash::new(vec![TEST_CONTEXT.static_path.clone()]);
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
|
|
|
args.insert("sha_type".to_string(), to_value(512).unwrap());
|
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "379dfab35123b9159d9e4e92dc90e2be44cf3c2f7f09b2e2df80a1b219b461de3556c93e1a9ceb3008e999e2d6a54b4f1d65ee9be9be63fa45ec88931623372f");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_when_file_not_found_for_hash() {
|
|
|
|
let static_fn = GetFileHash::new(vec![TEST_CONTEXT.static_path.clone()]);
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("doesnt-exist").unwrap());
|
|
|
|
assert_eq!(
|
2020-06-18 19:11:22 +00:00
|
|
|
format!(
|
|
|
|
"file `doesnt-exist` not found; searched in {}",
|
|
|
|
TEST_CONTEXT.static_path.to_str().unwrap()
|
|
|
|
),
|
2020-06-09 20:38:29 +00:00
|
|
|
format!("{}", static_fn.call(&args).unwrap_err())
|
|
|
|
);
|
|
|
|
}
|
2017-08-07 11:38:13 +00:00
|
|
|
}
|