typlite/writer/latex.rs
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
//! LaTeX writer implementation
use std::path::Path;
use cmark_writer::ast::Node;
use ecow::EcoString;
use tinymist_std::path::unix_slash;
use crate::common::{ExternalFrameNode, FigureNode, FormatWriter, HighlightNode, ListState};
use crate::Result;
/// LaTeX writer implementation
pub struct LaTeXWriter {
list_state: Option<ListState>,
}
impl Default for LaTeXWriter {
fn default() -> Self {
Self::new()
}
}
impl LaTeXWriter {
pub fn new() -> Self {
Self { list_state: None }
}
pub fn default_prelude(output: &mut EcoString) {
// Write LaTeX document preamble using the new method
output.push_str("\\documentclass[12pt,a4paper]{article}\n");
output.push_str("\\usepackage[utf8]{inputenc}\n");
output.push_str("\\usepackage{hyperref}\n"); // For links
output.push_str("\\usepackage{graphicx}\n"); // For images
output.push_str("\\usepackage{ulem}\n"); // For strikethrough \sout
output.push_str("\\usepackage{listings}\n"); // For code blocks
output.push_str("\\usepackage{xcolor}\n"); // For colored text and backgrounds
output.push_str("\\usepackage{amsmath}\n"); // Math formula support
output.push_str("\\usepackage{amssymb}\n"); // Additional math symbols
output.push_str("\\usepackage{array}\n"); // Enhanced table functionality
// Set code highlighting style
output.push_str("\\lstset{\n");
output.push_str(" basicstyle=\\ttfamily\\small,\n");
output.push_str(" breaklines=true,\n");
output.push_str(" frame=single,\n");
output.push_str(" numbers=left,\n");
output.push_str(" numberstyle=\\tiny,\n");
output.push_str(" keywordstyle=\\color{blue},\n");
output.push_str(" commentstyle=\\color{green!60!black},\n");
output.push_str(" stringstyle=\\color{red}\n");
output.push_str("}\n\n");
}
fn write_inline_nodes(&mut self, nodes: &[Node], output: &mut EcoString) -> Result<()> {
for node in nodes {
self.write_node(node, output)?;
}
Ok(())
}
/// Write the document to LaTeX format
fn write_node(&mut self, node: &Node, output: &mut EcoString) -> Result<()> {
match node {
Node::Document(blocks) => {
for block in blocks {
self.write_node(block, output)?;
}
}
Node::Paragraph(inlines) => {
self.write_inline_nodes(inlines, output)?;
output.push_str("\n\n");
}
Node::Heading {
level,
content,
heading_type: _,
} => {
if *level > 4 {
return Err(format!("heading level {} is not supported in LaTeX", level).into());
}
output.push('\\');
match level {
1 => output.push_str("chapter{"),
2 => output.push_str("section{"),
3 => output.push_str("subsection{"),
4 => output.push_str("subsubsection{"),
_ => return Err(format!("Heading level {} is not supported", level).into()),
}
self.write_inline_nodes(content, output)?;
output.push_str("}\n\n");
}
Node::BlockQuote(content) => {
output.push_str("\\begin{quote}\n");
for block in content {
self.write_node(block, output)?;
}
output.push_str("\\end{quote}\n");
}
Node::CodeBlock {
language,
content,
block_type: _,
} => {
if let Some(lang) = language {
if !lang.is_empty() {
output.push_str("\\begin{lstlisting}[language=");
output.push_str(lang);
output.push_str("]\n");
} else {
output.push_str("\\begin{verbatim}\n");
}
} else {
output.push_str("\\begin{verbatim}\n");
}
output.push_str(content);
if language.as_ref().is_none_or(|lang| lang.is_empty()) {
output.push_str("\n\\end{verbatim}");
} else {
output.push_str("\n\\end{lstlisting}");
}
output.push_str("\n\n");
}
Node::OrderedList { start: _, items } => {
let previous_state = self.list_state;
self.list_state = Some(ListState::Ordered);
output.push_str("\\begin{enumerate}\n");
for item in items {
match item {
cmark_writer::ast::ListItem::Ordered { content, .. }
| cmark_writer::ast::ListItem::Unordered { content } => {
output.push_str("\\item ");
for block in content {
match block {
// For paragraphs, we want inline content rather than creating a
// new paragraph
Node::Paragraph(inlines) => {
self.write_inline_nodes(inlines, output)?;
}
_ => self.write_node(block, output)?,
}
}
output.push('\n');
}
_ => {}
}
}
output.push_str("\\end{enumerate}\n\n");
self.list_state = previous_state;
}
Node::UnorderedList(items) => {
let previous_state = self.list_state;
self.list_state = Some(ListState::Unordered);
output.push_str("\\begin{itemize}\n");
for item in items {
match item {
cmark_writer::ast::ListItem::Ordered { content, .. }
| cmark_writer::ast::ListItem::Unordered { content } => {
output.push_str("\\item ");
for block in content {
match block {
// For paragraphs, we want inline content rather than creating a
// new paragraph
Node::Paragraph(inlines) => {
self.write_inline_nodes(inlines, output)?;
}
_ => self.write_node(block, output)?,
}
}
output.push('\n');
}
_ => {}
}
}
output.push_str("\\end{itemize}\n\n");
self.list_state = previous_state;
}
Node::Table {
headers,
rows,
alignments: _,
} => {
// Calculate column count
let col_count = headers
.len()
.max(rows.iter().map(|row| row.len()).max().unwrap_or(0));
output.push_str("\\begin{table}[htbp]\n");
output.push_str("\\centering\n");
output.push_str("\\begin{tabular}{");
// Add column format (centered alignment)
for _ in 0..col_count {
output.push('c');
}
output.push_str("}\n\\hline\n");
// Process header
if !headers.is_empty() {
for (i, cell) in headers.iter().enumerate() {
if i > 0 {
output.push_str(" & ");
}
self.write_node(cell, output)?;
}
output.push_str(" \\\\\n\\hline\n");
}
// Process all rows
for row in rows {
for (i, cell) in row.iter().enumerate() {
if i > 0 {
output.push_str(" & ");
}
self.write_node(cell, output)?;
}
output.push_str(" \\\\\n");
}
// Close table environment
output.push_str("\\hline\n");
output.push_str("\\end{tabular}\n");
output.push_str("\\end{table}\n\n");
}
Node::Custom(custom_node) => {
if let Some(figure_node) = custom_node.as_any().downcast_ref::<FigureNode>() {
// Start figure environment
output.push_str("\\begin{figure}[htbp]\n\\centering\n");
// Handle the body content (typically an image)
match &*figure_node.body {
Node::Paragraph(content) => {
for node in content {
// Special handling for image nodes in figures
if let Node::Image {
url,
title: _,
alt: _,
} = node
{
// Path to the image file
let path = unix_slash(Path::new(url));
// Write includegraphics command
output.push_str("\\includegraphics[width=0.8\\textwidth]{");
output.push_str(&path);
output.push_str("}\n");
} else {
// For non-image content, just render it normally
self.write_node(node, output)?;
}
}
}
// Directly handle the node if it's not in a paragraph
node => self.write_node(node, output)?,
}
// Add caption if present
if !figure_node.caption.is_empty() {
output.push_str("\\caption{");
output.push_str(&escape_latex(&figure_node.caption));
output.push_str("}\n");
}
// Close figure environment
output.push_str("\\end{figure}\n\n");
} else if let Some(external_frame) =
custom_node.as_any().downcast_ref::<ExternalFrameNode>()
{
// Handle externally stored frames
let path = unix_slash(&external_frame.file_path);
output.push_str("\\begin{figure}[htbp]\n");
output.push_str("\\centering\n");
output.push_str("\\includegraphics[width=0.8\\textwidth]{");
output.push_str(&path);
output.push_str("}\n");
if !external_frame.alt_text.is_empty() {
output.push_str("\\caption{");
output.push_str(&escape_latex(&external_frame.alt_text));
output.push_str("}\n");
}
output.push_str("\\end{figure}\n\n");
} else if let Some(highlight_node) =
custom_node.as_any().downcast_ref::<HighlightNode>()
{
output.push_str("\\colorbox{yellow}{");
for child in &highlight_node.content {
self.write_node(child, output)?;
}
output.push_str("}");
} else {
// Fallback for unknown custom nodes
output.push_str("[Unknown custom node]");
}
}
Node::Text(text) => {
output.push_str(&escape_latex(text));
}
Node::Emphasis(content) => {
output.push_str("\\textit{");
self.write_inline_nodes(content, output)?;
output.push_str("}");
}
Node::Strong(content) => {
output.push_str("\\textbf{");
self.write_inline_nodes(content, output)?;
output.push_str("}");
}
Node::Strikethrough(content) => {
output.push_str("\\sout{");
self.write_inline_nodes(content, output)?;
output.push_str("}");
}
Node::Link {
url,
title: _,
content,
} => {
output.push_str("\\href{");
output.push_str(url);
output.push_str("}{");
self.write_inline_nodes(content, output)?;
output.push_str("}");
}
Node::Image { url, title: _, alt } => {
let alt_text = if !alt.is_empty() {
let mut alt_str = EcoString::new();
self.write_inline_nodes(alt, &mut alt_str)?;
alt_str
} else {
"".into()
};
let path = unix_slash(Path::new(url));
output.push_str("\\begin{figure}\n");
output.push_str("\\centering\n");
output.push_str("\\includegraphics[width=0.8\\textwidth]{");
output.push_str(&path);
output.push_str("}\n");
if !alt_text.is_empty() {
output.push_str("\\caption{");
output.push_str(&alt_text);
output.push_str("}\n");
}
output.push_str("\\end{figure}\n\n");
}
Node::InlineCode(code) => {
output.push_str("\\texttt{");
output.push_str(&escape_latex(code));
output.push_str("}");
}
Node::HardBreak => {
output.push_str("\\\\\n");
}
Node::SoftBreak => {
output.push(' ');
}
Node::ThematicBreak => {
output.push_str("\\hrule\n\n");
}
Node::HtmlElement(element) => {
for child in &element.children {
self.write_node(child, output)?;
}
}
_ => {}
}
Ok(())
}
}
/// Escape LaTeX special characters in a string
fn escape_latex(text: &str) -> String {
text.replace('&', "\\&")
.replace('%', "\\%")
.replace('$', "\\$")
.replace('#', "\\#")
.replace('_', "\\_")
.replace('{', "\\{")
.replace('}', "\\}")
.replace('~', "\\textasciitilde{}")
.replace('^', "\\textasciicircum{}")
.replace('\\', "\\textbackslash{}")
}
impl FormatWriter for LaTeXWriter {
fn write_eco(&mut self, document: &Node, output: &mut EcoString) -> Result<()> {
output.push_str("\\begin{document}\n\n");
// Write the document content
self.write_node(document, output)?;
// Add document ending
output.push_str("\n\\end{document}\n");
Ok(())
}
fn write_vec(&mut self, document: &Node) -> Result<Vec<u8>> {
let mut output = EcoString::new();
self.write_eco(document, &mut output)?;
Ok(output.as_str().as_bytes().to_vec())
}
}