Skip to content

Commit

Permalink
feat: Support literalinclude (linenos)
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Oct 21, 2023
1 parent 8432fdb commit f6934ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 3 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ Basic usage
When you set extension into your ``conf.py`` of documentation,
builder changes behaviors of code highlightings.

.. code:: python
# Your conf.py
extensions = [
"atsphinx.highlightjs",
]
.. literalinclude:: conf.py
:emphasize-lines: 11-13
:language: python

Please see HTML source, this includes only ``<pre><code>`` element only
(if using Pygments, it renders parts of contents).
Expand Down
12 changes: 10 additions & 2 deletions src/atsphinx/highlightjs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ def register_highlightjs(app: Sphinx):

def visit_literal_block(self: SphinxTranslator, node: nodes.literal_block):
lang = node["language"]
self.body.append(f'<pre><code class="language-{lang}">')
code = node.rawsource
if "hl_lines" in node["highlight_args"]:
splitted = node.rawsource.split("\n")
code = "\n".join(
splitted[line-1]
for line in node["highlight_args"]["hl_lines"]
)
self.body.append(f'<pre><code class="language-{lang}">{code}</code></pre>')
raise nodes.SkipNode


def depart_literal_block(self: SphinxTranslator, node: nodes.literal_block):
self.body.append("</code></pre>")
pass


def setup(app: Sphinx): # noqa: D103
Expand Down

0 comments on commit f6934ba

Please sign in to comment.