sphinx-ultra 0.3.0

High-performance Rust-based Sphinx documentation builder for large codebases
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
use anyhow::{anyhow, Result};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Directive validation module for comprehensive validation
pub mod validation;

/// Represents a parsed Sphinx directive
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Directive {
    pub name: String,
    pub arguments: Vec<String>,
    pub options: HashMap<String, String>,
    pub content: Vec<String>,
    pub line_number: usize,
    pub source_file: String,
}

/// Directive processor trait
pub trait DirectiveProcessor {
    fn process(&self, directive: &Directive) -> Result<String>;
    fn get_name(&self) -> &str;
    fn get_option_spec(&self) -> HashMap<String, DirectiveOptionType>;
}

/// Directive option types
#[derive(Debug, Clone)]
pub enum DirectiveOptionType {
    Flag,
    String,
    Integer,
    Float,
    Choice(Vec<String>),
    Unchanged,
    UnchangedRequired,
    Path,
    Percentage,
    LengthOrPercentage,
    Class,
    ClassOption,
    Encoding,
}

/// Built-in directive processors
pub struct DirectiveRegistry {
    processors: HashMap<String, Box<dyn DirectiveProcessor + Send + Sync>>,
}

impl Default for DirectiveRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl DirectiveRegistry {
    pub fn new() -> Self {
        let mut registry = Self {
            processors: HashMap::new(),
        };

        // Register built-in directives
        registry.register_builtin_directives();
        registry
    }

    pub fn register(&mut self, processor: Box<dyn DirectiveProcessor + Send + Sync>) {
        self.processors
            .insert(processor.get_name().to_string(), processor);
    }

    pub fn get(&self, name: &str) -> Option<&(dyn DirectiveProcessor + Send + Sync)> {
        self.processors.get(name).map(|boxed| boxed.as_ref())
    }

    pub fn process_directive(&self, directive: &Directive) -> Result<String> {
        if let Some(processor) = self.get(&directive.name) {
            processor.process(directive)
        } else {
            // Return a warning comment for unknown directives
            Ok(format!("<!-- Unknown directive: {} -->", directive.name))
        }
    }

    fn register_builtin_directives(&mut self) {
        // Admonition directives
        self.register(Box::new(AdmonitionDirective::new("note")));
        self.register(Box::new(AdmonitionDirective::new("warning")));
        self.register(Box::new(AdmonitionDirective::new("important")));
        self.register(Box::new(AdmonitionDirective::new("tip")));
        self.register(Box::new(AdmonitionDirective::new("caution")));
        self.register(Box::new(AdmonitionDirective::new("danger")));
        self.register(Box::new(AdmonitionDirective::new("error")));
        self.register(Box::new(AdmonitionDirective::new("hint")));
        self.register(Box::new(AdmonitionDirective::new("attention")));
        self.register(Box::new(AdmonitionDirective::new("seealso")));
        self.register(Box::new(GenericAdmonitionDirective));

        // Code directives
        self.register(Box::new(CodeBlockDirective));
        self.register(Box::new(LiteralIncludeDirective));
        self.register(Box::new(HighlightDirective));

        // Structure directives
        self.register(Box::new(ToctreeDirective));
        self.register(Box::new(IndexDirective));
        self.register(Box::new(OnlyDirective));
        self.register(Box::new(IfConfigDirective));

        // Image directives
        self.register(Box::new(ImageDirective));
        self.register(Box::new(FigureDirective));

        // Table directives
        self.register(Box::new(TableDirective));
        self.register(Box::new(CsvTableDirective));
        self.register(Box::new(ListTableDirective));

        // Include directives
        self.register(Box::new(IncludeDirective));
        self.register(Box::new(RawDirective));

        // Math directives
        self.register(Box::new(MathDirective));

        // Domain-specific directives
        self.register(Box::new(AutoDocDirective));
        self.register(Box::new(AutoModuleDirective));
        self.register(Box::new(AutoClassDirective));
        self.register(Box::new(AutoFunctionDirective));

        // Meta directives
        self.register(Box::new(MetaDirective));
        self.register(Box::new(SidebarDirective));
        self.register(Box::new(TopicDirective));
        self.register(Box::new(RubricDirective));
        self.register(Box::new(EpigraphDirective));
        self.register(Box::new(HighlightsDirective));
        self.register(Box::new(PullQuoteDirective));
        self.register(Box::new(CompoundDirective));
        self.register(Box::new(ContainerDirective));

        // Version directives
        self.register(Box::new(VersionAddedDirective));
        self.register(Box::new(VersionChangedDirective));
        self.register(Box::new(DeprecatedDirective));
    }
}

