Skip to content

Commit

Permalink
Merge pull request #62 from rouault/language_option
Browse files Browse the repository at this point in the history
Add a 'language' option to customize output language
  • Loading branch information
jamadden authored Dec 6, 2024
2 parents 93b71e4 + b3730cc commit 7f586fa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace package.
- Now requires docutils >= 0.18.1. This effectively requires Sphinx
versions newer than 5.0.
- Add the ``language`` option. Added in :pr:`62` by Even Rouault.


0.17 (2021-03-31)
Expand Down
8 changes: 8 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ Reference
.. versionchanged:: 0.16
Add the ``caption`` and ``name`` options.

A ``language`` option can be given to specify the language of the output.
This can be set for example to ``json`` for a program whose output is JSON.
In that case, this must only be used with the :dir:`program-output` directive,
and not :dir:`command-output`.

.. versionchanged:: 0.18
Add the ``language`` option.

.. directive:: command-output

Same as :dir:`program-output`, but with enabled ``prompt`` option.
Expand Down
6 changes: 4 additions & 2 deletions src/sphinxcontrib/programoutput/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class ProgramOutputDirective(rst.Directive):
option_spec = dict(shell=flag, prompt=flag, nostderr=flag,
ellipsis=_slice, extraargs=unchanged,
returncode=nonnegative_int, cwd=unchanged,
caption=unchanged, name=unchanged)
caption=unchanged, name=unchanged,
language=unchanged)

def run(self):
env = self.state.document.settings.env
Expand All @@ -112,6 +113,7 @@ def run(self):
node['working_directory'] = cwd
node['use_shell'] = 'shell' in self.options
node['returncode'] = self.options.get('returncode', 0)
node['language'] = self.options.get('language', 'text')
if 'ellipsis' in self.options:
node['strip_lines'] = self.options['ellipsis']
if 'caption' in self.options:
Expand Down Expand Up @@ -312,7 +314,7 @@ def run_programs(app, doctree):
)

new_node = node_class(output, output)
new_node['language'] = 'text'
new_node['language'] = node['language']
node.replace_self(new_node)


Expand Down
11 changes: 11 additions & 0 deletions src/sphinxcontrib/programoutput/tests/test_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,17 @@ def test_name_with_caption(self):
self.assert_cache(self.app, 'echo spam', 'spam')


@with_content("""\
.. program-output:: python -c 'import json; d = {"foo": "bar"}; print(json.dumps(d))'
:language: json""",
ignore_warnings=False)
def test_language_json(self):
literal = self.doctree.next_node(literal_block)
self.assertTrue(literal)
self.assertEqual(literal.astext(), '{"foo": "bar"}')
self.assertEqual(literal["language"], "json")


def test_suite():
return unittest.defaultTestLoader.loadTestsFromName(__name__)

Expand Down

0 comments on commit 7f586fa

Please sign in to comment.