Typstify /calendar endpoint

This commit is contained in:
Joscha 2025-03-01 22:21:45 +01:00
parent 42f0885b43
commit 5f2dcf81d3
8 changed files with 93 additions and 126 deletions

View file

@ -0,0 +1,5 @@
{
"year": 2025,
"month": 3,
"feed": false
}

View file

@ -0,0 +1 @@
../lib

View file

@ -0,0 +1,36 @@
#import "@preview/oxifmt:0.2.1": strfmt
#import "lib/main.typ" as lib;
#show: it => lib.init(it)
#let data = json("data.json")
#let date = datetime(year: data.year, month: data.month, day: 1)
#let month_length = 32 - (date + duration(days: 31)).day()
#let head(name) = text(size: 32pt, name)
#let empty = box()
#let day(n) = box(
width: 100%,
height: 100%,
stroke: 2pt + black,
strfmt("{:02}", n),
)
#align(center + horizon)[
#set par(spacing: 8pt)
Ankreuzkalender #strfmt("{:04}-{:02}", date.year(), date.month())
#grid(
columns: (50pt,) * 7,
rows: 50pt,
gutter: 4pt,
head[Mo], head[Di], head[Mi], head[Do], head[Fr], head[Sa], head[So],
..for _ in range(date.weekday() - 1) { (empty,) },
..for i in range(month_length) { (day(i + 1),) },
)
]
#if data.feed {
lib.feed
}

View file

@ -0,0 +1,43 @@
use axum::{Form, extract::State};
use jiff::Zoned;
use serde::{Deserialize, Serialize};
use crate::{
drawer::{Command, NewTypstDrawing},
server::{Server, somehow},
};
#[derive(Serialize)]
struct Data {
year: i16,
month: i8,
feed: bool,
}
#[derive(Deserialize)]
pub struct FormData {
pub year: Option<i16>,
pub month: Option<i8>,
pub feed: Option<bool>,
}
pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow::Result<()> {
let date = Zoned::now().date();
let data = Data {
year: form.year.unwrap_or(date.year()),
month: form.month.unwrap_or(date.month()),
feed: form.feed.unwrap_or(true),
};
let typst = super::typst_with_lib()
.with_json("/data.json", &data)
.with_main_file(include_str!("main.typ"));
let _ = server
.tx
.send(Command::draw(NewTypstDrawing::new(typst)))
.await;
Ok(())
}