1
0
Fork 0
forked from acute/readme

feat: pass licence and language details to template

This commit is contained in:
Iain Learmonth 2025-06-13 21:40:38 +01:00
parent 65f1591c05
commit 686fd4a56c
7 changed files with 1170 additions and 100 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target
.idea

1057
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -7,10 +7,10 @@ href = "https://guardianproject.dev/jasima/lipu-pali"
name = "Example Project"
summary = "An example project for example use"
description = """
This is an extended description of the example project.
Look how much longer it is than the summary.
The summary is just one line, but the description is many.
"""
This is an extended description of the example project.
Look how much longer it is than the summary.
The summary is just one line, but the description is many.
"""
language = "Rust"
licence = "BSD-2-Clause"
lifecycle = "experimental"

View file

@ -1,24 +1,25 @@
[BSD-2-Clause]
name = "BSD 2-Clause"
class = "permissive"
grant = """
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

View file

@ -1,14 +1,17 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use tera::Tera;
use serde::Deserialize;
#[derive(Deserialize, serde::Serialize)]
use std::fs::File;
use std::io::Write;
use tera::{to_value, try_get_value, Result, Tera, Value};
#[derive(Deserialize, Serialize)]
struct ReadmeConfig {
header: Image,
project: Project
project: Project,
}
#[derive(Deserialize, serde::Serialize)]
#[derive(Deserialize, Serialize)]
struct Project {
name: String,
summary: String,
@ -19,39 +22,69 @@ struct Project {
badges: Vec<Image>,
}
#[derive(Deserialize, serde::Serialize)]
#[derive(Deserialize, Serialize)]
struct Image {
alt: String,
href: String,
src: String,
}
#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
struct Language {
color: String
color: String,
}
#[derive(Deserialize, Serialize)]
struct Licence {
name: String,
class: String,
grant: String,
}
fn licence_badge(name: &Value, _: &HashMap<String, Value>) -> Result<Value> {
let mut s = try_get_value!("licence_badge", "value", String, name);
s = s.to_lowercase().replace("-", "--").replace(" ", "%20");
Ok(to_value(s.as_str())?)
}
fn licence_colour(name: &Value, _: &HashMap<String, Value>) -> Result<Value> {
let s = try_get_value!("licence_colour", "value", String, name);
let result = match s.as_str() {
"publicdomain" => "7cd958",
"permissive" => "green",
"copyleft" => "orange",
"proprietary" => "red",
_ => "grey",
};
Ok(to_value(result)?)
}
fn main() {
let mut readme_template = Tera::new("templates/**/*").unwrap();
readme_template.add_template_file("templates/readme_templ.j2", Option::from("readme")).unwrap();
let mut context = tera::Context::new();
let template = include_str!("template.md");
let mut tera = Tera::default();
tera.add_raw_template("template.md", template)
.expect("failed to add template");
tera.register_filter("licence_badge", licence_badge);
tera.register_filter("licence_colour", licence_colour);
let language_colours = include_str!("languages.toml");
let language_colours_config: HashMap<String, Language> = toml::from_str(language_colours).unwrap();
// println!("{:?}", colours_config);
let languages_toml = include_str!("languages.toml");
let languages: HashMap<String, Language> = toml::from_str(languages_toml).unwrap();
let licences_toml = include_str!("licences.toml");
let licences: HashMap<String, Licence> = toml::from_str(licences_toml).unwrap();
let toml_filename = "README.toml";
let contents: String = fs::read_to_string(toml_filename).unwrap();
let readme_config: ReadmeConfig = toml::from_str(&contents).expect("Failed to parse readme.toml");
let project_language = &readme_config.project.language;
let project_language_colour = language_colours_config.get(project_language).unwrap();
context.insert("config", &readme_config);
let rendered = readme_template.render("readme", &context).unwrap();
//println!("{}", rendered);
let readme_config: ReadmeConfig =
toml::from_str(&contents).expect("Failed to parse readme.toml");
let mut context = tera::Context::new();
context.insert("config", &readme_config);
context.insert("language", &languages.get(&readme_config.project.language));
context.insert("licence", &licences.get(&readme_config.project.licence));
let rendered = tera.render("template.md", &context).unwrap();
let mut file = File::create("README.md").expect("could not create README.md");
file.write_all(rendered.as_bytes())
.expect("failed to write to README.md");
}

30
src/template.md Normal file
View file

@ -0,0 +1,30 @@
<h1 align="center">
<br>
<a href="{{ config.header.href }}"> <img src="{{ config.header.src }}" alt="{{ config.header.alt }}"> </a>
<br>
</h1>
<p align="center">
{% if licence.name -%}
<img src="https://img.shields.io/badge/license-{{ licence.name | licence_badge }}-{{ licence.class | licence_colour }}?style=flat-square">
{% endif -%}
{% for badge in config.project.badges -%}
{% if badge.href -%}
<a href="{{ badge.href }}"><img alt="{{ badge.alt }}" src="{{ badge.src }}"></a>
{% else -%}
<img alt="{{badge.alt}}" src="{{badge.src}}">
{% endif -%}
{% endfor -%}
</p>
## {{ config.project.name }}
*{{ config.project.summary }}*
{{ config.project.description }}
### Licence
Copyright © 2022-2025 irl.
{{ licence.grant }}

View file

@ -1,53 +0,0 @@
<h1 align="center">
<br>
<a href="{{ config.header.href }}"> <img src="{{ config.header.src }}" alt="{{ config.header.alt }}"> </a>
<br>
</h1>
<p align="center">
{% for badge in config.project.badges %}
{% if badge.href %}
<a href="{{ badge.href }}"><img alt="{{ badge.alt }}" src="{{ badge.src }}"></a>
{% else %}
<img alt="{{badge.alt}}" src="{{badge.src}}">
{% endif %}
{% endfor %}
</p>
## {{ config.project.name }}
{{ config.project.summary }}
{{ config.project.description }}
### Usage
Use these as examples, but I would strongly recommend you do not include these directly in your IaC.
If something here fits your needs really well, maybe [get in touch](https://irl.xyz/contact/) with me and check if I'm
planning to maintain that role or playbook.
Sometimes a bit of external motivation is the push I need to get something finished, otherwise you can always
[hire me to do it](https://www.sr2.uk/).
### Licence
Copyright © 2022-2025 irl.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.