feat: format jinja template

This commit is contained in:
egarette@silique.fr 2025-03-27 08:36:49 +01:00
parent 88f837aa07
commit a4eab14564
4 changed files with 27 additions and 6 deletions

View file

@ -28,6 +28,7 @@ classifiers = [
]
dependencies = [
"rougail >= 1.1,<2",
"djlint == 1.36.4",
]
[project.urls]

View file

@ -27,6 +27,9 @@ from ruamel.yaml.error import CommentMark
from ruamel.yaml.comments import CommentedSeq
from ruamel.yaml.scalarstring import LiteralScalarString, FoldedScalarString
from djlint.settings import Config
from djlint.reformat import formatter
from tiramisu import undefined
from tiramisu.config import get_common_path
@ -72,6 +75,10 @@ class RougailOutputFormatter:
filename = Path(filenames[0])
if not filename.is_file():
raise Exception(_('only a file is allowed'))
self.config = Config()
self.config.profile = 'jinja'
self.original_yaml = RougailUpgrade(rougailconfig).run(filename)
datas = RougailUpgrade(rougailconfig).run(filename)
self.rougail = RougailConvert(rougailconfig)
@ -299,10 +306,16 @@ class RougailOutputFormatter:
if isinstance(value, JinjaCalculation):
jinja = CommentedMap()
# replace \n to space a add index of \n (now a space) to fold_pos
jinja_values = value.jinja.strip()
jinja_values = formatter(self.config, value.jinja.strip())[:-1]
if key == 'default' and not multi:
jinja["jinja"] = FoldedScalarString(jinja_values.replace('\n', ' '))
jinja["jinja"].fold_pos = [i for i, ltr in enumerate(jinja_values) if ltr == '\n']
jinja["jinja"] = FoldedScalarString(jinja_values)
fold_pos = []
old_i = 0
for i, ltr in enumerate(jinja_values):
if ltr == '\n':
fold_pos.append(i - old_i)
old_i = 1
jinja["jinja"].fold_pos = fold_pos
elif key == 'secret_manager':
return self.object_to_yaml("params", type_, value.params, multi, object_path)
else:

View file

@ -5,8 +5,15 @@ var1:
description: a first variable
default:
jinja: >-
{% if var2 is defined %} {{ var2 }} {% elif var3 is defined %} {{ var3 }} {%
elif var4 is defined %} {{ var4 }} {% else %} {{ _.var2 }} {% endif %}
{% if var2 is defined %}
{{ var2 }}
{% elif var3 is defined %}
{{ var3 }}
{% elif var4 is defined %}
{{ var4 }}
{% else %}
{{ _.var2 }}
{% endif %}
description: returns a value
params:
var2:

View file

@ -11,7 +11,7 @@ excludes = [
]
test_ok = get_structures_list(excludes)
# test_ok = [Path('../rougail-tests/structures/16_6exists_family')]
# test_ok = [Path('../rougail-tests/structures/00_9default_calculation_param_optional')]
def idfn(fixture_value):