1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! LaTeX printer for Markdown AST
//!
//! This module provides functionality to render a Markdown Abstract Syntax Tree (AST)
//! into LaTeX format. The printer supports full CommonMark + GitHub Flavored Markdown
//! features and offers configurable output styles.
//!
//! # Features
//!
//! - **Full AST coverage**: All block and inline elements from CommonMark + GFM
//! - **Configurable table styles**: `tabular`, `longtabu`, `booktabs`
//! - **Configurable code styles**: `verbatim`, `listings`, `minted`
//! - **Proper LaTeX escaping**: All special characters are properly escaped
//! - **GitHub extensions**: Alerts, task lists, footnotes, strikethrough
//! - **Width control**: Configurable line width for pretty-printing
//!
//! # Basic Usage
//!
//! ```rust
//! use markdown_ppp::ast::*;
//! use markdown_ppp::latex_printer::{render_latex, config::Config};
//!
//! let doc = Document {
//! blocks: vec![
//! Block::Heading(Heading {
//! kind: HeadingKind::Atx(1),
//! content: vec![Inline::Text("Hello LaTeX".to_string())],
//! }),
//! Block::Paragraph(vec![
//! Inline::Text("This is ".to_string()),
//! Inline::Strong(vec![Inline::Text("bold".to_string())]),
//! Inline::Text(" and ".to_string()),
//! Inline::Emphasis(vec![Inline::Text("italic".to_string())]),
//! Inline::Text(" text with special chars: $100 & 50%.".to_string()),
//! ]),
//! ],
//! };
//!
//! let latex = render_latex(&doc, Config::default());
//! // Produces:
//! // \section{Hello LaTeX}
//! //
//! // This is \textbf{bold} and \textit{italic} text with special chars: \$100 \& 50\%.
//! ```
//!
//! # Advanced Configuration
//!
//! ```rust
//! # use markdown_ppp::ast::*;
//! # use markdown_ppp::latex_printer::{render_latex, config::*};
//! let config = Config::default()
//! .with_width(120)
//! .with_table_style(TableStyle::Booktabs)
//! .with_code_block_style(CodeBlockStyle::Minted);
//!
//! # let doc = Document { blocks: vec![] };
//! let latex = render_latex(&doc, config);
//! ```
//!
//! # LaTeX Element Mappings
//!
//! | Markdown | LaTeX |
//! |-------------------|--------------------------------------|
//! | `# Heading` | `\section{Heading}` |
//! | `**bold**` | `\textbf{bold}` |
//! | `*italic*` | `\textit{italic}` |
//! | `~~strike~~` | `\sout{strike}` |
//! | `` `code` `` | `\texttt{code}` |
//! | `> quote` | `\begin{quote}...\end{quote}` |
//! | `- list` | `\begin{itemize}...\end{itemize}` |
//! | `1. ordered` | `\begin{enumerate}...\end{enumerate}` |
//! | `[link](url)` | `\href{url}{link}` |
//! | `` | `\includegraphics{url}` |
//! | Tables | `\begin{tabular}...` (configurable) |
//! | Code blocks | `\begin{verbatim}...` (configurable) |
use crate*;
use ;
use ;
/// Internal state for LaTeX rendering
///
/// This structure holds the rendering context including the pretty-printer arena,
/// configuration, and pre-processed indices for footnotes and link definitions.
pub
/// Render the given Markdown AST to LaTeX
///
/// This is the main entry point for LaTeX rendering. It takes a parsed Markdown
/// document and configuration, then produces LaTeX source code.
///
/// # Arguments
///
/// * `ast` - The parsed Markdown document as an AST
/// * `config` - Configuration for rendering (table styles, code styles, width, etc.)
///
/// # Returns
///
/// LaTeX source code as a string. This will be a document fragment suitable
/// for inclusion in a larger LaTeX document, not a complete document with
/// `\documentclass` etc.
///
/// # Examples
///
/// ```rust
/// use markdown_ppp::ast::*;
/// use markdown_ppp::latex_printer::{render_latex, config::Config};
///
/// let doc = Document {
/// blocks: vec![
/// Block::Paragraph(vec![
/// Inline::Text("Visit ".to_string()),
/// Inline::Link(Link {
/// destination: "https://example.com".to_string(),
/// title: None,
/// children: vec![Inline::Text("this link".to_string())],
/// }),
/// Inline::Text(" for more info.".to_string()),
/// ]),
/// Block::List(List {
/// kind: ListKind::Bullet(ListBulletKind::Star),
/// items: vec![ListItem {
/// task: Some(TaskState::Complete),
/// blocks: vec![Block::Paragraph(vec![
/// Inline::Strong(vec![Inline::Text("Bold".to_string())]),
/// Inline::Text(" item with special chars: $100 & 50%".to_string()),
/// ])],
/// }],
/// }),
/// ],
/// };
///
/// let latex = render_latex(&doc, Config::default());
/// // Produces LaTeX with proper escaping and formatting:
/// // Visit \href{https://example.com}{this link} for more info.
/// //
/// // \begin{itemize}
/// // \item $\boxtimes$ \textbf{Bold} item with special chars: \$100 \& 50\%
/// // \end{itemize}
/// ```
///
/// # LaTeX Packages Required
///
/// The generated LaTeX may require these packages depending on features used:
///
/// - `hyperref` - for links (`\href`)
/// - `graphicx` - for images (`\includegraphics`)
/// - `ulem` - for strikethrough (`\sout`)
/// - `booktabs` - if using booktabs table style
/// - `longtabu` - if using longtabu table style
/// - `listings` - if using listings code style
/// - `minted` - if using minted code style
/// Internal trait for converting AST nodes to pretty-printer documents
///
/// This trait is implemented by all AST node types and provides the core
/// rendering logic for each element type.