-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Render code block for highlight.js
- Loading branch information
Showing
8 changed files
with
107 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,9 @@ | |
Change logs | ||
=========== | ||
|
||
v0.0.0 | ||
v0.0.1 | ||
====== | ||
|
||
:date: 2023-05-01 | ||
:date: 2023-10-21 | ||
|
||
Initial commit. | ||
First release. |
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
This file was deleted.
Oops, something went wrong.
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,49 @@ | ||
"""Override code-block output for highlight.js""" | ||
from pathlib import Path | ||
|
||
from docutils import nodes | ||
from sphinx.application import Sphinx | ||
from sphinx.config import Config | ||
from sphinx.util.docutils import SphinxTranslator | ||
|
||
__version__ = "0.0.0" | ||
here = Path(__file__).parent | ||
|
||
|
||
def register_ext_static(app: Sphinx, config: Config): | ||
if not hasattr(config, "html_static_path"): | ||
config.html_static_path = [] | ||
config.html_static_path += [str(here / "_static")] | ||
|
||
|
||
def register_highlightjs(app: Sphinx): | ||
"""Append static contents to highlight by highlight.js.""" | ||
app.builder.add_css_file("https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css") | ||
app.builder.add_css_file("highlight.css") | ||
app.builder.add_js_file("https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js") | ||
app.builder.add_js_file("highlight.js") | ||
|
||
|
||
def visit_literal_block(self: SphinxTranslator, node: nodes.literal_block): | ||
lang = node["language"] | ||
self.body.append(f'<pre><code class="language-{lang}">') | ||
|
||
|
||
def depart_literal_block(self: SphinxTranslator, node: nodes.literal_block): | ||
self.body.append("</code></pre>") | ||
|
||
|
||
def setup(app: Sphinx): # noqa: D103 | ||
app.connect("config-inited", register_ext_static) | ||
app.connect("builder-inited", register_highlightjs) | ||
app.add_node( | ||
nodes.literal_block, | ||
override=True, | ||
html=(visit_literal_block, depart_literal_block), | ||
) | ||
return { | ||
"version": __version__, | ||
"env_version": 1, | ||
"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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Reset pre style for Pygments in Sphinx. | ||
*/ | ||
pre:has(code) { | ||
padding: 0px; | ||
} |
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,4 @@ | ||
/** | ||
* Affect highlight.js | ||
*/ | ||
hljs.highlightAll(); |