Skip to content

Commit

Permalink
Change rich markdown theme/colors (#2486)
Browse files Browse the repository at this point in the history
* Change rich markdown theme/colors

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ssbarnea and pre-commit-ci[bot] committed Sep 22, 2022
1 parent 2482270 commit 55068d7
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/_static/theme_overrides.css
Expand Up @@ -16,3 +16,9 @@
.icon-home {
display: none !important;
}

/* Avoid using the red text for tt blocks */
.rst-content code.literal,
.rst-content tt.literal {
color: #888888 !important;
}
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -165,7 +165,7 @@
# show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"
pygments_style = "ansible"

highlight_language = "YAML+Jinja"

Expand Down
76 changes: 76 additions & 0 deletions src/ansiblelint/color.py
Expand Up @@ -4,10 +4,53 @@
from typing import Any

import rich
import rich.markdown
from rich.console import Console
from rich.default_styles import DEFAULT_STYLES
from rich.style import Style
from rich.syntax import Syntax
from rich.theme import Theme

# WARNING: When making style changes, be sure you test the output of
# `ansible-lint -L` on multiple terminals with dark/light themes, including:
# - iTerm2 (macOS) - bold might not be rendered differently
# - vscode integrated terminal - bold might not be rendered differently, links will not work
#
# When it comes to colors being used, try to match:
# - Ansible official documentation theme, https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
# - VSCode Ansible extension for syntax highlighting
# - GitHub markdown theme
#
# Current values: (docs)
# codeblock border: #404040
# codeblock background: #edf0f2
# codeblock comment: #6a737d (also italic)
# teletype-text: #e74c3c (red)
# teletype-text-border: 1px solid #e1e4e5 (background white)
# text: #404040
# codeblock other-text: #555555 (black-ish)
# codeblock property: #22863a (green)
# codeblock integer: 032f62 (blue)
# codeblock command: #0086b3 (blue) - [shell]
# == python ==
# class: #445588 (dark blue and bold)
# docstring: #dd1144 (red)
# self: #999999 (light-gray)
# method/function: #990000 (dark-red)
# number: #009999 cyan
# keywords (def,None,False,len,from,import): #007020 (green) bold
# super|dict|print: #0086b3 light-blue
# __name__: #bb60d5 (magenta)
# string: #dd1144 (light-red)
DEFAULT_STYLES.update(
{
# "code": Style(color="bright_black", bgcolor="red"),
"markdown.code": Style(color="bright_black"),
"markdown.code_block": Style(dim=True, color="cyan"),
}
)


_theme = Theme(
{
"info": "cyan",
Expand Down Expand Up @@ -44,3 +87,36 @@ def reconfigure(new_options: dict[str, Any]) -> None:
def render_yaml(text: str) -> Syntax:
"""Colorize YAMl for nice display."""
return Syntax(text, "yaml", theme="ansi_dark")


# pylint: disable=redefined-outer-name,unused-argument
def _rich_heading_custom_rich_console(
self: rich.markdown.Heading,
console: rich.console.Console,
options: rich.console.ConsoleOptions,
) -> rich.console.RenderResult:
"""Override for rich console heading."""
yield f"[bold]{self.level * '#'} {self.text}[/]"


# pylint: disable=redefined-outer-name,unused-argument
def _rich_codeblock_custom_rich_console(
self: rich.markdown.CodeBlock,
console: Console,
options: rich.console.ConsoleOptions,
) -> rich.console.RenderResult:
code = str(self.text).rstrip()
syntax = Syntax(
code,
self.lexer_name,
theme=self.theme,
word_wrap=True,
background_color="default",
)
yield syntax


# Monkey-patch rich to alter its rendering of headings
# https://github.com/python/mypy/issues/2427
rich.markdown.Heading.__rich_console__ = _rich_heading_custom_rich_console # type: ignore
rich.markdown.CodeBlock.__rich_console__ = _rich_codeblock_custom_rich_console # type: ignore
2 changes: 2 additions & 0 deletions src/ansiblelint/rules/jinja.py
Expand Up @@ -279,6 +279,8 @@ def uncook(value: str, implicit: bool = False) -> str:

except jinja2.exceptions.TemplateSyntaxError as exc:
return "", str(exc.message), "invalid"
# https://github.com/PyCQA/pylint/issues/7433 - py311 only
# pylint: disable=c-extension-no-member
except (NotImplementedError, black.parsing.InvalidInput) as exc:
# black is not able to recognize all valid jinja2 templates, so we
# just ignore InvalidInput errors.
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/no_jinja_when.md
Expand Up @@ -3,8 +3,9 @@
This rule checks conditional statements for Jinja expressions in curly brackets `{{ }}`.
Ansible processes conditionals statements that use the `when`, `failed_when`, and `changed_when` clauses as Jinja expressions.

An Ansible rule is "always use `{{ }}` except with `when*:`".
Using `{{ }}` in conditionals creates a nested expression, which is an Ansible anti-pattern and does not produce expected results.
An Ansible rule is to always use `{{ }}` except with `when` keys.
Using `{{ }}` in conditionals creates a nested expression, which is an Ansible
anti-pattern and does not produce expected results.

## Problematic Code

Expand Down
6 changes: 3 additions & 3 deletions src/ansiblelint/rules/risky_file_permissions.py
Expand Up @@ -72,9 +72,9 @@ class MissingFilePermissionsRule(AnsibleLintRule):
description = (
"Missing or unsupported mode parameter can cause unexpected file "
"permissions based "
"on version of Ansible being used. Be explicit, like ``mode: 0644`` to "
"avoid hitting this rule. Special ``preserve`` value is accepted "
f"only by {', '.join(_modules_with_preserve)} modules."
"on version of Ansible being used. Be explicit, like `mode: 0644` to "
"avoid hitting this rule. Special `preserve` value is accepted "
f"only by {', '.join([f'`{x}`' for x in _modules_with_preserve])} modules."
)
link = "https://github.com/ansible/ansible/issues/71200"
severity = "VERY_HIGH"
Expand Down

0 comments on commit 55068d7

Please sign in to comment.