From 4d0b6df0f2885868a16eb746e6cad53f349b1dae Mon Sep 17 00:00:00 2001 From: William Desportes Date: Sun, 2 Oct 2022 20:27:52 +0200 Subject: [PATCH 1/6] Add tests for the library --- Cargo.lock | 347 ++++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 4 + src/lib.rs | 85 +++++++++++++ 3 files changed, 431 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8bcef4b..75ef883 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base-x" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270" + [[package]] name = "base64" version = "0.13.0" @@ -191,8 +197,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" dependencies = [ "iana-time-zone", + "js-sys", "num-integer", "num-traits", + "time 0.1.44", + "wasm-bindgen", "winapi 0.3.9", ] @@ -289,6 +298,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "const_fn" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935" + [[package]] name = "core-foundation" version = "0.9.3" @@ -342,6 +357,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctor" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdffe87e1d521a10f9696f833fe502293ea446d7f256c06128293a4119bdf4cb" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "darling" version = "0.12.4" @@ -414,6 +439,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.9.0" @@ -463,12 +494,88 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "discard" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" + +[[package]] +name = "dtoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" + [[package]] name = "either" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "encoding" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" +dependencies = [ + "encoding-index-japanese", + "encoding-index-korean", + "encoding-index-simpchinese", + "encoding-index-singlebyte", + "encoding-index-tradchinese", +] + +[[package]] +name = "encoding-index-japanese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-korean" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-simpchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-singlebyte" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-tradchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding_index_tests" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" + [[package]] name = "encoding_rs" version = "0.8.31" @@ -771,7 +878,7 @@ checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" dependencies = [ "bytes", "fnv", - "itoa", + "itoa 1.0.3", ] [[package]] @@ -818,7 +925,7 @@ dependencies = [ "http-body", "httparse", "httpdate", - "itoa", + "itoa 1.0.3", "pin-project-lite", "socket2", "tokio", @@ -941,6 +1048,12 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b" +[[package]] +name = "itoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" + [[package]] name = "itoa" version = "1.0.3" @@ -1015,6 +1128,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "log" version = "0.4.17" @@ -1024,6 +1143,25 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "lopdf" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8146695b97752d9c66da0092c6364f8f3ca683f5ea34341db21e5550c3b8c4f4" +dependencies = [ + "chrono", + "dtoa", + "encoding", + "flate2", + "itoa 0.4.8", + "lazy_static", + "linked-hash-map", + "log", + "pom", + "time 0.2.27", + "weezl", +] + [[package]] name = "md-5" version = "0.9.1" @@ -1040,6 +1178,8 @@ name = "md2pdf" version = "0.0.3" dependencies = [ "clap 4.0.8", + "lopdf", + "pretty_assertions", "pulldown-cmark", "tectonic", ] @@ -1327,6 +1467,15 @@ version = "6.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" +[[package]] +name = "output_vt100" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "parse-zoneinfo" version = "0.3.0" @@ -1389,7 +1538,7 @@ checksum = "4c8717927f9b79515e565a64fe46c38b8cd0427e64c40680b14a7365ab09ac8d" dependencies = [ "once_cell", "pest", - "sha1", + "sha1 0.10.5", ] [[package]] @@ -1493,12 +1642,30 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" +[[package]] +name = "pom" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e2192780e9f8e282049ff9bffcaa28171e1cb0844f49ed5374e518ae6024ec" + [[package]] name = "ppv-lite86" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "pretty_assertions" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a25e9bcb20aa780fd0bb16b72403a9064d6b3f22f026946029acb941a50af755" +dependencies = [ + "ctor", + "diff", + "output_vt100", + "yansi", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1523,6 +1690,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + [[package]] name = "proc-macro2" version = "1.0.46" @@ -1732,6 +1905,15 @@ version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + [[package]] name = "ryu" version = "1.0.11" @@ -1780,6 +1962,21 @@ dependencies = [ "libc", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "serde" version = "1.0.145" @@ -1806,7 +2003,7 @@ version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" dependencies = [ - "itoa", + "itoa 1.0.3", "ryu", "serde", ] @@ -1818,11 +2015,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" dependencies = [ "form_urlencoded", - "itoa", + "itoa 1.0.3", "ryu", "serde", ] +[[package]] +name = "sha1" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1da05c97445caa12d05e848c4a4fcbbea29e748ac28f7e80e9b010392063770" +dependencies = [ + "sha1_smol", +] + [[package]] name = "sha1" version = "0.10.5" @@ -1834,6 +2040,12 @@ dependencies = [ "digest 0.10.5", ] +[[package]] +name = "sha1_smol" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" + [[package]] name = "sha2" version = "0.9.9" @@ -1881,6 +2093,64 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "standback" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e113fb6f3de07a243d434a56ec6f186dfd51cb08448239fe7bcae73f87ff28ff" +dependencies = [ + "version_check", +] + +[[package]] +name = "stdweb" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5" +dependencies = [ + "discard", + "rustc_version", + "stdweb-derive", + "stdweb-internal-macros", + "stdweb-internal-runtime", + "wasm-bindgen", +] + +[[package]] +name = "stdweb-derive" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "serde_derive", + "syn", +] + +[[package]] +name = "stdweb-internal-macros" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11" +dependencies = [ + "base-x", + "proc-macro2", + "quote", + "serde", + "serde_derive", + "serde_json", + "sha1 0.6.1", + "syn", +] + +[[package]] +name = "stdweb-internal-runtime" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" + [[package]] name = "strsim" version = "0.8.0" @@ -2319,6 +2589,55 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi 0.3.9", +] + +[[package]] +name = "time" +version = "0.2.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4752a97f8eebd6854ff91f1c1824cd6160626ac4bd44287f7f4ea2035a02a242" +dependencies = [ + "const_fn", + "libc", + "standback", + "stdweb", + "time-macros", + "version_check", + "winapi 0.3.9", +] + +[[package]] +name = "time-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "957e9c6e26f12cb6d0dd7fc776bb67a706312e7299aed74c8dd5b17ebb27e2f1" +dependencies = [ + "proc-macro-hack", + "time-macros-impl", +] + +[[package]] +name = "time-macros-impl" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3c141a1b43194f3f56a1411225df8646c55781d5f26db825b3d98507eb482f" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "standback", + "syn", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2591,6 +2910,12 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -2692,6 +3017,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "weezl" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" + [[package]] name = "which" version = "4.3.0" @@ -2817,6 +3148,12 @@ dependencies = [ "dirs 4.0.0", ] +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "zip" version = "0.5.13" diff --git a/Cargo.toml b/Cargo.toml index 7aa3621..0e419fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,7 @@ path = "src/lib.rs" [[bin]] name = "md2pdf" path = "src/main.rs" + +[dev-dependencies] +pretty_assertions = "1.3.0" +lopdf = "0.27.0" diff --git a/src/lib.rs b/src/lib.rs index 7260522..326acfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,3 +128,88 @@ pub fn markdown_to_pdf(markdown: String) -> Result, tectonic::Error> { tectonic::latex_to_pdf(markdown_to_latex(markdown)) } +#[cfg(test)] +mod tests { + use super::{markdown_to_latex, markdown_to_pdf}; + use pretty_assertions::assert_eq; + use std::io::Cursor; + use lopdf::Document; + + const MARKDOWN_IN: &str = r#"# First title +Some content +## Second level +Text +[link](https://example.com) +**Bold** +__Italic__ +"#; + const LATEXT_OUT: &str = r#"\documentclass{scrartcl} +\usepackage{graphicx} +\usepackage{hyperref} +\usepackage{listings} +\usepackage{xcolor} +\definecolor{colKeys}{rgb}{0,0.5,0} +\definecolor{colIdentifier}{rgb}{0,0,0} +\definecolor{colComments}{rgb}{0,0.5,1} +\definecolor{colString}{rgb}{0.6,0.1,0.1} +\definecolor{colBackground}{rgb}{0.95,0.95,1} +\lstset{%configuration de listings + float=hbp,% + basicstyle=\ttfamily\small,% + % + identifierstyle=\color{colIdentifier}, % + keywordstyle=\color{colKeys}, % + stringstyle=\color{colString}, % + commentstyle=\color{colComments}\textit, % + % + backgroundcolor=\color{colBackground},% + % + columns=flexible, % + tabsize=2, % + frame=trbl, % + %frameround=tttt,% + extendedchars=true, % + showspaces=false, % + showstringspaces=false, % + numbers=left, % + numberstyle=\tiny, % + breaklines=true, % + breakautoindent=true, % + captionpos=b,% + xrightmargin=0.2cm, % + xleftmargin=0.2cm +} +\begin{document} +\section{First title} +Some content\subsection{Second level} +Text +\href{https://example.com}{link} +\textbf{Bold} +\textbf{Italic} +\end{document} +"#; + + #[test] + fn test_md_to_latex() { + let output = markdown_to_latex(MARKDOWN_IN.to_string()); + assert_eq!(LATEXT_OUT, output); + } + + #[test] + fn test_latex_to_pdf() { + let output = markdown_to_pdf(MARKDOWN_IN.to_string()); + + match output { + Ok(data) => { + let mut file = Cursor::new(data); + match Document::load_from(&mut file){ + Ok(doc) => { + assert_eq!("1.5", doc.version); + }, + Err(_) => assert!(true) + } + }, + Err(_) => assert!(true) + } + } +} \ No newline at end of file -- 2.40.1 From 0d1b9de1d69c46edef90012466fb473a1014f1a6 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Sun, 2 Oct 2022 20:28:50 +0200 Subject: [PATCH 2/6] Run cargo-fmt --- src/lib.rs | 36 +++++++++++------------ src/main.rs | 84 ++++++++++++++++++++++++++--------------------------- 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 326acfb..9f051e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ 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{hyperref} \usepackage{listings} @@ -50,11 +50,11 @@ pub fn markdown_to_latex(markdown: String) -> String { match event { Event::Start(Tag::Header(level)) => { output.push_str("\\"); - for _ in 1 .. level { + for _ in 1..level { output.push_str("sub"); } output.push_str("section{"); - }, + } Event::End(Tag::Header(_)) => output.push_str("}\n"), 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(&*url); output.push_str("}{"); - }, + } Event::End(Tag::Link(_, _, _)) => { output.push_str("}"); - }, + } Event::Start(Tag::Image(_, path, title)) => { 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(&*title); output.push_str("}\n\\end{figure}\n"); - }, + } Event::Start(Tag::Item) => output.push_str("\\item "), Event::End(Tag::Item) => output.push_str("\n"), Event::Start(Tag::CodeBlock(lang)) => { - if ! lang.is_empty() { + if !lang.is_empty() { output.push_str("\\begin{lstlisting}[language="); output.push_str(&*lang); output.push_str("]\n"); } else { output.push_str("\\begin{lstlisting}\n"); } - }, + } Event::End(Tag::CodeBlock(_)) => { output.push_str("\n\\end{lstlisting}\n"); - }, + } Event::Text(t) => { output.push_str(&*t); - }, + } Event::SoftBreak => { output.push('\n'); - }, + } _ => (), } @@ -131,9 +131,9 @@ pub fn markdown_to_pdf(markdown: String) -> Result, tectonic::Error> { #[cfg(test)] mod tests { use super::{markdown_to_latex, markdown_to_pdf}; + use lopdf::Document; use pretty_assertions::assert_eq; use std::io::Cursor; - use lopdf::Document; const MARKDOWN_IN: &str = r#"# First title Some content @@ -202,14 +202,14 @@ Text match output { Ok(data) => { let mut file = Cursor::new(data); - match Document::load_from(&mut file){ + match Document::load_from(&mut file) { Ok(doc) => { assert_eq!("1.5", doc.version); - }, - Err(_) => assert!(true) + } + Err(_) => assert!(true), } - }, - Err(_) => assert!(true) + } + Err(_) => assert!(true), } } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 3d9b4c4..01ed7af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ +use std::ffi::OsStr; use std::fs::File; use std::io::{Read, Write}; -use std::process::exit; 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}; @@ -17,36 +17,40 @@ macro_rules! unwrap { exit(1); } } - } + }; } fn main() { - let matches = Command::new(crate_name!()) .bin_name(crate_name!()) .version(crate_version!()) .author(crate_authors!("\n")) .about(crate_description!()) - .arg(Arg::new("INPUT") - .long("input") - .short('i') - .help("Input markdown files") - .required(true) - .value_parser(clap::value_parser!(PathBuf)) - ) - .arg(Arg::new("OUTPUT") - .long("output") - .short('o') - .help("Output tex or pdf file") - .required(true) - .value_parser(clap::value_parser!(PathBuf)) - ) + .arg( + Arg::new("INPUT") + .long("input") + .short('i') + .help("Input markdown files") + .required(true) + .value_parser(clap::value_parser!(PathBuf)), + ) + .arg( + Arg::new("OUTPUT") + .long("output") + .short('o') + .help("Output tex or pdf file") + .required(true) + .value_parser(clap::value_parser!(PathBuf)), + ) .get_matches(); let input_path = matches.get_one::("INPUT").unwrap(); let mut content = String::new(); 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::("OUTPUT").unwrap(); let output_path_ext = output_path.extension().and_then(OsStr::to_str); @@ -56,37 +60,33 @@ fn main() { 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); - }, - } - }, + } + Some("pdf") => match markdown_to_pdf(content) { + Ok(data) => match output.write(&data) { + Ok(_) => { + exit(0); + } Err(error) => { - eprintln!( - "error while compiling latex: {}", error.description() - ); + 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()); + eprintln!( + "unknown file format ({}) for output: {}", + ext, + output_path.display() + ); exit(1); - }, + } None => { eprintln!("unknown file format for output: {}", output_path.display()); exit(1); - }, + } } } -- 2.40.1 From 1867256af51074321590ab4adcd9c28ec75a41e3 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Sun, 2 Oct 2022 20:30:01 +0200 Subject: [PATCH 3/6] Mark uage block as sh command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 867be4c..99d2fdf 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ### Usage -``` +```sh md2pdf -i input.md -o output.pdf ``` -- 2.40.1 From 958fd07bed05addae494b4cee72992a6a9ce1a90 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Mon, 3 Oct 2022 17:03:15 +0200 Subject: [PATCH 4/6] Add more cases to the input code to test --- src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9f051e1..496ccd8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -142,6 +142,12 @@ Text [link](https://example.com) **Bold** __Italic__ + +some code: +```sh +sudo make-it-work +``` +issue [#12345](https://example.com) "#; const LATEXT_OUT: &str = r#"\documentclass{scrartcl} \usepackage{graphicx} @@ -185,7 +191,11 @@ Some content\subsection{Second level} Text \href{https://example.com}{link} \textbf{Bold} -\textbf{Italic} +\textbf{Italic}some code:\begin{lstlisting}[language=sh] +sudo make-it-work + +\end{lstlisting} +issue \href{https://example.com}{#12345} \end{document} "#; -- 2.40.1 From e6236810d3d4eace52c83dd8dcecafbdf9a3867c Mon Sep 17 00:00:00 2001 From: William Desportes Date: Mon, 3 Oct 2022 17:40:16 +0200 Subject: [PATCH 5/6] Import logic from crate md2tex Source: https://github.com/lbeckman314/md2tex/blob/25fa878ccce122c224c24659ee1c1dd30c8a5d51/src/lib.rs --- Cargo.lock | 10 +++ Cargo.toml | 1 + src/lib.rs | 245 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 249 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 75ef883..469e1fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -304,6 +304,15 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -1178,6 +1187,7 @@ name = "md2pdf" version = "0.0.3" dependencies = [ "clap 4.0.8", + "convert_case", "lopdf", "pretty_assertions", "pulldown-cmark", diff --git a/Cargo.toml b/Cargo.toml index 0e419fb..63dbccf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ readme = "README.md" tectonic = "0.9" pulldown-cmark = "0.5.3" clap = { version = "4.0.7", features = ["cargo"] } +convert_case = "0.6.0" [lib] name = "md2pdf" diff --git a/src/lib.rs b/src/lib.rs index 496ccd8..de82966 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +use convert_case::{Case, Casing}; use pulldown_cmark::{Event, Parser, Tag}; pub const LATEX_HEADER: &str = r#"\documentclass{scrartcl} @@ -41,26 +42,83 @@ pub const LATEX_HEADER: &str = r#"\documentclass{scrartcl} pub const LATEX_FOOTER: &str = "\n\\end{document}\n"; +/// Used to keep track of current pulldown_cmark "event". +/// TODO: Is there a native pulldown_cmark method to do this? +#[derive(Debug)] +enum EventType { + //Code, + Emphasis, + Header, + //Html, + Strong, + Table, + TableHead, + Text, +} + +struct CurrentType { + event_type: EventType, +} + +/** + * Part of this function is Copyright Liam Beckman (license: MPL-2.0) + * Source: https://github.com/lbeckman314/md2tex/blob/25fa878ccce122c224c24659ee1c1dd30c8a5d51/src/lib.rs + * + */ pub fn markdown_to_latex(markdown: String) -> String { let mut output = String::from(LATEX_HEADER); let parser = Parser::new(&markdown); + let mut header_value = String::new(); + + let mut current: CurrentType = CurrentType { + event_type: EventType::Text, + }; + let mut cells = 0; + + let mut equation_mode = false; + let mut buffer = String::new(); + for event in parser { match event { Event::Start(Tag::Header(level)) => { + current.event_type = EventType::Header; + output.push_str("\n"); output.push_str("\\"); - for _ in 1..level { - output.push_str("sub"); + match level { + -1 => output.push_str("part{"), + 0 => output.push_str("chapter{"), + 1 => output.push_str("section{"), + 2 => output.push_str("subsection{"), + 3 => output.push_str("subsubsection{"), + 4 => output.push_str("paragraph{"), + 5 => output.push_str("subparagraph{"), + _ => eprintln!("header is out of range."), } - output.push_str("section{"); } - Event::End(Tag::Header(_)) => output.push_str("}\n"), + Event::End(Tag::Header(_)) => { + output.push_str("}\n"); + output.push_str("\\"); + output.push_str("label{"); + output.push_str(&header_value); + output.push_str("}\n"); - Event::Start(Tag::Emphasis) => output.push_str("\\emph{"), + output.push_str("\\"); + output.push_str("label{"); + output.push_str(&header_value.to_case(Case::Kebab)); + output.push_str("}\n"); + } + Event::Start(Tag::Emphasis) => { + current.event_type = EventType::Emphasis; + output.push_str("\\emph{"); + } Event::End(Tag::Emphasis) => output.push_str("}"), - Event::Start(Tag::Strong) => output.push_str("\\textbf{"), + Event::Start(Tag::Strong) => { + current.event_type = EventType::Strong; + output.push_str("\\textbf{"); + } Event::End(Tag::Strong) => output.push_str("}"), Event::Start(Tag::List(None)) => output.push_str("\\begin{itemize}\n"), @@ -69,6 +127,17 @@ pub fn markdown_to_latex(markdown: String) -> String { Event::Start(Tag::List(Some(_))) => output.push_str("\\begin{enumerate}\n"), Event::End(Tag::List(Some(_))) => output.push_str("\\end{enumerate}\n"), + Event::Start(Tag::Paragraph) => { + output.push_str("\n"); + } + + Event::End(Tag::Paragraph) => { + // ~ adds a space to prevent + // "There's no line here to end" error on empty lines. + output.push_str(r"~\\"); + output.push_str("\n"); + } + Event::Start(Tag::Link(_, url, _)) => { output.push_str("\\href{"); output.push_str(&*url); @@ -79,6 +148,95 @@ pub fn markdown_to_latex(markdown: String) -> String { output.push_str("}"); } + Event::Start(Tag::Table(_)) => { + current.event_type = EventType::Table; + let table_start = vec![ + "\n", + r"\begingroup", + r"\setlength{\LTleft}{-20cm plus -1fill}", + r"\setlength{\LTright}{\LTleft}", + r"\begin{longtable}{!!!}", + r"\hline", + r"\hline", + "\n", + ]; + for element in table_start { + output.push_str(element); + output.push_str("\n"); + } + } + + Event::Start(Tag::TableHead) => { + current.event_type = EventType::TableHead; + } + + Event::End(Tag::TableHead) => { + output.truncate(output.len() - 2); + output.push_str(r"\\"); + output.push_str("\n"); + + output.push_str(r"\hline"); + output.push_str("\n"); + + // we presume that a table follows every table head. + current.event_type = EventType::Table; + } + + Event::End(Tag::Table(_)) => { + let table_end = vec![ + r"\arrayrulecolor{black}\hline", + r"\end{longtable}", + r"\endgroup", + "\n", + ]; + + for element in table_end { + output.push_str(element); + output.push_str("\n"); + } + + let mut cols = String::new(); + for _i in 0..cells { + cols.push_str(&format!( + r"C{{{width}\textwidth}} ", + width = 1. / cells as f64 + )); + } + output = output.replace("!!!", &cols); + cells = 0; + current.event_type = EventType::Text; + } + + Event::Start(Tag::TableCell) => match current.event_type { + EventType::TableHead => { + output.push_str(r"\bfseries{"); + } + _ => (), + }, + + Event::End(Tag::TableCell) => { + match current.event_type { + EventType::TableHead => { + output.push_str(r"}"); + cells += 1; + } + _ => (), + } + + output.push_str(" & "); + } + + Event::Start(Tag::TableRow) => { + current.event_type = EventType::Table; + } + + Event::End(Tag::TableRow) => { + output.truncate(output.len() - 2); + output.push_str(r"\\"); + output.push_str(r"\arrayrulecolor{lightgray}\hline"); + output.push_str("\n"); + } + Event::Start(Tag::Image(_, path, title)) => { output.push_str("\\begin{figure}\n"); output.push_str("\\centering\n"); @@ -105,16 +263,89 @@ pub fn markdown_to_latex(markdown: String) -> String { Event::End(Tag::CodeBlock(_)) => { output.push_str("\n\\end{lstlisting}\n"); + current.event_type = EventType::Text; + } + + Event::Code(t) => { + output.push_str("\\lstinline|"); + match current.event_type { + EventType::Header => output + .push_str(&*t.replace("#", r"\#").replace("…", "...").replace("З", "3")), + _ => output + .push_str(&*t.replace("…", "...").replace("З", "3").replace("�", r"\�")), + } + output.push_str("|"); } Event::Text(t) => { - output.push_str(&*t); + // if "\(" or "\[" are encountered, then begin equation + // and don't replace any characters. + let delim_start = vec![r"\(", r"\["]; + let delim_end = vec![r"\)", r"\]"]; + + if buffer.len() > 100 { + buffer.clear(); + } + + buffer.push_str(&t.clone().into_string()); + + match current.event_type { + EventType::Strong + | EventType::Emphasis + | EventType::Text + | EventType::Header + | EventType::Table => { + // TODO more elegant way to do ordered `replace`s (structs?). + if delim_start + .into_iter() + .any(|element| buffer.contains(element)) + { + let popped = output.pop().unwrap(); + if popped != '\\' { + output.push(popped); + } + output.push_str(&*t); + equation_mode = true; + } else if delim_end + .into_iter() + .any(|element| buffer.contains(element)) + || equation_mode == true + { + let popped = output.pop().unwrap(); + if popped != '\\' { + output.push(popped); + } + output.push_str(&*t); + equation_mode = false; + } else { + output.push_str( + &*t.replace(r"\", r"\\") + .replace("&", r"\&") + .replace(r"\s", r"\textbackslash{}s") + .replace(r"\w", r"\textbackslash{}w") + .replace("_", r"\_") + .replace(r"\<", "<") + .replace(r"%", r"\%") + .replace(r"$", r"\$") + .replace(r"—", "---") + .replace("#", r"\#"), + ); + } + header_value = t.into_string(); + } + _ => output.push_str(&*t), + } } Event::SoftBreak => { output.push('\n'); } + Event::HardBreak => { + output.push_str(r"\\"); + output.push('\n'); + } + _ => (), } } -- 2.40.1 From 4098a4144405455abb2c73dcdc84a5cde4d78c64 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Mon, 3 Oct 2022 17:42:00 +0200 Subject: [PATCH 6/6] Adjust tests after recent changes --- src/lib.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index de82966..98d7d83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -417,16 +417,30 @@ issue [#12345](https://example.com) xleftmargin=0.2cm } \begin{document} + \section{First title} -Some content\subsection{Second level} +\label{First title} +\label{first-title} + +Some content~\\ + +\subsection{Second level} +\label{Second level} +\label{second-level} + Text \href{https://example.com}{link} \textbf{Bold} -\textbf{Italic}some code:\begin{lstlisting}[language=sh] +\textbf{Italic}~\\ + +some code:~\\ +\begin{lstlisting}[language=sh] sudo make-it-work \end{lstlisting} -issue \href{https://example.com}{#12345} + +issue \href{https://example.com}{\#12345}~\\ + \end{document} "#; -- 2.40.1