| 1 |
from collections import defaultdict |
| 2 |
|
| 3 |
from docutils.parsers.rst import directives |
| 4 |
|
| 5 |
from sphinx import addnodes |
| 6 |
from sphinx.application import Sphinx |
| 7 |
from sphinx.directives import ObjectDescription |
| 8 |
from sphinx.domains import Domain, Index |
| 9 |
from sphinx.roles import XRefRole |
| 10 |
from sphinx.util.nodes import make_refnode |
| 11 |
from sphinx.util.typing import ExtensionMetadata |
| 12 |
|
| 13 |
|
| 14 |
class RecipeDirective(ObjectDescription): |
| 15 |
"""A custom directive that describes a recipe.""" |
| 16 |
|
| 17 |
has_content = True |
| 18 |
required_arguments = 1 |
| 19 |
option_spec = { |
| 20 |
'contains': directives.unchanged_required, |
| 21 |
} |
| 22 |
|
| 23 |
def handle_signature(self, sig, signode): |
| 24 |
signode += addnodes.desc_name(text=sig) |
| 25 |
return sig |
| 26 |
|
| 27 |
def add_target_and_index(self, name_cls, sig, signode): |
| 28 |
signode['ids'].append('recipe' + '-' + sig) |
| 29 |
if 'contains' in self.options: |
| 30 |
ingredients = [x.strip() for x in self.options.get('contains').split(',')] |
| 31 |
|
| 32 |
recipes = self.env.get_domain('recipe') |
| 33 |
recipes.add_recipe(sig, ingredients) |
| 34 |
|
| 35 |
|
| 36 |
class IngredientIndex(Index): |
| 37 |
"""A custom index that creates an ingredient matrix.""" |
| 38 |
|
| 39 |
name = 'ingredient' |
| 40 |
localname = 'Ingredient Index' |
| 41 |
shortname = 'Ingredient' |
| 42 |
|
| 43 |
def generate(self, docnames=None): |
| 44 |
content = defaultdict(list) |
| 45 |
|
| 46 |
recipes = { |
| 47 |
name: (dispname, typ, docname, anchor) |
| 48 |
for name, dispname, typ, docname, anchor, _ in self.domain.get_objects() |
| 49 |
} |
| 50 |
recipe_ingredients = self.domain.data['recipe_ingredients'] |
| 51 |
ingredient_recipes = defaultdict(list) |
| 52 |
|
| 53 |
# flip from recipe_ingredients to ingredient_recipes |
| 54 |
for recipe_name, ingredients in recipe_ingredients.items(): |
| 55 |
for ingredient in ingredients: |
| 56 |
ingredient_recipes[ingredient].append(recipe_name) |
| 57 |
|
| 58 |
# convert the mapping of ingredient to recipes to produce the expected |
| 59 |
# output, shown below, using the ingredient name as a key to group |
| 60 |
# |
| 61 |
# name, subtype, docname, anchor, extra, qualifier, description |
| 62 |
for ingredient, recipe_names in ingredient_recipes.items(): |
| 63 |
for recipe_name in recipe_names: |
| 64 |
dispname, typ, docname, anchor = recipes[recipe_name] |
| 65 |
content[ingredient].append(( |
| 66 |
dispname, |
| 67 |
0, |
| 68 |
docname, |
| 69 |
anchor, |
| 70 |
docname, |
| 71 |
'', |
| 72 |
typ, |
| 73 |
)) |
| 74 |
|
| 75 |
# convert the dict to the sorted list of tuples expected |
| 76 |
content = sorted(content.items()) |
| 77 |
|
| 78 |
return content, True |
| 79 |
|
| 80 |
|
| 81 |
class RecipeIndex(Index): |
| 82 |
"""A custom index that creates an recipe matrix.""" |
| 83 |
|
| 84 |
name = 'recipe' |
| 85 |
localname = 'Recipe Index' |
| 86 |
shortname = 'Recipe' |
| 87 |
|
| 88 |
def generate(self, docnames=None): |
| 89 |
content = defaultdict(list) |
| 90 |
|
| 91 |
# sort the list of recipes in alphabetical order |
| 92 |
recipes = self.domain.get_objects() |
| 93 |
recipes = sorted(recipes, key=lambda recipe: recipe[0]) |
| 94 |
|
| 95 |
# generate the expected output, shown below, from the above using the |
| 96 |
# first letter of the recipe as a key to group thing |
| 97 |
# |
| 98 |
# name, subtype, docname, anchor, extra, qualifier, description |
| 99 |
for _name, dispname, typ, docname, anchor, _priority in recipes: |
| 100 |
content[dispname[0].lower()].append(( |
| 101 |
dispname, |
| 102 |
0, |
| 103 |
docname, |
| 104 |
anchor, |
| 105 |
docname, |
| 106 |
'', |
| 107 |
typ, |
| 108 |
)) |
| 109 |
|
| 110 |
# convert the dict to the sorted list of tuples expected |
| 111 |
content = sorted(content.items()) |
| 112 |
|
| 113 |
return content, True |
| 114 |
|
| 115 |
|
| 116 |
class RecipeDomain(Domain): |
| 117 |
name = 'recipe' |
| 118 |
label = 'Recipe Sample' |
| 119 |
roles = { |
| 120 |
'ref': XRefRole(), |
| 121 |
} |
| 122 |
directives = { |
| 123 |
'recipe': RecipeDirective, |
| 124 |
} |
| 125 |
indices = { |
| 126 |
RecipeIndex, |
| 127 |
IngredientIndex, |
| 128 |
} |
| 129 |
initial_data = { |
| 130 |
'recipes': [], # object list |
| 131 |
'recipe_ingredients': {}, # name -> object |
| 132 |
} |
| 133 |
data_version = 0 |
| 134 |
|
| 135 |
def get_full_qualified_name(self, node): |
| 136 |
return f'recipe.{node.arguments[0]}' |
| 137 |
|
| 138 |
def get_objects(self): |
| 139 |
yield from self.data['recipes'] |
| 140 |
|
| 141 |
def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode): |
| 142 |
match = [ |
| 143 |
(docname, anchor) |
| 144 |
for name, sig, typ, docname, anchor, prio in self.get_objects() |
| 145 |
if sig == target |
| 146 |
] |
| 147 |
|
| 148 |
if len(match) > 0: |
| 149 |
todocname = match[0][0] |
| 150 |
targ = match[0][1] |
| 151 |
|
| 152 |
return make_refnode(builder, fromdocname, todocname, targ, contnode, targ) |
| 153 |
else: |
| 154 |
print('Awww, found nothing') |
| 155 |
return None |
| 156 |
|
| 157 |
def add_recipe(self, signature, ingredients): |
| 158 |
"""Add a new recipe to the domain.""" |
| 159 |
name = f'recipe.{signature}' |
| 160 |
anchor = f'recipe-{signature}' |
| 161 |
|
| 162 |
self.data['recipe_ingredients'][name] = ingredients |
| 163 |
# name, dispname, type, docname, anchor, priority |
| 164 |
self.data['recipes'].append(( |
| 165 |
name, |
| 166 |
signature, |
| 167 |
'Recipe', |
| 168 |
self.env.current_document.docname, |
| 169 |
anchor, |
| 170 |
0, |
| 171 |
)) |
| 172 |
|
| 173 |
|
| 174 |
def setup(app: Sphinx) -> ExtensionMetadata: |
| 175 |
app.add_domain(RecipeDomain) |
| 176 |
|
| 177 |
return { |
| 178 |
'version': '0.1', |
| 179 |
'parallel_read_safe': True, |
| 180 |
'parallel_write_safe': True, |
| 181 |
} |