Updates for md2pdf #1

Merged
tforgione merged 6 commits from williamdes/md2pdf:master into master 2022-10-02 15:27:28 +02:00
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"
Review

I could not upgrade above, there is a compilation (build) error I can not solve.

RUST_BACKTRACE=full cargo build
   Compiling tectonic_bridge_harfbuzz v0.2.5
   Compiling num-integer v0.1.45
   Compiling nix v0.22.3
   Compiling thiserror-impl v1.0.37
   Compiling rand_chacha v0.2.0
   Compiling xdg v2.4.1
   Compiling rand_pcg v0.2.1
   Compiling serde_derive v1.0.145
   Compiling openssl-macros v0.1.0
   Compiling parse-zoneinfo v0.3.0
   Compiling darling_core v0.12.4
   Compiling globset v0.4.6
error: failed to run custom build command for `tectonic_bridge_harfbuzz v0.2.5`

Caused by:
  process didn't exit successfully: `/mnt/Dev/@williamdes/md2pdf/target/debug/build/tectonic_bridge_harfbuzz-8690ea9a67121bf2/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotPresent', /home/williamdes/.cargo/registry/src/github.com-1ecc6299db9ec823/tectonic_bridge_harfbuzz-0.2.5/build.rs:76:77
  stack backtrace:
     0:     0x55b46e83bc3d - std::backtrace_rs::backtrace::libunwind::trace::h8217d0a8f3fd2f41
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
     1:     0x55b46e83bc3d - std::backtrace_rs::backtrace::trace_unsynchronized::h308103876b3af410
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
     2:     0x55b46e83bc3d - std::sys_common::backtrace::_print_fmt::hc208018c6153605e
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:66:5
     3:     0x55b46e83bc3d - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf89a7ed694dfb585
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:45:22
     4:     0x55b46e85bf8c - core::fmt::write::h21038c1382fe4264
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/fmt/mod.rs:1197:17
     5:     0x55b46e838181 - std::io::Write::write_fmt::h7dbb1c9a3c254aef
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/io/mod.rs:1672:15
     6:     0x55b46e83d515 - std::sys_common::backtrace::_print::h4e8889719c9ddeb8
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:48:5
     7:     0x55b46e83d515 - std::sys_common::backtrace::print::h1506fe2cb3022667
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:35:9
     8:     0x55b46e83d515 - std::panicking::default_hook::{{closure}}::hd9d7ce2a8a782440
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:295:22
     9:     0x55b46e83d236 - std::panicking::default_hook::h5b16ec25444b1b5d
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:314:9
    10:     0x55b46e83daa6 - std::panicking::rust_panic_with_hook::hb0138cb6e6fea3e4
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:698:17
    11:     0x55b46e83d997 - std::panicking::begin_panic_handler::{{closure}}::h4cb67095557cd1aa
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:588:13
    12:     0x55b46e83c0f4 - std::sys_common::backtrace::__rust_end_short_backtrace::h2bfcac279dcdc911
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:138:18
    13:     0x55b46e83d6c9 - rust_begin_unwind
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5
    14:     0x55b46e7b17b3 - core::panicking::panic_fmt::h1de71520faaa17d3
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14
    15:     0x55b46e7b1843 - core::result::unwrap_failed::hc0baa33ef8bc7db8
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1805:5
    16:     0x55b46e7f275c - core::result::Result<T,E>::unwrap::h5772858b296be7d4
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1098:23
    17:     0x55b46e7b1ef3 - build_script_build::inner::build_harfbuzz::h8d91ba24c35a2b03
                                 at /home/williamdes/.cargo/registry/src/github.com-1ecc6299db9ec823/tectonic_bridge_harfbuzz-0.2.5/build.rs:76:38
    18:     0x55b46e7b4f66 - build_script_build::main::h5ce42637885aa4ea
                                 at /home/williamdes/.cargo/registry/src/github.com-1ecc6299db9ec823/tectonic_bridge_harfbuzz-0.2.5/build.rs:150:5
    19:     0x55b46e7b353b - core::ops::function::FnOnce::call_once::hffc9b1c427ac03ce
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5
    20:     0x55b46e7b346e - std::sys_common::backtrace::__rust_begin_short_backtrace::h5adb0fd18077f5c9
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:122:18
    21:     0x55b46e7b4e31 - std::rt::lang_start::{{closure}}::h2282c690b02fce50
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:145:18
    22:     0x55b46e83409e - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h4937aaa125c8d4b2
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:280:13
    23:     0x55b46e83409e - std::panicking::try::do_call::h6f5c70e8b0a34f92
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:492:40
    24:     0x55b46e83409e - std::panicking::try::h68766ba264ecf2e2
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:456:19
    25:     0x55b46e83409e - std::panic::catch_unwind::hc36033d2f9cc04af
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panic.rs:137:14
    26:     0x55b46e83409e - std::rt::lang_start_internal::{{closure}}::h78c037f4a1a28ded
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:128:48
    27:     0x55b46e83409e - std::panicking::try::do_call::he6e1fffda4c750ee
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:492:40
    28:     0x55b46e83409e - std::panicking::try::h48a77ddbb2f4c87a
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:456:19
    29:     0x55b46e83409e - std::panic::catch_unwind::hfa809b06a550a9e7
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panic.rs:137:14
    30:     0x55b46e83409e - std::rt::lang_start_internal::h4db69ed48eaca005
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:128:20
    31:     0x55b46e7b4e00 - std::rt::lang_start::h5727ba89c99e395f
                                 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:144:17
    32:     0x55b46e7b4f8c - main
    33:     0x7f61e80f7d90 - __libc_start_call_main
                                 at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    34:     0x7f61e80f7e40 - __libc_start_main_impl
                                 at ./csu/../csu/libc-start.c:392:3
    35:     0x55b46e7b19f5 - _start
    36:                0x0 - <unknown>
