docloom 0.2.1

Programmatically compose documents and render them to Markdown or styled terminal output.
Documentation
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Markdown renderer and supporting types.
//!
//! The `md` module turns [`crate::Block`] and [`crate::Inline`]
//! structures into Markdown text with customizable styling.
//!
//! # Examples
//! ```rust
//! use docloom::md::{FenceStyle, ListMarker, Style, doc};
//! use docloom::prelude::*;
//!
//! // Optional style configuration
//! let style = Style {
//!     code_fence: FenceStyle::Tilde,
//!     list_marker: ListMarker::Asterisk,
//!     max_heading: 3,
//! };
//!
//! let rendered = doc([
//!     h1("Docloom"),
//!     p("Render Markdown from structured blocks."),
//!     ul(["Configure Markdown output.", "Render it anywhere."]),
//! ])
//! .with_style(style)
//! .to_string();
//! ```

use itemize::IntoItems;
use std::fmt;

use super::{Alignment, Block, Inline, Render, Renderable};

/// Markdown document wrapper that renders blocks with a [`Style`].
pub struct Doc {
    content: Vec<Block>,
    style: Style,
}

impl Doc {
    /// Create a new document from items convertible to [`Block`].
    pub fn new(value: impl IntoItems<Block>) -> Self {
        Self {
            content: value.into_items().collect(),
            style: Style::default(),
        }
    }

    /// Override the rendering style to use when formatting the document.
    pub fn with_style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }
}

impl fmt::Display for Doc {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.content
            .render_with(&mut Renderer::with_style(f, self.style))
    }
}

/// Construct a [`Doc`] from any value that can become a sequence of blocks.
pub fn doc(value: impl IntoItems<Block>) -> Doc {
    Doc::new(value)
}

/// Configuration values that affect Markdown output.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Style {
    /// Fence style to use when rendering code blocks.
    pub code_fence: FenceStyle,
    /// Bullet marker to use for unordered lists.
    pub list_marker: ListMarker,
    /// Maximum heading level emitted when rendering blocks.
    pub max_heading: u8,
}

impl Default for Style {
    fn default() -> Self {
        Self {
            code_fence: FenceStyle::Backtick,
            list_marker: ListMarker::Dash,
            max_heading: 6,
        }
    }
}

/// Fence marker options for code blocks.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum FenceStyle {
    /// Render code blocks using backtick fences.
    Backtick,
    /// Render code blocks using tilde fences.
    Tilde,
}

/// Marker options for unordered lists.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum ListMarker {
    /// Use `*` for unordered list items.
    Asterisk,
    /// Use `-` for unordered list items.
    Dash,
}

/// Renderer that writes Markdown to any [`fmt::Write`] target.
pub struct Renderer<'a, W> {
    writer: &'a mut W,
    style: Style,
}

impl<'a, W> Renderer<'a, W> {
    /// Create a renderer that writes to `writer` with the default [`Style`].
    pub fn new(writer: &'a mut W) -> Self {
        Self::with_style(writer, Style::default())
    }

    /// Create a renderer with a custom [`Style`].
    pub fn with_style(writer: &'a mut W, style: Style) -> Self {
        Self { writer, style }
    }

    /// Render an arbitrary [`crate::Renderable`] value to the writer.
    pub fn render<R>(&mut self, r: &R) -> Result<(), fmt::Error>
    where
        R: for<'b> Renderable<Renderer<'b, W>, Output = Result<(), fmt::Error>> + ?Sized,
    {
        r.render_with(self)
    }
}

impl Renderer<'_, String> {
    /// Render a value to a [`String`] using the default [`Style`].
    pub fn to_string<R>(r: &R) -> String
    where
        R: for<'b> Renderable<Renderer<'b, String>, Output = Result<(), fmt::Error>> + ?Sized,
    {
        Self::to_string_with_style(r, Style::default())
    }

    /// Render a value to a [`String`] using the default style, returning errors.
    pub fn try_to_string<R>(r: &R) -> Result<String, fmt::Error>
    where
        R: for<'b> Renderable<Renderer<'b, String>, Output = Result<(), fmt::Error>> + ?Sized,
    {
        Self::try_to_string_with_style(r, Style::default())
    }

    /// Render a value to a [`String`] with a custom [`Style`].
    pub fn to_string_with_style<R>(r: &R, style: Style) -> String
    where
        R: for<'b> Renderable<Renderer<'b, String>, Output = Result<(), fmt::Error>> + ?Sized,
    {
        Self::try_to_string_with_style(r, style).unwrap()
    }

    /// Render a value to a [`String`] with a custom [`Style`], returning errors.
    pub fn try_to_string_with_style<R>(r: &R, style: Style) -> Result<String, fmt::Error>
    where
        R: for<'b> Renderable<Renderer<'b, String>, Output = Result<(), fmt::Error>> + ?Sized,
    {
        let mut buf = String::new();
        r.render_with(&mut Renderer::with_style(&mut buf, style))?;
        Ok(buf)
    }
}

impl<'a, W: fmt::Write> Render for Renderer<'a, W> {
    type Output = Result<(), fmt::Error>;

