forked from tforgione/md2pdf
Merge pull request 'Updates for md2pdf' (#1) from williamdes/md2pdf:master into master
Reviewed-on: tforgione/md2pdf#1
This commit is contained in:
commit
4dbf939317
File diff suppressed because it is too large
Load Diff
17
Cargo.toml
17
Cargo.toml
|
@ -3,14 +3,23 @@ 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 = "2018"
|
edition = "2021"
|
||||||
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.1.11"
|
tectonic = "0.5.2"
|
||||||
pulldown-cmark = "0.5.2"
|
pulldown-cmark = "0.5.3"
|
||||||
clap = "2.33.0"
|
clap = { version = "4.0.7", features = ["cargo"] }
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "md2pdf"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "md2pdf"
|
||||||
|
path = "src/main.rs"
|
||||||
|
|
|
@ -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{");
|
||||||
|
|
72
src/main.rs
72
src/main.rs
|
@ -1,8 +1,10 @@
|
||||||
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, App, Arg};
|
use clap::{crate_authors, crate_description, crate_name, crate_version, Command, Arg};
|
||||||
|
|
||||||
use md2pdf::{markdown_to_latex, markdown_to_pdf};
|
use md2pdf::{markdown_to_latex, markdown_to_pdf};
|
||||||
|
|
||||||
|
@ -20,41 +22,71 @@ macro_rules! unwrap {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let matches = App::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::with_name("INPUT")
|
.arg(Arg::new("INPUT")
|
||||||
.long("input")
|
.long("input")
|
||||||
.short("i")
|
.short('i')
|
||||||
.help("Input markdown files")
|
.help("Input markdown files")
|
||||||
.required(true)
|
.required(true)
|
||||||
.takes_value(true))
|
.value_parser(clap::value_parser!(PathBuf))
|
||||||
.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)
|
||||||
.takes_value(true))
|
.value_parser(clap::value_parser!(PathBuf))
|
||||||
|
)
|
||||||
.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(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");
|
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");
|
let mut output = unwrap!(File::create(output_path), "couldn't open output file");
|
||||||
|
|
||||||
if output_path.ends_with(".tex") {
|
match output_path_ext {
|
||||||
let tex = markdown_to_latex(content);
|
Some("tex") => {
|
||||||
unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
|
let tex = markdown_to_latex(content);
|
||||||
} else if output_path.ends_with(".pdf") {
|
unwrap!(output.write(tex.as_bytes()), "couldn't write output file");
|
||||||
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");
|
Some("pdf") => {
|
||||||
} else {
|
match markdown_to_pdf(content) {
|
||||||
eprintln!("unknown file format for output: {}", output_path);
|
Ok(data) => {
|
||||||
exit(1);
|
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);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue