Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

texinfo: improve variable in :samp: directives #9391

Merged
merged 1 commit into from Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion sphinx/writers/texinfo.py
Expand Up @@ -194,6 +194,7 @@ def __init__(self, document: nodes.document, builder: "TexinfoBuilder") -> None:
self.curfilestack: List[str] = []
self.footnotestack: List[Dict[str, List[Union[collected_footnote, bool]]]] = [] # NOQA
self.in_footnote = 0
self.in_samp = 0
self.handled_abbrs: Set[str] = set()
self.colwidths: List[int] = None

Expand Down Expand Up @@ -809,15 +810,23 @@ def depart_strong(self, node: Element) -> None:
self.body.append('}')

def visit_emphasis(self, node: Element) -> None:
self.body.append('@emph{')
element = 'emph' if not self.in_samp else 'var'
self.body.append('@%s{' % element)

def depart_emphasis(self, node: Element) -> None:
self.body.append('}')

def is_samp(self, node: Element) -> bool:
return 'samp' in node['classes']

def visit_literal(self, node: Element) -> None:
if self.is_samp(node):
self.in_samp += 1
self.body.append('@code{')

def depart_literal(self, node: Element) -> None:
if self.is_samp(node):
self.in_samp -= 1
self.body.append('}')

def visit_superscript(self, node: Element) -> None:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_build_texinfo.py
Expand Up @@ -112,3 +112,14 @@ def test_texinfo_escape_id(app, status, warning):
assert translator.escape_id('Hello(world)') == 'Hello world'
assert translator.escape_id('Hello world.') == 'Hello world'
assert translator.escape_id('.') == '.'


@pytest.mark.sphinx('texinfo', testroot='root')
def test_texinfo_samp_with_variable(app, status, warning):
app.build()

output = (app.outdir / 'sphinxtests.texi').read_text()

assert '@code{@var{variable_only}}' in output
assert '@code{@var{variable} and text}' in output
assert '@code{Show @var{variable} in the middle}' in output