|
| 1 | +import os |
| 2 | + |
| 3 | +import sphinx.addnodes |
| 4 | + |
| 5 | +from docutils import nodes |
| 6 | +from docutils.parsers.rst import Directive |
| 7 | +from sphinx.util import logging |
| 8 | +from sphinx.util.docutils import SphinxDirective |
| 9 | + |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class AddCardsDirective(SphinxDirective): |
| 15 | + """ |
| 16 | + Directive to automatically add cards based on toctree entries. |
| 17 | + """ |
| 18 | + |
| 19 | + has_content = False |
| 20 | + required_arguments = 0 |
| 21 | + optional_arguments = 0 |
| 22 | + final_argument_whitespace = False |
| 23 | + option_spec = {} |
| 24 | + |
| 25 | + def run(self): |
| 26 | + env = self.env |
| 27 | + toctrees = env.tocs.get(env.docname, None) |
| 28 | + |
| 29 | + if not toctrees: |
| 30 | + logger.warning(f"No toctrees found in document {env.docname}") |
| 31 | + return [] |
| 32 | + |
| 33 | + # Find all toctrees in the document |
| 34 | + all_cards_container = nodes.container() |
| 35 | + all_cards_container["classes"] = ["all-tutorial-cards"] |
| 36 | + |
| 37 | + # Process each toctree |
| 38 | + for toctreenode in toctrees.traverse(addnodes.toctree): |
| 39 | + # Get caption |
| 40 | + caption = toctreenode.get("caption", "") |
| 41 | + |
| 42 | + # Create section container |
| 43 | + section_container = nodes.container() |
| 44 | + section_container["classes"] = ["tutorial-section"] |
| 45 | + |
| 46 | + # Add section title if caption exists |
| 47 | + if caption: |
| 48 | + title_node = nodes.paragraph() |
| 49 | + title_node["classes"] = ["tutorial-section-title"] |
| 50 | + title_node += nodes.Text(caption) |
| 51 | + section_container += title_node |
| 52 | + |
| 53 | + # Create cards container |
| 54 | + cards_container = nodes.container() |
| 55 | + cards_container["classes"] = ["tutorial-cards-container"] |
| 56 | + |
| 57 | + # Find all entries in this toctree |
| 58 | + for entry in toctreenode["entries"]: |
| 59 | + doc_name = entry[1] |
| 60 | + title = env.titles.get(doc_name, nodes.title()).astext() or doc_name |
| 61 | + |
| 62 | + # Try to get description from the document |
| 63 | + description = "" |
| 64 | + doc_path = os.path.join(self.env.srcdir, doc_name + ".rst") |
| 65 | + if os.path.exists(doc_path): |
| 66 | + try: |
| 67 | + with open(doc_path, "r") as f: |
| 68 | + content = f.read() |
| 69 | + # Extract first paragraph after title as description |
| 70 | + lines = content.split("\n") |
| 71 | + for i, line in enumerate(lines): |
| 72 | + if i > 2 and line.strip() and not line.startswith(".."): |
| 73 | + description = line.strip() |
| 74 | + break |
| 75 | + except Exception as e: |
| 76 | + logger.warning(f"Error reading {doc_path}: {e}") |
| 77 | + |
| 78 | + # Create card |
| 79 | + card = nodes.container() |
| 80 | + card["classes"] = ["tutorial-card"] |
| 81 | + |
| 82 | + # Add link |
| 83 | + card_link = nodes.reference("", "") |
| 84 | + card_link["refuri"] = entry["refuri"] |
| 85 | + card_link["classes"] = ["card-link"] |
| 86 | + |
| 87 | + # Add title |
| 88 | + title_node = nodes.paragraph() |
| 89 | + title_node["classes"] = ["card-title"] |
| 90 | + title_node += nodes.Text(title) |
| 91 | + card_link += title_node |
| 92 | + |
| 93 | + # Add description if available |
| 94 | + if description: |
| 95 | + desc_node = nodes.paragraph() |
| 96 | + desc_node["classes"] = ["card-description"] |
| 97 | + desc_node += nodes.Text( |
| 98 | + description[:100] + "..." |
| 99 | + if len(description) > 100 |
| 100 | + else description |
| 101 | + ) |
| 102 | + card_link += desc_node |
| 103 | + |
| 104 | + card += card_link |
| 105 | + cards_container += card |
| 106 | + |
| 107 | + section_container += cards_container |
| 108 | + all_cards_container += section_container |
| 109 | + |
| 110 | + return [all_cards_container] |
| 111 | + |
| 112 | + |
| 113 | +def setup(app): |
| 114 | + app.add_directive("add-cards", AddCardsDirective) |
| 115 | + app.add_css_file("tutorial_cards.css") |
| 116 | + |
| 117 | + return { |
| 118 | + "version": "0.1", |
| 119 | + "parallel_read_safe": True, |
| 120 | + "parallel_write_safe": True, |
| 121 | + } |
0 commit comments