Skip to content

Commit 76e7ce3

Browse files
Add template for patchouli:relations pages (#92)
2 parents 780c5cc + 49544b3 commit 76e7ce3

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

noxfile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,21 @@ def dummy_setup(session: nox.Session):
383383
"trigger": "story/smelt_iron",
384384
"text": "Wow, what a wonderful quest this is here",
385385
},
386+
{
387+
"type": "patchouli:relations",
388+
"entries": [
389+
"dummy:otherrecipes",
390+
"dummy:bar",
391+
],
392+
"text": "have a look at these related entries!!",
393+
},
394+
{
395+
"type": "patchouli:link",
396+
"url": "https://github.com/hexdoc-dev/hexdoc",
397+
"link_text": "hexdoc GitHub",
398+
"text": "Link page",
399+
},
400+
{"type": "patchouli:empty"},
386401
],
387402
},
388403
"entries/otherrecipes.json": {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "pages/patchouli/text.html.jinja" %}
2+
3+
{% import "macros/formatting.html.jinja" as fmt with context %}
4+
5+
{% block inner_body %}
6+
<ul>
7+
{% for ref in page.get_entries() %}
8+
<li>{{ fmt.maybe_spoilered_link(book.all_entries[ref]) }}</li>
9+
{% endfor %}
10+
</ul>
11+
{{ super() }}
12+
{% endblock %}

src/hexdoc/patchouli/book.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Book(HexdocModel):
3434

3535
# not in book.json
3636
_categories: dict[ResourceLocation, Category] = PrivateAttr(default_factory=dict)
37+
_all_entries: dict[ResourceLocation, Entry] = PrivateAttr(default_factory=dict)
3738

3839
# required
3940
name: LocalizedStr
@@ -77,6 +78,13 @@ class Book(HexdocModel):
7778
def categories(self):
7879
return self._categories
7980

81+
@property
82+
def all_entries(self):
83+
"""
84+
Note: includes external entries as well (not just internal ones)
85+
"""
86+
return self._all_entries
87+
8088
def _load_categories(self, context: ContextSource, book_ctx: BookContext):
8189
categories = Category.load_all(
8290
cast_context(context),
@@ -120,6 +128,7 @@ def _load_entries(
120128
# i used the entry to insert the entry (pretty sure thanos said that)
121129
if resource_dir.internal:
122130
internal_entries[entry.category_id][entry.id] = entry
131+
self._all_entries[entry.id] = entry
123132

124133
link_base = book_ctx.get_link_base(resource_dir)
125134
book_ctx.book_links[entry.book_link_key] = link_base.with_fragment(

src/hexdoc/patchouli/page/pages.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections import defaultdict
22
from typing import Self
33

4+
from jinja2 import pass_context
5+
from jinja2.runtime import Context
46
from pydantic import Field, ValidationInfo, field_validator, model_validator
57

68
from hexdoc.core import Entity, ItemStack, ResourceLocation
@@ -148,6 +150,13 @@ class RelationsPage(PageWithText, type="patchouli:relations"):
148150
entries: list[ResourceLocation]
149151
title: LocalizedStr = LocalizedStr.with_value("Related Chapters")
150152

153+
@pass_context
154+
def get_entries(self, context: Context) -> list[ResourceLocation]:
155+
for entry in self.entries:
156+
if entry not in context["book"].all_entries:
157+
raise ValueError(f"Broken entry reference in relations: {entry}")
158+
return self.entries
159+
151160

152161
class SmeltingPage(PageWithDoubleRecipe[SmeltingRecipe], type="patchouli:smelting"):
153162
pass

0 commit comments

Comments
 (0)