Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unify value format of tabulated vars #792

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions ansibledoctor/doc_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Prepare output and write compiled jinja2 templates."""

import json
import os
import re
from functools import reduce
Expand Down Expand Up @@ -128,24 +129,31 @@ def _to_nice_yaml(self, a, indent=4, **kw):
yaml.dump(a, stream, **kw)
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."""
if a is None or a == "":
return ""

if (isinstance(a, list) and len(a) < 1) or (isinstance(a, dict) and not a):
return ""

if isinstance(a, list) and len(a) == 1:
return f"`{a[0]}`"

if isinstance(a, list) and skip_list_len > 0 and len(a) > skip_list_len:
if isinstance(a, list) and len(a) > 1 and preserve_ms:
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:
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):
default = None
Expand Down
6 changes: 3 additions & 3 deletions ansibledoctor/templates/readme/_vars_tabulated.j2
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
|
{% for key, item in var | dictsort %}
|{{ 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 %}
|{{ item.description | default([]) | safe_join("<br />") | replace("\n", "<br />") | replace("|", "\|") -}}
{% endif %}
{% 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 %}
{% if "deprecated" in found_columns %}
|
Expand All @@ -42,7 +42,7 @@ False
{% endif %}
{% endif %}
{% 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 %}
|
{% endfor %}
Expand Down
8 changes: 4 additions & 4 deletions example/other-role/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Role to demonstrate ansible-doctor.

- [Requirements](#requirements)
- [Default Variables](#default-variables)
- [demo_bool](#demo_bool)
- [other_role_bool](#other_role_bool)
- [other_role_deprecated](#other_role_deprecated)
- [other_role_deprecated_info](#other_role_deprecated_info)
- [other_role_dict](#other_role_dict)
Expand All @@ -36,18 +36,18 @@ Role to demonstrate ansible-doctor.

## Default Variables

### demo_bool
### other_role_bool

#### Default value

```YAML
demo_bool: true
other_role_bool: true
```

#### Example usage

```YAML
demo_bool: false
other_role_bool: false
```

### other_role_deprecated
Expand Down
4 changes: 2 additions & 2 deletions example/other-role/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ other_role_unset:
other_role_empty: ""
other_role_single: "b"

# @var demo_bool:example: $ false
demo_bool: true
# @var other_role_bool:example: $ false
other_role_bool: True

# @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
Expand Down