ndg_commonmark/processor/
process.rs1use 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#[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
50pub fn process_safe<F>(content: &str, processor_fn: F, fallback: &str) -> String
66where
67 F: FnOnce(&str) -> String,
68{
69 if content.is_empty() {
71 return String::new();
72 }
73
74 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 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 if fallback.is_empty() {
94 content.to_string()
95 } else {
96 fallback.to_string()
97 }
98 },
99 }
100}
101
102pub 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#[must_use]
148pub fn create_processor(preset: ProcessorPreset) -> MarkdownProcessor {
149 let options = match preset {
150 ProcessorPreset::Basic => {
151 MarkdownOptions {
152 gfm: true,
153 nixpkgs: false,
154 highlight_code: true,
155 highlight_theme: None,
156 manpage_urls_path: None,
157 auto_link_options: true,
158 tab_style: TabStyle::None,
159 valid_options: None,
160 }
161 },
162 ProcessorPreset::Ndg => {
163 MarkdownOptions {
164 gfm: true,
165 nixpkgs: false,
166 highlight_code: true,
167 highlight_theme: Some("github".to_string()),
168 manpage_urls_path: None,
169 auto_link_options: true,
170 tab_style: TabStyle::None,
171 valid_options: None,
172 }
173 },
174 ProcessorPreset::Nixpkgs => {
175 MarkdownOptions {
176 gfm: true,
177 nixpkgs: true,
178 highlight_code: true,
179 highlight_theme: Some("github".to_string()),
180 manpage_urls_path: None,
181 auto_link_options: true,
182 tab_style: TabStyle::None,
183 valid_options: None,
184 }
185 },
186 };
187
188 MarkdownProcessor::new(options)
189}
190
191#[derive(Debug, Clone, Copy)]
194pub enum ProcessorPreset {
195 Basic,
197 Nixpkgs,
199 Ndg,
202}
203
204#[must_use]
216pub fn process_markdown_string(
217 content: &str,
218 preset: ProcessorPreset,
219) -> MarkdownResult {
220 let processor = create_processor(preset);
221 process_with_recovery(&processor, content)
222}
223
224pub fn process_markdown_file(
240 file_path: &Path,
241 preset: ProcessorPreset,
242) -> Result<MarkdownResult, String> {
243 let content = fs::read_to_string(file_path).map_err(|e| {
244 format!("Failed to read file {}: {}", file_path.display(), e)
245 })?;
246
247 let base_dir = file_path.parent().unwrap_or_else(|| Path::new("."));
248 let processor = create_processor(preset).with_base_dir(base_dir);
249 Ok(process_with_recovery(&processor, &content))
250}
251
252pub fn process_markdown_file_with_basedir(
271 file_path: &Path,
272 base_dir: &Path,
273 preset: ProcessorPreset,
274) -> Result<MarkdownResult, String> {
275 let content = fs::read_to_string(file_path).map_err(|e| {
276 format!("Failed to read file {}: {}", file_path.display(), e)
277 })?;
278
279 let processor = create_processor(preset).with_base_dir(base_dir);
280 Ok(process_with_recovery(&processor, &content))
281}
282
283#[cfg(test)]
284mod tests {
285 use std::path::Path;
286
287 use super::*;
288
289 #[test]
290 fn test_safely_process_markup_success() {
291 let content = "test content";
292 let result =
293 process_safe(content, |s| format!("processed: {s}"), "fallback");
294 assert_eq!(result, "processed: test content");
295 }
296
297 #[test]
298 #[allow(clippy::panic)]
299 fn test_safely_process_markup_fallback() {
300 let content = "test content";
301 let result = process_safe(content, |_| panic!("test panic"), "fallback");
302 assert_eq!(result, "fallback");
303 }
304
305 #[test]
306 fn test_process_markdown_string() {
307 let content = "# Test Header\n\nSome content.";
308 let result = process_markdown_string(content, ProcessorPreset::Basic);
309
310 assert!(result.html.contains("<h1"));
311 assert!(result.html.contains("Test Header"));
312 assert_eq!(result.title, Some("Test Header".to_string()));
313 assert_eq!(result.headers.len(), 1);
314 }
315
316 #[test]
317 fn test_create_processor_presets() {
318 let basic = create_processor(ProcessorPreset::Basic);
319 assert!(basic.options.gfm);
320 assert!(!basic.options.nixpkgs);
321 assert!(basic.options.highlight_code);
322
323 let enhanced = create_processor(ProcessorPreset::Ndg);
324 assert!(enhanced.options.gfm);
325 assert!(!enhanced.options.nixpkgs);
326 assert!(enhanced.options.highlight_code);
327
328 let nixpkgs = create_processor(ProcessorPreset::Nixpkgs);
329 assert!(nixpkgs.options.gfm);
330 assert!(nixpkgs.options.nixpkgs);
331 assert!(nixpkgs.options.highlight_code);
332 }
333
334 #[test]
335 #[allow(clippy::panic)]
336 fn test_process_batch() {
337 let processor = create_processor(ProcessorPreset::Basic);
338 let paths = vec![Path::new("test1.md"), Path::new("test2.md")];
339
340 let read_fn = |path: &Path| -> Result<String, std::io::Error> {
341 match path.file_name().and_then(|n| n.to_str()) {
342 Some("test1.md") => Ok("# Test 1".to_string()),
343 Some("test2.md") => Ok("# Test 2".to_string()),
344 _ => {
345 Err(std::io::Error::new(
346 std::io::ErrorKind::NotFound,
347 "File not found",
348 ))
349 },
350 }
351 };
352
353 let results = process_batch(
354 &processor,
355 paths.into_iter().map(std::path::Path::to_path_buf),
356 read_fn,
357 );
358 assert_eq!(results.len(), 2);
359
360 for (path, result) in results {
361 match result {
362 Ok(markdown_result) => {
363 assert!(markdown_result.html.contains("<h1"));
364 if path.contains("test1") {
365 assert!(markdown_result.html.contains("Test 1"));
366 } else {
367 assert!(markdown_result.html.contains("Test 2"));
368 }
369 },
370 Err(e) => panic!("Unexpected error for path {path}: {e}"),
371 }
372 }
373 }
374}