Skip to main content

ndg_commonmark/processor/
process.rs

1//! Main processing functions for Markdown content.
2use std::{
3  fs,
4  io::Error,
5  path::{Path, PathBuf},
6};
7
8use log::error;
9
10use super::types::{MarkdownOptions, MarkdownProcessor, TabStyle};
11use crate::types::MarkdownResult;
12
13/// Process markdown content with error recovery.
14///
15/// Attempts to process the markdown content and falls back to
16/// safe alternatives if processing fails at any stage.
17///
18/// # Arguments
19///
20/// * `processor` - The configured markdown processor
21/// * `content` - The raw markdown content to process
22///
23/// # Returns
24///
25/// A `MarkdownResult` with processed HTML, headers, and title
26#[must_use]
27pub fn process_with_recovery(
28  processor: &MarkdownProcessor,
29  content: &str,
30) -> MarkdownResult {
31  match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
32    processor.render(content)
33  })) {
34    Ok(result) => result,
35    Err(panic_err) => {
36      error!("Panic during markdown processing: {panic_err:?}");
37      MarkdownResult {
38        html: "<div class=\"error\">Critical error processing markdown \
39               content</div>"
40          .to_string(),
41
42        headers:        Vec::new(),
43        title:          None,
44        included_files: Vec::new(),
45      }
46    },
47  }
48}
49
50/// Safely process markup content with error recovery.
51///
52/// Provides a safe wrapper around markup processing operations
53/// that may fail, and ensures that partial or fallback content is returned
54/// rather than complete failure.
55///
56/// # Arguments
57///
58/// * `content` - The content to process
59/// * `processor_fn` - The processing function to apply
60/// * `fallback` - Fallback content to use if processing fails
61///
62/// # Returns
63///
64/// The processed content or fallback on error
65pub fn process_safe<F>(content: &str, processor_fn: F, fallback: &str) -> String
66where
67  F: FnOnce(&str) -> String,
68{
69  // Avoid processing empty strings
70  if content.is_empty() {
71    return String::new();
72  }
73
74  // Catch any potential panics caused by malformed input or processing errors
75  let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
76    processor_fn(content)
77  }));
78
79  match result {
80    Ok(processed_text) => processed_text,
81    Err(e) => {
82      // Log the error but allow the program to continue
83      if let Some(error_msg) = e.downcast_ref::<String>() {
84        error!("Error processing markup: {error_msg}");
85      } else if let Some(error_msg) = e.downcast_ref::<&str>() {
86        error!("Error processing markup: {error_msg}");
87      } else {
88        error!("Unknown error occurred while processing markup");
89      }
90
91      // Return the original text or default value to prevent breaking the
92      // entire document
93      if fallback.is_empty() {
94        content.to_string()
95      } else {
96        fallback.to_string()
97      }
98    },
99  }
100}
101
102/// Process a batch of markdown files with consistent error handling.
103///
104/// This function processes multiple markdown files using the same processor
105/// configuration, collecting results and handling errors gracefully.
106///
107/// # Arguments
108/// * `processor` - The configured markdown processor
109/// * `files` - Iterator of file paths to process
110/// * `read_file_fn` - Function to read file content from path
111///
112/// # Returns
113/// Vector of tuples containing (`file_path`, `processing_result`)
114pub fn process_batch<I, F>(
115  processor: &MarkdownProcessor,
116  files: I,
117  read_file_fn: F,
118) -> Vec<(String, Result<MarkdownResult, String>)>
119where
120  I: Iterator<Item = PathBuf>,
121  F: Fn(&Path) -> Result<String, Error>,
122{
123  files
124    .map(|path| {
125      let path_str = path.display().to_string();
126      let result = match read_file_fn(&path) {
127        Ok(content) => Ok(process_with_recovery(processor, &content)),
128        Err(e) => Err(format!("Failed to read file: {e}")),
129      };
130      (path_str, result)
131    })
132    .collect()
133}
134
135/// Create a processor with sensible defaults for library usage.
136///
137/// Provides a convenient way to create a processor with
138/// commonly used settings for different use cases.
139///
140/// # Arguments
141///
142/// * `preset` - The preset configuration to use
143///
144/// # Returns
145///
146/// A configured `MarkdownProcessor`
147#[must_use]
148pub fn create_processor(preset: ProcessorPreset) -> MarkdownProcessor {
149  MarkdownProcessor::new(options_for_preset(preset))
150}
151
152fn options_for_preset(preset: ProcessorPreset) -> MarkdownOptions {
153  match preset {
154    ProcessorPreset::Basic => {
155      MarkdownOptions {
156        gfm:                 true,
157        nixpkgs:             false,
158        highlight_code:      true,
159        highlight_theme:     None,
160        manpage_urls_path:   None,
161        syntax_queries_path: None,
162        auto_link_options:   true,
163        tab_style:           TabStyle::None,
164        valid_options:       None,
165      }
166    },
167    ProcessorPreset::Ndg => {
168      MarkdownOptions {
169        gfm:                 true,
170        nixpkgs:             false,
171        highlight_code:      true,
172        highlight_theme:     Some("github".to_string()),
173        manpage_urls_path:   None,
174        syntax_queries_path: None,
175        auto_link_options:   true,
176        tab_style:           TabStyle::None,
177        valid_options:       None,
178      }
179    },
180    ProcessorPreset::Nixpkgs => {
181      MarkdownOptions {
182        gfm:                 true,
183        nixpkgs:             true,
184        highlight_code:      true,
185        highlight_theme:     Some("github".to_string()),
186        manpage_urls_path:   None,
187        syntax_queries_path: None,
188        auto_link_options:   true,
189        tab_style:           TabStyle::None,
190        valid_options:       None,
191      }
192    },
193  }
194}
195
196/// Preset configurations for common use cases. In some cases those presets will
197/// require certain feature flags to be enabled.
198#[derive(Debug, Clone, Copy)]
199pub enum ProcessorPreset {
200  /// Markdown processing with only Github Flavored Markdown (GFM) support
201  Basic,
202  /// Markdown processing with only Nixpkgs-flavored `CommonMark` support
203  Nixpkgs,
204  /// Enhanced Markdown processing with support for GFM and Nixpkgs-flavored
205  /// `CommonMark` support
206  Ndg,
207}
208
209/// Process markdown content from a string with error recovery.
210///
211/// This is a convenience function that combines processor creation and
212/// content processing in a single call.
213///
214/// # Arguments
215/// * `content` - The markdown content to process
216/// * `preset` - The processor preset to use
217///
218/// # Returns
219/// A `MarkdownResult` with processed content
220#[must_use]
221pub fn process_markdown_string(
222  content: &str,
223  preset: ProcessorPreset,
224) -> MarkdownResult {
225  let processor = create_processor(preset);
226  process_with_recovery(&processor, content)
227}
228
229/// Process markdown content from a file with error recovery.
230///
231/// This function reads a markdown file and processes it with the specified
232/// configuration, handling both file I/O and processing errors.
233///
234/// # Arguments
235/// * `file_path` - Path to the markdown file
236/// * `preset` - The processor preset to use
237///
238/// # Returns
239/// A `Result` containing the `MarkdownResult` or an error message
240///
241/// # Errors
242///
243/// Returns an error if the file cannot be read.
244pub fn process_markdown_file(
245  file_path: &Path,
246  preset: ProcessorPreset,
247) -> Result<MarkdownResult, String> {
248  let content = fs::read_to_string(file_path).map_err(|e| {
249    format!("Failed to read file {}: {}", file_path.display(), e)
250  })?;
251
252  let base_dir = file_path.parent().unwrap_or_else(|| Path::new("."));
253  let processor = create_processor(preset).with_base_dir(base_dir);
254  Ok(process_with_recovery(&processor, &content))
255}
256
257/// Process markdown content from a file with custom base directory.
258///
259/// This function reads a markdown file and processes it with the specified
260/// configuration and base directory for resolving includes.
261///
262/// # Arguments
263///
264/// * `file_path` - Path to the markdown file
265/// * `base_dir` - Base directory for resolving relative includes
266/// * `preset` - The processor preset to use
267///
268/// # Returns
269///
270/// A `Result` containing the `MarkdownResult` or an error message
271///
272/// # Errors
273///
274/// Returns an error if the file cannot be read.
275pub fn process_markdown_file_with_basedir(
276  file_path: &Path,
277  base_dir: &Path,
278  preset: ProcessorPreset,
279) -> Result<MarkdownResult, String> {
280  let content = fs::read_to_string(file_path).map_err(|e| {
281    format!("Failed to read file {}: {}", file_path.display(), e)
282  })?;
283
284  let processor = create_processor(preset).with_base_dir(base_dir);
285  Ok(process_with_recovery(&processor, &content))
286}
287
288#[cfg(test)]
289mod tests {
290  use std::path::Path;
291
292  use super::*;
293
294  #[test]
295  fn test_safely_process_markup_success() {
296    let content = "test content";
297    let result =
298      process_safe(content, |s| format!("processed: {s}"), "fallback");
299    assert_eq!(result, "processed: test content");
300  }
301
302  #[test]
303  #[allow(clippy::panic)]
304  fn test_safely_process_markup_fallback() {
305    let content = "test content";
306    let result = process_safe(content, |_| panic!("test panic"), "fallback");
307    assert_eq!(result, "fallback");
308  }
309
310  #[test]
311  fn test_create_processor_presets() {
312    let basic = options_for_preset(ProcessorPreset::Basic);
313    assert!(basic.gfm);
314    assert!(!basic.nixpkgs);
315    assert!(basic.highlight_code);
316
317    let enhanced = options_for_preset(ProcessorPreset::Ndg);
318    assert!(enhanced.gfm);
319    assert!(!enhanced.nixpkgs);
320    assert!(enhanced.highlight_code);
321
322    let nixpkgs = options_for_preset(ProcessorPreset::Nixpkgs);
323    assert!(nixpkgs.gfm);
324    assert!(nixpkgs.nixpkgs);
325    assert!(nixpkgs.highlight_code);
326  }
327
328  #[test]
329  #[allow(clippy::panic)]
330  fn test_process_batch() {
331    let processor = MarkdownProcessor::new(MarkdownOptions {
332      highlight_code: false,
333      ..options_for_preset(ProcessorPreset::Basic)
334    });
335    let paths = vec![Path::new("test1.md"), Path::new("test2.md")];
336
337    let read_fn = |path: &Path| -> Result<String, std::io::Error> {
338      match path.file_name().and_then(|n| n.to_str()) {
339        Some("test1.md") => Ok("# Test 1".to_string()),
340        Some("test2.md") => Ok("# Test 2".to_string()),
341        _ => {
342          Err(std::io::Error::new(
343            std::io::ErrorKind::NotFound,
344            "File not found",
345          ))
346        },
347      }
348    };
349
350    let results = process_batch(
351      &processor,
352      paths.into_iter().map(std::path::Path::to_path_buf),
353      read_fn,
354    );
355    assert_eq!(results.len(), 2);
356
357    for (path, result) in results {
358      match result {
359        Ok(markdown_result) => {
360          assert!(markdown_result.html.contains("<h1"));
361          if path.contains("test1") {
362            assert!(markdown_result.html.contains("Test 1"));
363          } else {
364            assert!(markdown_result.html.contains("Test 2"));
365          }
366        },
367        Err(e) => panic!("Unexpected error for path {path}: {e}"),
368      }
369    }
370  }
371}