use std::fs;
use anyhow::Result;
use crate::charset::Charset;
use crate::example::expl_clean_word;
use crate::*;
// =====================================================================
// FORMATTING TO HTML
// =====================================================================
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");
}
fn format_batch_aux<'a>(
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,
r#"
Batch #{:03}
"#,
i
)?;
writeln!(f, r#"
index"#)?;
for j in 0..count {
if j != i {
writeln!(f, r#" {:03}"#, j, j)?;
} else {
writeln!(f, " {:03}", j)?;
}
}
writeln!(f, r#"
"#)?;
writeln!(f, "
Level: {}
", batch.level)?;
write!(f, 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,
r#"{}"#,
c, c
)?;
}
ex_prev = ex_prev.union(&ex_chars);
}
writeln!(f, r#"
"#)?;
for ex in batch.examples.iter() {
writeln!(f, "")?;
write!(f, r#"
"#)?;
let furi = ex.furigana_markup();
for c in furi.chars() {
let class = if batch.chars.contains(c) {
Some("char_cur")
} else if batch.chars_p1.contains(c) {
Some("char_p1")
} else if batch.chars_p2.contains(c) {
Some("char_p2")
} else if batch.chars_bad.contains(c) {
Some("char_bad")
} else {
None
};
if let Some(cls) = class {
write!(
f,
r#"{}"#,
c, cls, c
)?;
} else {
write!(f, "{}", c)?;
}
}
writeln!(f, "
")?;
writeln!(f, r#"
{}
"#, ex.en)?;
writeln!(f, r#"Explanation"#)?;
let mut expl_batch = Vec::new();
let mut expl_all = Vec::new();
for word in ex.expl.split(|c| c == ' ' || c == '~') {
let (keb, reb) = expl_clean_word(word);
let wchars = Charset::new(keb);
if !wchars.intersects(&ex.chars) {
continue;
}
if let Some(ents) = dict_idx.get(keb) {
for ent in ents.iter() {
if let Some(s) = dict_str(keb, reb, ent) {
if wchars.intersects(&batch.chars) {
expl_batch.push(s);
} else {
expl_all.push(s);
}
}
}
}
}
for be in expl_batch {
writeln!(f, r#"
{}
"#, be)?;
}
writeln!(f, r#"
"#)?;
for c in ex.chars.inter(&batch.chars).chars().iter() {
writeln!(
f,
r#"{}"#,
c, c
)?;
}
writeln!(f, r#"