Compare commits

..

No commits in common. "cb90c6a4adeade07f55e53b1a9aab2969f69527a" and "c4bf81cdb771497735220df56b364a074d369f0f" have entirely different histories.

4 changed files with 558 additions and 1895 deletions

2362
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,23 +3,14 @@ name = "md2pdf"
version = "0.0.1" version = "0.0.1"
authors = ["Thomas Forgione <thomas@forgione.fr>"] authors = ["Thomas Forgione <thomas@forgione.fr>"]
description = "A small utility to convert markdown files to pdf exploiting tectonic." description = "A small utility to convert markdown files to pdf exploiting tectonic."
edition = "2021" edition = "2018"
license = "MIT" license = "MIT"
repository = "https://gitea.tforgione.fr/tforgione/md2pdf" repository = "https://gitea.tforgione.fr/tforgione/md2pdf"
readme = "README.md" readme = "README.md"
cargo = "1.64"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
tectonic = "0.5.2" tectonic = "0.1.11"
pulldown-cmark = "0.5.3" pulldown-cmark = "0.5.2"
clap = { version = "4.0.7", features = ["cargo"] } clap = "2.33.0"
[lib]
name = "md2pdf"
path = "src/lib.rs"
[[bin]]
name = "md2pdf"
path = "src/main.rs"

View File

@ -82,7 +82,7 @@ pub fn markdown_to_latex(markdown: String) -> String {
Event::Start(Tag::Image(_, path, title)) => { Event::Start(Tag::Image(_, path, title)) => {
output.push_str("\\begin{figure}\n"); output.push_str("\\begin{figure}\n");
output.push_str("\\centering\n"); output.push_str("\\centering\n");
output.push_str("\\includegraphics[width=\\textwidth]{"); output.push_str("\\includegraphics[width=\\textwidth]{");;
output.push_str(&*path); output.push_str(&*path);
output.push_str("}\n"); output.push_str("}\n");
output.push_str("\\caption{"); output.push_str("\\caption{");

View File

@ -1,10 +1,8 @@
use std::fs::File; use std::fs::File;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::process::exit; use std::process::exit;
use std::path::PathBuf;
use std::ffi::OsStr;
use clap::{crate_authors, crate_description, crate_name, crate_version, Command, Arg}; use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
use md2pdf::{markdown_to_latex, markdown_to_pdf}; use md2pdf::{markdown_to_latex, markdown_to_pdf};
@ -22,71 +20,41 @@ macro_rules! unwrap {
fn main() { fn main() {
let matches = Command::new(crate_name!()) let matches = App::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(Arg::with_name("INPUT")
.long("input") .long("input")
.short('i') .short("i")
.help("Input markdown files") .help("Input markdown files")
.required(true) .required(true)
.value_parser(clap::value_parser!(PathBuf)) .takes_value(true))
) .arg(Arg::with_name("OUTPUT")
.arg(Arg::new("OUTPUT")
.long("output") .long("output")
.short('o') .short("o")
.help("Output tex or pdf file") .help("Output tex or pdf file")
.required(true) .required(true)
.value_parser(clap::value_parser!(PathBuf)) .takes_value(true))
)
.get_matches(); .get_matches();
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(matches.value_of("INPUT").unwrap()), "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.value_of("OUTPUT").unwrap();
let output_path_ext = output_path.extension().and_then(OsStr::to_str);
let mut output = unwrap!(File::create(output_path), "couldn't open output file"); let mut output = unwrap!(File::create(output_path), "couldn't open output file");
match output_path_ext { if output_path.ends_with(".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");
}, } else if output_path.ends_with(".pdf") {
Some("pdf") => { let data = unwrap!(markdown_to_pdf(content), "error while compiling latex, this is most likely a bug");
match markdown_to_pdf(content) { unwrap!(output.write(&data), "coudln't write output file");
Ok(data) => { } else {
match output.write(&data) { eprintln!("unknown file format for output: {}", output_path);
Ok(_) => {
exit(0);
},
Err(error) => {
eprintln!(
"error while writing file: {}", error
);
exit(1);
},
}
},
Err(error) => {
eprintln!(
"error while compiling latex: {}", error.description()
);
exit(1); exit(1);
} }
}
},
Some(ext) => {
eprintln!("unknown file format ({}) for output: {}", ext, output_path.display());
exit(1);
},
None => {
eprintln!("unknown file format for output: {}", output_path.display());
exit(1);
},
}
} }