Skip to content

Commit

Permalink
charts: render multiline strings using block-style
Browse files Browse the repository at this point in the history
This changed is added because multi-line strings are an issue in
rendered charts since Jinja "commands" are split.

This fix is needed to properly add and render DEX charts as done
in the next commit
  • Loading branch information
gdemonet authored and Ebaneck committed Nov 20, 2019
1 parent fab2af4 commit 1ada06c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions charts/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
`app.kubernetes.io/component` fields
'''

import re
import sys
import subprocess

import yaml
from yaml.dumper import SafeDumper
from yaml.representer import SafeRepresenter


BOILERPLATE = '''
Expand Down Expand Up @@ -65,12 +68,35 @@ def fixup_dict(doc):

return dict((key, fixup_doc(value)) for (key, value) in doc.items())

# Represent multiline strings as literal blocks {{{
class multiline_str(str): pass

def representer_multiline_str(dumper, data):
scalar = SafeRepresenter.represent_str(dumper, data)
scalar.style = '|'
return scalar

SafeDumper.add_representer(multiline_str, representer_multiline_str)

def fixup_string(value):
if '\n' in value:
# Remove empty lines
value = '\n'.join(
line for line in value.splitlines()
if not re.match('^\s*$', line)
)
return multiline_str(value)
return value
# }}}


def fixup_doc(doc):
if isinstance(doc, dict):
return fixup_dict(doc)
elif isinstance(doc, list):
return [fixup_doc(d) for d in doc]
elif isinstance(doc, str):
return fixup_string(doc)
else:
return doc

Expand Down

0 comments on commit 1ada06c

Please sign in to comment.