Skip to content

Commit

Permalink
enhance lines stripping on include
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Dec 25, 2021
1 parent 71133e0 commit cdd3047
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions renoir/parsing/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(self, ctx: 'Elements', idx: int, text: str):
self.nle = True
if self.prev() is None:
self.nle = True
elif not text.strip(' ') and self.next() is None:
self.nlb = True

def prev(self) -> Optional['Element']:
try:
Expand Down
2 changes: 1 addition & 1 deletion renoir/parsing/lexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class IncludeLexer(Lexer):
def process(self, ctx, value):
#: if we have a value, just add the new content
if value:
with ctx.load(value):
with ctx.load(value, strip_ending_new_line=True):
ctx.parse()
included_id = ctx.state._id
#: otherwise, inject in the extended node
Expand Down
10 changes: 6 additions & 4 deletions renoir/parsing/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ def __init__(
def _tag_split_text(self, text):
return self.r_tag.split(text.replace('\t', ' '))

def _get_file_text(self, ctx, filename, ctxpath=None):
def _get_file_text(self, ctx, filename, ctxpath=None, strip_ending_new_line=False):
#: remove quotation from filename string
try:
filename = eval(filename, self.scope)
except Exception:
raise TemplateError(
'Invalid template filename', ctx.state.source, ctx.state.lines)
'Invalid template filename', ctx.state.source, ctx.state.lines
)
#: resolve paths
preload_params = {}
if any(filename.startswith(relpath) for relpath in ["./", "../"]):
Expand All @@ -97,6 +98,8 @@ def _get_file_text(self, ctx, filename, ctxpath=None):
ctx.state.source, ctx.state.lines
)
text = self.templater.prerender(text, file_path)
if strip_ending_new_line and text.endswith("\n"):
text = text[:-1]
return filename, file_path, text

def parse_plain_block(self, ctx, element):
Expand Down Expand Up @@ -215,8 +218,7 @@ def reindent(self, text):
return '\n'.join(new_lines)

def render(self):
rv = self.reindent(self.content.render(self))
return rv
return self.reindent(self.content.render(self))


class IndentTemplateParser(TemplateParser):
Expand Down
5 changes: 4 additions & 1 deletion renoir/parsing/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ def __call__(

def load(self, name, **kwargs):
name, file_path, text = self.parser._get_file_text(
self, name, ctxpath=self.cwd
self,
name,
ctxpath=self.cwd,
strip_ending_new_line=kwargs.pop("strip_ending_new_line", False)
)
self.state.dependencies.append(name)
kwargs['source'] = file_path
Expand Down
5 changes: 0 additions & 5 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def test_plain(templater):
inclusion_nest:
first: foo
second: bar
main_key: main_val
added:
indent:
Expand Down Expand Up @@ -148,7 +147,6 @@ def test_plain_indent(templater_indent):
</head>
<body>
<div>header1</div>
<div>header2</div>
<div class="page">
<a href="/" class="title"><h1>Test</h1></a>
Expand All @@ -170,7 +168,6 @@ def test_plain_indent(templater_indent):
</div>
<div>footer</div>
</body>
</html>"""

Expand All @@ -194,7 +191,6 @@ def test_html(templater_html):
</head>
<body>
<div>header1</div>
<div>header2</div>
<div class="page">
<a href="/" class="title"><h1>Test</h1></a>
Expand All @@ -216,7 +212,6 @@ def test_html(templater_html):
</div>
<div>footer</div>
</body>
</html>"""

Expand Down

0 comments on commit cdd3047

Please sign in to comment.