Skip to content

Commit

Permalink
Replace removed Jinja2 test "search" with a custom one. (#197)
Browse files Browse the repository at this point in the history
* Create the custom jinja2 test called "search" which uses the `regex_search` function

* Remove unnecessary notes
  • Loading branch information
sHermanGriffiths authored Aug 2, 2024
1 parent 4fec0e9 commit b2d57e8
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions n2y/plugins/jinjarenderpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,17 @@
from n2y.utils import available_from_list, pandoc_ast_to_markdown


def join_to(foreign_keys, table, primary_key="notion_id"):
"""
Given a set of ids for an object, and a list of the objects these ids refer
to, select out the objects by joining using the specified primary key
(which defaults to 'id').
"""
joined = []
for foreign_key in foreign_keys:
selected_row = None
for row in table:
if row[primary_key] == foreign_key:
selected_row = row
break
joined.append(selected_row)
return joined
def regex_search(value, pattern):
return bool(re.search(pattern, value))


def _canonicalize(markdown):
markdown = markdown.replace("\u201d", '"').replace("\u201c", '"')
markdown = markdown.replace("\u2019", "'").replace("\u2018", "'")
markdown = markdown.replace("\u2013", "--").replace("\u2014", "---")
markdown = markdown.replace("\u2026", "...")
markdown = markdown.replace("\u00a0", " ")
return markdown


def list_matches(string, text):
Expand Down Expand Up @@ -111,13 +107,21 @@ def fuzzy_find_in(term_list, text, key="Name", by_length=True, reverse=True):
return found


def _canonicalize(markdown):
markdown = markdown.replace("\u201d", '"').replace("\u201c", '"')
markdown = markdown.replace("\u2019", "'").replace("\u2018", "'")
markdown = markdown.replace("\u2013", "--").replace("\u2014", "---")
markdown = markdown.replace("\u2026", "...")
markdown = markdown.replace("\u00a0", " ")
return markdown
def join_to(foreign_keys, table, primary_key="notion_id"):
"""
Given a set of ids for an object, and a list of the objects these ids refer
to, select out the objects by joining using the specified primary key
(which defaults to 'id').
"""
joined = []
for foreign_key in foreign_keys:
selected_row = None
for row in table:
if row[primary_key] == foreign_key:
selected_row = row
break
joined.append(selected_row)
return joined


def _create_jinja_environment():
Expand All @@ -128,6 +132,7 @@ def _create_jinja_environment():
)
environment.globals["first_pass_output"] = FirstPassOutput()
environment.filters["fuzzy_find_in"] = fuzzy_find_in
environment.tests["search"] = regex_search
environment.filters["join_to"] = join_to
return environment

Expand Down Expand Up @@ -329,6 +334,16 @@ def _get_yaml_from_mentions(self):
url_property=export_defaults["url_property"],
)

def _prepare_jinja_environment(self):
self.jinja_environment.tests = {
k: self._debug_assist(v, "test", k)
for k, v in self.jinja_environment.tests.items()
}
self.jinja_environment.filters = {
k: self._debug_assist(v, "filter", k)
for k, v in self.jinja_environment.filters.items()
}

def _specify_err_msg(self, err: Exception):
block_ref: str = f"See the Notion code block here: {self.notion_url}."
line_num: str = traceback.format_exc().split('>", line ')[1][0]
Expand Down Expand Up @@ -401,14 +416,7 @@ def _error_ast(self):
return [Para([Code(("", ["markdown"], []), self.error)])]

def _render_text(self):
self.jinja_environment.filters = {
k: self._debug_assist(v, "filter", k)
for k, v in self.jinja_environment.filters.items()
}
self.jinja_environment.tests = {
k: self._debug_assist(v, "test", k)
for k, v in self.jinja_environment.tests.items()
}
self._prepare_jinja_environment()
if not getattr(self, "context", None):
self.context = {
"databases": self.databases,
Expand Down

0 comments on commit b2d57e8

Please sign in to comment.