md2pdf/src/lib.rs

226 lines
6.1 KiB
Rust
Raw Normal View History

2019-07-15 12:04:34 +02:00
use pulldown_cmark::{Event, Parser, Tag};
2022-10-02 20:28:50 +02:00
pub const LATEX_HEADER: &str = r#"\documentclass{scrartcl}
2019-07-15 12:04:34 +02:00
\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}
"#;
pub const LATEX_FOOTER: &str = "\n\\end{document}\n";
pub fn markdown_to_latex(markdown: String) -> String {
let mut output = String::from(LATEX_HEADER);
let parser = Parser::new(&markdown);
for event in parser {
match event {
Event::Start(Tag::Header(level)) => {
output.push_str("\\");
2022-10-02 20:28:50 +02:00
for _ in 1..level {
2019-07-15 12:04:34 +02:00
output.push_str("sub");
}
output.push_str("section{");
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
Event::End(Tag::Header(_)) => output.push_str("}\n"),
Event::Start(Tag::Emphasis) => output.push_str("\\emph{"),
Event::End(Tag::Emphasis) => output.push_str("}"),
Event::Start(Tag::Strong) => output.push_str("\\textbf{"),
Event::End(Tag::Strong) => output.push_str("}"),
Event::Start(Tag::List(None)) => output.push_str("\\begin{itemize}\n"),
Event::End(Tag::List(None)) => output.push_str("\\end{itemize}\n"),
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::Link(_, url, _)) => {
output.push_str("\\href{");
output.push_str(&*url);
output.push_str("}{");
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
Event::End(Tag::Link(_, _, _)) => {
output.push_str("}");
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
Event::Start(Tag::Image(_, path, title)) => {
output.push_str("\\begin{figure}\n");
output.push_str("\\centering\n");
2022-10-01 13:43:23 +02:00
output.push_str("\\includegraphics[width=\\textwidth]{");
2019-07-15 12:04:34 +02:00
output.push_str(&*path);
output.push_str("}\n");
output.push_str("\\caption{");
output.push_str(&*title);
output.push_str("}\n\\end{figure}\n");
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
Event::Start(Tag::Item) => output.push_str("\\item "),
Event::End(Tag::Item) => output.push_str("\n"),
Event::Start(Tag::CodeBlock(lang)) => {
2022-10-02 20:28:50 +02:00
if !lang.is_empty() {
2019-07-15 12:04:34 +02:00
output.push_str("\\begin{lstlisting}[language=");
output.push_str(&*lang);
output.push_str("]\n");
} else {
output.push_str("\\begin{lstlisting}\n");
}
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
Event::End(Tag::CodeBlock(_)) => {
output.push_str("\n\\end{lstlisting}\n");
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
Event::Text(t) => {
output.push_str(&*t);
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
Event::SoftBreak => {
output.push('\n');
2022-10-02 20:28:50 +02:00
}
2019-07-15 12:04:34 +02:00
_ => (),
}
}
output.push_str(LATEX_FOOTER);
output
}
pub fn markdown_to_pdf(markdown: String) -> Result<Vec<u8>, tectonic::Error> {
tectonic::latex_to_pdf(markdown_to_latex(markdown))
}
2022-10-02 20:27:52 +02:00
#[cfg(test)]
mod tests {
use super::{markdown_to_latex, markdown_to_pdf};
2022-10-02 20:28:50 +02:00
use lopdf::Document;
2022-10-02 20:27:52 +02:00
use pretty_assertions::assert_eq;
use std::io::Cursor;
const MARKDOWN_IN: &str = r#"# First title
Some content
## Second level
Text
[link](https://example.com)
**Bold**
__Italic__
some code:
```sh
sudo make-it-work
```
issue [#12345](https://example.com)
2022-10-02 20:27:52 +02:00
"#;
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}some code:\begin{lstlisting}[language=sh]
sudo make-it-work
\end{lstlisting}
issue \href{https://example.com}{#12345}
2022-10-02 20:27:52 +02:00
\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);
2022-10-02 20:28:50 +02:00
match Document::load_from(&mut file) {
2022-10-02 20:27:52 +02:00
Ok(doc) => {
assert_eq!("1.5", doc.version);
2022-10-02 20:28:50 +02:00
}
Err(_) => assert!(true),
2022-10-02 20:27:52 +02:00
}
2022-10-02 20:28:50 +02:00
}
Err(_) => assert!(true),
2022-10-02 20:27:52 +02:00
}
}
2022-10-02 20:28:50 +02:00
}