Merge pull request 'Updates for md2pdf' (#1) from williamdes/md2pdf:master into master

Reviewed-on: #1
This commit is contained in:
tforgione 2022-10-02 15:27:27 +02:00
commit 4dbf939317
4 changed files with 1886 additions and 549 deletions

2344
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,14 +3,23 @@ name = "md2pdf"
version = "0.0.1"
authors = ["Thomas Forgione <thomas@forgione.fr>"]
description = "A small utility to convert markdown files to pdf exploiting tectonic."
edition = "2018"
edition = "2021"
license = "MIT"
repository = "https://gitea.tforgione.fr/tforgione/md2pdf"
readme = "README.md"
cargo = "1.64"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tectonic = "0.1.11"
pulldown-cmark = "0.5.2"
clap = "2.33.0"
tectonic = "0.5.2"
pulldown-cmark = "0.5.3"
clap = { version = "4.0.7", features = ["cargo"] }
[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)) => {
output.push_str("\\begin{figure}\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("}\n");
output.push_str("\\caption{");

View File

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