    /// Render a [`crate::Block`] into Markdown.
    fn render_block(&mut self, inner: &Block) -> Self::Output {
        use Block::*;
        match inner {
            Paragraph(inner) => {
                inner.render_with(self)?;
                writeln!(self.writer)
            }
            Heading { level, content } => {
                // Apply max_heading style
                let clamped_level = (*level).min(self.style.max_heading);
                write!(self.writer, "{} ", "#".repeat(clamped_level as usize))?;
                content.render_with(self)?;
                writeln!(self.writer)
            }
            CodeBlock { language, content } => {
                // Apply code_fence style
                let fence = match self.style.code_fence {
                    FenceStyle::Backtick => "```",
                    FenceStyle::Tilde => "~~~",
                };

                if let Some(lang) = language {
                    writeln!(self.writer, "{}{}", fence, lang)?;
                } else {
                    writeln!(self.writer, "{}", fence)?;
                }
                writeln!(self.writer, "{content}")?;
                writeln!(self.writer, "{}", fence)?;
                writeln!(self.writer)
            }
            List { ordered, items } => {
                for (idx, item) in items.iter().enumerate() {
                    if *ordered {
                        write!(self.writer, "{}. ", idx + 1)?;
                    } else {
                        // Apply list_marker style
                        let marker = match self.style.list_marker {
                            ListMarker::Asterisk => "*",
                            ListMarker::Dash => "-",
                        };
                        write!(self.writer, "{} ", marker)?;
                    }
                    item.render_with(self)?;
                    writeln!(self.writer)?;
                }
                writeln!(self.writer)
            }
            TaskList { items } => {
                for (checked, item) in items.iter() {
                    let mark = if *checked { "x" } else { " " };
                    write!(self.writer, "- [{mark}] ")?;
                    item.render_with(self)?;
                    writeln!(self.writer)?;
                }
                writeln!(self.writer)
            }
            Table {
                headers,
                rows,
                alignments,
            } => {
                let col_count = headers.len();
                let mut widths: Vec<usize> =
                    headers.iter().map(|h| Self::measure_inline(h)).collect();

                for row in rows {
                    for (i, cell) in row.iter().enumerate().take(col_count) {
                        widths[i] = widths[i].max(Self::measure_inline(cell));
                    }
                }

                // at least 3 chars for separator row
                for w in &mut widths {
                    *w = (*w).max(3);
                }

                // header row
                write!(self.writer, "|")?;
                for (i, h) in headers.iter().enumerate() {
                    // Render to a temporary string first
                    let rendered = Renderer::to_string(h);
                    write!(self.writer, " {:width$} |", rendered, width = widths[i])?;
                }
                writeln!(self.writer)?;

                // separator row with alignment
                write!(self.writer, "|")?;
                for (w, align) in widths.iter().zip(alignments.iter()) {
                    let spec = match align {
                        Alignment::Left => {
                            let dashes = "-".repeat((*w).saturating_sub(1));
                            format!(":{dashes}")
                        }
                        Alignment::Center => {
                            let dashes = "-".repeat((*w).saturating_sub(2).max(1));
                            format!(":{dashes}:")
                        }
                        Alignment::Right => {
                            let dashes = "-".repeat((*w).saturating_sub(1));
                            format!("{dashes}:")
                        }
                    };
                    write!(self.writer, " {:width$} |", spec, width = *w)?;
                }
                writeln!(self.writer)?;

                // body rows
                for row in rows {
                    write!(self.writer, "|")?;
                    for (i, w) in widths.iter().enumerate() {
                        if let Some(cell) = row.get(i) {
                            let rendered = Renderer::to_string(cell);
                            write!(self.writer, " {:width$} |", rendered, width = *w)?;
                        } else {
                            // Empty cell if row doesn't have enough columns
                            write!(self.writer, " {:width$} |", "", width = *w)?;
                        }
                    }
                    writeln!(self.writer)?;
                }

                writeln!(self.writer)
            }
            Blockquote(inner) => {
                // Render each block individually and add to blockquote
                for (i, block) in inner.iter().enumerate() {
                    // Render this block to a string
                    let mut block_content = String::new();
                    block.render_with(&mut Renderer::with_style(&mut block_content, self.style))?;

                    // Remove trailing newlines from the block content
                    let block_content = block_content.trim_end();

                    // Add "> " prefix to each line of this block
                    for line in block_content.lines() {
                        writeln!(self.writer, "> {}", line)?;
                    }

                    // Add a blank blockquote line between blocks (except after the last one)
                    if i < inner.len() - 1 {
                        writeln!(self.writer, ">")?;
                    }
                }
                Ok(())
            }
            Image { alt, url } => writeln!(self.writer, "![{alt}]({url})"),
            HorizontalRule => writeln!(self.writer, "---"),
            BlockList(inner) => {
                for block in inner.iter() {
                    block.render_with(self)?;
                }
                Ok(())
            }
        }
    }

