1
0
Fork 0
forked from acute/readme

Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

7 changed files with 99 additions and 1169 deletions

2
.gitignore vendored
View file

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

1057
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,4 @@
[BSD-2-Clause]
name = "BSD 2-Clause"
class = "permissive"
grant = """
Redistribution and use in source and binary forms, with or without

View file

@ -1,17 +1,14 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::Write;
use tera::{to_value, try_get_value, Result, Tera, Value};
#[derive(Deserialize, Serialize)]
use tera::Tera;
use serde::Deserialize;
#[derive(Deserialize, serde::Serialize)]
struct ReadmeConfig {
header: Image,
project: Project,
project: Project
}
#[derive(Deserialize, Serialize)]
#[derive(Deserialize, serde::Serialize)]
struct Project {
name: String,
summary: String,
@ -22,69 +19,39 @@ struct Project {
badges: Vec<Image>,
}
#[derive(Deserialize, Serialize)]
#[derive(Deserialize, serde::Serialize)]
struct Image {
alt: String,
href: String,
src: String,
}
#[derive(Deserialize, Serialize)]
#[derive(Deserialize)]
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 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 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 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 language_colours = include_str!("languages.toml");
let language_colours_config: HashMap<String, Language> = toml::from_str(language_colours).unwrap();
// println!("{:?}", colours_config);
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 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();
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");
let rendered = readme_template.render("readme", &context).unwrap();
//println!("{}", rendered);
}

View file

@ -1,30 +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">
{% 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 }}

53
templates/readme_templ.j2 Normal file
View file

@ -0,0 +1,53 @@
<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.