llvm-c166/lldb/docs/_ext/build_include.py
Nerixyz b9077c8e6a
[lldb][Docs] Fix presentation of some default values (#192239)
There were two bugs with the display of default values:

1. If a default value contains a backtick, that would render
incorrectly. For example
[`disassembly-format`](https://lldb.llvm.org/use/settings.html#disassembly-format).
Fixed by doing the wrapping when we generate the Markdown instead of
when parsing the directive. MyST will already parse the content of the
directive as Markdown. We can escape backticks inside the string by
changing the fence. Markdown can take any number of backticks at the
start as long as they match the amount at the end
([spec](https://spec.commonmark.org/0.31.2/#code-spans)).
2. When the docs were built on Windows, UTF-8 was not correctly picked
up, because the default encoding isn't utf8 there.
[`separator`](https://lldb.llvm.org/use/settings.html#separator) was one
example (renders correctly on the Website but not on my machine).
2026-04-15 16:03:18 +02:00

48 lines
1.6 KiB
Python

from docutils.parsers.rst import directives, Directive
from docutils import utils, statemachine
from sphinx.application import Sphinx
import os
from pathlib import Path
class BuildInclude(Directive):
"""
Directive to include generated files from the build directory (specified by LLDB_BUILD_DIR).
This is a simplified version of the `include` directive from docutils with the change that paths
are relative to the build directory.
"""
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {"parser": directives.parser_name}
def run(self):
path = directives.path(self.arguments[0])
path = utils.relative_path(None, Path(os.environ["LLDB_BUILD_DIR"]) / path)
with open(path, encoding="utf-8") as f:
rawtext = f.read()
include_lines = statemachine.string2lines(
rawtext, self.state.document.settings.tab_width, convert_whitespace=True
)
# parse into a dummy document and return created nodes
document = utils.new_document(path, self.state.document.settings)
parser = self.options["parser"]()
parser.parse("\n".join(include_lines), document)
# clean up doctree and complete parsing
document.transformer.populate_from_components((parser,))
document.transformer.apply_transforms()
self.state.document.settings.record_dependencies.add(path)
return document.children
def setup(app: Sphinx):
app.add_directive("build-include", BuildInclude)
return {
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}