fix: unify value format of tabulated vars (#792)

This commit is contained in:
Robert Kaussow 2024-10-08 10:08:06 +02:00 committed by GitHub
parent 4bb11fd6cd
commit ee2cb214a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 15 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Prepare output and write compiled jinja2 templates.""" """Prepare output and write compiled jinja2 templates."""
import json
import os import os
import re import re
from functools import reduce from functools import reduce
@ -128,7 +129,7 @@ class Generator:
yaml.dump(a, stream, **kw) yaml.dump(a, stream, **kw)
return stream.getvalue().rstrip() return stream.getvalue().rstrip()
def _to_code(self, a, to_multiline=False, skip_list_len=0, lang="plain"): def _to_code(self, a, to_multiline=False, tab_var=False, preserve_ms=False, lang="plain"):
"""Wrap a string in backticks.""" """Wrap a string in backticks."""
if a is None or a == "": if a is None or a == "":
return "" return ""
@ -136,16 +137,23 @@ class Generator:
if (isinstance(a, list) and len(a) < 1) or (isinstance(a, dict) and not a): if (isinstance(a, list) and len(a) < 1) or (isinstance(a, dict) and not a):
return "" return ""
if isinstance(a, list) and len(a) == 1: if isinstance(a, list) and len(a) > 1 and preserve_ms:
return f"`{a[0]}`"
if isinstance(a, list) and skip_list_len > 0 and len(a) > skip_list_len:
return a return a
if isinstance(a, list) and len(a) == 1:
return f"`{self._tab_var(a[0], tab_var)}`"
if (isinstance(a, list)) and to_multiline: if (isinstance(a, list)) and to_multiline:
return "```" + lang + "\n" + "\n".join(a) + "\n```" return "```" + lang + "\n" + "\n".join(a) + "\n```"
return f"`{a}`" return f"`{self._tab_var(a, tab_var)}`"
def _tab_var(self, a, tab_var):
"""Wrap a string in backticks."""
if not tab_var:
return a
return json.dumps(a)
def _deep_get(self, _, dictionary, keys): def _deep_get(self, _, dictionary, keys):
default = None default = None

View File

@ -18,12 +18,12 @@
| |
{% for key, item in var | dictsort %} {% for key, item in var | dictsort %}
|{{ key | to_code -}} |{{ key | to_code -}}
|{{ (item.value | default({}))[key] | default | to_code -}} |{{ (item.value | default({}))[key] | default | to_code(tab_var=true) -}}
{% if "description" in found_columns %} {% if "description" in found_columns %}
|{{ item.description | default([]) | safe_join("<br />") | replace("\n", "<br />") | replace("|", "\|") -}} |{{ item.description | default([]) | safe_join("<br />") | replace("\n", "<br />") | replace("|", "\|") -}}
{% endif %} {% endif %}
{% if "type" in found_columns %} {% if "type" in found_columns %}
|{{ item.type | default([]) | to_code(skip_list_len=1) | safe_join("<br />") -}} |{{ item.type | default([]) | to_code(preserve_ms=true) | safe_join("<br />") -}}
{% endif %} {% endif %}
{% if "deprecated" in found_columns %} {% if "deprecated" in found_columns %}
| |
@ -42,7 +42,7 @@ False
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if "example" in found_columns %} {% if "example" in found_columns %}
|{{ item.example | default([]) | to_code(skip_list_len=1) | safe_join("<br />") | replace("\n", "<br />") | replace("|", "\|") -}} |{{ item.example | default([]) | to_code(tab_var=true,preserve_ms=true) | safe_join("<br />") | replace("\n", "<br />") | replace("|", "\|") -}}
{% endif %} {% endif %}
| |
{% endfor %} {% endfor %}

View File

@ -9,7 +9,7 @@ Role to demonstrate ansible-doctor.
- [Requirements](#requirements) - [Requirements](#requirements)
- [Default Variables](#default-variables) - [Default Variables](#default-variables)
- [demo_bool](#demo_bool) - [other_role_bool](#other_role_bool)
- [other_role_deprecated](#other_role_deprecated) - [other_role_deprecated](#other_role_deprecated)
- [other_role_deprecated_info](#other_role_deprecated_info) - [other_role_deprecated_info](#other_role_deprecated_info)
- [other_role_dict](#other_role_dict) - [other_role_dict](#other_role_dict)
@ -36,18 +36,18 @@ Role to demonstrate ansible-doctor.
## Default Variables ## Default Variables
### demo_bool ### other_role_bool
#### Default value #### Default value
```YAML ```YAML
demo_bool: true other_role_bool: true
``` ```
#### Example usage #### Example usage
```YAML ```YAML
demo_bool: false other_role_bool: false
``` ```
### other_role_deprecated ### other_role_deprecated

View File

@ -6,8 +6,8 @@ other_role_unset:
other_role_empty: "" other_role_empty: ""
other_role_single: "b" other_role_single: "b"
# @var demo_bool:example: $ false # @var other_role_bool:example: $ false
demo_bool: true other_role_bool: True
# @var other_role_empty_dict:description: > # @var other_role_empty_dict:description: >
# ... or valid json can be used. In this case, the json will be automatically prefixed with the annotation key # ... or valid json can be used. In this case, the json will be automatically prefixed with the annotation key