/// Parse a directive from RST text
pub fn parse_directive(
    text: &str,
    line_number: usize,
    source_file: &str,
) -> Result<Option<Directive>> {
    let directive_regex = Regex::new(r"^\.\. ([a-zA-Z][a-zA-Z0-9_-]*)::\s*(.*?)$")?;

    if let Some(captures) = directive_regex.captures(text) {
        let name = captures.get(1).unwrap().as_str().to_string();
        let args_str = captures.get(2).unwrap().as_str();

        // Parse arguments (simple space-separated for now)
        let arguments: Vec<String> = if args_str.is_empty() {
            Vec::new()
        } else {
            args_str.split_whitespace().map(|s| s.to_string()).collect()
        };

        Ok(Some(Directive {
            name,
            arguments,
            options: HashMap::new(),
            content: Vec::new(),
            line_number,
            source_file: source_file.to_string(),
        }))
    } else {
        Ok(None)
    }
}

// Admonition Directive
struct AdmonitionDirective {
    name: String,
}

impl AdmonitionDirective {
    fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl DirectiveProcessor for AdmonitionDirective {
    fn process(&self, directive: &Directive) -> Result<String> {
        let class = if self.name == "seealso" {
            "seealso"
        } else {
            &self.name
        };
        let title = if directive.arguments.is_empty() {
            match self.name.as_str() {
                "note" => "Note",
                "warning" => "Warning",
                "important" => "Important",
                "tip" => "Tip",
                "caution" => "Caution",
                "danger" => "Danger",
                "error" => "Error",
                "hint" => "Hint",
                "attention" => "Attention",
                "seealso" => "See also",
                _ => &self.name,
            }
        } else {
            &directive.arguments[0]
        };

        let content = directive.content.join("\n");

        Ok(format!(
            "<div class=\"admonition {}\"><p class=\"admonition-title\">{}</p>{}</div>",
            class, title, content
        ))
    }

    fn get_name(&self) -> &str {
        &self.name
    }

    fn get_option_spec(&self) -> HashMap<String, DirectiveOptionType> {
        let mut options = HashMap::new();
        options.insert("class".to_string(), DirectiveOptionType::ClassOption);
        options.insert("name".to_string(), DirectiveOptionType::String);
        options
    }
}

// Generic Admonition Directive
struct GenericAdmonitionDirective;

impl DirectiveProcessor for GenericAdmonitionDirective {
    fn process(&self, directive: &Directive) -> Result<String> {
        let default_title = "Admonition".to_string();
        let title = directive.arguments.first().unwrap_or(&default_title);
        let content = directive.content.join("\n");

        Ok(format!(
            "<div class=\"admonition admonition-generic\"><p class=\"admonition-title\">{}</p>{}</div>",
            title, content
        ))
    }

    fn get_name(&self) -> &str {
        "admonition"
    }