warning: build failed, waiting for other jobs to finish...
I could not upgrade above, there is a compilation (build) error I can not solve. ``` RUST_BACKTRACE=full cargo build Compiling tectonic_bridge_harfbuzz v0.2.5 Compiling num-integer v0.1.45 Compiling nix v0.22.3 Compiling thiserror-impl v1.0.37 Compiling rand_chacha v0.2.0 Compiling xdg v2.4.1 Compiling rand_pcg v0.2.1 Compiling serde_derive v1.0.145 Compiling openssl-macros v0.1.0 Compiling parse-zoneinfo v0.3.0 Compiling darling_core v0.12.4 Compiling globset v0.4.6 error: failed to run custom build command for `tectonic_bridge_harfbuzz v0.2.5` Caused by: process didn't exit successfully: `/mnt/Dev/@williamdes/md2pdf/target/debug/build/tectonic_bridge_harfbuzz-8690ea9a67121bf2/build-script-build` (exit status: 101) --- stderr thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotPresent', /home/williamdes/.cargo/registry/src/github.com-1ecc6299db9ec823/tectonic_bridge_harfbuzz-0.2.5/build.rs:76:77 stack backtrace: 0: 0x55b46e83bc3d - std::backtrace_rs::backtrace::libunwind::trace::h8217d0a8f3fd2f41 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x55b46e83bc3d - std::backtrace_rs::backtrace::trace_unsynchronized::h308103876b3af410 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x55b46e83bc3d - std::sys_common::backtrace::_print_fmt::hc208018c6153605e at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:66:5 3: 0x55b46e83bc3d - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf89a7ed694dfb585 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:45:22 4: 0x55b46e85bf8c - core::fmt::write::h21038c1382fe4264 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/fmt/mod.rs:1197:17 5: 0x55b46e838181 - std::io::Write::write_fmt::h7dbb1c9a3c254aef at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/io/mod.rs:1672:15 6: 0x55b46e83d515 - std::sys_common::backtrace::_print::h4e8889719c9ddeb8 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:48:5 7: 0x55b46e83d515 - std::sys_common::backtrace::print::h1506fe2cb3022667 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:35:9 8: 0x55b46e83d515 - std::panicking::default_hook::{{closure}}::hd9d7ce2a8a782440 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:295:22 9: 0x55b46e83d236 - std::panicking::default_hook::h5b16ec25444b1b5d at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:314:9 10: 0x55b46e83daa6 - std::panicking::rust_panic_with_hook::hb0138cb6e6fea3e4 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:698:17 11: 0x55b46e83d997 - std::panicking::begin_panic_handler::{{closure}}::h4cb67095557cd1aa at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:588:13 12: 0x55b46e83c0f4 - std::sys_common::backtrace::__rust_end_short_backtrace::h2bfcac279dcdc911 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:138:18 13: 0x55b46e83d6c9 - rust_begin_unwind at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5 14: 0x55b46e7b17b3 - core::panicking::panic_fmt::h1de71520faaa17d3 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14 15: 0x55b46e7b1843 - core::result::unwrap_failed::hc0baa33ef8bc7db8 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1805:5 16: 0x55b46e7f275c - core::result::Result<T,E>::unwrap::h5772858b296be7d4 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1098:23 17: 0x55b46e7b1ef3 - build_script_build::inner::build_harfbuzz::h8d91ba24c35a2b03 at /home/williamdes/.cargo/registry/src/github.com-1ecc6299db9ec823/tectonic_bridge_harfbuzz-0.2.5/build.rs:76:38 18: 0x55b46e7b4f66 - build_script_build::main::h5ce42637885aa4ea at /home/williamdes/.cargo/registry/src/github.com-1ecc6299db9ec823/tectonic_bridge_harfbuzz-0.2.5/build.rs:150:5 19: 0x55b46e7b353b - core::ops::function::FnOnce::call_once::hffc9b1c427ac03ce at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5 20: 0x55b46e7b346e - std::sys_common::backtrace::__rust_begin_short_backtrace::h5adb0fd18077f5c9 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:122:18 21: 0x55b46e7b4e31 - std::rt::lang_start::{{closure}}::h2282c690b02fce50 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:145:18 22: 0x55b46e83409e - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h4937aaa125c8d4b2 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:280:13 23: 0x55b46e83409e - std::panicking::try::do_call::h6f5c70e8b0a34f92 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:492:40 24: 0x55b46e83409e - std::panicking::try::h68766ba264ecf2e2 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:456:19 25: 0x55b46e83409e - std::panic::catch_unwind::hc36033d2f9cc04af at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panic.rs:137:14 26: 0x55b46e83409e - std::rt::lang_start_internal::{{closure}}::h78c037f4a1a28ded at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:128:48 27: 0x55b46e83409e - std::panicking::try::do_call::he6e1fffda4c750ee at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:492:40 28: 0x55b46e83409e - std::panicking::try::h48a77ddbb2f4c87a at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:456:19 29: 0x55b46e83409e - std::panic::catch_unwind::hfa809b06a550a9e7 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panic.rs:137:14 30: 0x55b46e83409e - std::rt::lang_start_internal::h4db69ed48eaca005 at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:128:20 31: 0x55b46e7b4e00 - std::rt::lang_start::h5727ba89c99e395f at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:144:17 32: 0x55b46e7b4f8c - main 33: 0x7f61e80f7d90 - __libc_start_call_main at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16 34: 0x7f61e80f7e40 - __libc_start_main_impl at ./csu/../csu/libc-start.c:392:3 35: 0x55b46e7b19f5 - _start 36: 0x0 - <unknown> warning: build failed, waiting for other jobs to finish... ```
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);
},
}
}