|
| 1 | +/* |
| 2 | + * format_writers.rs |
| 3 | + * Copyright (c) 2025 Posit, PBC |
| 4 | + */ |
| 5 | + |
| 6 | +//! Format-specific writers for template context building. |
| 7 | +//! |
| 8 | +//! This module provides a trait for format-specific AST-to-string conversion, |
| 9 | +//! and implementations for HTML output. |
| 10 | +
|
| 11 | +use anyhow::Result; |
| 12 | +use quarto_markdown_pandoc::pandoc::block::Block; |
| 13 | +use quarto_markdown_pandoc::pandoc::inline::Inlines; |
| 14 | + |
| 15 | +/// Format-specific writers for converting Pandoc AST to strings. |
| 16 | +/// |
| 17 | +/// Implementations of this trait provide the format-specific rendering |
| 18 | +/// needed when converting document metadata to template values. |
| 19 | +pub trait FormatWriters { |
| 20 | + /// Write blocks to a string. |
| 21 | + fn write_blocks(&self, blocks: &[Block]) -> Result<String>; |
| 22 | + |
| 23 | + /// Write inlines to a string. |
| 24 | + fn write_inlines(&self, inlines: &Inlines) -> Result<String>; |
| 25 | +} |
| 26 | + |
| 27 | +/// HTML format writers. |
| 28 | +/// |
| 29 | +/// Uses the HTML writer from quarto-markdown-pandoc to convert |
| 30 | +/// Pandoc AST nodes to HTML strings. |
| 31 | +pub struct HtmlWriters; |
| 32 | + |
| 33 | +impl FormatWriters for HtmlWriters { |
| 34 | + fn write_blocks(&self, blocks: &[Block]) -> Result<String> { |
| 35 | + let mut buf = Vec::new(); |
| 36 | + quarto_markdown_pandoc::writers::html::write_blocks(blocks, &mut buf)?; |
| 37 | + Ok(String::from_utf8_lossy(&buf).into_owned()) |
| 38 | + } |
| 39 | + |
| 40 | + fn write_inlines(&self, inlines: &Inlines) -> Result<String> { |
| 41 | + let mut buf = Vec::new(); |
| 42 | + quarto_markdown_pandoc::writers::html::write_inlines(inlines, &mut buf)?; |
| 43 | + Ok(String::from_utf8_lossy(&buf).into_owned()) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use super::*; |
| 50 | + use quarto_markdown_pandoc::pandoc::Inline; |
| 51 | + use quarto_markdown_pandoc::pandoc::block::Paragraph; |
| 52 | + use quarto_markdown_pandoc::pandoc::inline::{Emph, Space, Str}; |
| 53 | + |
| 54 | + fn dummy_source_info() -> quarto_source_map::SourceInfo { |
| 55 | + quarto_source_map::SourceInfo::from_range( |
| 56 | + quarto_source_map::FileId(0), |
| 57 | + quarto_source_map::Range { |
| 58 | + start: quarto_source_map::Location { |
| 59 | + offset: 0, |
| 60 | + row: 0, |
| 61 | + column: 0, |
| 62 | + }, |
| 63 | + end: quarto_source_map::Location { |
| 64 | + offset: 0, |
| 65 | + row: 0, |
| 66 | + column: 0, |
| 67 | + }, |
| 68 | + }, |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_html_writers_inlines() { |
| 74 | + let writers = HtmlWriters; |
| 75 | + let inlines = vec![ |
| 76 | + Inline::Str(Str { |
| 77 | + text: "Hello".to_string(), |
| 78 | + source_info: dummy_source_info(), |
| 79 | + }), |
| 80 | + Inline::Space(Space { |
| 81 | + source_info: dummy_source_info(), |
| 82 | + }), |
| 83 | + Inline::Emph(Emph { |
| 84 | + content: vec![Inline::Str(Str { |
| 85 | + text: "world".to_string(), |
| 86 | + source_info: dummy_source_info(), |
| 87 | + })], |
| 88 | + source_info: dummy_source_info(), |
| 89 | + }), |
| 90 | + ]; |
| 91 | + |
| 92 | + let result = writers.write_inlines(&inlines).unwrap(); |
| 93 | + assert_eq!(result, "Hello <em>world</em>"); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_html_writers_blocks() { |
| 98 | + let writers = HtmlWriters; |
| 99 | + let blocks = vec![Block::Paragraph(Paragraph { |
| 100 | + content: vec![Inline::Str(Str { |
| 101 | + text: "A paragraph.".to_string(), |
| 102 | + source_info: dummy_source_info(), |
| 103 | + })], |
| 104 | + source_info: dummy_source_info(), |
| 105 | + })]; |
| 106 | + |
| 107 | + let result = writers.write_blocks(&blocks).unwrap(); |
| 108 | + assert_eq!(result, "<p>A paragraph.</p>\n"); |
| 109 | + } |
| 110 | +} |
0 commit comments