-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more metadata; render
README.md
with Rust (#43)
- Loading branch information
Showing
9 changed files
with
1,687 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Render | ||
|
||
on: | ||
push: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
permissions: | ||
# Give the default GITHUB_TOKEN write permission to commit and push the | ||
# added or changed files to the repository. | ||
contents: write | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Render list | ||
run: make render | ||
|
||
- uses: stefanzweifel/git-auto-commit-action@v5.0.0 | ||
if: github.event_name == 'push' && github.ref == 'refs/heads/master' | ||
with: | ||
commit_message: Commit list | ||
commit_user_name: Idiomatic Rust Bot | ||
commit_user_email: bot@idiomatic.rs | ||
commit_author: Idiomatic Rust Bot <bot@idiomatic.rs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.PHONY: render | ||
render: | ||
cargo run --manifest-path=render/Cargo.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "render" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
askama = "0.12.1" | ||
indexmap = "2.2.1" | ||
itertools = "0.12.1" | ||
serde = { version = "1.0.196", features = ["serde_derive"] } | ||
serde_json = "1.0.113" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::fs; | ||
|
||
use askama::Template; | ||
use indexmap::IndexMap; | ||
use itertools::Itertools; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Template)] | ||
#[template(path = "README.md")] | ||
struct ReadmeTemplate { | ||
projects: Vec<Resource>, | ||
workshops: Vec<Resource>, | ||
books: Vec<Resource>, | ||
articles: YearMap, | ||
talks: YearMap, | ||
forum: YearMap, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
struct Resource { | ||
title: String, | ||
url: String, | ||
description: String, | ||
tags: Vec<String>, | ||
official: bool, | ||
year: usize, | ||
#[serde(rename = "difficultyLevel")] | ||
difficulty_level: String, | ||
duration: Option<String>, | ||
#[serde(rename = "interactivityLevel")] | ||
interactivity_level: String, | ||
free: bool, | ||
category: String, | ||
} | ||
|
||
type Resources = Vec<Resource>; | ||
|
||
type YearMap = IndexMap<usize, Resources>; | ||
|
||
fn group_by_year(resources: &Resources, category: &str) -> YearMap { | ||
resources | ||
.iter() | ||
.filter(|r| r.category == category) | ||
.sorted_by_key(|r| r.year) | ||
.rev() | ||
.fold(YearMap::new(), |mut map, r| { | ||
map.entry(r.year).or_insert_with(Vec::new).push(r.clone()); | ||
map | ||
}) | ||
} | ||
|
||
fn sort_by_title(resources: &Resources, category: &str) -> Resources { | ||
resources | ||
.iter() | ||
.filter(|r| r.category == category) | ||
.sorted_by_key(|r| r.title.to_lowercase()) | ||
.cloned() | ||
.collect() | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let file = std::fs::File::open("resources.json")?; | ||
let resources: Resources = serde_json::from_reader(file)?; | ||
|
||
let readme = ReadmeTemplate { | ||
projects: sort_by_title(&resources, "project"), | ||
workshops: sort_by_title(&resources, "workshop"), | ||
books: sort_by_title(&resources, "book"), | ||
articles: group_by_year(&resources, "article"), | ||
talks: group_by_year(&resources, "talk"), | ||
forum: group_by_year(&resources, "forum"), | ||
}; | ||
|
||
fs::write("README.md", readme.render()?)?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.