    /// Render an [`crate::Inline`] into Markdown.
    fn render_inline(&mut self, inner: &Inline) -> fmt::Result {
        use Inline::*;
        match inner {
            Text(text) => write!(self.writer, "{text}"),
            Bold(inner) => {
                write!(self.writer, "**")?;
                inner.render_with(self)?;
                write!(self.writer, "**")?;
                Ok(())
            }
            Italic(inner) => {
                write!(self.writer, "*")?;
                inner.render_with(self)?;
                write!(self.writer, "*")?;
                Ok(())
            }
            Strikethrough(inner) => {
                write!(self.writer, "~~")?;
                inner.render_with(self)?;
                write!(self.writer, "~~")?;
                Ok(())
            }
            Code(text) => write!(self.writer, "`{text}`"),
            Link { text, url } => {
                write!(self.writer, "[")?;
                text.render_with(self)?;
                write!(self.writer, "]({url})")?;
                Ok(())
            }
            Image { alt, url } => writeln!(self.writer, "![{alt}]({url})"),
            LineBreak => writeln!(self.writer, "  "),
        }
    }
}

impl<'a, W: fmt::Write> Renderer<'a, W> {
    fn measure_inline(inline: &Inline) -> usize {
        match inline {
            Inline::Text(t) => t.to_string().chars().count(),
            Inline::Bold(content) | Inline::Italic(content) => {
                content.iter().map(Self::measure_inline).sum::<usize>() + 4
            }
            Inline::Strikethrough(content) => {
                content.iter().map(Self::measure_inline).sum::<usize>() + 4
            }
            Inline::Code(t) => t.to_string().chars().count() + 2,
            Inline::Link { text, .. } => 2 + text.iter().map(Self::measure_inline).sum::<usize>(),
            Inline::Image { alt, url } => {
                5 + alt.to_string().chars().count() + url.to_string().chars().count()
            }
            Inline::LineBreak => 0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::build::*;

    #[test]
    fn test_markdown_table() {
        let table = table(("Name", "Age"), (("Alice", "30"), ("Bob", "25")));
        let markdown = Renderer::to_string(&table);
        println!("{markdown}");
        assert_eq!(
            markdown.trim(),
            r#"
| Name  | Age |
| :---- | :-- |
| Alice | 30  |
| Bob   | 25  |
            "#
            .trim()
        );
    }

    #[test]
    fn test_markdown_table_with_alignment() {
        let table = table(
            (
                Align::left("Name"),
                Align::center("Age"),
                Align::right("Score"),
            ),
            (("Alice", "30", "95"), ("Bob", "25", "87")),
        );
        let markdown = Renderer::to_string(&table);
        println!("{markdown}");
        // Check the separator row contains proper alignment specs
        let lines: Vec<&str> = markdown.lines().collect();
        let separator = lines[1]; // Second line is the separator
        assert!(separator.contains(":----")); // left alignment
        assert!(separator.contains(":-:")); // center alignment (Age has 3 chars)
        assert!(separator.contains("----:")); // right alignment
    }

    #[test]
    fn test_markdown_strikethrough() {
        // Simple strikethrough
        let strike = p(vec![strikethrough("crossed out text")]);
        let markdown = Renderer::to_string(&strike);
        assert_eq!(markdown.trim(), "~~crossed out text~~");

        // Strikethrough with nested content
        let strike = p((
            "This has ",
            strikethrough(("struck ", "bold".bold(), " text")),
            " in it.",
        ));
        let markdown = Renderer::to_string(&strike);
        assert_eq!(markdown.trim(), "This has ~~struck **bold** text~~ in it.");

        // Multiple inline styles
        let mixed = p((
            bold("Bold"),
            text(", "),
            "italic".italic(),
            ", ",
            "strikethrough".strikethrough(),
            ", and ",
            code("code"),
        ));
        let markdown = Renderer::to_string(&mixed);
        assert_eq!(
            markdown.trim(),
            "**Bold**, *italic*, ~~strikethrough~~, and `code`"
        );
    }

    #[test]
    fn test_markdown_blockquote() {
        // Simple blockquote with a paragraph
        let bq = quote(vec![p("This is a quoted paragraph.")]);
        let markdown = Renderer::to_string(&bq);
        println!("Simple blockquote output:\n{}", markdown);
        assert_eq!(markdown.trim(), "> This is a quoted paragraph.");

        // Blockquote with multiple paragraphs
        let bq = quote(vec![p("First paragraph."), p("Second paragraph.")]);
        let markdown = Renderer::to_string(&bq);
        println!("Multiple paragraphs blockquote output:\n{}", markdown);
        println!("Trimmed output:\n{}", markdown.trim());
        // The markdown renderer should preserve blank lines between paragraphs
        assert_eq!(
            markdown.trim(),
            "> First paragraph.\n>\n> Second paragraph."
        );

        // Blockquote with different block types
        let bq = quote(vec![
            h2("Quote Header"),
            p("Quote content."),
            list(false, vec![p("Item 1"), p("Item 2")]),
        ]);
        let markdown = Renderer::to_string(&bq);
        println!("Complex blockquote output:\n{}", markdown);
        assert!(markdown.contains("> ## Quote Header"));
        assert!(markdown.contains("> Quote content."));
        assert!(markdown.contains("> - Item 1"));
        assert!(markdown.contains("> - Item 2"));
    }
}