Add tests #2
34
src/lib.rs
34
src/lib.rs
|
@ -1,6 +1,6 @@
|
||||||
use pulldown_cmark::{Event, Parser, Tag};
|
use pulldown_cmark::{Event, Parser, Tag};
|
||||||
|
|
||||||
pub const LATEX_HEADER:&str = r#"\documentclass{scrartcl}
|
pub const LATEX_HEADER: &str = r#"\documentclass{scrartcl}
|
||||||
\usepackage{graphicx}
|
\usepackage{graphicx}
|
||||||
\usepackage{hyperref}
|
\usepackage{hyperref}
|
||||||
\usepackage{listings}
|
\usepackage{listings}
|
||||||
|
@ -50,11 +50,11 @@ pub fn markdown_to_latex(markdown: String) -> String {
|
||||||
match event {
|
match event {
|
||||||
Event::Start(Tag::Header(level)) => {
|
Event::Start(Tag::Header(level)) => {
|
||||||
output.push_str("\\");
|
output.push_str("\\");
|
||||||
for _ in 1 .. level {
|
for _ in 1..level {
|
||||||
output.push_str("sub");
|
output.push_str("sub");
|
||||||
}
|
}
|
||||||
output.push_str("section{");
|
output.push_str("section{");
|
||||||
},
|
}
|
||||||
Event::End(Tag::Header(_)) => output.push_str("}\n"),
|
Event::End(Tag::Header(_)) => output.push_str("}\n"),
|
||||||
|
|
||||||
Event::Start(Tag::Emphasis) => output.push_str("\\emph{"),
|
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("\\href{");
|
||||||
output.push_str(&*url);
|
output.push_str(&*url);
|
||||||
output.push_str("}{");
|
output.push_str("}{");
|
||||||
},
|
}
|
||||||
|
|
||||||
Event::End(Tag::Link(_, _, _)) => {
|
Event::End(Tag::Link(_, _, _)) => {
|
||||||
output.push_str("}");
|
output.push_str("}");
|
||||||
},
|
}
|
||||||
|
|
||||||
Event::Start(Tag::Image(_, path, title)) => {
|
Event::Start(Tag::Image(_, path, title)) => {
|
||||||
output.push_str("\\begin{figure}\n");
|
output.push_str("\\begin{figure}\n");
|
||||||
|
@ -88,32 +88,32 @@ pub fn markdown_to_latex(markdown: String) -> String {
|
||||||
output.push_str("\\caption{");
|
output.push_str("\\caption{");
|
||||||
output.push_str(&*title);
|
output.push_str(&*title);
|
||||||
output.push_str("}\n\\end{figure}\n");
|
output.push_str("}\n\\end{figure}\n");
|
||||||
},
|
}
|
||||||
|
|
||||||
Event::Start(Tag::Item) => output.push_str("\\item "),
|
Event::Start(Tag::Item) => output.push_str("\\item "),
|
||||||
Event::End(Tag::Item) => output.push_str("\n"),
|
Event::End(Tag::Item) => output.push_str("\n"),
|
||||||
|
|
||||||
Event::Start(Tag::CodeBlock(lang)) => {
|
Event::Start(Tag::CodeBlock(lang)) => {
|
||||||
if ! lang.is_empty() {
|
if !lang.is_empty() {
|
||||||
output.push_str("\\begin{lstlisting}[language=");
|
output.push_str("\\begin{lstlisting}[language=");
|
||||||
output.push_str(&*lang);
|
output.push_str(&*lang);
|
||||||
output.push_str("]\n");
|
output.push_str("]\n");
|
||||||
} else {
|
} else {
|
||||||
output.push_str("\\begin{lstlisting}\n");
|
output.push_str("\\begin{lstlisting}\n");
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
Event::End(Tag::CodeBlock(_)) => {
|
Event::End(Tag::CodeBlock(_)) => {
|
||||||
output.push_str("\n\\end{lstlisting}\n");
|
output.push_str("\n\\end{lstlisting}\n");
|
||||||
},
|
}
|
||||||
|
|
||||||
Event::Text(t) => {
|
Event::Text(t) => {
|
||||||
output.push_str(&*t);
|
output.push_str(&*t);
|
||||||
},
|
}
|
||||||
|
|
||||||
Event::SoftBreak => {
|
Event::SoftBreak => {
|
||||||
output.push('\n');
|
output.push('\n');
|
||||||
},
|
}
|
||||||
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
@ -131,9 +131,9 @@ pub fn markdown_to_pdf(markdown: String) -> Result<Vec<u8>, tectonic::Error> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{markdown_to_latex, markdown_to_pdf};
|
use super::{markdown_to_latex, markdown_to_pdf};
|
||||||
|
use lopdf::Document;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use lopdf::Document;
|
|
||||||
|
|
||||||
const MARKDOWN_IN: &str = r#"# First title
|
const MARKDOWN_IN: &str = r#"# First title
|
||||||
Some content
|
Some content
|
||||||
|
@ -202,14 +202,14 @@ Text
|
||||||
match output {
|
match output {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
let mut file = Cursor::new(data);
|
let mut file = Cursor::new(data);
|
||||||
match Document::load_from(&mut file){
|
match Document::load_from(&mut file) {
|
||||||
Ok(doc) => {
|
Ok(doc) => {
|
||||||
assert_eq!("1.5", doc.version);
|
assert_eq!("1.5", doc.version);
|
||||||
},
|
}
|
||||||
Err(_) => assert!(true)
|
Err(_) => assert!(true),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(_) => assert!(true)
|
Err(_) => assert!(true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
84
src/main.rs
84
src/main.rs
|
@ -1,10 +1,10 @@
|
||||||
|
use std::ffi::OsStr;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::process::exit;
|
|
||||||
use std::path::PathBuf;
|
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};
|
use md2pdf::{markdown_to_latex, markdown_to_pdf};
|
||||||
|
|
||||||
|
@ -17,36 +17,40 @@ macro_rules! unwrap {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let matches = Command::new(crate_name!())
|
let matches = Command::new(crate_name!())
|
||||||
.bin_name(crate_name!())
|
.bin_name(crate_name!())
|
||||||
.version(crate_version!())
|
.version(crate_version!())
|
||||||
.author(crate_authors!("\n"))
|
.author(crate_authors!("\n"))
|
||||||
.about(crate_description!())
|
.about(crate_description!())
|
||||||
.arg(Arg::new("INPUT")
|
.arg(
|
||||||
.long("input")
|
Arg::new("INPUT")
|
||||||
.short('i')
|
.long("input")
|
||||||
.help("Input markdown files")
|
.short('i')
|
||||||
.required(true)
|
.help("Input markdown files")
|
||||||
.value_parser(clap::value_parser!(PathBuf))
|
.required(true)
|
||||||
)
|
.value_parser(clap::value_parser!(PathBuf)),
|
||||||
.arg(Arg::new("OUTPUT")
|
)
|
||||||
.long("output")
|
.arg(
|
||||||
.short('o')
|
Arg::new("OUTPUT")
|
||||||
.help("Output tex or pdf file")
|
.long("output")
|
||||||
.required(true)
|
.short('o')
|
||||||
.value_parser(clap::value_parser!(PathBuf))
|
.help("Output tex or pdf file")
|
||||||
)
|
.required(true)
|
||||||
|
.value_parser(clap::value_parser!(PathBuf)),
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let input_path = matches.get_one::<PathBuf>("INPUT").unwrap();
|
let input_path = matches.get_one::<PathBuf>("INPUT").unwrap();
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
let mut input = unwrap!(File::open(input_path), "couldn't open input file");
|
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 = matches.get_one::<PathBuf>("OUTPUT").unwrap();
|
||||||
let output_path_ext = output_path.extension().and_then(OsStr::to_str);
|
let output_path_ext = output_path.extension().and_then(OsStr::to_str);
|
||||||
|
@ -56,37 +60,33 @@ fn main() {
|
||||||
Some("tex") => {
|
Some("tex") => {
|
||||||
let tex = markdown_to_latex(content);
|
let tex = markdown_to_latex(content);
|
||||||
unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
|
unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
|
||||||
},
|
}
|
||||||
Some("pdf") => {
|
Some("pdf") => match markdown_to_pdf(content) {
|
||||||
match markdown_to_pdf(content) {
|
Ok(data) => match output.write(&data) {
|
||||||
Ok(data) => {
|
Ok(_) => {
|
||||||
match output.write(&data) {
|
exit(0);
|
||||||
Ok(_) => {
|
}
|
||||||
exit(0);
|
|
||||||
},
|
|
||||||
Err(error) => {
|
|
||||||
eprintln!(
|
|
||||||
"error while writing file: {}", error
|
|
||||||
);
|
|
||||||
exit(1);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
eprintln!(
|
eprintln!("error while writing file: {}", error);
|
||||||
"error while compiling latex: {}", error.description()
|
|
||||||
);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("error while compiling latex: {}", error.description());
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Some(ext) => {
|
Some(ext) => {
|
||||||
eprintln!("unknown file format ({}) for output: {}", ext, output_path.display());
|
eprintln!(
|
||||||
|
"unknown file format ({}) for output: {}",
|
||||||
|
ext,
|
||||||
|
output_path.display()
|
||||||
|
);
|
||||||
exit(1);
|
exit(1);
|
||||||
},
|
}
|
||||||
None => {
|
None => {
|
||||||
eprintln!("unknown file format for output: {}", output_path.display());
|
eprintln!("unknown file format for output: {}", output_path.display());
|
||||||
exit(1);
|
exit(1);
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue