mdbook-include-rs 0.1.0

An mdBook preprocessor that includes external Rust source files
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use anyhow::{Context, Result};
use mdbook::book::{Book, BookItem};
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use regex::{Captures, Regex};
use std::fs;
use std::path::Path;
use toml::Value;

/// Preprocessor that handles include-rs code blocks
pub struct IncludeDocPreprocessor;

impl Preprocessor for IncludeDocPreprocessor {
    fn name(&self) -> &str {
        "include-rs"
    }

    fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
        let config_section = ctx.config.get_preprocessor(self.name());
        // Get global base_dir from config if provided, otherwise set to None
        let global_base_dir = if let Some(config) = config_section {
            if let Some(Value::String(dir)) = config.get("base-dir") {
                Some(ctx.root.join(dir))
            } else {
                None
            }
        } else {
            None
        };

        let src_dir = ctx.root.join("src");

        book.for_each_mut(|item| {
            if let BookItem::Chapter(chapter) = item {
                // Get the directory of the chapter markdown file to use as the base if no global base_dir
                let base_dir = if let Some(ref global_dir) = global_base_dir {
                    global_dir.clone()
                } else if let Some(ref source_path) = chapter.source_path {
                    // The SUMMARY.md file is always in src
                    // Use the directory containing the markdown file as base
                    if let Some(parent) = source_path.parent() {
                        src_dir.join(parent)
                    } else {
                        src_dir.clone()
                    }
                } else {
                    // Fallback to root if no source path
                    src_dir.clone()
                };

                if let Err(e) = process_markdown(&base_dir, &mut chapter.content) {
                    eprintln!("Error processing chapter '{}': {}", chapter.name, e);
                }
            }
        });

        Ok(book)
    }

    fn supports_renderer(&self, _renderer: &str) -> bool {
        // This preprocessor supports all renderers
        true
    }
}

/// Process the markdown content to find and replace include-doc code blocks
pub fn process_markdown(base_dir: &Path, content: &mut String) -> Result<()> {
    // This regex finds our directives anywhere in the content
    let re = Regex::new(r"(?ms)^#!\[((?:source_file|function_body)![\s\S]*?)\]$")?;

    let result = re.replace_all(content, |caps: &Captures| {
        let include_doc_directive = caps.get(1).map_or("", |m| m.as_str());

        // Process the directive with include_doc_macro
        match process_include_doc_directive(base_dir, include_doc_directive) {
            Ok(processed) => processed,
            Err(e) => {
                eprintln!("Error processing include-doc directive: {}", e);
                format!("Error processing include-doc directive: {}", e)
            }
        }
    });

    *content = result.to_string();
    Ok(())
}

/// Process an include-doc directive
fn process_include_doc_directive(base_dir: &Path, directive: &str) -> Result<String> {
    // Find if it's a source_file or function_body directive
    if directive.starts_with("source_file!") {
        process_source_file_directive(base_dir, directive)
    } else if directive.starts_with("function_body!") {
        process_function_body_directive(base_dir, directive)
    } else {
        // Pass through unchanged if not recognized
        Ok(directive.to_string())
    }
}

