Add metadata_only to get_section

This commit is contained in:
Vincent Prouillet 2018-10-18 16:49:33 +02:00
parent 957c6bed9d
commit 86c418372f
3 changed files with 32 additions and 4 deletions

View file

@ -268,6 +268,10 @@ impl Section {
pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingSection<'a> { pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingSection<'a> {
SerializingSection::from_section(self, library) SerializingSection::from_section(self, library)
} }
pub fn to_serialized_basic<'a>(&'a self, library: &'a Library) -> SerializingSection<'a> {
SerializingSection::from_section_basic(self, Some(library))
}
} }
/// Used to create a default index section if there is no _index.md in the root content directory /// Used to create a default index section if there is no _index.md in the root content directory

View file

@ -75,11 +75,17 @@ pub fn make_get_page(library: &Library) -> GlobalFn {
pub fn make_get_section(library: &Library) -> GlobalFn { pub fn make_get_section(library: &Library) -> GlobalFn {
let mut sections = HashMap::new(); let mut sections = HashMap::new();
let mut sections_basic = HashMap::new();
for section in library.sections_values() { for section in library.sections_values() {
sections.insert( sections.insert(
section.file.relative.clone(), section.file.relative.clone(),
to_value(library.get_section(&section.file.path).unwrap().to_serialized(library)).unwrap(), to_value(library.get_section(&section.file.path).unwrap().to_serialized(library)).unwrap(),
); );
sections_basic.insert(
section.file.relative.clone(),
to_value(library.get_section(&section.file.path).unwrap().to_serialized_basic(library)).unwrap(),
);
} }
Box::new(move |args| -> Result<Value> { Box::new(move |args| -> Result<Value> {
@ -89,7 +95,19 @@ pub fn make_get_section(library: &Library) -> GlobalFn {
"`get_section` requires a `path` argument with a string value" "`get_section` requires a `path` argument with a string value"
); );
match sections.get(&path) { let metadata_only = args
.get("metadata_only")
.map_or(false, |c| {
from_value::<bool>(c.clone()).unwrap_or(false)
});
let container = if metadata_only {
&sections_basic
} else {
&sections
};
match container.get(&path) {
Some(p) => Ok(p.clone()), Some(p) => Ok(p.clone()),
None => Err(format!("Section `{}` not found.", path).into()) None => Err(format!("Section `{}` not found.", path).into())
} }

View file

@ -92,6 +92,12 @@ Takes a path to a `_index.md` file and returns the associated section
{% set section = get_section(path="blog/_index.md") %} {% set section = get_section(path="blog/_index.md") %}
``` ```
If you only need the metadata of the section, you can pass `metadata_only=true` to the function:
```jinja2
{% set section = get_section(path="blog/_index.md", metadata_only=true) %}
```
### ` get_url` ### ` get_url`
Gets the permalink for the given path. Gets the permalink for the given path.
If the path starts with `./`, it will be understood as an internal If the path starts with `./`, it will be understood as an internal
@ -108,11 +114,11 @@ we want to link to the file that is located at `static/css/app.css`:
{{/* get_url(path="css/app.css") */}} {{/* get_url(path="css/app.css") */}}
``` ```
For assets it is reccommended that you pass `trailing_slash=false` to the `get_url` function. This prevents errors By default, assets will not have a trailing slash. You can force one by passing `trailing_slash=true` to the `get_url` function.
when dealing with certain hosting providers. An example is: An example is:
```jinja2 ```jinja2
{{/* get_url(path="css/app.css", trailing_slash=false) */}} {{/* get_url(path="css/app.css", trailing_slash=true) */}}
``` ```
In the case of non-internal links, you can also add a cachebust of the format `?t=1290192` at the end of a URL In the case of non-internal links, you can also add a cachebust of the format `?t=1290192` at the end of a URL