From 7aeebcc98109bea16786b62d590abd65aa5d623f Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 30 Mar 2017 17:17:12 +0900 Subject: [PATCH] Add current_url and current_path To every templates, useful when doing navigation to know which link is currently active --- CHANGELOG.md | 2 ++ README.md | 3 ++- src/page.rs | 21 +++++++++++---------- src/section.rs | 14 ++++++++------ src/site.rs | 8 +++++++- test_site/templates/category.html | 2 +- test_site/templates/index.html | 2 +- test_site/templates/tag.html | 2 +- tests/page.rs | 6 +++--- tests/site.rs | 2 +- 10 files changed, 37 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eee64ef..178f8970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.0.4 (unreleased) - Fix RSS feed link and description +- Renamed `Page::url` and `Section::url` to `Page::path` and `Section::path` +- Pass `current_url` and `current_path` to every template ## 0.0.3 (2017-04-05) - Add some colours in console diff --git a/README.md b/README.md index 2d5b9b61..61f793a3 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,8 @@ Each kind of page get their own variables: - categories.html: gets `categories` - category.html: gets `category` and `pages` -Additionally, all pages get a `config` variable representing the data in `config.toml`. +Additionally, all pages get a `config` variable representing the data in `config.toml`, `current_url` that represent +the absolute URL of the current page and `current_path` that represents the path of the URL of the current page, starting with `/`. If you want to know all the data present in a template content, simply put `{{ __tera_context }}` in the templates and it will print it. diff --git a/src/page.rs b/src/page.rs index 761f8d66..4ccf88d4 100644 --- a/src/page.rs +++ b/src/page.rs @@ -67,8 +67,8 @@ pub struct Page { /// The slug of that page. /// First tries to find the slug in the meta and defaults to filename otherwise pub slug: String, - /// The relative URL of the page - pub url: String, + /// The URL path of the page + pub path: String, /// The full URL for that page pub permalink: String, /// The summary for the article, defaults to empty string @@ -99,7 +99,7 @@ impl Page { assets: vec![], content: "".to_string(), slug: "".to_string(), - url: "".to_string(), + path: "".to_string(), permalink: "".to_string(), summary: "".to_string(), meta: meta, @@ -151,7 +151,7 @@ impl Page { // 4. Find sections // Pages with custom urls exists outside of sections if let Some(ref u) = page.meta.url { - page.url = u.trim().to_string(); + page.path = u.trim().to_string(); } else { if !page.components.is_empty() { // If we have a folder with an asset, don't consider it as a component @@ -162,13 +162,13 @@ impl Page { } // Don't add a trailing slash to sections - page.url = format!("{}/{}", page.components.join("/"), page.slug); + page.path = format!("{}/{}", page.components.join("/"), page.slug); } else { - page.url = page.slug.clone(); + page.path = page.slug.clone(); } } - page.permalink = config.make_permalink(&page.url); + page.permalink = config.make_permalink(&page.path); Ok(page) } @@ -209,11 +209,12 @@ impl Page { Some(ref l) => l.to_string(), None => "page.html".to_string() }; - // TODO: create a helper to create context to ensure all contexts - // have the same names + let mut context = Context::new(); context.add("config", config); context.add("page", self); + context.add("current_url", &self.permalink); + context.add("current_path", &self.path); tera.render(&tpl_name, &context) .chain_err(|| format!("Failed to render page '{}'", self.file_path.display())) @@ -228,7 +229,7 @@ impl ser::Serialize for Page { state.serialize_field("description", &self.meta.description)?; state.serialize_field("date", &self.meta.date)?; state.serialize_field("slug", &self.slug)?; - state.serialize_field("url", &format!("/{}", self.url))?; + state.serialize_field("path", &format!("/{}", self.path))?; state.serialize_field("permalink", &self.permalink)?; state.serialize_field("tags", &self.meta.tags)?; state.serialize_field("draft", &self.meta.draft)?; diff --git a/src/section.rs b/src/section.rs index 566c1cde..adc36372 100644 --- a/src/section.rs +++ b/src/section.rs @@ -21,8 +21,8 @@ pub struct Section { pub parent_path: PathBuf, /// The folder names from `content` to this section file pub components: Vec, - /// The relative URL of the page - pub url: String, + /// The URL path of the page + pub path: String, /// The full URL for that page pub permalink: String, /// The front matter meta-data @@ -40,7 +40,7 @@ impl Section { relative_path: "".to_string(), parent_path: file_path.parent().unwrap().to_path_buf(), components: vec![], - url: "".to_string(), + path: "".to_string(), permalink: "".to_string(), meta: meta, pages: vec![], @@ -52,8 +52,8 @@ impl Section { let (meta, _) = split_content(file_path, content)?; let mut section = Section::new(file_path, meta); section.components = find_content_components(§ion.file_path); - section.url = section.components.join("/"); - section.permalink = config.make_permalink(§ion.url); + section.path = section.components.join("/"); + section.permalink = config.make_permalink(§ion.path); section.relative_path = format!("{}/_index.md", section.components.join("/")); @@ -80,6 +80,8 @@ impl Section { let mut context = Context::new(); context.add("config", config); context.add("section", self); + context.add("current_url", &self.permalink); + context.add("current_path", &self.path); tera.render(&tpl_name, &context) .chain_err(|| format!("Failed to render section '{}'", self.file_path.display())) @@ -91,7 +93,7 @@ impl ser::Serialize for Section { let mut state = serializer.serialize_struct("section", 6)?; state.serialize_field("title", &self.meta.title)?; state.serialize_field("description", &self.meta.description)?; - state.serialize_field("url", &format!("/{}", self.url))?; + state.serialize_field("path", &format!("/{}", self.path))?; state.serialize_field("permalink", &self.permalink)?; state.serialize_field("pages", &self.pages)?; state.serialize_field("subsections", &self.subsections)?; diff --git a/src/site.rs b/src/site.rs index 8aea1c90..59463718 100644 --- a/src/site.rs +++ b/src/site.rs @@ -303,7 +303,7 @@ impl Site { // Copy the nesting of the content directory if we have sections for that page let mut current_path = public.to_path_buf(); - for component in page.url.split('/') { + for component in page.path.split('/') { current_path.push(component); if !current_path.exists() { @@ -342,6 +342,8 @@ impl Site { context.add("pages", &populate_previous_and_next_pages(&pages, false)); context.add("sections", &self.sections.values().collect::>()); context.add("config", &self.config); + context.add("current_url", &self.config.base_url); + context.add("current_path", &""); let index = self.tera.render("index.html", &context)?; create_file(public.join("index.html"), &self.inject_livereload(index))?; @@ -405,6 +407,8 @@ impl Site { let mut context = Context::new(); context.add(name, &sorted_items); context.add("config", &self.config); + context.add("current_url", &self.config.make_permalink(name)); + context.add("current_path", &format!("/{}", name)); // And render it immediately let list_output = self.tera.render(list_tpl_name, &context)?; create_file(output_path.join("index.html"), &self.inject_livereload(list_output))?; @@ -424,6 +428,8 @@ impl Site { context.add(&format!("{}_slug", var_name), &slug); context.add("pages", &pages); context.add("config", &self.config); + context.add("current_url", &self.config.make_permalink(&format!("{}/{}", name, slug))); + context.add("current_path", &format!("/{}/{}", name, slug)); let single_output = self.tera.render(single_tpl_name, &context)?; create_directory(&output_path.join(&slug))?; diff --git a/test_site/templates/category.html b/test_site/templates/category.html index 70079cbc..eaa13feb 100644 --- a/test_site/templates/category.html +++ b/test_site/templates/category.html @@ -3,6 +3,6 @@ Category: {{ category }} {% for page in pages %} {% endfor %} diff --git a/test_site/templates/index.html b/test_site/templates/index.html index 417d614f..db32cff9 100644 --- a/test_site/templates/index.html +++ b/test_site/templates/index.html @@ -17,7 +17,7 @@
{% for page in pages %} {% endfor %}
diff --git a/test_site/templates/tag.html b/test_site/templates/tag.html index 62f6c1a7..c77f0b01 100644 --- a/test_site/templates/tag.html +++ b/test_site/templates/tag.html @@ -2,6 +2,6 @@ Tag: {{ tag }} {% for page in pages %} {% endfor %} diff --git a/tests/page.rs b/tests/page.rs index 8f115275..ebe55248 100644 --- a/tests/page.rs +++ b/tests/page.rs @@ -79,7 +79,7 @@ Hello world"#; assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); - assert_eq!(page.url, "posts/intro/hello-world"); + assert_eq!(page.path, "posts/intro/hello-world"); assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world"); } @@ -98,7 +98,7 @@ Hello world"#; assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); - assert_eq!(page.url, "posts/intro/hello-world"); + assert_eq!(page.path, "posts/intro/hello-world"); assert_eq!(page.permalink, format!("{}{}", conf.base_url, "/posts/intro/hello-world")); } @@ -115,7 +115,7 @@ Hello world"#; assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); - assert_eq!(page.url, "hello-world"); + assert_eq!(page.path, "hello-world"); assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "hello-world")); } diff --git a/tests/site.rs b/tests/site.rs index af650fa6..e4abc9af 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -28,7 +28,7 @@ fn test_can_parse_site() { // Make sure the page with a url doesn't have any sections let url_post = &site.pages[&posts_path.join("fixed-url.md")]; - assert_eq!(url_post.url, "a-fixed-url"); + assert_eq!(url_post.path, "a-fixed-url"); // Make sure the article in a folder with only asset doesn't get counted as a section let asset_folder_post = &site.pages[&posts_path.join("with-assets").join("index.md")];