Skip to content

Commit

Permalink
Merge pull request #9391 from marxin/info-samp-with-variable
Browse files Browse the repository at this point in the history
texinfo: improve variable in :samp: directives
  • Loading branch information
tk0miya committed Dec 11, 2021
2 parents 7d6d562 + faed8cb commit b232b00
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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

0 comments on commit b232b00

Please sign in to comment.