/// Process a source_file directive
fn process_source_file_directive(base_dir: &Path, directive: &str) -> Result<String> {
    // Extract the file path from the directive
    // The pattern is: #source_file!("path/to/file.rs")
    // Using (?s) to enable DOTALL mode for handling multi-line input
    let re = Regex::new(r#"(?s)source_file!\s*\(\s*"([^"]+)"\s*\)"#)?;

    if let Some(caps) = re.captures(directive) {
        let file_path = caps.get(1).map_or("", |m| m.as_str());
        let absolute_path = base_dir.join(file_path);

        // Check if file exists
        if !absolute_path.exists() {
            return Err(anyhow::anyhow!(
                "File not found: {}",
                absolute_path.display()
            ));
        }

        // Read the file content
        let file_content = fs::read_to_string(&absolute_path)
            .with_context(|| format!("Failed to read file: {}", absolute_path.display()))?;

        // Filter out 'use' statements
        let filtered_content = file_content
            .lines()
            .filter(|line| !line.trim_start().starts_with("use "))
            .collect::<Vec<_>>()
            .join("\n");

        Ok(filtered_content)
    } else {
        Err(anyhow::anyhow!(
            "Invalid source_file directive: {}",
            directive
        ))
    }
}

/// Process a function_body directive
fn process_function_body_directive(base_dir: &Path, directive: &str) -> Result<String> {
    // Extract the file path and function name from the directive
    // The pattern is: #function_body!("path/to/file.rs", function_name, [optional, dependencies])
    // Using (?s) to enable DOTALL mode for handling multi-line input
    let re = Regex::new(
        r#"(?s)function_body!\s*\(\s*"([^"]+)"\s*,\s*([^,\]]+)(?:\s*,\s*\[(.*?)])?\s*\)"#,
    )?;

    if let Some(caps) = re.captures(directive) {
        let file_path = caps.get(1).map_or("", |m| m.as_str());
        let function_name = caps.get(2).map_or("", |m| m.as_str()).trim();
        let dependencies = caps.get(3).map(|m| m.as_str().trim());

        let absolute_path = base_dir.join(file_path);
        eprintln!("base_dir: {:?}", base_dir);
        eprintln!("file_path: {:?}", file_path);
        eprintln!("absolute_path: {:?}", absolute_path);
        // Check if file exists
        if !absolute_path.exists() {
            return Err(anyhow::anyhow!(
                "File not found: {}",
                absolute_path.display()
            ));
        }

        // Read the file content
        let file_content = fs::read_to_string(&absolute_path)
            .with_context(|| format!("Failed to read file: {}", absolute_path.display()))?;

        // Parse the file and extract the function body and any dependencies
        if let Some(deps) = dependencies {
            extract_function_with_dependencies(&file_content, function_name, deps)
        } else {
            extract_function_body(&file_content, function_name)
        }
    } else {
        Err(anyhow::anyhow!(
            "Invalid function_body directive: {}",
            directive
        ))
    }
}

/// Extract a function body from Rust code
fn extract_function_body(content: &str, function_name: &str) -> Result<String> {
    // Parse the Rust file
    let syntax = syn::parse_file(content)
        .map_err(|e| anyhow::anyhow!("Failed to parse Rust code: {}", e))?;

    // Helper function to extract function body using regex
    fn extract_body_with_regex(code: &str) -> Option<String> {
        if let Ok(re) = Regex::new(r"fn\s+[^{]+\{([\s\S]*)\}\s*$") {
            if let Some(caps) = re.captures(code) {
                if let Some(body) = caps.get(1) {
                    // Get the body and trim it
                    return Some(body.as_str().trim().to_string());
                }
            }
        }
        None
    }

    // Look for the function in top-level items
    for item in &syntax.items {
        if let syn::Item::Fn(item_fn) = item {
            if item_fn.sig.ident == function_name {
                // Generate full function code
                let fn_code = prettyplease::unparse(&syn::File {
                    shebang: None,
                    attrs: vec![],
                    items: vec![syn::Item::Fn(item_fn.clone())],
                });

                // Try to extract just the body
                if let Some(body) = extract_body_with_regex(&fn_code) {
                    return Ok(body);
                }

                // Fallback: return the full function code
                let result = fn_code
                    .lines()
                    .filter(|line| !line.trim_start().starts_with("use "))
                    .collect::<Vec<_>>()
                    .join("\n");

                return Ok(result);
            }
        }
    }

    // Also look for methods inside impl blocks
    for item in &syntax.items {
        if let syn::Item::Impl(item_impl) = item {
            for impl_item in &item_impl.items {
                if let syn::ImplItem::Fn(method) = impl_item {
                    if method.sig.ident == function_name {
                        // Create a standalone function from the method
                        let standalone_fn = syn::ItemFn {
                            attrs: method.attrs.clone(),
                            vis: syn::Visibility::Inherited,
                            sig: method.sig.clone(),
                            block: Box::new(method.block.clone()),
                        };

                        // Generate code for just this function
                        let fn_code = prettyplease::unparse(&syn::File {
                            shebang: None,
                            attrs: vec![],
                            items: vec![syn::Item::Fn(standalone_fn)],
                        });

                        // Try to extract just the body
                        if let Some(body) = extract_body_with_regex(&fn_code) {
                            return Ok(body);
                        }

                        // Fallback: return the function code
                        let result = fn_code
                            .lines()
                            .filter(|line| !line.trim_start().starts_with("use "))
                            .collect::<Vec<_>>()
                            .join("\n");

                        return Ok(result);
                    }
                }
            }
        }
    }

    Err(anyhow::anyhow!("Function '{}' not found", function_name))
}

/// Extract a function with its dependencies from Rust code
fn extract_function_with_dependencies(
    content: &str,
    function_name: &str,
    dependencies: &str,
) -> Result<String> {
    // Parse the Rust file
    let syntax = syn::parse_file(content)
        .map_err(|e| anyhow::anyhow!("Failed to parse Rust code: {}", e))?;

    // Split dependencies by comma and trim whitespace
    let deps: Vec<&str> = dependencies.split(',').map(|s| s.trim()).collect();

    // First collect all the code blocks we need (main function and dependencies)
    let mut main_function_code = String::new();
    let mut dependency_blocks: Vec<(String, String)> = Vec::new(); // (name, code)

    // First, try to find the main function/method
    let function_name = function_name.trim();

    // Handle method reference in format StructName::method_name
    if function_name.contains("::") {
        let parts: Vec<&str> = function_name.split("::").collect();
        if parts.len() == 2 {
            let struct_name = parts[0].trim();
            let method_name = parts[1].trim();

            let mut struct_method_code = String::new();
            if find_struct_method(&syntax, struct_name, method_name, &mut struct_method_code) {
                main_function_code = struct_method_code;
            } else {
                return Err(anyhow::anyhow!(
                    "Method '{}' for struct '{}' not found",
                    method_name,
                    struct_name
                ));
            }
        } else {
            return Err(anyhow::anyhow!(
                "Invalid function name format: {}",
                function_name
            ));
        }
    } else {
        // Try regular function or method
        let found_main_function =
            find_function_or_method(&syntax, function_name, &mut main_function_code)?;
        if !found_main_function {
            return Err(anyhow::anyhow!(
                "Main function '{}' not found",
                function_name
            ));
        }
    }

    // Now process each dependency
    for dep in deps {
        // Check if it's a struct dependency
        if dep.starts_with("struct ") {
            let struct_name = dep.trim_start_matches("struct ").trim();
            let mut struct_code = String::new();

            if find_struct(&syntax, struct_name, &mut struct_code) {
                dependency_blocks.push((format!("struct {}", struct_name), struct_code));
            } else {
                return Err(anyhow::anyhow!("Struct '{}' not found", struct_name));
            }
        }
        // Check if it's a trait dependency
        else if dep.starts_with("trait ") {
            let trait_name = dep.trim_start_matches("trait ").trim();
            let mut trait_code = String::new();

            if find_trait(&syntax, trait_name, &mut trait_code) {
                dependency_blocks.push((format!("trait {}", trait_name), trait_code));
            } else {
                return Err(anyhow::anyhow!("Trait '{}' not found", trait_name));
            }
        }
        // Check if it's an impl dependency
        else if dep.starts_with("impl ") {
            let impl_info = dep.trim_start_matches("impl ").trim();
            let mut impl_code = String::new();

            // Handle both regular impl and trait impl
            if impl_info.contains(" for ") {
                // Format is "impl TraitName for StructName"
                let parts: Vec<&str> = impl_info.split(" for ").collect();
                if parts.len() == 2 {
                    let trait_name = parts[0].trim();
                    let struct_name = parts[1].trim();

                    if find_trait_impl(&syntax, trait_name, struct_name, &mut impl_code) {
                        dependency_blocks.push((
                            format!("impl {} for {}", trait_name, struct_name),
                            impl_code,
                        ));
                    } else {
                        return Err(anyhow::anyhow!(
                            "Impl of trait '{}' for '{}' not found",
                            trait_name,
                            struct_name
                        ));
                    }
                }
            } else if impl_info.contains("::") {
                // Format is "StructName::method_name" - looking for a specific method
                let parts: Vec<&str> = impl_info.split("::").collect();
                if parts.len() == 2 {
                    let struct_name = parts[0].trim();
                    let method_name = parts[1].trim();

                    if find_struct_method(&syntax, struct_name, method_name, &mut impl_code) {
                        dependency_blocks
                            .push((format!("impl {}::{}", struct_name, method_name), impl_code));
                    } else {
                        return Err(anyhow::anyhow!(
                            "Method '{}' for struct '{}' not found",
                            method_name,
                            struct_name
                        ));
                    }
                }
            } else {
                // Format is "StructName" - all methods
                let struct_name = impl_info;

                if find_struct_impl(&syntax, struct_name, &mut impl_code) {
                    dependency_blocks.push((format!("impl {}", struct_name), impl_code));
                } else {
                    return Err(anyhow::anyhow!("Impl for '{}' not found", struct_name));
                }
            }
        }
        // Otherwise assume it's a function
        else {
            let mut fn_code = String::new();
            if find_function_or_method(&syntax, dep, &mut fn_code)? {
                dependency_blocks.push((dep.to_string(), fn_code));
            } else {
                return Err(anyhow::anyhow!("Function/method '{}' not found", dep));
            }
        }
    }

    // Combine the results in the requested order
    let mut result = String::new();

    // First all dependencies
    let total_deps = dependency_blocks.len();
    for (i, (_, code)) in dependency_blocks.iter().enumerate() {
        result.push_str(code);
        if i < total_deps - 1 || total_deps > 0 {
            // Add exactly one blank line between dependencies
            result.push_str("\n\n");
        }
    }

    // For the main function, just get the function body (handled by extract_function_body for regular case)
    // For methods with impl blocks, we need to extract the function body from the impl block
    if main_function_code.contains("impl") {
        // This is an impl block with a method
        // Extract just the function body from an impl method
        let re = Regex::new(r"fn\s+[^{]+\{([\s\S]*)\}\s*\}")?;
        if let Some(caps) = re.captures(&main_function_code) {
            if let Some(body) = caps.get(1) {
                // Get the body and remove any initial indentation
                let body_text = body.as_str().trim();
                result.push_str(body_text);
            } else {
                // Fallback to the original code
                result.push_str(&main_function_code);
            }
        } else {
            // If regex failed, fallback to the original code
            result.push_str(&main_function_code);
        }
    } else {
        // For regular functions, the body is already extracted
        result.push_str(&main_function_code);
    }

    Ok(result.trim().to_string())
}

// Helper functions for extracting different Rust code elements

fn find_function_or_method(syntax: &syn::File, name: &str, output: &mut String) -> Result<bool> {
    // Check top-level functions
    for item in &syntax.items {
        if let syn::Item::Fn(item_fn) = item {
            if item_fn.sig.ident == name {
                // Generate code for just this function without use statements
                let fn_code = prettyplease::unparse(&syn::File {
                    shebang: None,
                    attrs: vec![],
                    items: vec![syn::Item::Fn(item_fn.clone())],
                });

                *output = fn_code
                    .lines()
                    .filter(|line| !line.trim_start().starts_with("use "))
                    .collect::<Vec<_>>()
                    .join("\n");

                return Ok(true);
            }
        }
    }

    // Check methods in impl blocks
    for item in &syntax.items {
        if let syn::Item::Impl(item_impl) = item {
            for impl_item in &item_impl.items {
                if let syn::ImplItem::Fn(method) = impl_item {
                    if method.sig.ident == name {
                        // For methods, we try to keep some context by showing type path
                        let type_name = if let Some((_, path, _)) = &item_impl.trait_ {
                            format!("<impl {} for ", path.segments.last().unwrap().ident)
                        } else {
                            "impl ".to_string()
                        };

                        let type_path = if let syn::Type::Path(type_path) = &*item_impl.self_ty {
                            format!(
                                "{}{}",
                                type_name,
                                type_path.path.segments.last().unwrap().ident
                            )
                        } else {
                            "Unknown".to_string()
                        };

                        // Create a standalone function from the method
                        let standalone_fn = syn::ItemFn {
                            attrs: method.attrs.clone(),
                            vis: syn::Visibility::Inherited,
                            sig: method.sig.clone(),
                            block: Box::new(method.block.clone()),
                        };

                        let fn_code = prettyplease::unparse(&syn::File {
                            shebang: None,
                            attrs: vec![],
                            items: vec![syn::Item::Fn(standalone_fn)],
                        });

                        // Add a comment showing where this method comes from
                        *output = format!("// From {}\n{}", type_path, fn_code);

                        return Ok(true);
                    }
                }
            }
        }
    }

    Ok(false)
}

fn find_struct(syntax: &syn::File, name: &str, output: &mut String) -> bool {
    for item in &syntax.items {
        if let syn::Item::Struct(item_struct) = item {
            if item_struct.ident == name {
                let struct_code = prettyplease::unparse(&syn::File {
                    shebang: None,
                    attrs: vec![],
                    items: vec![syn::Item::Struct(item_struct.clone())],
                });

                // Remove trailing newlines to ensure consistent formatting
                *output = struct_code.trim_end().to_string();
                return true;
            }
        }
    }
    false
}

fn find_trait(syntax: &syn::File, name: &str, output: &mut String) -> bool {
    for item in &syntax.items {
        if let syn::Item::Trait(item_trait) = item {
            if item_trait.ident == name {
                let trait_code = prettyplease::unparse(&syn::File {
                    shebang: None,
                    attrs: vec![],
                    items: vec![syn::Item::Trait(item_trait.clone())],
                });

                // Remove trailing newlines to ensure consistent formatting
                *output = trait_code.trim_end().to_string();
                return true;
            }
        }
    }
    false
}

fn find_struct_impl(syntax: &syn::File, name: &str, output: &mut String) -> bool {
    for item in &syntax.items {
        if let syn::Item::Impl(item_impl) = item {
            // Check if this is an impl for the specified struct (and not a trait impl)
            if item_impl.trait_.is_none() {
                if let syn::Type::Path(type_path) = &*item_impl.self_ty {
                    if type_path.path.segments.last().unwrap().ident == name {
                        let impl_code = prettyplease::unparse(&syn::File {
                            shebang: None,
                            attrs: vec![],
                            items: vec![syn::Item::Impl(item_impl.clone())],
                        });

                        // Remove trailing newlines to ensure consistent formatting
                        *output = impl_code.trim_end().to_string();
                        return true;
                    }
                }
            }
        }
    }
    false
}

fn find_struct_method(
    syntax: &syn::File,
    struct_name: &str,
    method_name: &str,
    output: &mut String,
) -> bool {
    for item in &syntax.items {
        if let syn::Item::Impl(item_impl) = item {
            // Check if this is an impl for the specified struct
            if let syn::Type::Path(type_path) = &*item_impl.self_ty {
                if type_path.path.segments.last().unwrap().ident == struct_name {
                    // Find the specific method
                    for impl_item in &item_impl.items {
                        if let syn::ImplItem::Fn(method) = impl_item {
                            if method.sig.ident == method_name {
                                // Create a special impl block with just this method
                                let mut method_impl = item_impl.clone();
                                method_impl.items = vec![syn::ImplItem::Fn(method.clone())];

                                let impl_code = prettyplease::unparse(&syn::File {
                                    shebang: None,
                                    attrs: vec![],
                                    items: vec![syn::Item::Impl(method_impl)],
                                });

                                // Remove trailing newlines to ensure consistent formatting
                                *output = impl_code.trim_end().to_string();
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    false
}

fn find_trait_impl(
    syntax: &syn::File,
    trait_name: &str,
    struct_name: &str,
    output: &mut String,
) -> bool {
    for item in &syntax.items {
        if let syn::Item::Impl(item_impl) = item {
            // Check if this is an impl of the specified trait for the specified struct
            if let Some((_, path, _)) = &item_impl.trait_ {
                if path.segments.last().unwrap().ident == trait_name {
                    if let syn::Type::Path(type_path) = &*item_impl.self_ty {
                        if type_path.path.segments.last().unwrap().ident == struct_name {
                            let impl_code = prettyplease::unparse(&syn::File {
                                shebang: None,
                                attrs: vec![],
                                items: vec![syn::Item::Impl(item_impl.clone())],
                            });

                            // Remove trailing newlines to ensure consistent formatting
                            *output = impl_code.trim_end().to_string();
                            return true;
                        }
                    }
                }
            }
        }
    }
    false
}