    fn get_option_spec(&self) -> HashMap<String, DirectiveOptionType> {
        let mut options = HashMap::new();
        options.insert("class".to_string(), DirectiveOptionType::ClassOption);
        options.insert("name".to_string(), DirectiveOptionType::String);
        options
    }
}

// Code Block Directive
struct CodeBlockDirective;

impl DirectiveProcessor for CodeBlockDirective {
    fn process(&self, directive: &Directive) -> Result<String> {
        let default_language = "text".to_string();
        let language = directive.arguments.first().unwrap_or(&default_language);
        let _linenos = directive.options.contains_key("linenos");
        let _emphasize_lines = directive.options.get("emphasize-lines");
        let caption = directive.options.get("caption");
        let _name = directive.options.get("name");

        let content = directive.content.join("\n");

        let mut html = String::new();

        if let Some(caption_text) = caption {
            html.push_str(&format!(
                "<div class=\"code-block-caption\">{}</div>",
                caption_text
            ));
        }

        html.push_str(&format!(
            "<div class=\"highlight-{}\"><pre><code class=\"language-{}\">{}</code></pre></div>",
            language,
            language,
            html_escape::encode_text(&content)
        ));

        Ok(html)
    }

    fn get_name(&self) -> &str {
        "code-block"
    }

    fn get_option_spec(&self) -> HashMap<String, DirectiveOptionType> {
        let mut options = HashMap::new();
        options.insert("linenos".to_string(), DirectiveOptionType::Flag);
        options.insert("lineno-start".to_string(), DirectiveOptionType::Integer);
        options.insert("emphasize-lines".to_string(), DirectiveOptionType::String);
        options.insert("caption".to_string(), DirectiveOptionType::String);
        options.insert("name".to_string(), DirectiveOptionType::String);
        options.insert("dedent".to_string(), DirectiveOptionType::Integer);
        options.insert("force".to_string(), DirectiveOptionType::Flag);
        options
    }
}

// Literal Include Directive
struct LiteralIncludeDirective;

impl DirectiveProcessor for LiteralIncludeDirective {
    fn process(&self, directive: &Directive) -> Result<String> {
        let filename = directive
            .arguments
            .first()
            .ok_or_else(|| anyhow!("literalinclude directive requires a filename"))?;

        let language = directive
            .options
            .get("language")
            .cloned()
            .or_else(|| {
                std::path::Path::new(filename)
                    .extension()
                    .and_then(|ext| ext.to_str())
                    .map(|ext| {
                        match ext {
                            "py" => "python",
                            "rs" => "rust",
                            "js" => "javascript",
                            "ts" => "typescript",
                            "cpp" | "cc" | "cxx" => "cpp",
                            "c" => "c",
                            "h" | "hpp" => "cpp",
                            "java" => "java",
                            "go" => "go",
                            "php" => "php",
                            "rb" => "ruby",
                            "sh" | "bash" => "bash",
                            "ps1" => "powershell",
                            "sql" => "sql",
                            "xml" => "xml",
                            "html" => "html",
                            "css" => "css",
                            "json" => "json",
                            "yaml" | "yml" => "yaml",
                            "toml" => "toml",
                            "ini" => "ini",
                            "md" => "markdown",
                            "rst" => "rst",
                            "tex" => "latex",
                            _ => "text",
                        }
                        .to_string()
                    })
            })
            .unwrap_or_else(|| "text".to_string());

        // For now, return a placeholder. In a full implementation,
        // you would read the file and include its contents
        Ok(format!(
            "<div class=\"literal-include\"><div class=\"highlight-{}\"><pre><code class=\"language-{}\"><!-- Content of {} would be included here --></code></pre></div></div>",
            language, language, filename
        ))
    }

    fn get_name(&self) -> &str {
        "literalinclude"
    }

