-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom parser for Sphinx with support for Nikola titles
- Loading branch information
Showing
4 changed files
with
44 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import sphinx.parsers | ||
from docutils.statemachine import StringList | ||
from typing import TYPE_CHECKING, Any, Dict, List, Type, Union | ||
|
||
if TYPE_CHECKING: | ||
from sphinx.application import Sphinx | ||
|
||
TITLE_MARKER = ".. title:" | ||
|
||
|
||
class NikolaTitlesRSTParser(sphinx.parsers.RSTParser): | ||
def decorate(self, content: StringList) -> None: | ||
"""Preprocess reST content before parsing.""" | ||
super().decorate(content) | ||
for line in content[:20]: | ||
if line.startswith(TITLE_MARKER): | ||
title = line[len(TITLE_MARKER) :].strip() | ||
fence = "=" * len(title) | ||
content.insert(0, "", "<generated>", 0) | ||
content.insert(0, fence, "<generated>", 0) | ||
content.insert(0, title, "<generated>", 0) | ||
content.insert(0, fence, "<generated>", 0) | ||
|
||
|
||
def setup(app: "Sphinx") -> Dict[str, Any]: | ||
app.add_source_parser(NikolaTitlesRSTParser, override=True) | ||
|
||
return { | ||
"version": "0.1.0", | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
-r ../../requirements-extras.txt | ||
-e ../.. | ||
pyparsing>=2.0.2 | ||
Sphinx>=4.2.0 |