From 6150eba66beb96d03a3e2af839737e3ef4c0a086 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 15 Mar 2024 16:19:21 +0100 Subject: generation is POST and we have a
now --- src/format.rs | 40 +++++++++++++++++++++++++++++- src/server.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 98 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/format.rs b/src/format.rs index 801611d..b9da487 100644 --- a/src/format.rs +++ b/src/format.rs @@ -156,7 +156,22 @@ pub fn format_batch_to<'a>( buf, r#"

Extra examples (reading practice)

"# )?; - // TODO + writeln!( + buf, + r#" +

practice levels

+

- {:03}

+

"#, + i, i + )?; writeln!(buf, "
")?; writeln!(buf, "

\(≧▽≦)/

")?; @@ -266,6 +281,29 @@ pub fn format_index_to( writeln!(buf, "
")?; + writeln!(buf, r#"

Extra reading practice

"#)?; + writeln!( + buf, + r#"
+

practice levels

+

-

+

"# + )?; + + writeln!(buf, "
")?; + let all_chars = Charset::from_iter( batches .iter() diff --git a/src/server.rs b/src/server.rs index c90ee40..774cff4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,16 +1,16 @@ use std::fs; -use std::io::Write; -use futures::stream::TryStreamExt; use anyhow::{anyhow, Result}; +use futures::stream::TryStreamExt; use rand::prelude::*; +use serde::Deserialize; use http_types::mime; use tide::Request; use crate::datafiles::*; -use crate::format::*; use crate::example::*; +use crate::format::*; use crate::*; pub async fn server_main() -> tide::Result<()> { @@ -85,7 +85,7 @@ pub async fn server_main() -> tide::Result<()> { app.at("/script.js").serve_file("static/script.js")?; app.at("/jquery.js").serve_file("static/jquery.js")?; app.at("/about.html").get(about_page); - app.at("/ex/:start/:end").get(gen_examples_page); + app.at("/gen.html").post(gen_examples_page); app.at("/:batch").get(batch_page); // ---- serve actual http ---- @@ -152,9 +152,16 @@ async fn batch_page(req: Request) -> tide::Result { .build()) } -async fn gen_examples_page(req: Request) -> tide::Result { - let first_level: usize = req.param("start")?.parse()?; - let last_level: usize = req.param("end")?.parse()?; +#[derive(Deserialize)] +struct GenParam { + first_level: usize, + last_level: usize, +} + +async fn gen_examples_page(mut req: Request) -> tide::Result { + let param: GenParam = req.body_form().await?; + let first_level = std::cmp::min(param.first_level, param.last_level); + let last_level = std::cmp::max(param.first_level, param.last_level); let allowed_chars = Charset::from_iter( req.state() @@ -179,15 +186,16 @@ async fn gen_examples_page(req: Request) -> tide::Result { let state: State = req.state(); std::thread::spawn(move || { - tx.send_blocking(Ok(format!(r#" + tx.send_blocking(Ok(format!( + r#" {:03} - {:03} practice - - - + + +
@@ -206,7 +214,10 @@ async fn gen_examples_page(req: Request) -> tide::Result {
- "#, first_level, last_level, first_level, last_level).into_bytes()))?; + "#, + first_level, last_level, first_level, last_level + ) + .into_bytes()))?; gen_examples(state, &allowed_chars, &needed_chars, 50, |mut ex| { ex.gen_furigana(&req.state().jmdict_idx, &req.state().furigana_overrides); @@ -221,12 +232,18 @@ async fn gen_examples_page(req: Request) -> tide::Result { if let Some(ents) = state.jmdict_idx.get(keb) { for ent in ents.iter() { let ent_r_ele = ent.children().find(|x| x.has_tag_name("r_ele")).unwrap(); - let ent_reb = ent_r_ele.children().find(|x| x.has_tag_name("reb")).unwrap(); + let ent_reb = ent_r_ele + .children() + .find(|x| x.has_tag_name("reb")) + .unwrap(); let ent_reb = ent_reb.text().unwrap().trim(); if reb.map(|x| x != ent_reb).unwrap_or(false) { continue; } - expl += &format!(r#"  {}  "#, keb); + expl += &format!( + r#"  {}  "#, + keb + ); for sense in ent.children().filter(|x| x.has_tag_name("sense")) { if let Some(s) = sense.children().find(|x| x.has_tag_name("gloss")) { @@ -236,7 +253,10 @@ async fn gen_examples_page(req: Request) -> tide::Result { expl += s.text().unwrap().trim(); } } - expl += &format!(r#"{}"#, ent_reb); + expl += &format!( + r#"{}"#, + ent_reb + ); } } } @@ -247,20 +267,28 @@ async fn gen_examples_page(req: Request) -> tide::Result { "furi": ex.furigana_markup(), "vocab": expl + "", }); - tx.send_blocking(Ok(format!("\n", serde_json::to_string(&item)?).into_bytes()))?; + tx.send_blocking(Ok(format!( + "\n", + serde_json::to_string(&item)? + ) + .into_bytes()))?; Ok(()) })?; tx.send_blocking(Ok(br#" - "#.to_vec()))?; + "# + .to_vec()))?; Ok::<_, anyhow::Error>(()) }); Ok(tide::Response::builder(200) - .body(tide::Body::from_reader(Box::pin(rx).into_async_read(), None)) + .body(tide::Body::from_reader( + Box::pin(rx).into_async_read(), + None, + )) .content_type(mime::HTML) .build()) } @@ -284,7 +312,9 @@ fn gen_examples( count: usize, mut callback: F, ) -> Result<()> -where F: FnMut(Example) -> Result<()> { +where + F: FnMut(Example) -> Result<()>, +{ let mut rng = thread_rng(); let mut generated = 0; @@ -292,7 +322,16 @@ where F: FnMut(Example) -> Result<()> { .examples .iter() .filter(|x| x.chars.diff(&allowed_chars).is_empty() && x.chars.intersects(&needed_chars)) - .map(|ex| (ex, *ex.chars.iter().filter_map(|x| data.example_freq.get(&x)).min().unwrap())) + .map(|ex| { + ( + ex, + *ex.chars + .iter() + .filter_map(|x| data.example_freq.get(&x)) + .min() + .unwrap(), + ) + }) .collect::>(); let mut remaining_needed = needed_chars.clone(); -- cgit v1.2.3