Skip to content

Commit f3c4d08

Browse files
authored
DOP-5604: dismissible-skills-card directive (#664)
* RstSpec and postprocess handler for dismissible skills card * Use DismissibleSkillsCard node * Clean postprocess * Correct errors * Fix types * PR feedback * remove unused import * add postprocess test
1 parent 4e923e0 commit f3c4d08

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

snooty/postprocess.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,47 @@ def exit_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
19921992
self.collapsible_detected = False
19931993

19941994

1995+
class DismissibleSkillsCardHandler(Handler):
1996+
"""Handles 'dismissible-skills-card' directives on a page.
1997+
Only one is allowed per page. Adds the data to page AST options."""
1998+
1999+
@dataclass
2000+
class DismissibleSkillsCard:
2001+
skill: str
2002+
url: str
2003+
2004+
def __init__(self, context: Context) -> None:
2005+
super().__init__(context)
2006+
self.dismissible_skills_card: Optional[
2007+
DismissibleSkillsCardHandler.DismissibleSkillsCard
2008+
] = None
2009+
2010+
def enter_page(self, fileid_stack: FileIdStack, page: Page) -> None:
2011+
self.dismissible_skills_card = None
2012+
2013+
def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
2014+
if not isinstance(node, n.Directive) or node.name != "dismissible-skills-card":
2015+
return
2016+
if self.dismissible_skills_card:
2017+
self.context.diagnostics[fileid_stack.current].append(
2018+
DuplicateDirective(node.name, node.span[0])
2019+
)
2020+
2021+
skill = node.options.get("skill")
2022+
url = node.options.get("url")
2023+
if skill and url:
2024+
self.dismissible_skills_card = self.DismissibleSkillsCard(
2025+
skill=skill, url=url
2026+
)
2027+
2028+
def exit_page(self, fileid_stack: FileIdStack, page: Page) -> None:
2029+
if self.dismissible_skills_card:
2030+
page.ast.options["dismissible_skills_card"] = {
2031+
"skill": self.dismissible_skills_card.skill,
2032+
"url": self.dismissible_skills_card.url,
2033+
}
2034+
2035+
19952036
class NestedDirectiveHandler(Handler):
19962037
"""Prevents a directive from being nested deeper than intended on a page and from being used twice in a single page."""
19972038

@@ -2261,6 +2302,7 @@ class Postprocessor:
22612302
ImageHandler,
22622303
CollapsibleHandler,
22632304
WayfindingHandler,
2305+
DismissibleSkillsCardHandler,
22642306
MethodSelectorHandler,
22652307
MultiPageTutorialHandler,
22662308
ComposableTutorialHandler,

snooty/rstspec.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,16 @@ help = "Content chosen to be displayed"
878878
options.selections = {type = "string", required = true}
879879
content_type = "block"
880880

881+
[directive."mongodb:dismissible-skills-card"]
882+
help = "A dismissible card that links to Skills"
883+
options.skill = {type = "string", required = true}
884+
options.url = {type = "string", required = true}
885+
example = """
886+
.. dismissible-skills-card::
887+
:skill: CRUD Operations
888+
:url: https://learn.mongodb.com/courses/crud-operations-in-mongodb
889+
"""
890+
881891
###### Misc.
882892
[directive.atf-image]
883893
help = "Path to the image to use for the above-the-fold header image"

snooty/test_postprocess.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,3 +4770,27 @@ def test_composable_collisions() -> None:
47704770
assert len(diagnostics) == 2
47714771
assert isinstance(diagnostics[0], DuplicateDirective)
47724772
assert isinstance(diagnostics[1], UnexpectedDirectiveOrder)
4773+
4774+
4775+
def test_dismissible_skills_card() -> None:
4776+
with make_test(
4777+
{
4778+
Path(
4779+
"source/index.txt"
4780+
): """
4781+
===================
4782+
Heading of the page
4783+
===================
4784+
4785+
.. dismissible-skills-card::
4786+
:skill: WOW Lightsaber Skill
4787+
:url: https://learn.mongodb.com/courses/crud-operations-in-mongodb
4788+
4789+
""",
4790+
}
4791+
) as result:
4792+
page = result.pages[FileId("index.txt")]
4793+
assert (page.ast.options.get("dismissible_skills_card")) == {
4794+
"skill": "WOW Lightsaber Skill",
4795+
"url": "https://learn.mongodb.com/courses/crud-operations-in-mongodb",
4796+
}

0 commit comments

Comments
 (0)