Typstify /text endpoint
This commit is contained in:
parent
6112a8c02f
commit
98071dfe32
6 changed files with 30 additions and 73 deletions
|
|
@ -1,9 +1,9 @@
|
|||
use showbits_typst::Typst;
|
||||
|
||||
pub use self::{image::*, text::*};
|
||||
pub use self::image::*;
|
||||
|
||||
mod image;
|
||||
mod text;
|
||||
pub mod text;
|
||||
|
||||
fn typst_with_lib() -> Typst {
|
||||
Typst::new()
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@
|
|||
|
||||
#let data = json("data.json")
|
||||
|
||||
#if data.at("force_wrap", default: false) {
|
||||
#if data.force_wrap {
|
||||
show regex("."): it => it + sym.zws
|
||||
data.text
|
||||
} else { data.text }
|
||||
|
||||
#if data.at("feed", default: false) {
|
||||
#if data.feed {
|
||||
lib.feed
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,13 @@
|
|||
use axum::{Form, extract::State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use showbits_typst::Typst;
|
||||
|
||||
use crate::{
|
||||
drawer::{Command, NewTypstDrawing},
|
||||
server::Server,
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Text {
|
||||
pub struct Data {
|
||||
pub text: String,
|
||||
#[serde(default)]
|
||||
pub force_wrap: bool,
|
||||
|
|
@ -10,10 +15,13 @@ pub struct Text {
|
|||
pub feed: bool,
|
||||
}
|
||||
|
||||
impl From<Text> for Typst {
|
||||
fn from(value: Text) -> Self {
|
||||
super::typst_with_lib()
|
||||
.with_json("/data.json", &value)
|
||||
.with_main_file(include_str!("main.typ"))
|
||||
}
|
||||
pub async fn post(server: State<Server>, request: Form<Data>) {
|
||||
let typst = super::typst_with_lib()
|
||||
.with_json("/data.json", &request.0)
|
||||
.with_main_file(include_str!("main.typ"));
|
||||
|
||||
let _ = server
|
||||
.tx
|
||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
||||
.await;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ mod egg;
|
|||
mod image;
|
||||
mod new_typst;
|
||||
mod photo;
|
||||
mod text;
|
||||
mod tictactoe;
|
||||
mod typst;
|
||||
|
||||
|
|
@ -18,8 +17,8 @@ use crate::persistent_printer::PersistentPrinter;
|
|||
pub use self::{
|
||||
backlog::BacklogDrawing, calendar::CalendarDrawing, cells::CellsDrawing,
|
||||
chat_message::ChatMessageDrawing, egg::EggDrawing, image::ImageDrawing,
|
||||
new_typst::NewTypstDrawing, photo::PhotoDrawing, text::TextDrawing,
|
||||
tictactoe::TicTacToeDrawing, typst::TypstDrawing,
|
||||
new_typst::NewTypstDrawing, photo::PhotoDrawing, tictactoe::TicTacToeDrawing,
|
||||
typst::TypstDrawing,
|
||||
};
|
||||
|
||||
pub const FEED: f32 = 96.0;
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
@ -14,18 +14,18 @@ use showbits_common::widgets::DitherAlgorithm;
|
|||
use tokio::{net::TcpListener, sync::mpsc};
|
||||
|
||||
use crate::{
|
||||
documents::{Image, Text},
|
||||
documents::{self, Image},
|
||||
drawer::{
|
||||
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};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Server {
|
||||
tx: mpsc::Sender<Command>,
|
||||
pub struct Server {
|
||||
pub tx: mpsc::Sender<Command>,
|
||||
}
|
||||
|
||||
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("/image", post(post_image).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("/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))
|
||||
.fallback(get(get_static_file))
|
||||
.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())
|
||||
}
|
||||
|
||||
// /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
|
||||
|
||||
async fn post_tictactoe(server: State<Server>) {
|
||||
|
|
@ -228,15 +216,6 @@ async fn post_typst(server: State<Server>, request: Form<PostTypstForm>) {
|
|||
.await;
|
||||
}
|
||||
|
||||
// /test
|
||||
|
||||
async fn post_test(server: State<Server>, request: Form<Text>) {
|
||||
let _ = server
|
||||
.tx
|
||||
.send(Command::draw(NewTypstDrawing::new(request.0)))
|
||||
.await;
|
||||
}
|
||||
|
||||
// /test2
|
||||
|
||||
async fn post_test2(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue