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 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#[derive(Debug, Clone, Copy)]
199pub enum ProcessorPreset {
200 Basic,
202 Nixpkgs,
204 Ndg,
207}
208
209#[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
229pub 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
257pub 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}