    fn get_option_spec(&self) -> HashMap<String, DirectiveOptionType> {
        let mut options = HashMap::new();
        options.insert("language".to_string(), DirectiveOptionType::String);
        options.insert("linenos".to_string(), DirectiveOptionType::Flag);
        options.insert("lineno-start".to_string(), DirectiveOptionType::Integer);
        options.insert("emphasize-lines".to_string(), DirectiveOptionType::String);
        options.insert("lines".to_string(), DirectiveOptionType::String);
        options.insert("start-line".to_string(), DirectiveOptionType::Integer);
        options.insert("end-line".to_string(), DirectiveOptionType::Integer);
        options.insert("start-after".to_string(), DirectiveOptionType::String);
        options.insert("end-before".to_string(), DirectiveOptionType::String);
        options.insert("prepend".to_string(), DirectiveOptionType::String);
        options.insert("append".to_string(), DirectiveOptionType::String);
        options.insert("dedent".to_string(), DirectiveOptionType::Integer);
        options.insert("tab-width".to_string(), DirectiveOptionType::Integer);
        options.insert("encoding".to_string(), DirectiveOptionType::Encoding);
        options.insert("pyobject".to_string(), DirectiveOptionType::String);
        options.insert("caption".to_string(), DirectiveOptionType::String);
        options.insert("name".to_string(), DirectiveOptionType::String);
        options.insert("class".to_string(), DirectiveOptionType::ClassOption);
        options.insert("diff".to_string(), DirectiveOptionType::String);
        options
    }
}

// Highlight Directive
struct HighlightDirective;

impl DirectiveProcessor for HighlightDirective {
    fn process(&self, directive: &Directive) -> Result<String> {
        let default_language = "text".to_string();
        let language = directive.arguments.first().unwrap_or(&default_language);
        // This directive sets the highlighting language for subsequent code blocks
        Ok(format!("<!-- highlight language set to {} -->", language))
    }

    fn get_name(&self) -> &str {
        "highlight"
    }

    fn get_option_spec(&self) -> HashMap<String, DirectiveOptionType> {
        let mut options = HashMap::new();
        options.insert("linenothreshold".to_string(), DirectiveOptionType::Integer);
        options.insert("force".to_string(), DirectiveOptionType::Flag);
        options
    }
}

// Additional directive implementations would go here...
// For brevity, I'll provide stub implementations for the remaining directives

macro_rules! stub_directive {
    ($name:ident, $directive_name:expr) => {
        struct $name;

        impl DirectiveProcessor for $name {
            fn process(&self, directive: &Directive) -> Result<String> {
                Ok(format!(
                    "<!-- {} directive: {} -->",
                    $directive_name,
                    directive.arguments.join(" ")
                ))
            }

            fn get_name(&self) -> &str {
                $directive_name
            }

            fn get_option_spec(&self) -> HashMap<String, DirectiveOptionType> {
                HashMap::new()
            }
        }
    };
}

stub_directive!(ToctreeDirective, "toctree");
stub_directive!(IndexDirective, "index");
stub_directive!(OnlyDirective, "only");
stub_directive!(IfConfigDirective, "ifconfig");
stub_directive!(ImageDirective, "image");
stub_directive!(FigureDirective, "figure");
stub_directive!(TableDirective, "table");
stub_directive!(CsvTableDirective, "csv-table");
stub_directive!(ListTableDirective, "list-table");
stub_directive!(IncludeDirective, "include");
stub_directive!(RawDirective, "raw");
stub_directive!(MathDirective, "math");
stub_directive!(AutoDocDirective, "autodoc");
stub_directive!(AutoModuleDirective, "automodule");
stub_directive!(AutoClassDirective, "autoclass");
stub_directive!(AutoFunctionDirective, "autofunction");
stub_directive!(MetaDirective, "meta");
stub_directive!(SidebarDirective, "sidebar");
stub_directive!(TopicDirective, "topic");
stub_directive!(RubricDirective, "rubric");
stub_directive!(EpigraphDirective, "epigraph");
stub_directive!(HighlightsDirective, "highlights");
stub_directive!(PullQuoteDirective, "pull-quote");
stub_directive!(CompoundDirective, "compound");
stub_directive!(ContainerDirective, "container");
stub_directive!(VersionAddedDirective, "versionadded");
stub_directive!(VersionChangedDirective, "versionchanged");
stub_directive!(DeprecatedDirective, "deprecated");