Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 147 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sha2 = "0.11"

[dependencies]
cfb = "0.14.0"
office-crypto = "0.3.0"
csv = "1.4.0"
flate2 = "1"
encoding_rs = "0.8.35"
Expand Down
44 changes: 36 additions & 8 deletions examples/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ use std::process::ExitCode;

use anydoc::{ConvertError, Format};

const USAGE: &str = "usage: convert <file> [-f csv] [-o out.md] [--assets dir]";
const USAGE: &str = "usage: convert <file> [-f csv] [-o out.md] [--assets dir] [-p PASSWORD]";
/// Env fallback for `-p/--password`: argv leaks into shell history and `ps`.
const PASSWORD_ENV: &str = "ANYDOC_PASSWORD";

fn main() -> ExitCode {
let args: Vec<String> = std::env::args().skip(1).collect();
let mut input: Option<PathBuf> = None;
let mut output: Option<PathBuf> = None;
let mut format: Option<Format> = None;
let mut assets: Option<PathBuf> = None;
let mut password: Option<String> = None;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
Expand All @@ -34,6 +37,16 @@ fn main() -> ExitCode {
i += 1;
assets = args.get(i).map(PathBuf::from);
}
"-p" | "--password" => {
i += 1;
match args.get(i) {
Some(pw) => password = Some(pw.clone()),
None => {
eprintln!("error: --password requires a value");
return ExitCode::FAILURE;
}
}
}
other => input = Some(PathBuf::from(other)),
}
i += 1;
Expand All @@ -43,7 +56,7 @@ fn main() -> ExitCode {
return ExitCode::FAILURE;
};

match run(&input, output.as_deref(), format, assets.as_deref()) {
match run(&input, output.as_deref(), format, assets.as_deref(), password) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("error: {e:#}");
Expand All @@ -57,12 +70,27 @@ fn run(
output: Option<&Path>,
format: Option<Format>,
assets: Option<&Path>,
password: Option<String>,
) -> Result<(), ConvertError> {
let bytes = std::fs::read(input)?;
// Without -f the format comes from the file content, with the extension as
// the fallback.
let password = password.or_else(|| std::env::var(PASSWORD_ENV).ok());
let raw = std::fs::read(input)?;

// Decrypt before detection: an encrypted OOXML container has no
// recognizable signature until after decryption, mirroring the library's
// to_markdown_with_password semantics (#130 review).
let decrypted;
let bytes: &[u8] = match password.as_deref() {
Some(pw) if !pw.is_empty() && anydoc::is_encrypted_ooxml(&raw) => {
decrypted = anydoc::decrypt_ooxml(raw.clone(), pw)?;
&decrypted
}
_ => &raw,
};

// Without -f the format comes from the (now plaintext) content, with the
// extension as the fallback.
let format =
match format.or_else(|| Format::from_bytes(&bytes)).or_else(|| Format::from_path(input)) {
match format.or_else(|| Format::from_bytes(bytes)).or_else(|| Format::from_path(input)) {
Some(format) => format,
None => {
return Err(ConvertError::Unsupported(format!(
Expand All @@ -73,7 +101,7 @@ fn run(
};

let start = std::time::Instant::now();
let markdown = anydoc::to_markdown_bytes(&bytes, format)?;
let markdown = anydoc::to_markdown_bytes(bytes, format)?;
let elapsed = start.elapsed().as_secs_f64() * 1000.0;
eprintln!("converted {} in {}", input.display(), millis(elapsed));

Expand All @@ -88,7 +116,7 @@ fn run(
// Images and embedded objects live on the document model, not in the
// Markdown, so they need a second pass to write out.
if let Some(dir) = assets {
let document = anydoc::to_document(&bytes, format)?;
let document = anydoc::to_document(bytes, format)?;
std::fs::create_dir_all(dir)?;
let stem = input.file_stem().unwrap_or_default().to_string_lossy();
for asset in &document.assets {
Expand Down
Loading