Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use first pass output for jinja templates whenever possible #153

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions n2y/plugins/jinjarenderpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def render_from_string(source, context=None, environment=None):
if context is None:
context = {}
template = environment.from_string(source)
template.root_render_func
output = template.render(context)

# output string usually loses trailing new line.
Expand Down Expand Up @@ -284,6 +283,8 @@ def __init__(self, client, notion_data, page, get_children=True):
self.error: str | None = None
self.exc_info: JinjaExceptionInfo | None = None
self.databases: JinjaDatabaseCache | None = None
self.jinja_code: str = self.rich_text.to_plain_text()
self.uses_first_pass_output: bool = 'first_pass_output' in self.jinja_code
self.page_props: PageProperties = self.page.properties_to_values(self.pandoc_format)
self.jinja_environment: jinja2.Environment = self.client.plugin_data[
'jinjarenderpage'][self.page.notion_id]['environment']
Expand Down Expand Up @@ -392,8 +393,6 @@ def _render_text(self):
k: self._debug_assist(v, 'test', k)
for k, v in self.jinja_environment.tests.items()
}
if not getattr(self, 'jinja_code', None):
self.jinja_code = self.rich_text.to_plain_text()
if not getattr(self, 'context', None):
self.context = {
"databases": self.databases,
Expand All @@ -413,7 +412,11 @@ def render_content(notion_id, level_adjustment=0):
options=[],
)
return content
self.jinja_environment.filters['render_content'] = render_content
self.jinja_environment.filters['render_content'] = self._debug_assist(
render_content,
'filter',
'render_content'
)
try:
self.rendered_text = render_from_string(
self.jinja_code, self.context, self.jinja_environment
Expand All @@ -425,7 +428,7 @@ def render_content(notion_id, level_adjustment=0):
def to_pandoc(self):
if self.databases is None:
self._get_yaml_from_mentions()
if self.render_count < 2:
if self.render_count < 1 or self.render_count < 2 and self.uses_first_pass_output:
self._render_text()
if self.error:
children_ast = self._error_ast()
Expand Down