|
| 1 | +# -*- coding: utf-8; -*- |
| 2 | +""" |
| 3 | +Stepper directive for step-by-step documentation. |
| 4 | +""" |
| 5 | + |
| 6 | +from docutils import nodes |
| 7 | +from sphinx.util.docutils import SphinxDirective |
| 8 | + |
| 9 | + |
| 10 | +# Define custom nodes |
| 11 | +class stepper_node(nodes.General, nodes.Element): |
| 12 | + pass |
| 13 | + |
| 14 | +class step_node(nodes.General, nodes.Element): |
| 15 | + pass |
| 16 | + |
| 17 | +class StepperDirective(SphinxDirective): |
| 18 | + """Container for step-by-step instructions.""" |
| 19 | + |
| 20 | + has_content = True |
| 21 | + option_spec = { |
| 22 | + 'class': str, # directives.class_option is for multiple classes |
| 23 | + } |
| 24 | + |
| 25 | + def run(self): |
| 26 | + stepper = stepper_node() |
| 27 | + stepper['classes'] = self.options.get('class', '').split() # Add custom classes if provided |
| 28 | + self_content = nodes.paragraph() |
| 29 | + self.state.nested_parse(self.content, self.content_offset, self_content) |
| 30 | + |
| 31 | + step_number = 1 |
| 32 | + for child in self_content.children: |
| 33 | + if isinstance(child, step_node): |
| 34 | + child['step-number'] = step_number |
| 35 | + step_number += 1 |
| 36 | + stepper += child # Add all children from self_content to stepper |
| 37 | + |
| 38 | + return [stepper] |
| 39 | + |
| 40 | + |
| 41 | +class StepDirective(SphinxDirective): |
| 42 | + """Individual step in a stepper.""" |
| 43 | + |
| 44 | + has_content = True |
| 45 | + required_arguments = 1 # Step title |
| 46 | + optional_arguments = 0 |
| 47 | + final_argument_whitespace = True # Allow spaces in title |
| 48 | + option_spec = { |
| 49 | + 'class': str, |
| 50 | + } |
| 51 | + |
| 52 | + def run(self): |
| 53 | + step = step_node() |
| 54 | + step['classes'] = ['stepper-step'] |
| 55 | + step['classes'].extend(self.options.get('class', '').split()) |
| 56 | + |
| 57 | + # Set title |
| 58 | + step['step-title'] = self.arguments[0] |
| 59 | + |
| 60 | + # Parse content |
| 61 | + self.state.nested_parse( |
| 62 | + self.content, |
| 63 | + self.content_offset, |
| 64 | + step |
| 65 | + ) |
| 66 | + return [step] |
| 67 | + |
| 68 | +# HTML Translator functions |
| 69 | +def html_visit_stepper_node(self, node): |
| 70 | + self.context.append('') |
| 71 | + self.body.append(self.starttag(node, 'div', CLASS='stepper')) |
| 72 | + |
| 73 | +def html_depart_stepper_node(self, node): |
| 74 | + self.body.append('</div>\n') |
| 75 | + self.context.pop() |
| 76 | + |
| 77 | +def html_visit_step_node(self, node): |
| 78 | + self.context.append('') |
| 79 | + self.body.append(self.starttag(node, 'div', CLASS='stepper-step')) |
| 80 | + self.body.append(f'<div class="stepper-header">') |
| 81 | + self.body.append(f'<div class="stepper-number">{node.get("step-number", "")}</div>') |
| 82 | + self.body.append(f'<div class="stepper-title">{node.get("step-title", "")}</div>') |
| 83 | + self.body.append(f'</div>') |
| 84 | + self.body.append(f'<div class="stepper-content">') |
| 85 | + |
| 86 | +def html_depart_step_node(self, node): |
| 87 | + self.body.append('</div>\n') # Close stepper-content |
| 88 | + self.body.append('</div>\n') # Close stepper-step |
| 89 | + self.context.pop() |
| 90 | + |
| 91 | + |
| 92 | +def setup(app): |
| 93 | + """Register the stepper directives and nodes.""" |
| 94 | + app.add_node(stepper_node, html=(html_visit_stepper_node, html_depart_stepper_node)) |
| 95 | + app.add_node(step_node, html=(html_visit_step_node, html_depart_step_node)) |
| 96 | + |
| 97 | + app.add_directive('stepper', StepperDirective) |
| 98 | + app.add_directive('step', StepDirective) |
| 99 | + |
| 100 | + return { |
| 101 | + 'version': '0.1', |
| 102 | + 'parallel_read_safe': True, |
| 103 | + 'parallel_write_safe': True, |
| 104 | + } |
0 commit comments