Add tests #2

Merged
tforgione merged 6 commits from williamdes/md2pdf:improvements into master 2022-10-03 16:51:30 +01:00
2 changed files with 60 additions and 60 deletions
Showing only changes of commit 0d1b9de1d6 - Show all commits

View File

@ -54,7 +54,7 @@ pub fn markdown_to_latex(markdown: String) -> String {
output.push_str("sub");
}
output.push_str("section{");
},
}
Event::End(Tag::Header(_)) => output.push_str("}\n"),
Event::Start(Tag::Emphasis) => output.push_str("\\emph{"),
@ -73,11 +73,11 @@ pub fn markdown_to_latex(markdown: String) -> String {
output.push_str("\\href{");
output.push_str(&*url);
output.push_str("}{");
},
}
Event::End(Tag::Link(_, _, _)) => {
output.push_str("}");
},
}
Event::Start(Tag::Image(_, path, title)) => {
output.push_str("\\begin{figure}\n");
@ -88,7 +88,7 @@ pub fn markdown_to_latex(markdown: String) -> String {
output.push_str("\\caption{");
output.push_str(&*title);
output.push_str("}\n\\end{figure}\n");
},
}
Event::Start(Tag::Item) => output.push_str("\\item "),
Event::End(Tag::Item) => output.push_str("\n"),
@ -101,19 +101,19 @@ pub fn markdown_to_latex(markdown: String) -> String {
} else {
output.push_str("\\begin{lstlisting}\n");
}
},
}
Event::End(Tag::CodeBlock(_)) => {
output.push_str("\n\\end{lstlisting}\n");
},
}
Event::Text(t) => {
output.push_str(&*t);
},
}
Event::SoftBreak => {
output.push('\n');
},
}
_ => (),
}
@ -131,9 +131,9 @@ pub fn markdown_to_pdf(markdown: String) -> Result<Vec<u8>, tectonic::Error> {
#[cfg(test)]
mod tests {
use super::{markdown_to_latex, markdown_to_pdf};
use lopdf::Document;
use pretty_assertions::assert_eq;
use std::io::Cursor;
use lopdf::Document;
const MARKDOWN_IN: &str = r#"# First title
Some content
@ -205,11 +205,11 @@ Text
match Document::load_from(&mut file) {
Ok(doc) => {
assert_eq!("1.5", doc.version);
},
Err(_) => assert!(true)
}
},
Err(_) => assert!(true)
Err(_) => assert!(true),
}
}
Err(_) => assert!(true),
}
}
}

View File

@ -1,10 +1,10 @@
use std::ffi::OsStr;
use std::fs::File;
use std::io::{Read, Write};
use std::process::exit;
use std::path::PathBuf;
use std::ffi::OsStr;
use std::process::exit;
use clap::{crate_authors, crate_description, crate_name, crate_version, Command, Arg};
use clap::{crate_authors, crate_description, crate_name, crate_version, Arg, Command};
use md2pdf::{markdown_to_latex, markdown_to_pdf};
@ -17,36 +17,40 @@ macro_rules! unwrap {
exit(1);
}
}
}
};
}
fn main() {
let matches = Command::new(crate_name!())
.bin_name(crate_name!())
.version(crate_version!())
.author(crate_authors!("\n"))
.about(crate_description!())
.arg(Arg::new("INPUT")
.arg(
Arg::new("INPUT")
.long("input")
.short('i')
.help("Input markdown files")
.required(true)
.value_parser(clap::value_parser!(PathBuf))
.value_parser(clap::value_parser!(PathBuf)),
)
.arg(Arg::new("OUTPUT")
.arg(
Arg::new("OUTPUT")
.long("output")
.short('o')
.help("Output tex or pdf file")
.required(true)
.value_parser(clap::value_parser!(PathBuf))
.value_parser(clap::value_parser!(PathBuf)),
)
.get_matches();
let input_path = matches.get_one::<PathBuf>("INPUT").unwrap();
let mut content = String::new();
let mut input = unwrap!(File::open(input_path), "couldn't open input file");
unwrap!(input.read_to_string(&mut content), "couldn't read file content");
unwrap!(
input.read_to_string(&mut content),
"couldn't read file content"
);
let output_path = matches.get_one::<PathBuf>("OUTPUT").unwrap();
let output_path_ext = output_path.extension().and_then(OsStr::to_str);
@ -56,37 +60,33 @@ fn main() {
Some("tex") => {
let tex = markdown_to_latex(content);
unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
},
Some("pdf") => {
match markdown_to_pdf(content) {
Ok(data) => {
match output.write(&data) {
}
Some("pdf") => match markdown_to_pdf(content) {
Ok(data) => match output.write(&data) {
Ok(_) => {
exit(0);
},
}
Err(error) => {
eprintln!(
"error while writing file: {}", error
);
eprintln!("error while writing file: {}", error);
exit(1);
},
}
},
Err(error) => {
eprintln!(
"error while compiling latex: {}", error.description()
);
eprintln!("error while compiling latex: {}", error.description());
exit(1);
}
}
},
Some(ext) => {
eprintln!("unknown file format ({}) for output: {}", ext, output_path.display());
eprintln!(
"unknown file format ({}) for output: {}",
ext,
output_path.display()
);
exit(1);
},
}
None => {
eprintln!("unknown file format for output: {}", output_path.display());
exit(1);
},
}
}
}