Skip to content

Commit

Permalink
feat(front): Change permalink variable names
Browse files Browse the repository at this point in the history
General reasons
- The hope is this will make the meaning more clear
- This is more aligned with the planned document_attributes

Specific reasons
- path: unclear if includes filename or not (it doesn't)
- filename: unclear of includes extension or not (it doesn't)
- output_ext: overly verbose.  We don't call `path`, `output_path`, so
  why does this need it?

Finish fixing cobalt-org#198

BREAKING CHANGE: permalink variables renamed
- path -> parent
- filename -> name
- output_ext -> ext

`cobalt migrate` should take care of this.
  • Loading branch information
epage committed Jan 4, 2018
1 parent c6c4d7a commit e78b806
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/cobalt_model/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use error::Result;
use super::datetime;
use super::slug;

const PATH_ALIAS: &'static str = "/{{path}}/{{filename}}{{output_ext}}";
const PATH_ALIAS: &'static str = "/{{parent}}/{{name}}{{ext}}";
lazy_static!{
static ref PERMALINK_ALIASES: HashMap<&'static str, &'static str> = [
("path", PATH_ALIAS),
Expand Down
10 changes: 4 additions & 6 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ fn format_path_variable(source_file: &Path) -> String {
fn permalink_attributes(front: &cobalt_model::Frontmatter, dest_file: &Path) -> liquid::Object {
let mut attributes = liquid::Object::new();

attributes.insert("path".to_owned(),
attributes.insert("parent".to_owned(),
Value::Str(format_path_variable(dest_file)));

let filename = dest_file.file_stem().and_then(|s| s.to_str());
if let Some(filename) = filename {
attributes.insert("filename".to_owned(), Value::str(filename));
}
let filename = dest_file.file_stem().and_then(|s| s.to_str()).unwrap_or("");
attributes.insert("name".to_owned(), Value::str(filename));

attributes.insert("output_ext".to_owned(), Value::str(".html"));
attributes.insert("ext".to_owned(), Value::str(".html"));

// TODO(epage): Add `collection` (the collection's slug), see #257
// or `parent.slug`, see #323
Expand Down
10 changes: 8 additions & 2 deletions src/jekyll_model/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ fn migrate_variable(var: String) -> Part {
let name: &str = &var;
VARIABLES.contains(&name)
};
let var = match var.as_str() {
"path" => "parent".to_owned(),
"filename" => "name".to_owned(),
"output_ext" => "ext".to_owned(),
x => x.to_owned(),
};
let variable = if native_variable {
format!("{{{{ {} }}}}", var)
} else {
Expand Down Expand Up @@ -106,7 +112,7 @@ mod test {
#[test]
fn migrate_variable_known() {
let fixture = "path".to_owned();
let expected = Part::Constant("{{ path }}".to_owned());
let expected = Part::Constant("{{ parent }}".to_owned());
let actual = migrate_variable(fixture);
assert_eq!(actual, expected);
}
Expand Down Expand Up @@ -137,7 +143,7 @@ mod test {
#[test]
fn convert_permalink_known_variable() {
assert_eq!(convert_permalink("hello/:path/world/:i_day/"),
"/hello/{{ path }}/world/{{ i_day }}/".to_owned());
"/hello/{{ parent }}/world/{{ i_day }}/".to_owned());
}

#[test]
Expand Down
10 changes: 8 additions & 2 deletions src/legacy_model/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ fn migrate_variable(var: String) -> Part {
let name: &str = &var;
VARIABLES.contains(&name)
};
let var = match var.as_str() {
"path" => "parent".to_owned(),
"filename" => "name".to_owned(),
"output_ext" => "ext".to_owned(),
x => x.to_owned(),
};
let variable = if native_variable {
format!("{{{{ {} }}}}", var)
} else {
Expand All @@ -109,7 +115,7 @@ mod tests {
#[test]
fn migrate_variable_known() {
let fixture = "path".to_owned();
let expected = Part::Constant("{{ path }}".to_owned());
let expected = Part::Constant("{{ parent }}".to_owned());
let actual = migrate_variable(fixture);
assert_eq!(actual, expected);
}
Expand Down Expand Up @@ -140,7 +146,7 @@ mod tests {
#[test]
fn convert_permalink_known_variable() {
assert_eq!(convert_permalink("hello/:path/world/:i_day/"),
"/hello/{{ path }}/world/{{ i_day }}/".to_owned());
"/hello/{{ parent }}/world/{{ i_day }}/".to_owned());
}

#[test]
Expand Down

0 comments on commit e78b806

Please sign in to comment.