use std::path::{Path, PathBuf};
use easydoc_core::{DocumentContent, DocxRow, EventSink, Result};
use easydoc_markdown::{MarkdownBuilder, MarkdownResult};
use easydoc_reader::DocReadBuilder;
use easydoc_writer::{DocBuilder, DocEditor, TableWriteBuilder};
pub struct EasyDoc;
impl EasyDoc {
#[must_use]
pub fn document(path: impl Into<PathBuf>) -> DocBuilder {
DocBuilder::new(path)
}
#[must_use]
pub fn write_table<T: DocxRow>(
path: impl Into<PathBuf>,
data: &[T],
) -> TableWriteBuilder<'_, T> {
TableWriteBuilder::new(path, data)
}
pub fn document_to_bytes(f: impl FnOnce(DocBuilder) -> DocBuilder) -> Result<Vec<u8>> {
let builder = DocBuilder::new("memory.docx");
f(builder).save_to_bytes()
}
pub fn write_table_to_bytes<T: DocxRow>(data: &[T]) -> Result<Vec<u8>> {
TableWriteBuilder::new("memory.docx", data).do_write_to_bytes()
}
pub fn edit(path: impl AsRef<Path>) -> Result<DocEditor> {
DocEditor::open(path.as_ref())
}
pub fn fill_template(
template: impl AsRef<Path>,
output: impl AsRef<Path>,
data: &std::collections::HashMap<String, String>,
) -> Result<()> {
easydoc_template::fill_template(template.as_ref(), output.as_ref(), data)
}
pub fn fill_template_list<T: serde::Serialize + std::fmt::Debug>(
template: impl AsRef<Path>,
output: impl AsRef<Path>,
data: &[T],
list_field: &str,
) -> Result<()> {
easydoc_template::fill_template_list(template.as_ref(), output.as_ref(), data, list_field)
}
#[must_use]
pub fn read(path: impl Into<PathBuf>) -> DocReadBuilder {
DocReadBuilder::new(path)
}
pub fn read_text(path: impl AsRef<Path>) -> Result<String> {
easydoc_reader::read_text(path.as_ref())
}
pub fn read_tables<T: DocxRow>(path: impl AsRef<Path>) -> Result<Vec<Vec<T>>> {
easydoc_reader::read_tables::<T>(path.as_ref())
}
#[must_use]
pub fn markdown(path: impl Into<PathBuf>) -> MarkdownBuilder {
MarkdownBuilder::new(path)
}
pub fn to_markdown(path: impl Into<PathBuf>) -> Result<String> {
Ok(MarkdownBuilder::new(path).do_convert()?.markdown)
}
pub fn write_markdown(
source: impl Into<PathBuf>,
output: impl AsRef<Path>,
) -> Result<MarkdownResult> {
MarkdownBuilder::new(source).write_to(output)
}
pub fn read_events(path: impl AsRef<Path>, sink: &mut dyn EventSink) -> Result<()> {
let mut reader = easydoc_reader::DocxSaxReader::from_path(path.as_ref())?;
reader.read_events(sink)
}
pub fn view_as(path: impl AsRef<Path>, mode: &easydoc_reader::ViewMode) -> Result<String> {
let content = easydoc_reader::read_document(path.as_ref())?;
easydoc_reader::render_view(&content, mode)
}
pub fn load(path: impl AsRef<Path>) -> Result<DocumentContent> {
easydoc_reader::read_document(path.as_ref())
}
pub fn write_content(content: &DocumentContent, output: impl AsRef<Path>) -> Result<()> {
let docx = easydoc_writer::content_renderer::render_document_content(content)?;
easydoc_ooxml::AtomicFile::write(output.as_ref(), |file| {
docx.build()
.pack(file)
.map_err(|e| easydoc_core::DocError::Zip(e.to_string()))
})
}
pub fn write_content_to_bytes(content: &DocumentContent) -> Result<Vec<u8>> {
let docx = easydoc_writer::content_renderer::render_document_content(content)?;
let mut buf = Vec::new();
let cursor = std::io::Cursor::new(&mut buf);
docx.build()
.pack(cursor)
.map_err(|e| easydoc_core::DocError::Zip(e.to_string()))?;
Ok(buf)
}
}