markdown_ppp/printer/
mod.rs

1//! Markdown pretty-printer for formatting AST back to Markdown
2//!
3//! This module provides functionality to render a Markdown Abstract Syntax Tree (AST)
4//! back to formatted Markdown text. The printer supports configurable formatting
5//! options and produces clean, readable Markdown output.
6//!
7//! # Features
8//!
9//! - **Full AST support**: All CommonMark + GFM elements are supported
10//! - **Configurable formatting**: Control line width, indentation, and spacing
11//! - **Pretty-printing**: Intelligent line wrapping and formatting
12//! - **Round-trip capability**: Parse → Render → Parse produces equivalent AST
13//! - **GitHub extensions**: Tables, task lists, alerts, footnotes, strikethrough
14//!
15//! # Basic Usage
16//!
17//! ```rust
18//! use markdown_ppp::ast::*;
19//! use markdown_ppp::printer::{render_markdown, config::Config};
20//!
21//! let doc = Document {
22//!     blocks: vec![
23//!         Block::Heading(Heading {
24//!             kind: HeadingKind::Atx(1),
25//!             content: vec![Inline::Text("Hello World".to_string())],
26//!         }),
27//!         Block::Paragraph(vec![
28//!             Inline::Text("This is ".to_string()),
29//!             Inline::Strong(vec![Inline::Text("formatted".to_string())]),
30//!             Inline::Text(" text.".to_string()),
31//!         ]),
32//!     ],
33//! };
34//!
35//! let config = Config::default();
36//! let markdown = render_markdown(&doc, config);
37//! println!("{}", markdown);
38//! ```
39//!
40//! # Configuration
41//!
42//! Customize the output format using configuration:
43//!
44//! ```rust
45//! use markdown_ppp::printer::{render_markdown, config::Config};
46//! use markdown_ppp::ast::{Document, Block, Inline};
47//!
48//! let config = Config::default().with_width(120);
49//! let doc = Document { blocks: vec![Block::Paragraph(vec![Inline::Text("Hello".to_string())])] };
50//! let markdown = render_markdown(&doc, config);
51//! ```
52
53mod block;
54mod blockquote;
55
56/// Configuration options for Markdown pretty-printing.
57pub mod config;
58mod github_alert;
59mod heading;
60mod inline;
61mod list;
62mod table;
63mod tests;
64
65use crate::ast::*;
66use pretty::{Arena, DocBuilder};
67use std::rc::Rc;
68
69/// Render a Markdown AST back to formatted Markdown text
70///
71/// This function takes a parsed Markdown document (AST) and renders it back
72/// to clean, well-formatted Markdown text. The output follows consistent
73/// formatting rules and can be customized via configuration options.
74///
75/// # Arguments
76///
77/// * `ast` - The Markdown document AST to render
78/// * `config` - Configuration options controlling the output format
79///
80/// # Returns
81///
82/// A `String` containing the formatted Markdown text
83///
84/// # Examples
85///
86/// Basic rendering:
87/// ```rust
88/// use markdown_ppp::ast::*;
89/// use markdown_ppp::printer::{render_markdown, config::Config};
90///
91/// let doc = Document {
92///     blocks: vec![
93///         Block::Paragraph(vec![
94///             Inline::Text("Hello ".to_string()),
95///             Inline::Strong(vec![Inline::Text("world".to_string())]),
96///         ]),
97///     ],
98/// };
99///
100/// let config = Config::default();
101/// let markdown = render_markdown(&doc, config);
102/// assert!(markdown.contains("**world**"));
103/// ```
104///
105/// With custom width:
106/// ```rust
107/// use markdown_ppp::ast::Document;
108/// use markdown_ppp::printer::{render_markdown, config::Config};
109///
110/// let doc = Document { blocks: vec![] };
111/// let config = Config::default().with_width(60);
112/// let markdown = render_markdown(&doc, config);
113/// ```
114///
115/// # Round-trip Guarantee
116///
117/// For most valid Markdown documents, the following property holds:
118/// ```text
119/// parse(render(parse(input))) ≈ parse(input)
120/// ```
121/// Where ≈ means semantically equivalent AST structures.
122pub fn render_markdown(ast: &Document, config: crate::printer::config::Config) -> String {
123    let config = Rc::new(config);
124    let arena = Arena::new();
125    let doc = ast.to_doc(config.clone(), &arena);
126
127    let mut buf = Vec::new();
128    doc.render(config.width, &mut buf).unwrap();
129    String::from_utf8(buf).unwrap()
130}
131
132trait ToDoc<'a> {
133    fn to_doc(
134        &self,
135        config: Rc<crate::printer::config::Config>,
136        arena: &'a Arena<'a>,
137    ) -> DocBuilder<'a, Arena<'a>, ()>;
138}
139
140impl<'a> ToDoc<'a> for Document {
141    fn to_doc(
142        &self,
143        config: Rc<crate::printer::config::Config>,
144        arena: &'a Arena<'a>,
145    ) -> DocBuilder<'a, Arena<'a>, ()> {
146        self.blocks.to_doc(config, arena)
147    }
148}