From 588f41a3ea77067b1ed940ee0b96f365accd431b Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 14 Jun 2023 13:31:01 +0200 Subject: [PATCH] feat: add support for indentation with spaces (#508) --- ansibledoctor/config.py | 6 ++++++ ansibledoctor/doc_generator.py | 8 +++++--- docs/content/usage/configuration.md | 6 ++++++ example/demo-role/README.md | 6 ++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ansibledoctor/config.py b/ansibledoctor/config.py index 802db32..0876219 100644 --- a/ansibledoctor/config.py +++ b/ansibledoctor/config.py @@ -87,6 +87,12 @@ class Config(): "file": True, "type": environs.Env().str }, + "template_autotrim": { + "default": True, + "env": "TEMPLATE_AUTOTRIM", + "file": True, + "type": environs.Env().bool + }, "force_overwrite": { "default": False, "env": "FORCE_OVERWRITE", diff --git a/ansibledoctor/doc_generator.py b/ansibledoctor/doc_generator.py index 69948f0..5921263 100644 --- a/ansibledoctor/doc_generator.py +++ b/ansibledoctor/doc_generator.py @@ -166,10 +166,12 @@ class Generator: value = [value] normalized = jinja2.filters.do_join(eval_ctx, value, d, attribute=None) - for s in [r" +(\n|\t| )", r"(\n|\t) +"]: - normalized = re.sub(s, "\\1", normalized) - return normalized + if self.config.config["template_autotrim"]: + for s in [r" +(\n|\t| )", r"(\n|\t) +"]: + normalized = re.sub(s, "\\1", normalized) + + return jinja2.filters.do_mark_safe(normalized) def render(self): self.logger.info(f"Using output dir: {self.config.config.get('output_dir')}") diff --git a/docs/content/usage/configuration.md b/docs/content/usage/configuration.md index ffc7c62..78deb6e 100644 --- a/docs/content/usage/configuration.md +++ b/docs/content/usage/configuration.md @@ -39,6 +39,11 @@ output_dir: # Default is built-in templates directory. template_dir: template: readme +# By default, double spaces, spaces before and after line breaks or tab characters, etc. +# are automatically removed before the template is rendered. As a result, indenting +# with spaces does not work. If you want to use spaces to indent text, you must disable +# this option. +template_autotrim: True # Don't ask to overwrite if output file exists. force_overwrite: False @@ -97,6 +102,7 @@ ANSIBLE_DOCTOR_LOG_JSON=false ANSIBLE_DOCTOR_OUTPUT_DIR= ANSIBLE_DOCTOR_TEMPLATE_DIR= ANSIBLE_DOCTOR_TEMPLATE=readme +ANSIBLE_DOCTOR_TEMPLATE_AUTOTRIM=true ANSIBLE_DOCTOR_FORCE_OVERWRITE=false ANSIBLE_DOCTOR_CUSTOM_HEADER= ANSIBLE_DOCTOR_EXCLUDE_FILES= diff --git a/example/demo-role/README.md b/example/demo-role/README.md index fbe3e98..7386d75 100644 --- a/example/demo-role/README.md +++ b/example/demo-role/README.md @@ -8,6 +8,7 @@ the default description with an annotation. ## Table of content +- [Requirements](#requirements) - [Default Variables](#default-variables) - [demo_role_dict](#demo_role_dict) - [demo_role_empty](#demo_role_empty) @@ -26,6 +27,11 @@ the default description with an annotation. --- +## Requirements + +- Minimum Ansible version: `2.4` + + ## Default Variables ### demo_role_dict