Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MiniJinja for templating #83

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,565 changes: 901 additions & 664 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ serde_json = "1.0"
url = "2.2"
tokio = { version="1.7", features = ["macros"] }
log = "0.4"
pretty_env_logger = "0.4"
pretty_env_logger = "0.5"
chrono = { version = "0.4", features = ["serde"] }
regex = "1.5"
rand = "0.8"
mime = "0.3"
toml = "0.5"
async-process = "1.2"
toml = "0.8"
async-process = "2.2"
minijinja = { version = "2.0.1", features = ["builtins", "debug", "deserialization", "json", "std_collections"] }
minijinja-contrib = { version = "2.0.1", features = ["datetime", "rand"] }
time = "0.3.36"

[features]
default = [
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ The bot was inspired by [twim-o-matic](https://github.com/matrix-org/twim-o-mati
- Approval of messages by a defined group of editors
- Messages can be sorted into projects / sections by using emoji reactions
- Support for images / videos
- Markdown generation (can be used for blogs, e.g. Hugo)
- Template-based output generation, may output any text format like HTML or
Markdown (can be used for blogs, e.g. Hugo)

### Screenshots
![](doc/images/render_command.png)
Expand Down
2 changes: 1 addition & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ In this closed room administrative commands can be executed.
| !update-config | Updates the bot configuration by executing `update_config_command` command |

### Configuration
In order to use the bot, two configuration files are required. The `config.toml` configuration file contains the bot settings (username, room ids, etc) and the definitions for the sections and projects. The second configuration file `template.md` serves as a template for the actual summary.
In order to use the bot, two configuration files are required. The `config.toml` configuration file contains the bot settings (username, room ids, etc) and the definitions for the sections and projects. The second configuration file `template.md` serves as a template for the actual summary, and its path may be set using the `TEMPLATE_PATH` environment variable.

The password for the bot is supplied via the `BOT_PASSWORD` environment variable. Setting this will depend on how you start the bot.

Expand Down
2 changes: 0 additions & 2 deletions doc/example_config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ reporting_room_id = '!roomid:domain.org'
admin_room_id = '!adminroomid:domain.org'
notice_emoji = '⭕'
restrict_notice = true
image_markdown = '> ![]({{file}})'
video_markdown = '> {{< video src="{{file}}" >}}'
verbs = ["reports", "says", "announces"]
min_length = 30
# Set to '' to disable text response
Expand Down
4 changes: 1 addition & 3 deletions doc/example_config/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ services:
environment:
- BOT_PASSWORD=secretpassword
- CONFIG_PATH=/data/config.toml
- REPORT_TEMPLATE_PATH=/data/report_template.md
- SECTION_TEMPLATE_PATH=/data/section_template.md
- PROJECT_TEMPLATE_PATH=/data/project_template.md
- TEMPLATE_PATH=/data/template.md
- STORE_PATH=/data/store.json

volumes:
Expand Down
5 changes: 0 additions & 5 deletions doc/example_config/project_template.md

This file was deleted.

16 changes: 0 additions & 16 deletions doc/example_config/report_template.md

This file was deleted.

5 changes: 0 additions & 5 deletions doc/example_config/section_template.md

This file was deleted.

94 changes: 94 additions & 0 deletions doc/example_config/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: "#{{ timestamp | dateformat(format='[week_number]') }}: This Week in X"
author: {{ editor }}
date: {{ timestamp | datetimeformat(format="iso") }}
tags: {{ projects }}
categories: ["weekly-update"]
draft: false
---
{#-
# Here are some pointers to get started writing custom templates:
#
# - This template is processed using MiniJinja:
# https://docs.rs/minijinja/latest/minijinja/
#
# - Template syntax is mostly compatible with Jinja2:
# https://jinja.palletsprojects.com/en/3.1.x/templates/
#
# - Date formatting is done using time.rs format specifiers:
# https://time-rs.github.io/book/api/format-description.html
#
# - Adding {{ debug() }} will insert the contents of the environment
# used for processing the template. This is useful for writing custom
# templates.
#
# - On top of the MiniJinja built-ins (including the minijinja-contrib ones),
# the following globals are available during template processing:
#
# editor: display name of the editor that issued the !render command.
# timestamp: date/time of the !render command (see more below).
# sections: sections containing projects and news items.
# projects: names of the projects that have news items.
# config: contents of the TOML configuration file.
#
# - The "timestamp" global contains the date and time at which the !render
# command was issued. The timedelta() filter can be used to derive new
# date/time values by adding and subtracting time periods, e.g.:
# "now() | timedelta(weeks=-1)" obtains the date/time for one week
# ago. Accepted parameters are "seconds", "minutes", "hours", "days",
# "weeks", "months", and "years".
#
# - Macros can be used to avoid repeating template fragments. See below
# for an example macro to handle both section and project news.
#
# - Hebbot will detect when the template has changed on disk and reload
# the file contents the next time it receives a !render command.
-#}

{%- macro news(news_items) -%}
{%- for item in news_items %}

[{{ item.reporter_display_name }}](https://matrix.to/#{{ item.reporter_id }}) {{ config.verbs | random }}

> {{ item.message | replace("\n", "\n> ") }}
{%- if item.images -%}
{%- for imgId, image in item.images | dictsort %}
> ![]({{ image[0] }})
{%- endfor %}
{%- endif -%} {#- news item images #}

{%- if item.videos -%}
{%- for vidId, video in item.videos | dictsort %}
> {{ "{{" }}<video src="{{ video[0] }}">{{ "}}" }}
{%- endfor %}
{%- endif -%} {#- news item videos #}

{%- endfor %} {#- news_items #}
{%- endmacro %}

Update on what happened across the X project in the week from {{
timestamp | timedelta(weeks = -1) | dateformat(format="[month repr:long] [day padding:none]")
}} to {{
timestamp | dateformat(format="[month repr:long] [day padding:none]") }}.

{%- for key, entry in sections | dictsort %}

## {{entry.section.title}} {{entry.section.emoji}}

{{- news(entry.news) }}

{%- for entry in entry.projects %}

### {{ entry.project.title}} [↗]({{ entry.project.website }}) {{ entry.project.emoji }}

{{ entry.project.description }}

{{- news(entry.news) }}

{%- endfor %} {#- projects #}

{%- endfor %} {#- sections #}

# That’s all for this week!

See you next week!
9 changes: 9 additions & 0 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,15 @@ impl Bot {

render::render(news, config, editor)
};
let result = match result {
Ok(result) => result,
Err(error) => {
let msg = format!("❌ Could not render template: <pre>{}</pre>", error);
self.send_message(&msg, BotMsgType::AdminRoomHtmlNotice)
.await;
return;
}
};

// Upload rendered content as markdown file
let bytes = result.rendered.into_bytes();
Expand Down
2 changes: 0 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct Config {
pub admin_room_id: String,
pub notice_emoji: String,
pub restrict_notice: bool,
pub image_markdown: String,
pub video_markdown: String,
pub verbs: Vec<String>,
pub min_length: usize,
pub ack_text: String,
Expand Down
Loading
Loading