I am trying to make a sphinx extension (for html output) that generates a code block and inserts text and span
s into it.
However when I create a literal_block
(code block) and add inline
s (spans) to it, only the text of the inline nodes will appear inside the code block but not as span elements.
from docutils import nodes
from docutils.parsers import rst
from sphinx.addnodes import highlightlang
class MyDirective(rst.Directive):
# for simplicity and debug purposes no content is used for now
has_content = True
def run(self):
node = nodes.literal_block()
node += nodes.inline("",
text = "Hello World",
classes = ["myclass"]
)
return [
highlightlang(
lang = "none",
force = None,
linenothreshold = -1
),
node
]
def setup(app):
app.add_directive("helloworld", MyDirective)
Expecting something like
<div class="highlight-none notranslate">
<div class="highlight">
<pre><span class="myclass">Hello World</span></pre>
</div>
</div>
Getting something like
<div class="highlight-none notranslate">
<div class="highlight">
<pre>Hello World</pre>
</div>
</div>