Typstify /text endpoint

This commit is contained in:
Joscha 2025-03-01 16:48:06 +01:00
parent 6112a8c02f
commit 98071dfe32
6 changed files with 30 additions and 73 deletions

View file

@ -1,9 +1,9 @@
use showbits_typst::Typst; use showbits_typst::Typst;
pub use self::{image::*, text::*}; pub use self::image::*;
mod image; mod image;
mod text; pub mod text;
fn typst_with_lib() -> Typst { fn typst_with_lib() -> Typst {
Typst::new() Typst::new()

View file

@ -3,11 +3,11 @@
#let data = json("data.json") #let data = json("data.json")
#if data.at("force_wrap", default: false) { #if data.force_wrap {
show regex("."): it => it + sym.zws show regex("."): it => it + sym.zws
data.text data.text
} else { data.text } } else { data.text }
#if data.at("feed", default: false) { #if data.feed {
lib.feed lib.feed
} }

View file

@ -1,8 +1,13 @@
use axum::{Form, extract::State};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use showbits_typst::Typst;
use crate::{
drawer::{Command, NewTypstDrawing},
server::Server,
};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Text { pub struct Data {
pub text: String, pub text: String,
#[serde(default)] #[serde(default)]
pub force_wrap: bool, pub force_wrap: bool,
@ -10,10 +15,13 @@ pub struct Text {
pub feed: bool, pub feed: bool,
} }
impl From<Text> for Typst { pub async fn post(server: State<Server>, request: Form<Data>) {
fn from(value: Text) -> Self { let typst = super::typst_with_lib()
super::typst_with_lib() .with_json("/data.json", &request.0)
.with_json("/data.json", &value) .with_main_file(include_str!("main.typ"));
.with_main_file(include_str!("main.typ"))
} let _ = server
.tx
.send(Command::draw(NewTypstDrawing::new(typst)))
.await;
} }

View file

@ -6,7 +6,6 @@ mod egg;
mod image; mod image;
mod new_typst; mod new_typst;
mod photo; mod photo;
mod text;
mod tictactoe; mod tictactoe;
mod typst; mod typst;
@ -18,8 +17,8 @@ use crate::persistent_printer::PersistentPrinter;
pub use self::{ pub use self::{
backlog::BacklogDrawing, calendar::CalendarDrawing, cells::CellsDrawing, backlog::BacklogDrawing, calendar::CalendarDrawing, cells::CellsDrawing,
chat_message::ChatMessageDrawing, egg::EggDrawing, image::ImageDrawing, chat_message::ChatMessageDrawing, egg::EggDrawing, image::ImageDrawing,
new_typst::NewTypstDrawing, photo::PhotoDrawing, text::TextDrawing, new_typst::NewTypstDrawing, photo::PhotoDrawing, tictactoe::TicTacToeDrawing,
tictactoe::TicTacToeDrawing, typst::TypstDrawing, typst::TypstDrawing,
}; };
pub const FEED: f32 = 96.0; pub const FEED: f32 = 96.0;

View file

@ -1,29 +0,0 @@
use showbits_common::{Node, Tree, WidgetExt, color::WHITE, widgets::Text};
use taffy::style_helpers::percent;
use crate::persistent_printer::PersistentPrinter;
use super::{Context, Drawing};
pub struct TextDrawing(pub String);
impl Drawing for TextDrawing {
fn draw(&self, printer: &mut PersistentPrinter, ctx: &mut Context) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);
let text = Text::new()
.with_metrics(Text::default_metrics().scale(2.0))
.and_plain(&self.0)
.widget(&mut ctx.font_stuff)
.node()
.register(&mut tree)?;
let root = Node::empty()
.with_size_width(percent(1.0))
.and_child(text)
.register(&mut tree)?;
printer.print_tree(&mut tree, ctx, root)?;
Ok(())
}
}

View file

@ -14,18 +14,18 @@ use showbits_common::widgets::DitherAlgorithm;
use tokio::{net::TcpListener, sync::mpsc}; use tokio::{net::TcpListener, sync::mpsc};
use crate::{ use crate::{
documents::{Image, Text}, documents::{self, Image},
drawer::{ drawer::{
CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing, CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing,
NewTypstDrawing, PhotoDrawing, TextDrawing, TicTacToeDrawing, TypstDrawing, NewTypstDrawing, PhotoDrawing, TicTacToeDrawing, TypstDrawing,
}, },
}; };
use self::{r#static::get_static_file, statuscode::status_code}; use self::{r#static::get_static_file, statuscode::status_code};
#[derive(Clone)] #[derive(Clone)]
struct Server { pub struct Server {
tx: mpsc::Sender<Command>, pub tx: mpsc::Sender<Command>,
} }
pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()> { pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()> {
@ -36,10 +36,12 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
.route("/egg", post(post_egg).fallback(get_static_file)) .route("/egg", post(post_egg).fallback(get_static_file))
.route("/image", post(post_image).fallback(get_static_file)) .route("/image", post(post_image).fallback(get_static_file))
.route("/photo", post(post_photo).fallback(get_static_file)) .route("/photo", post(post_photo).fallback(get_static_file))
.route("/text", post(post_text)) .route(
"/text",
post(documents::text::post).fallback(get_static_file),
)
.route("/tictactoe", post(post_tictactoe)) .route("/tictactoe", post(post_tictactoe))
.route("/typst", post(post_typst).fallback(get_static_file)) .route("/typst", post(post_typst).fallback(get_static_file))
.route("/test", post(post_test).fallback(get_static_file))
.route("/test2", post(post_test2).fallback(get_static_file)) .route("/test2", post(post_test2).fallback(get_static_file))
.fallback(get(get_static_file)) .fallback(get(get_static_file))
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
@ -194,20 +196,6 @@ async fn post_photo(server: State<Server>, mut multipart: Multipart) -> somehow:
Ok(Redirect::to("photo").into_response()) Ok(Redirect::to("photo").into_response())
} }
// /text
#[derive(Deserialize)]
struct PostTextForm {
text: String,
}
async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
let _ = server
.tx
.send(Command::draw(TextDrawing(request.0.text)))
.await;
}
// /tictactoe // /tictactoe
async fn post_tictactoe(server: State<Server>) { async fn post_tictactoe(server: State<Server>) {
@ -228,15 +216,6 @@ async fn post_typst(server: State<Server>, request: Form<PostTypstForm>) {
.await; .await;
} }
// /test
async fn post_test(server: State<Server>, request: Form<Text>) {
let _ = server
.tx
.send(Command::draw(NewTypstDrawing::new(request.0)))
.await;
}
// /test2 // /test2
async fn post_test2(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> { async fn post_test2(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {