Skip to main content

litchi/markdown/
traits.rs

1/// Core trait for Markdown conversion.
2///
3/// This module defines the `ToMarkdown` trait that enables types to be
4/// converted to Markdown format.
5use crate::common::Result;
6use super::config::MarkdownOptions;
7
8/// Core trait for types that can be converted to Markdown.
9///
10/// This trait is implemented for Document, Presentation, and their constituent
11/// parts (paragraphs, runs, tables, etc.).
12///
13/// # Examples
14///
15/// ```rust,no_run
16/// use litchi::{Document, markdown::ToMarkdown};
17///
18/// # fn main() -> Result<(), litchi::Error> {
19/// let doc = Document::open("document.docx")?;
20///
21/// // Convert entire document
22/// let markdown = doc.to_markdown()?;
23///
24/// // Or convert individual parts
25/// for para in doc.paragraphs()? {
26///     let para_md = para.to_markdown()?;
27///     println!("{}", para_md);
28/// }
29/// # Ok(())
30/// # }
31/// ```
32pub trait ToMarkdown {
33    /// Convert this item to Markdown with default options.
34    ///
35    /// # Examples
36    ///
37    /// ```rust,no_run
38    /// use litchi::{Document, markdown::ToMarkdown};
39    ///
40    /// # fn main() -> Result<(), litchi::Error> {
41    /// let doc = Document::open("document.docx")?;
42    /// let markdown = doc.to_markdown()?;
43    /// # Ok(())
44    /// # }
45    /// ```
46    fn to_markdown(&self) -> Result<String> {
47        self.to_markdown_with_options(&MarkdownOptions::default())
48    }
49
50    /// Convert this item to Markdown with custom options.
51    ///
52    /// # Arguments
53    ///
54    /// * `options` - Configuration for the conversion
55    ///
56    /// # Examples
57    ///
58    /// ```rust,no_run
59    /// use litchi::{Document, markdown::{ToMarkdown, MarkdownOptions}};
60    ///
61    /// # fn main() -> Result<(), litchi::Error> {
62    /// let doc = Document::open("document.docx")?;
63    /// let options = MarkdownOptions::new().with_styles(true);
64    /// let markdown = doc.to_markdown_with_options(&options)?;
65    /// # Ok(())
66    /// # }
67    /// ```
68    fn to_markdown_with_options(&self, options: &MarkdownOptions) -> Result<String>;
69}
70