feat: add option for tabulating variables (#693)

This commit is contained in:
Chip Selden 2024-06-01 13:05:16 -07:00 committed by GitHub
parent fe4e4e5f7a
commit 4051d2915d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 94 additions and 1 deletions

View File

@ -123,6 +123,12 @@ class Config:
"file": True,
"type": environs.Env().bool,
},
"tabulate_variables": {
"default": False,
"env": "TABULATE_VARIABLES",
"file": True,
"type": environs.Env().bool,
},
}
ANNOTATIONS = {

View File

@ -123,7 +123,10 @@ class Generator:
jenv.filters["safe_join"] = self._safe_join
# keep the old name of the function to not break custom templates.
jenv.filters["save_join"] = self._safe_join
data = jenv.from_string(data).render(role_data, role=role_data)
tabulate_vars = self.config.config.get("tabulate_variables")
data = jenv.from_string(data).render(
role_data, role=role_data, tabulate_vars=tabulate_vars
)
if not self.config.config["dry_run"]:
with open(doc_file, "wb") as outfile:
outfile.write(header_content.encode("utf-8"))

View File

@ -2,10 +2,12 @@
{% set var = role.var | default({}) %}
{% if var %}
- [Default Variables](#default-variables)
{% if not tabulate_vars %}
{% for key, item in var | dictsort %}
- [{{ key }}](#{{ key }})
{% endfor %}
{% endif %}
{% endif %}
{% if tag %}
- [Discovered Tags](#discovered-tags)
{% endif %}

View File

@ -0,0 +1,36 @@
{% set var = role.var | default({}) %}
{% if var %}
## Default Variables
{% set columns = ["variable", "default", "description", "type", "deprecated", "example"] %}
{% set found_columns = ["variable", "default"] + var.values() | map("list") | sum(start=["key"]) | unique | list %}
{% for c in columns %}
{% if c in found_columns %}
| {{ c | capitalize }} {# trimnewline #}
{% endif %}
{% endfor %}
|
{% for c in columns %}
{% if c in found_columns %}
| {{ "-" * (c | length) }} {# trimnewline #}
{% endif %}
{% endfor %}
|
{% for key, item in var | dictsort %}
| {{ key }} {# trimnewline #}
| {{ (item.value | default({}))[key] | default }} {# trimnewline #}
{% if "description" in found_columns %}
| {{ item.description | default([]) | safe_join("<br>") | replace("\n", "<br>") | replace("|", "\|") }} {# trimnewline #}
{% endif %}
{% if "type" in found_columns %}
| {{ item.type | default([]) | join("<br>") }} {# trimnewline #}
{% endif %}
{% if "deprecated" in found_columns %}
| {{ item.deprecated | default }} {# trimnewline #}
{% endif %}
{% if "example" in found_columns %}
| {{ item.example | default([]) | safe_join("<br>") | replace("\n", "<br>") | replace("|", "\|") }} {# trimnewline #}
{% endif %}
|
{% endfor %}
{% endif %}

View File

@ -23,7 +23,11 @@ summary: {{ meta.summary.value | safe_join(" ") }}
{% include '_requirements.j2' %}
{# Vars #}
{% if tabulate_vars %}
{% include '_vars_tabulated.j2' %}
{% else %}
{% include '_vars.j2' %}
{% endif %}
{# Tag #}
{% include '_tag.j2' %}

View File

@ -15,7 +15,11 @@
{% include '_requirements.j2' %}
{# Vars #}
{% if tabulate_vars %}
{% include '_vars_tabulated.j2' %}
{% else %}
{% include '_vars.j2' %}
{% endif %}
{# Tag #}
{% include '_tag.j2' %}

View File

@ -4,10 +4,12 @@
{% set var = role.var | default({}) %}
{% if var %}
- [Default Variables](#default-variables)
{% if not tabulate_vars %}
{% for key, item in var | dictsort %}
- [{{ key }}](#{{ key }})
{% endfor %}
{% endif %}
{% endif %}
{% if tag %}
- [Discovered Tags](#discovered-tags)
{% endif %}

View File

@ -0,0 +1,36 @@
{% set var = role.var | default({}) %}
{% if var %}
## Default Variables
{% set columns = ["variable", "default", "description", "type", "deprecated", "example"] %}
{% set found_columns = ["variable", "default"] + var.values() | map("list") | sum(start=["key"]) | unique | list %}
{% for c in columns %}
{% if c in found_columns %}
| {{ c | capitalize }} {# trimnewline #}
{% endif %}
{% endfor %}
|
{% for c in columns %}
{% if c in found_columns %}
| {{ "-" * (c | length) }} {# trimnewline #}
{% endif %}
{% endfor %}
|
{% for key, item in var | dictsort %}
| {{ key }} {# trimnewline #}
| {{ (item.value | default({}))[key] | default }} {# trimnewline #}
{% if "description" in found_columns %}
| {{ item.description | default([]) | safe_join("<br>") | replace("\n", "<br>") | replace("|", "\|") }} {# trimnewline #}
{% endif %}
{% if "type" in found_columns %}
| {{ item.type | default([]) | join("<br>") }} {# trimnewline #}
{% endif %}
{% if "deprecated" in found_columns %}
| {{ item.deprecated | default }} {# trimnewline #}
{% endif %}
{% if "example" in found_columns %}
| {{ item.example | default([]) | safe_join("<br>") | replace("\n", "<br>") | replace("|", "\|") }} {# trimnewline #}
{% endif %}
|
{% endfor %}
{% endif %}