use super::error::Result;
use super::parser::Record;
#[derive(Debug, Default)]
pub struct HwpDocument {
pub sections: Vec<Section>,
}
impl HwpDocument {
pub fn extract_text(&self) -> String {
let mut out = String::new();
for section in &self.sections {
for para in §ion.paragraphs {
if let Some(ref t) = para.text
&& !t.content.is_empty()
{
out.push_str(&t.content);
out.push('\n');
}
}
}
out
}
}
#[derive(Debug, Default)]
pub struct Section {
pub paragraphs: Vec<Paragraph>,
}
#[derive(Debug, Default)]
pub struct Paragraph {
pub text: Option<ParaText>,
}
#[derive(Debug)]
pub struct ParaText {
pub content: String,
}
impl ParaText {
pub fn from_record(record: &Record) -> Result<Self> {
let mut reader = record.data_reader();
let mut chars: Vec<u16> = Vec::with_capacity(record.data.len() / 2);
while reader.remaining() >= 2 {
chars.push(reader.read_u16()?);
}
let mut content = String::with_capacity(chars.len());
let mut i = 0;
while i < chars.len() {
let ch = chars[i];
match ch {
0x0000 => {} 0x0001..=0x0008 => {
i += 7;
}
0x0009 => {
content.push('\t');
i += 7;
}
0x000A => content.push('\n'), 0x000D => {} 0x000B..=0x000C | 0x000E..=0x001F => {
i += 7;
}
0xF020..=0xF07F => {} _ => {
if let Some(c) = char::from_u32(ch as u32) {
content.push(c);
}
}
}
i += 1;
}
Ok(Self { content })
}
}