Skip to content

Commit

Permalink
refactor(changelog): use tera whitespace escapes instead of antislash
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Apr 13, 2022
1 parent 2e4ffd8 commit 54b3d95
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ clap_complete = { version = "3.0", optional = true }
conventional_commit_parser = "0.9.4"
pest = "2.1.3"
pest_derive = "2.1.0"
tera = "1.12.1"
tera = "1.15.0"
globset = "0.4.8"

[dev-dependencies]
Expand Down
32 changes: 18 additions & 14 deletions src/conventional/changelog/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod test {
use conventional_commit_parser::commit::{CommitType, ConventionalCommit, Footer};
use git2::Oid;
use indoc::indoc;
use speculoos::prelude::*;
use pretty_assertions::assert_eq;

use crate::conventional::changelog::release::{ChangelogCommit, Release};
use crate::conventional::changelog::renderer::Renderer;
Expand All @@ -103,26 +103,27 @@ mod test {
let renderer = Renderer::default();

// Act
let changelog = renderer.render(release);
let changelog = renderer.render(release)?;

// Assert
assert_that!(changelog).is_ok().is_equal_to(
assert_eq!(
changelog,
indoc! {
"## 1.0.0 - 2015-09-05
#### Bug Fixes
- **(parser)** fix parser implementation - (17f7e23) - *oknozor*
#### Features
- **(parser)** implement the changelog generator - (17f7e23) - *oknozor*
- awesome feature - (17f7e23) - Paul Delafosse"
- awesome feature - (17f7e23) - Paul Delafosse
"
}
.to_string(),
);

Ok(())
}

#[test]
fn should_full_hash_template() -> Result<()> {
fn should_render_full_hash_template() -> Result<()> {
// Arrange
let release = Release::fixture();
let renderer = Renderer::try_new(Template {
Expand All @@ -131,18 +132,20 @@ mod test {
})?;

// Act
let changelog = renderer.render(release);
let changelog = renderer.render(release)?;

// Assert
assert_that!(changelog).is_ok().is_equal_to(
assert_eq!(
changelog,
indoc! {
"#### Bug Fixes
- 17f7e23081db15e9318aeb37529b1d473cf41cbe - **(parser)** fix parser implementation - @oknozor
#### Features
- 17f7e23081db15e9318aeb37529b1d473cf41cbe - **(parser)** implement the changelog generator - @oknozor
- 17f7e23081db15e9318aeb37529b1d473cf41cbe - awesome feature - Paul Delafosse"
- 17f7e23081db15e9318aeb37529b1d473cf41cbe - awesome feature - Paul Delafosse
"
}
.to_string(),
);

Ok(())
Expand All @@ -162,19 +165,20 @@ mod test {
})?;

// Act
let changelog = renderer.render(release);
let changelog = renderer.render(release)?;

// Assert
assert_that!(changelog).is_ok().is_equal_to(
assert_eq!(
changelog,
indoc! {
"## [1.0.0](https://github.com/cocogitto/cocogitto/compare/0.1.0..1.0.0) - 2015-09-05
#### Bug Fixes
- **(parser)** fix parser implementation - ([17f7e23](https://github.com/cocogitto/cocogitto/commit/17f7e23081db15e9318aeb37529b1d473cf41cbe)) - [@oknozor](https://github.com/oknozor)
#### Features
- **(parser)** implement the changelog generator - ([17f7e23](https://github.com/cocogitto/cocogitto/commit/17f7e23081db15e9318aeb37529b1d473cf41cbe)) - [@oknozor](https://github.com/oknozor)
- awesome feature - ([17f7e23](https://github.com/cocogitto/cocogitto/commit/17f7e23081db15e9318aeb37529b1d473cf41cbe)) - Paul Delafosse"
- awesome feature - ([17f7e23](https://github.com/cocogitto/cocogitto/commit/17f7e23081db15e9318aeb37529b1d473cf41cbe)) - Paul Delafosse
"
}
.to_string(),
);

Ok(())
Expand Down
10 changes: 1 addition & 9 deletions src/conventional/changelog/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use itertools::Itertools;
use tera::{get_json_pointer, to_value, try_get_value, Context, Tera, Value};

use crate::conventional::changelog::release::Release;
Expand Down Expand Up @@ -35,7 +34,7 @@ impl Renderer {
let mut release = self.render_release(&version)?;
let mut version = version;
while let Some(previous) = version.previous.map(|v| *v) {
release.push_str("\n\n- - -\n\n");
release.push_str("\n- - -\n\n");
release.push_str(self.render_release(&previous)?.as_str());
version = previous;
}
Expand All @@ -56,13 +55,6 @@ impl Renderer {

self.tera
.render(self.template.kind.name(), &template_context)
.map(|changelog| {
changelog
.lines()
.map(|line| line.trim())
.filter(|line| *line != "\\")
.join("\n")
})
}

// From git-cliff: https://github.com/orhun/git-cliff/blob/main/git-cliff-core/src/template.rs
Expand Down
48 changes: 26 additions & 22 deletions src/conventional/changelog/template/full_hash
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{% for type, typed_commits in commits | sort(attribute="type")| group_by(attribute="type")%} \
\
{% for type, typed_commits in commits | sort(attribute="type")| group_by(attribute="type") -%}
#### {{ type | upper_first }}
\
{% for scope, scoped_commits in typed_commits | group_by(attribute="scope") %} \
{% for commit in scoped_commits | sort(attribute="scope") %} \
{% if commit.author %} \
{% set author = "@" ~ commit.author %} \
{% else %} \
{% set author = commit.signature %} \
{% endif %} \
- {{ commit.id }} - **({{ scope }})** {{ commit.summary }} - {{ author }}
{% endfor %} \
{% endfor %} \
{% for commit in typed_commits | unscoped %} \
{% if commit.author %} \
{% set author = "@" ~ commit.author %} \
{% else %} \
{% set author = commit.signature %} \
{% endif %} \
- {{ commit.id }} - {{ commit.summary }} - {{ author }}
{% endfor %} \
{% endfor %} \
{% for scope, scoped_commits in typed_commits | group_by(attribute="scope") -%}

{% for commit in scoped_commits | sort(attribute="scope") -%}
{% if commit.author -%}
{% set author = "@" ~ commit.author -%}
{% else -%}
{% set author = commit.signature -%}
{% endif -%}
- {{ commit.id }} - **({{ scope }})** {{ commit.summary }} - {{ author }}
{% endfor -%}

{% endfor -%}

{% for commit in typed_commits | unscoped -%}

{% if commit.author -%}
{% set author = "@" ~ commit.author -%}
{% else -%}
{% set author = commit.signature -%}
{% endif -%}
- {{ commit.id }} - {{ commit.summary }} - {{ author }}

{% endfor -%}

{% endfor -%}
85 changes: 44 additions & 41 deletions src/conventional/changelog/template/remote
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
{% if version.tag and from.tag %} \
{% if version.tag and from.tag -%}
## [{{ version.tag }}]({{repository_url ~ "/compare/" ~ from.tag ~ ".." ~ version.tag}}) - {{ date | date(format="%Y-%m-%d") }}
{% elif version.tag and from.id %} \
{% elif version.tag and from.id -%}
## [{{ version.tag }}]({{repository_url ~ "/compare/" ~ from.id ~ ".." ~ version.tag}}) - {{ date | date(format="%Y-%m-%d") }}
{% else %} \
{% set from = from.id %} \
{% set to = version.id %} \
\
{% set from_shorthand = from.id | truncate(length=7, end="") %} \
{% set to_shorthand = version.id | truncate(length=7, end="") %} \
\
{% else -%}
{% set from = from.id -%}
{% set to = version.id -%}

{% set from_shorthand = from.id | truncate(length=7, end="") -%}
{% set to_shorthand = version.id | truncate(length=7, end="") -%}

## Unreleased ([{{ from_shorthand ~ ".." ~ to_shorthand }}]({{repository_url ~ "/compare/" ~ from_shorthand ~ ".." ~ to_shorthand}}))
{% endif %} \
\
{% for type, typed_commits in commits | sort(attribute="type")| group_by(attribute="type")%} \
\
{% endif -%}

{% for type, typed_commits in commits | sort(attribute="type")| group_by(attribute="type")-%}

#### {{ type | upper_first }}
\
{% for scope, scoped_commits in typed_commits | group_by(attribute="scope") %} \
{% for commit in scoped_commits | sort(attribute="scope") %} \
{% if commit.author and repository_url %} \
{% set author = "@" ~ commit.author %} \
{% set author_link = platform ~ "/" ~ commit.author %} \
{% set author = "[" ~ author ~ "](" ~ author_link ~ ")" %} \
{% else %} \
{% set author = commit.signature %} \
{% endif %} \
{% set commit_link = repository_url ~ "/commit/" ~ commit.id %} \
{% set shorthand = commit.id | truncate(length=7, end="") %} \
- **({{ scope }})** {{ commit.summary }} - ([{{shorthand}}]({{ commit_link }})) - {{ author }}
{% endfor %} \
{% endfor %} \
{% for commit in typed_commits | unscoped %} \
{% if commit.author and repository_url %} \
{% set author = "@" ~ commit.author %} \
{% set author_link = platform ~ "/" ~ commit.author %} \
{% set author = "[" ~ author ~ "](" ~ author_link ~ ")" %} \
{% else %} \
{% set author = commit.signature %} \
{% endif %} \
{% set commit_link = repository_url ~ "/commit/" ~ commit.id %} \
{% set shorthand = commit.id | truncate(length=7, end="") %} \
- {{ commit.summary }} - ([{{shorthand}}]({{ commit_link }})) - {{ author }}
{% endfor %} \
{% endfor %} \
{% for scope, scoped_commits in typed_commits | group_by(attribute="scope") -%}

{% for commit in scoped_commits | sort(attribute="scope") -%}
{% if commit.author and repository_url -%}
{% set author = "@" ~ commit.author -%}
{% set author_link = platform ~ "/" ~ commit.author -%}
{% set author = "[" ~ author ~ "](" ~ author_link ~ ")" -%}
{% else -%}
{% set author = commit.signature -%}
{% endif -%}
{% set commit_link = repository_url ~ "/commit/" ~ commit.id -%}
{% set shorthand = commit.id | truncate(length=7, end="") -%}
- **({{ scope }})** {{ commit.summary }} - ([{{shorthand}}]({{ commit_link }})) - {{ author }}
{% endfor -%}

{% endfor -%}

{% for commit in typed_commits | unscoped -%}
{% if commit.author and repository_url -%}
{% set author = "@" ~ commit.author -%}
{% set author_link = platform ~ "/" ~ commit.author -%}
{% set author = "[" ~ author ~ "](" ~ author_link ~ ")" -%}
{% else -%}
{% set author = commit.signature -%}
{% endif -%}
{% set commit_link = repository_url ~ "/commit/" ~ commit.id -%}
{% set shorthand = commit.id | truncate(length=7, end="") -%}
- {{ commit.summary }} - ([{{shorthand}}]({{ commit_link }})) - {{ author }}
{% endfor -%}

{% endfor -%}

0 comments on commit 54b3d95

Please sign in to comment.