From bae3ade47130eff9b7d50efbf09f85a4d802cde6 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 23 May 2017 20:03:25 +0900 Subject: [PATCH] Add get_section fn and update readme --- CHANGELOG.md | 6 ++++-- README.md | 11 +++++++---- src/site.rs | 1 + src/templates/global_fns.rs | 24 +++++++++++++++++++++++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a113313f..abb420bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,11 @@ - Fix missing serialized data for sections - Change the single item template context for categories/tags -- Add a `get_url` global Tera function +- Add a `get_url` and a `get_section` global Tera function - Add a config option to control how many articles to show in RSS feed -- Move `insert_anchor_links` from config to being a section option +- Move `insert_anchor_links` from config to being a section option and it can +now be insert left or right + ## 0.0.5 (2017-05-15) diff --git a/README.md b/README.md index c2066ffb..20d75301 100644 --- a/README.md +++ b/README.md @@ -169,10 +169,13 @@ to link to. The path to the file starts from the `content` directory. For example, linking to a file located at `content/pages/about.md` would be `[my link](./pages/about.md)`. ### Anchors -Headers get an automatic id from their content in order to be able to add deep links. By default no links are actually created but -the `insert_anchor_links` option in `config.toml` can be set to `true` to link tags. The default template is very ugly and will need -CSS tweaks in your projet to look decent. The default template can also be easily overwritten by creating a `anchor-link.html` file in -the `templates` directory. +Headers get an automatic id from their content in order to be able to add deep links. +You can also choose, at the section level, whether to automatically insert an anchor link next to it. It is turned off by default +but can be turned on by setting `insert_anchor = "left"` or `insert_anchor = "right"` in the `_index.md` file. `left` will insert +the anchor link before the title text and right will insert it after. + +The default template is very basic and will need CSS tweaks in your projet to look decent. +It can easily be overwritten by creating a `anchor-link.html` file in the `templates` directory. ### Shortcodes Gutenberg uses markdown for content but sometimes you want to insert some HTML, for example for a YouTube video. diff --git a/src/site.rs b/src/site.rs index 1341afdb..132cbf95 100644 --- a/src/site.rs +++ b/src/site.rs @@ -132,6 +132,7 @@ impl Site { self.populate_tags_and_categories(); self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages)); + self.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections)); self.register_get_url_fn(); Ok(()) diff --git a/src/templates/global_fns.rs b/src/templates/global_fns.rs index f875ddcc..f9bebe60 100644 --- a/src/templates/global_fns.rs +++ b/src/templates/global_fns.rs @@ -3,7 +3,7 @@ use std::path::{PathBuf}; use tera::{GlobalFn, Value, from_value, to_value, Result}; -use content::Page; +use content::{Page, Section}; use site::resolve_internal_link; @@ -29,6 +29,28 @@ pub fn make_get_page(all_pages: &HashMap) -> GlobalFn { }) } +pub fn make_get_section(all_sections: &HashMap) -> GlobalFn { + let mut sections = HashMap::new(); + for section in all_sections.values() { + sections.insert(section.file.relative.clone(), section.clone()); + } + + Box::new(move |args| -> Result { + match args.get("path") { + Some(val) => match from_value::(val.clone()) { + Ok(v) => { + match sections.get(&v) { + Some(p) => Ok(to_value(p).unwrap()), + None => Err(format!("Section `{}` not found.", v).into()) + } + }, + Err(_) => Err(format!("`get_section` received path={:?} but it requires a string", val).into()), + }, + None => Err("`get_section` requires a `path` argument.".into()), + } + }) +} + pub fn make_get_url(permalinks: HashMap,) -> GlobalFn { Box::new(move |args| -> Result { match args.get("link") {