| 14 Aug 2023 |
@mightyiam:matrix.org | Yuki (they/them): help me with sphinx, I'm drowning | 15:18:50 |
@mightyiam:matrix.org | I'm trying to make an extension that replaces the nix-repl language tag with the text language tag. By language tag I mean the first word after the triple backtick in fenced code blocks.
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util.docutils import SphinxDirective
class Directive(SphinxDirective):
has_content = True
def run(self):
content = '\n'.join(self.content)
code_block = nodes.literal_block(classes=['nix-repl'], language='text')
code_block.append(nodes.Text(content))
return [code_block]
def setup(app):
app.add_directive('nix-repl', Directive)
| 15:20:59 |
@mightyiam:matrix.org | That stuff doesn't seem to do what I want, though. We still get a warning that there is no such language as nix-repl. | 15:21:42 |
Yuki Langley (they/them) | Why do you want to do this? | 15:23:18 |
Yuki Langley (they/them) | Is the goal to extract all code that has "nix-repl" | 15:23:47 |
Yuki Langley (they/them) | * Is the goal to extract all code that has "nix-repl"? | 15:23:48 |
osslate | If recognizing/extracting repl codeblocks is the goal, it might be better to engineer a solution that enumerates over nix (or text) codeblocks and identifies the nix repl prompt | 15:24:42 |
@mightyiam:matrix.org | Because I really want to mark some fenced code blocks as nix-repl.
I wrote a program that parses the same sources, unrelated to sphinx, runs those nix repl examples in actual nix repl.
| 15:24:52 |
@mightyiam:matrix.org | Identifying the prompt sounds like a workaround that affects the author of the documentation. | 15:26:22 |
@mightyiam:matrix.org | The author of the documentation "wants", according to me, to have nix-repl as the language tag. | 15:26:45 |
@mightyiam:matrix.org | All the silly extension has to do, is to replace all nix-repl language tags with text. | 15:27:20 |
osslate | Is this a thing that people have asked for previously? | 15:27:35 |
Yuki Langley (they/them) | sphinx.ext.doctest is an extension which does something similar: https://github.com/sphinx-doc/sphinx/blob/0bad447070d45fc5538aed9bf15a1716c628f321/sphinx/ext/doctest.py#L157
https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html#module-sphinx.ext.doctest
| 15:27:50 |
Yuki Langley (they/them) | * sphinx.ext.doctest is an extension which does something similar (but for Python): https://github.com/sphinx-doc/sphinx/blob/0bad447070d45fc5538aed9bf15a1716c628f321/sphinx/ext/doctest.py#L157
https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html#module-sphinx.ext.doctest
| 15:28:00 |
@mightyiam:matrix.org | The testing part is implemented. | 15:28:26 |
osslate | In any case, you might get better support from the Sphinx chat channel unless we've resident Sphinx experts about | 15:28:27 |
proofconstruction | In reply to @osslate:fsfe.org In any case, you might get better support from the Sphinx chat channel unless we've resident Sphinx experts about Yuki is the resident Sphinx expert 😁 | 15:28:57 |
@mightyiam:matrix.org | I've been asked to implement this, yes. | 15:29:03 |
osslate | Aha! | 15:29:11 |
Yuki Langley (they/them) | also just in case, You do directives in MyST like this:
\`\`\`{nix-repl}
\`\`\`
| 15:29:51 |
Yuki Langley (they/them) | * also just in case, You do directives in MyST like this:
```{nix-repl}
| 15:30:00 |
Yuki Langley (they/them) | * also just in case, You do directives in MyST like this:
```{nix-repl}
```
| 15:30:08 |