Skip to content

Commit

Permalink
Add option_parse_variable_part config value.
Browse files Browse the repository at this point in the history
Support parsing of "variable part" for option directives similarly
to samp roles.

Fixes: sphinx-doc#9965.
  • Loading branch information
marxin committed Jan 6, 2022
1 parent b5ef099 commit 7b0b566
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
8 changes: 8 additions & 0 deletions doc/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,14 @@ General configuration

.. versionadded:: 3.0

.. confval:: option_parse_variable_part

Default is ``False``.
Similarly to ``samp`` role, support parsing of "variable part" of a literal text.
For example, in ``..option: -foption={TYPE}``,
the part ``TYPE`` would be emphasized.

.. versionadded:: 4.4

.. _intl-options:

Expand Down
3 changes: 3 additions & 0 deletions doc/usage/restructuredtext/domains.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,9 @@ There is a set of directives allowing documenting command-line programs:
referenceable by :rst:role:`option` (in the example case, you'd use something
like ``:option:`dest_dir```, ``:option:`-m```, or ``:option:`--module```).

Use :confval:`option_parse_variable_part` for parsing of
"variable part" of a literal text (similarly to ``samp`` directive).

``cmdoption`` directive is a deprecated alias for the ``option`` directive.

.. rst:directive:: .. envvar:: name
Expand Down
1 change: 1 addition & 0 deletions sphinx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Config:
'smartquotes_excludes': ({'languages': ['ja'],
'builders': ['man', 'text']},
'env', []),
'option_parse_variable_part': (False, 'env', []),
}

def __init__(self, config: Dict[str, Any] = {}, overrides: Dict[str, Any] = {}) -> None:
Expand Down
9 changes: 7 additions & 2 deletions sphinx/domains/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from sphinx.directives import ObjectDescription
from sphinx.domains import Domain, ObjType
from sphinx.locale import _, __
from sphinx.roles import XRefRole
from sphinx.roles import EmphasizedLiteral, XRefRole
from sphinx.util import docname_join, logging, ws_re
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import clean_astext, make_id, make_refnode
Expand All @@ -43,6 +43,8 @@
# RE for grammar tokens
token_re = re.compile(r'`((~?\w*:)?\w+)`', re.U)

samp_role = EmphasizedLiteral()


class GenericObject(ObjectDescription[str]):
"""
Expand Down Expand Up @@ -201,7 +203,10 @@ def handle_signature(self, sig: str, signode: desc_signature) -> str:
if count:
signode += addnodes.desc_addname(', ', ', ')
signode += addnodes.desc_name(optname, optname)
signode += addnodes.desc_addname(args, args)
if self.env.config.option_parse_variable_part:
signode += samp_role.parse(args)
else:
signode += addnodes.desc_addname(args, args)
if not count:
firstname = optname
signode['allnames'] = [optname]
Expand Down
9 changes: 9 additions & 0 deletions tests/roots/test-root/objects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ Link to :option:`perl +p`, :option:`--ObjC++`, :option:`--plugin.option`, :optio

Link to :option:`hg commit` and :option:`git commit -p`.

.. option:: --abi={TYPE}

.. option:: --test={WHERE}-{COUNT}

.. option:: --wrap=\{\{value\}\}

.. option:: -allowable_client {client_name}

Foo bar.

User markup
===========
Expand Down
21 changes: 21 additions & 0 deletions tests/test_build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,3 +1673,24 @@ def test_html_signaturereturn_icon(app):
content = (app.outdir / 'index.html').read_text()

assert ('<span class="sig-return-icon">&#x2192;</span>' in content)


@pytest.mark.sphinx('html', testroot='root',
confoverrides={'option_parse_variable_part': True})
def test_option_with_variable_part(app, status, warning):
app.build()
content = (app.outdir / 'objects.html').read_text()
assert '<em><span class="pre">TYPE</span></em>' in content
assert '{TYPE}' not in content
assert ('<em><span class="pre">WHERE</span></em><span class="pre">-</span>'
'<em><span class="pre">COUNT</span></em>' in content)
assert '<span class="pre">={{value}}</span>' in content


@pytest.mark.sphinx('html', testroot='root')
def test_option_with_variable_part_default(app, status, warning):
app.build()
content = (app.outdir / 'objects.html').read_text()
assert '<span class="pre">={TYPE}</span>' in content
assert '<span class="pre">={WHERE}-{COUNT}</span></span>' in content
assert '<span class="pre">{client_name}</span>' in content

0 comments on commit 7b0b566

Please sign in to comment.