{%  macro fmt_commit(commit)
%}{#    # Use only the scope from commit summary, remove parentheses and tag.
#}{%    if commit.scope
%}{{        "**" ~ commit.scope ~ "**: " ~ commit.descriptions[0]
}}{%    else
%}{{        commit.commit.summary.rstrip()
}}{%    endif
%}{{    " ([`" ~ commit.short_hash ~ "`](" ~ commit.hexsha | commit_hash_url ~ "))"
}}{% endmacro
%}{% macro fmt_commits(commits)
%}{%    for commit in commits | sort(attribute="commit.committed_datetime", reverse=True)
%}{{        "\n\n* " ~ fmt_commit(commit)
}}{%    endfor
%}{% endmacro
%}{{ "# CHANGELOG"
}}{% if context.history.unreleased | length > 0
%}{#        # UNRELEASED
#}{{        "\n\n## Unreleased"
}}{%    for type_, commits in context.history.unreleased | dictsort
%}{{        "\n\n### " ~ type_ | capitalize
}}{%        if type_ == "unknown"
%}{{            " Scope"
}}{%        endif
%}{{        fmt_commits(commits)
}}{%    endfor
%}{% endif
%}{% for version, release in context.history.released.items()
%}{#    # RELEASED
#}{%    if release["elements"] | length > 0
%}{{        "\n\n## "  ~ version.as_semver_tag() ~ " (" ~ release.tagged_date.strftime("%Y-%m-%d") ~ ")"
}}{%        for type_, commits in release["elements"] | dictsort
%}{{            "\n\n### " ~ type_ | capitalize
}}{%            if type_ == "unknown"
%}{{                " Scope"
}}{%            endif
%}{{            fmt_commits(commits)
}}{%        endfor
%}{%    endif
%}{% endfor
%}
