using include directive with :code: option doesn't highlight the code
Describe the bug
When using the .. include:: directive with the :code: option, the included source code is not properly lexed / highlighted.
How to Reproduce
Here is a small MWE page:
https://dan.mccloy.info/data/sphinx-include-highlight-mwe/
and here is the rST source of that page:
https://dan.mccloy.info/data/sphinx-include-highlight-mwe/_sources/index.rst.txt
Environment Information
Platform: linux; (Linux-5.15.0-58-generic-x86_64-with-glibc2.35)
Python version: 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:06:46) [GCC 10.3.0])
Python implementation: CPython
Sphinx version: 5.3.0
Docutils version: 0.18.1
Jinja2 version: 3.1.2
Sphinx extensions
n/a, my `conf.py` was untouched after creation by `sphinx-quickstart`
Additional context
marginally related is #9407, since (as seen in the MWE) one of the possible approaches to this yields an extra blank line in the included code.
Like the "code" role, the "code" option of the "include" directive uses the Docutils version of the "code" directive. (Sphinx overwrites the "code" directive with its own, slightly different version.) By default, Docutils uses "long" class values (e.g. "operator word" instead of "ow"). This can be configured with the syntax_highlight configuration setting. Sphinx compatibility is achieved with
syntax-highlight: short
in a Docutils configuration file in the project root directory.
It may be a good idea, if Sphinx provided a custom default, either a docutils.conf file generated by sphinx-quickstart or a settings_overrides dict with {"syntax_highlight": "short"}.
Thanks @gmilde that's quite helpful. I'm able to get proper highlighting now with:
.. include:: ./extending.rst
:code: rst
:class: highlight
and a docutils.conf file as you described. Additionally this:
Sphinx overwrites the "code" directive with its own, slightly different version
turns out to be helpful info too; it explains (I think) why the enclosing HTML tags are different and consequently why it's so troublesome to write CSS that targets all of the different kinds of source code inclusion.
I'll leave the issue open, as I think there are some concrete actionable steps you've suggested for how Sphinx could make this easier/smoother for users / themers.