click_extra.sphinx packageΒΆ
Helpers and utilities for Sphinx.
- click_extra.sphinx.setup(app)[source]ΒΆ
Register extensions to Sphinx.
New directives, augmented with ANSI coloring.
Support for GitHub alerts syntax in included and regular source files.
Caution
This function forces the Sphinx app to use
sphinx.highlighting.PygmentsBridgeinstead of the default HTML formatter to add support for ANSI colors in code blocks.- Return type:
ExtensionMetadata
SubmodulesΒΆ
click_extra.sphinx.alerts moduleΒΆ
Utilities to convert GitHub alerts into MyST admonitions for Sphinx.
See also
GitHub documentation for `alerts syntax
<https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts>`_. - Announcement for alert support starting 2023-12-14.
- click_extra.sphinx.alerts.GITHUB_ALERT_PATTERN = re.compile('^\\s*\\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\\]\\s*$')ΒΆ
Regex pattern to match GitHub alert type declaration (without leading >).
- click_extra.sphinx.alerts.QUOTE_PREFIX_PATTERN = re.compile('^(\\s*)((?:>\\s*)+)(.*)$')ΒΆ
Regex pattern to extract indent, quote markers, and content.
- click_extra.sphinx.alerts.CODE_FENCE_PATTERN = re.compile('^(\\s*)(`{3,}|~{3,})(.*)$')ΒΆ
Regex pattern to match code fence opening/closing lines.
- click_extra.sphinx.alerts.INDENTED_CODE_BLOCK_PATTERN = re.compile('^( {4}|\\t)')ΒΆ
Regex pattern to match indented code block lines (4 spaces or 1 tab).
- class click_extra.sphinx.alerts.Alert(alert_type, indent, depth, has_nested=False, opening_line_index=0)[source]ΒΆ
Bases:
objectRepresents a GitHub alert being processed.
- class click_extra.sphinx.alerts.FenceState(char, length, indent, is_code_block)[source]ΒΆ
Bases:
objectTracks code fence state.
- class click_extra.sphinx.alerts.ParserState(result=<factory>, alert_stack=<factory>, fence_stack=<factory>, prev_line_blank=True, modified=False, just_opened_fence_directive=False)[source]ΒΆ
Bases:
objectMutable state for the alert parser.
-
fence_stack:
list[FenceState]ΒΆ
-
fence_stack:
- click_extra.sphinx.alerts.check_colon_fence(app: Sphinx) None[source]ΒΆ
Check that colon_fence support is enabled for MyST.
- Raises:
ConfigError β If
colon_fenceis not inmyst_enable_extensions.- Return type:
- click_extra.sphinx.alerts.count_quote_depth(line)[source]ΒΆ
Parse a line to extract indent, quote depth, and content.
- Returns:
Tuple of (indent, depth, content) where depth is the number of > markers.
- click_extra.sphinx.alerts.process_fence(state, indent, chars, after)[source]ΒΆ
Process a fence line, updating fence stack.
- Return type:
- click_extra.sphinx.alerts.close_alerts_to_depth(state, target_depth)[source]ΒΆ
Close all alerts deeper than target_depth.
- Return type:
- click_extra.sphinx.alerts.mark_parent_nested(state)[source]ΒΆ
Mark the parent alert as having a nested alert and update its opening.
- Return type:
- click_extra.sphinx.alerts.open_alert(state, alert_type, indent, depth)[source]ΒΆ
Open a new alert at the given depth.
- Return type:
- click_extra.sphinx.alerts.process_quoted_line(state, line)[source]ΒΆ
Process a line that starts with quote markers.
Returns True if the line was handled as part of an alert.
- Return type:
click_extra.sphinx.click moduleΒΆ
Sphinx rendering of CLI based on Click Extra.
See also
These directives are based on Palletsβ Sphinx Themes, released under a BSD-3-Clause license.
Compared to the latter, it:
Add support for MyST syntax.
Adds rendering of ANSI codes in CLI results.
Has better error handling and reporting which helps you pinpoint the failing code in your documentation.
Removes the
printlnfunction which was used to explicitly print a blank line. This is no longer needed as it is now handled natively.
- click_extra.sphinx.click.RST_INDENT = ' 'ΒΆ
The indentation used for rST code blocks lines.
- class click_extra.sphinx.click.TerminatedEchoingStdin(input, output)[source]ΒΆ
Bases:
EchoingStdinLike
click.testing.EchoingStdinbut adds a visible^Din place of the EOT character ().ClickRunner.invoke()addswhenterminate_input=True.
- click_extra.sphinx.click.patch_subprocess()[source]ΒΆ
Patch subprocess to work better with
ClickRunner.invoke().subprocess.calloutput is redirected toclick.echoso it shows up in the example output.
- class click_extra.sphinx.click.ClickRunner[source]ΒΆ
Bases:
ExtraCliRunnerA sub-class of
click.testing.CliRunnerwith additional features.This class inherits from
click_extra.testing.ExtraCliRunnerto have full control of contextual color settings by the way of thecolorparameter. It also produce unfiltered ANSI codes so that theDirectivesub-classes below can render colors in the HTML output.- force_color: bool = TrueΒΆ
Force color rendering in
invokecalls.
- isolation(*args, **kwargs)[source]ΒΆ
A context manager that sets up the isolation for invoking of a command line tool. This sets up <stdin> with the given input data and os.environ with the overrides from the given dictionary. This also rebinds some internals in Click to be mocked (like the prompt functionality).
This is automatically done in the
invoke()method.- Parameters:
input β the input stream to put into sys.stdin.
env β the environment overrides as dictionary.
color β whether the output should contain color codes. The application can still override this explicitly.
Added in version 8.2: An additional output stream is returned, which is a mix of <stdout> and <stderr> streams.
Changed in version 8.2: Always returns the <stderr> stream.
Changed in version 8.0: <stderr> is opened with
errors="backslashreplace"instead of the default"strict".Changed in version 4.0: Added the
colorparameter.
- invoke(cli, args=None, prog_name=None, input=None, terminate_input=False, env=None, _output_lines=None, **extra)[source]ΒΆ
Like
CliRunner.invoke()but displays what the user would enter in the terminal for env vars, command arguments, and prompts.- Parameters:
terminate_input β Whether to display
^Dafter a list of input._output_lines β A list used internally to collect lines to be displayed.
- Return type:
- execute_source(directive)[source]ΒΆ
Execute the given code, adding it to the runnerβs namespace.
- Return type:
- run_cli(directive)[source]ΒΆ
Execute the given
source_code.Returns a simulation of terminal execution, including a mix of input, output, prompts and tracebacks.
The execution context is augmented, so you can refer directly to these functions in the provided
source_code:invoke(): which is the same asExtraCliRunner.invoke()isolated_filesystem(): A context manager that changes to a temporary directory while executing the block.
If any local variable in the provided
source_codeconflicts with these functions, aRuntimeErroris raised to help you pinpoint the issue.
- class click_extra.sphinx.click.ClickDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]ΒΆ
Bases:
SphinxDirective- has_content = TrueΒΆ
May the directive have content?
- required_arguments = 0ΒΆ
Number of required directive arguments.
- optional_arguments = 1ΒΆ
The optional argument overrides the default Pygments language to use.
- final_argument_whitespace = FalseΒΆ
May the final argument contain whitespace?
-
option_spec:
ClassVar[dict[str,Callable[[str],Any]]] = {'caption': <function unchanged_required>, 'class': <function class_option>, 'dedent': <function optional_int>, 'emphasize-lines': <function unchanged_required>, 'force': <function flag>, 'hide-results': <function flag>, 'hide-source': <function flag>, 'language': <function unchanged_required>, 'lineno-start': <class 'int'>, 'linenos': <function flag>, 'name': <function unchanged>, 'show-results': <function flag>, 'show-source': <function flag>}ΒΆ Options supported by this directive.
Support the same options as
sphinx.directives.code.CodeBlock, and some specific to Click directives.
-
default_language:
strΒΆ Default highlighting language to use to render the code block.
All Pygmentsβ languages short names are recognized.
-
show_source_by_default:
bool= TrueΒΆ Whether to render the source code of the example in the code block.
-
show_results_by_default:
bool= TrueΒΆ Whether to render the results of the example in the code block.
-
runner_method:
strΒΆ The name of the method to call on the
ClickRunnerinstance.
- property runner: ClickRunnerΒΆ
Get or create the
ClickRunnerinstance associated with a document.Creates one runner per document.
- property language: str[source]ΒΆ
Short name of the Pygments lexer used to highlight the code block.
Returns, in order of precedence, the language specified in the :language: directive options, the first argument of the directive (if any), or the default set in the directive class.
- property code_block_options: list[str][source]ΒΆ
Render the options supported by Sphinxβ native code-block directive.
- property show_source: bool[source]ΒΆ
Whether to show the source code of the example in the code block.
The last occurrence of either
show-sourceorhide-sourceoptions wins. If neither is set, the default is taken fromshow_source_by_default.
- property show_results: bool[source]ΒΆ
Whether to show the results of running the example in the code block.
The last occurrence of either
show-resultsorhide-resultsoptions wins. If neither is set, the default is taken fromshow_results_by_default.
- class click_extra.sphinx.click.SourceDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]ΒΆ
Bases:
ClickDirectiveDirective to declare a Click CLI source code.
This directive is used to declare a Click CLI example in the documentation. It renders the source code of the example in a Python code block.
-
default_language:
str= 'python'ΒΆ Default highlighting language to use to render the code block.
All Pygmentsβ languages short names are recognized.
-
show_source_by_default:
bool= TrueΒΆ Whether to render the source code of the example in the code block.
-
show_results_by_default:
bool= FalseΒΆ Whether to render the results of the example in the code block.
-
runner_method:
str= 'execute_source'ΒΆ The name of the method to call on the
ClickRunnerinstance.
-
default_language:
- class click_extra.sphinx.click.RunDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]ΒΆ
Bases:
ClickDirectiveDirective to run a Click CLI example.
This directive is used to run a Click CLI example in the documentation. It renders the results of running the example in a shell session code block supporting ANSI colors.
-
default_language:
str= 'ansi-shell-session'ΒΆ Default highlighting language to use to render the code block.
All Pygmentsβ languages short names are recognized.
-
show_source_by_default:
bool= FalseΒΆ Whether to render the source code of the example in the code block.
-
show_results_by_default:
bool= TrueΒΆ Whether to render the results of the example in the code block.
-
runner_method:
str= 'run_cli'ΒΆ The name of the method to call on the
ClickRunnerinstance.
-
default_language:
- class click_extra.sphinx.click.DeprecatedExampleDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]ΒΆ
Bases:
SourceDirectiveDeprecated alias for SourceDirective.
Deprecated since version 7.3.0: Use
click:sourceinstead ofclick:example.
- class click_extra.sphinx.click.ClickDomain(env)[source]ΒΆ
Bases:
DomainSetup new directives under the same
clicknamespace:click:sourcewhich renders a Click CLI source codeclick:example, an alias toclick:source(deprecated)click:runwhich renders the results of running a Click CLI
- name = 'click'ΒΆ
domain name: should be short, but unique
- label = 'Click'ΒΆ
domain label: longer, more descriptive (used in messages)
- directives: dict[str, type[Directive]] = {'example': <class 'click_extra.sphinx.click.DeprecatedExampleDirective'>, 'run': <class 'click_extra.sphinx.click.RunDirective'>, 'source': <class 'click_extra.sphinx.click.SourceDirective'>}ΒΆ
directive name -> directive class
- click_extra.sphinx.click.cleanup_runner(app, doctree)[source]ΒΆ
Close and remove the
ClickRunnerinstance once the document has been read.- Return type: