From 307c2933c572ac2a2e09ee13351a84599979a9fa Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 30 Mar 2025 19:39:29 +0200 Subject: [PATCH] feat: support multi lines for help --- src/rougail/output_formatter/__init__.py | 11 ++++++++--- .../00_6secret_param/rougail/00-base.yml | 18 ++++++++++++++++++ tests/results/00_7help/rougail/00-base.yml | 18 ++++++++++++++++++ tests/results/00_7help_sup/rougail/00-base.yml | 18 ++++++++++++++++++ tests/test_load.py | 2 +- 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/results/00_6secret_param/rougail/00-base.yml create mode 100644 tests/results/00_7help/rougail/00-base.yml create mode 100644 tests/results/00_7help_sup/rougail/00-base.yml diff --git a/src/rougail/output_formatter/__init__.py b/src/rougail/output_formatter/__init__.py index 3bbe3c1..fccc0a5 100644 --- a/src/rougail/output_formatter/__init__.py +++ b/src/rougail/output_formatter/__init__.py @@ -25,7 +25,7 @@ from ruamel.yaml import YAML, CommentedMap from ruamel.yaml.tokens import CommentToken from ruamel.yaml.error import CommentMark from ruamel.yaml.comments import CommentedSeq -from ruamel.yaml.scalarstring import LiteralScalarString, FoldedScalarString +from ruamel.yaml.scalarstring import LiteralScalarString, FoldedScalarString, ScalarString from djlint.settings import Config from djlint.reformat import formatter @@ -285,12 +285,16 @@ class RougailOutputFormatter: return _get_last_obj(o[param], o, param, 'seq') return typ, parent, param param = list(obj)[-1] + if isinstance(obj[param], ScalarString): + enter = '\n' + else: + enter = '\n\n' typ, parent, param = _get_last_obj(obj[param], obj, param, 'map') if typ == 'seq': func = parent.yaml_key_comment_extend else: func = parent.yaml_value_comment_extend - func(param, [CommentToken('\n\n', CommentMark(0)), None]) + func(param, [CommentToken(enter, CommentMark(0)), None]) def object_to_yaml(self, key, type_, value, multi, object_path): if isinstance(value, list): @@ -307,7 +311,6 @@ class RougailOutputFormatter: return new_values if isinstance(value, JinjaCalculation): jinja = CommentedMap() - # replace \n to space a add index of \n (now a space) to fold_pos jinja_values = formatter(self.config, value.jinja.strip())[:-1] if key == 'default' and not multi: jinja["jinja"] = FoldedScalarString(jinja_values) @@ -370,6 +373,8 @@ class RougailOutputFormatter: return {value.key: variable} elif type_ == 'port' and isinstance(value, str) and value.isnumeric(): return int(value) + elif key == 'help' and '\n' in value: + return LiteralScalarString(value) return value def calc_variable_path(self, object_path, variable_path): diff --git a/tests/results/00_6secret_param/rougail/00-base.yml b/tests/results/00_6secret_param/rougail/00-base.yml new file mode 100644 index 0000000..ef079d8 --- /dev/null +++ b/tests/results/00_6secret_param/rougail/00-base.yml @@ -0,0 +1,18 @@ +--- +version: 1.1 + +secret1: + description: the first variable + type: secret + params: + min_len: 10 + +secret2: + description: the second variable + type: secret + params: + max_len: 10 + forbidden_char: + - $ + - ^ + default: value diff --git a/tests/results/00_7help/rougail/00-base.yml b/tests/results/00_7help/rougail/00-base.yml new file mode 100644 index 0000000..ded1ab7 --- /dev/null +++ b/tests/results/00_7help/rougail/00-base.yml @@ -0,0 +1,18 @@ +--- +version: 1.1 + +var1: + description: the first variable + help: |- + Multi line + + Help + + With useful information + +var2: + description: the second variable + help: |- + Multi line + Help + With useful information diff --git a/tests/results/00_7help_sup/rougail/00-base.yml b/tests/results/00_7help_sup/rougail/00-base.yml new file mode 100644 index 0000000..1e6d0b1 --- /dev/null +++ b/tests/results/00_7help_sup/rougail/00-base.yml @@ -0,0 +1,18 @@ +--- +version: 1.1 + +var1: + description: the first + help: |- + Multi line + + + + With useful information + +var2: + description: the second + help: |- + Multi line + + With useful information diff --git a/tests/test_load.py b/tests/test_load.py index 837c9d1..6f60d45 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -11,7 +11,7 @@ excludes = [ ] test_ok = get_structures_list(excludes) -# test_ok = [Path('../rougail-tests/structures/00_9default_calculation_param_optional')] +# test_ok = [Path('../rougail-tests/structures/00_7help')] def idfn(fixture_value):