From a6507a7a77cf8d6f002d58fbf1362d4c0fba1276 Mon Sep 17 00:00:00 2001
From: Alex Auvolat
Date: Sun, 10 Mar 2024 18:55:14 +0100
Subject: start web server
---
src/example.rs | 8 +++-
src/format.rs | 133 ++++++++++++++++++++++++++++++++-------------------------
src/main.rs | 31 ++++++++------
src/server.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 224 insertions(+), 72 deletions(-)
create mode 100644 src/server.rs
(limited to 'src')
diff --git a/src/example.rs b/src/example.rs
index 770ac8a..7d20a28 100644
--- a/src/example.rs
+++ b/src/example.rs
@@ -1,10 +1,14 @@
-use std::collections::{HashSet, HashMap};
+use std::collections::{HashMap, HashSet};
use crate::charset::Charset;
use crate::*;
impl Example {
- pub fn gen_furigana<'a>(&mut self, dict_idx: &DictIndex<'a>, overrides: &HashMap) {
+ pub fn gen_furigana<'a>(
+ &mut self,
+ dict_idx: &DictIndex<'a>,
+ overrides: &HashMap,
+ ) {
use std::fmt::Write;
if let Some(v) = overrides.get(&self.ja) {
diff --git a/src/format.rs b/src/format.rs
index caed70a..3519b13 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -1,4 +1,5 @@
use std::fs;
+use std::io::Write;
use anyhow::Result;
@@ -11,18 +12,21 @@ use crate::*;
// =====================================================================
pub fn format_batch<'a>(dict_idx: &DictIndex<'a>, count: usize, (i, batch): (usize, &Batch)) {
- format_batch_aux(dict_idx, count, i, batch).expect("format batch");
+ let mut f = io::BufWriter::new(
+ fs::File::create(format!("public/{:03}.html", i)).expect("create batch file"),
+ );
+ format_batch_to(&mut f, dict_idx, count, i, batch).expect("format batch");
}
-fn format_batch_aux<'a>(
+pub fn format_batch_to<'a>(
+ buf: &mut impl Write,
dict_idx: &DictIndex<'a>,
count: usize,
i: usize,
batch: &Batch,
) -> Result<()> {
- let mut f = io::BufWriter::new(fs::File::create(format!("public/{:03}.html", i))?);
write!(
- f,
+ buf,
r#"
@@ -34,35 +38,35 @@ fn format_batch_aux<'a>(
i
)?;
- writeln!(f, r#"index"#)?;
+ writeln!(buf, r#"
index"#)?;
for j in 0..count {
if j != i {
- writeln!(f, r#" {:03}"#, j, j)?;
+ writeln!(buf, r#" {:03}"#, j, j)?;
} else {
- writeln!(f, " {:03}", j)?;
+ writeln!(buf, " {:03}", j)?;
}
}
- writeln!(f, r#"
"#)?;
- writeln!(f, "Level: {}
", batch.level)?;
+ writeln!(buf, r#"
"#)?;
+ writeln!(buf, "Level: {}
", batch.level)?;
- write!(f, r#""#)?;
+ write!(buf, r#"
"#)?;
let mut ex_prev = Charset::default();
for ex in batch.examples.iter() {
let ex_chars = ex.chars.inter(&batch.chars);
for c in ex_chars.diff(&ex_prev).chars().iter() {
write!(
- f,
+ buf,
r#"{}"#,
c, c
)?;
}
ex_prev = ex_prev.union(&ex_chars);
}
- writeln!(f, r#"
"#)?;
+ writeln!(buf, r#""#)?;
for ex in batch.examples.iter() {
- writeln!(f, "
")?;
- write!(f, r#""#)?;
+ writeln!(buf, "
")?;
+ write!(buf, r#""#)?;
let furi = ex.furigana_markup();
for c in furi.chars() {
let class = if batch.chars.contains(c) {
@@ -78,18 +82,18 @@ fn format_batch_aux<'a>(
};
if let Some(cls) = class {
write!(
- f,
+ buf,
r#"{}"#,
c, cls, c
)?;
} else {
- write!(f, "{}", c)?;
+ write!(buf, "{}", c)?;
}
}
- writeln!(f, "
")?;
- writeln!(f, r#"{}
"#, ex.en)?;
+ writeln!(buf, "")?;
+ writeln!(buf, r#"{}
"#, ex.en)?;
- writeln!(f, r#"Explanation
"#)?;
+ writeln!(buf, r#"Explanation
"#)?;
let mut expl_batch = Vec::new();
let mut expl_all = Vec::new();
for word in ex.expl.split(|c| c == ' ' || c == '~') {
@@ -111,26 +115,26 @@ fn format_batch_aux<'a>(
}
}
for be in expl_batch {
- writeln!(f, r#"{}
"#, be)?;
+ writeln!(buf, r#"{}
"#, be)?;
}
- writeln!(f, r#""#)?;
+ writeln!(buf, r#"
"#)?;
for c in ex.chars.inter(&batch.chars).chars().iter() {
writeln!(
- f,
+ buf,
r#"{}"#,
c, c
)?;
}
- writeln!(f, r#"
"#)?;
+ writeln!(buf, r#""#)?;
for be in expl_all {
- writeln!(f, r#"{}
"#, be)?;
+ writeln!(buf, r#"{}
"#, be)?;
}
- writeln!(f, r#" "#)?;
+ writeln!(buf, r#" "#)?;
}
- writeln!(f, "
")?;
+ writeln!(buf, "
")?;
format_vocab(
- &mut f,
+ buf,
&batch
.extra_vocab
.iter()
@@ -139,7 +143,7 @@ fn format_batch_aux<'a>(
"Extra vocabulary (this level)",
)?;
format_vocab(
- &mut f,
+ buf,
&batch
.extra_vocab
.iter()
@@ -149,42 +153,42 @@ fn format_batch_aux<'a>(
)?;
writeln!(
- f,
+ buf,
r#"Extra examples (reading practice)
"#)?;
+ writeln!(buf, r#""#)?;
- writeln!(f, "
")?;
- writeln!(f, "\(≧▽≦)/
")?;
+ writeln!(buf, "
")?;
+ writeln!(buf, "\(≧▽≦)/
")?;
- write!(f, "")?;
- f.flush()?;
+ write!(buf, "