2017-05-03 14:16:09 +00:00
|
|
|
use std::collections::HashMap;
|
2019-01-27 17:57:07 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2018-10-18 15:32:30 +00:00
|
|
|
|
2019-05-30 18:06:24 +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;
|
|
|
|
use image::GenericImageView;
|
2019-06-02 18:21:06 +00:00
|
|
|
use library::{Library, Taxonomy};
|
|
|
|
use utils::site::resolve_internal_link;
|
2018-10-29 19:13:09 +00:00
|
|
|
|
2018-02-02 20:35:04 +00:00
|
|
|
use imageproc;
|
2017-05-03 14:16:09 +00:00
|
|
|
|
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());
|
|
|
|
let translations = &self.config.translations[lang.as_str()];
|
2018-01-12 23:10:19 +00:00
|
|
|
Ok(to_value(&translations[key.as_str()]).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>,
|
|
|
|
}
|
|
|
|
impl GetUrl {
|
|
|
|
pub fn new(config: Config, permalinks: HashMap<String, String>) -> Self {
|
2019-02-09 18:54:46 +00:00
|
|
|
Self { config, permalinks }
|
2017-05-23 11:03:25 +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"
|
|
|
|
);
|
2019-05-27 12:35:14 +00:00
|
|
|
if path.starts_with("@/") {
|
2019-01-23 18:20:02 +00:00
|
|
|
match resolve_internal_link(&path, &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(_) => {
|
|
|
|
Err(format!("Could not resolve URL for link `{}` not found.", path).into())
|
|
|
|
}
|
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 {
|
2019-01-23 18:20:02 +00:00
|
|
|
permalink = format!("{}?t={}", permalink, self.config.build_timestamp.unwrap());
|
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
|
|
|
}
|
|
|
|
|
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-01-23 18:20:02 +00:00
|
|
|
static DEFAULT_OP: &'static str = "fill";
|
|
|
|
static DEFAULT_FMT: &'static str = "auto";
|
|
|
|
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"),
|
|
|
|
"`get_image_meta` requires a `path` argument with a string value"
|
|
|
|
);
|
|
|
|
let src_path = self.content_path.join(&path);
|
2019-06-02 18:21:06 +00:00
|
|
|
if !src_path.exists() {
|
2019-05-30 18:06:24 +00:00
|
|
|
return Err(format!("`get_image_meta`: Cannot find path: {}", path).into());
|
|
|
|
}
|
|
|
|
let img = image::open(&src_path)
|
|
|
|
.map_err(|e| Error::chain(format!("Failed to process image: {}", path), e))?;
|
|
|
|
let mut map = tera::Map::new();
|
|
|
|
map.insert(String::from("height"), Value::Number(tera::Number::from(img.height())));
|
|
|
|
map.insert(String::from("width"), Value::Number(tera::Number::from(img.width())));
|
|
|
|
Ok(Value::Object(map))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:20:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GetTaxonomyUrl {
|
|
|
|
taxonomies: HashMap<String, HashMap<String, String>>,
|
|
|
|
}
|
|
|
|
impl GetTaxonomyUrl {
|
|
|
|
pub fn new(all_taxonomies: &[Taxonomy]) -> Self {
|
|
|
|
let mut taxonomies = HashMap::new();
|
|
|
|
for taxonomy in all_taxonomies {
|
|
|
|
let mut items = HashMap::new();
|
|
|
|
for item in &taxonomy.items {
|
|
|
|
items.insert(item.name.clone(), item.permalink.clone());
|
|
|
|
}
|
|
|
|
taxonomies.insert(taxonomy.kind.name.clone(), items);
|
|
|
|
}
|
2019-02-09 18:54:46 +00:00
|
|
|
Self { taxonomies }
|
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-01-23 18:20:02 +00:00
|
|
|
let container = match self.taxonomies.get(&kind) {
|
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
|
|
|
};
|
|
|
|
|
2018-11-19 14:04:22 +00:00
|
|
|
if let Some(permalink) = container.get(&name) {
|
|
|
|
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-01-23 18:20:02 +00:00
|
|
|
}
|
|
|
|
impl GetTaxonomy {
|
2019-01-27 17:57:07 +00:00
|
|
|
pub fn new(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 {
|
|
|
|
taxonomies.insert(taxo.kind.name.clone(), taxo);
|
2019-01-23 18:20:02 +00:00
|
|
|
}
|
2019-02-09 18:54:46 +00:00
|
|
|
Self { taxonomies, library }
|
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-02-09 18:54:46 +00:00
|
|
|
match self.taxonomies.get(&kind) {
|
|
|
|
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 {
|
2019-01-23 18:20:02 +00:00
|
|
|
use super::{GetTaxonomy, GetTaxonomyUrl, GetUrl, Trans};
|
2017-08-07 11:38:13 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2019-02-09 18:54:46 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2017-08-07 11:38:13 +00:00
|
|
|
|
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};
|
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();
|
2019-01-23 18:20:02 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new());
|
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());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "http://a-website.com/app.css?t=1");
|
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();
|
2019-01-23 18:20:02 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new());
|
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();
|
2019-01-23 18:20:02 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new());
|
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());
|
2019-01-23 18:20:02 +00:00
|
|
|
assert_eq!(static_fn.call(&args).unwrap(), "http://a-website.com/app.css/?t=1");
|
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();
|
2019-01-23 18:20:02 +00:00
|
|
|
let static_fn = GetUrl::new(config, HashMap::new());
|
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-01-29 18:20:03 +00:00
|
|
|
let config = Config::default();
|
2019-02-09 18:54:46 +00:00
|
|
|
let taxo_config = TaxonomyConfig {
|
|
|
|
name: "tags".to_string(),
|
|
|
|
lang: config.default_language.clone(),
|
|
|
|
..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(),
|
|
|
|
);
|
2018-10-31 07:18:57 +00:00
|
|
|
let tags = Taxonomy { kind: taxo_config, items: vec![tag] };
|
2017-11-16 17:08:06 +00:00
|
|
|
|
2018-10-02 14:42:34 +00:00
|
|
|
let taxonomies = vec![tags.clone()];
|
2019-01-27 17:57:07 +00:00
|
|
|
let static_fn = GetTaxonomy::new(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![])
|
|
|
|
);
|
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-01-29 18:20:03 +00:00
|
|
|
let config = Config::default();
|
2019-02-09 18:54:46 +00:00
|
|
|
let taxo_config = TaxonomyConfig {
|
|
|
|
name: "tags".to_string(),
|
|
|
|
lang: config.default_language.clone(),
|
|
|
|
..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);
|
2018-10-31 07:18:57 +00:00
|
|
|
let tags = Taxonomy { kind: taxo_config, items: vec![tag] };
|
2018-07-31 14:35:16 +00:00
|
|
|
|
2018-10-02 14:42:34 +00:00
|
|
|
let taxonomies = vec![tags.clone()];
|
2019-01-23 18:20:02 +00:00
|
|
|
let static_fn = GetTaxonomyUrl::new(&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()
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2018-01-12 23:10:19 +00:00
|
|
|
#[test]
|
|
|
|
fn can_translate_a_string() {
|
|
|
|
let trans_config = r#"
|
|
|
|
base_url = "https://remplace-par-ton-url.fr"
|
|
|
|
default_language = "fr"
|
|
|
|
|
|
|
|
[translations]
|
|
|
|
[translations.fr]
|
|
|
|
title = "Un titre"
|
|
|
|
|
|
|
|
[translations.en]
|
|
|
|
title = "A title"
|
|
|
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-08-07 11:38:13 +00:00
|
|
|
}
|