From 71f0a3c4e4df3390f5cdfa5e39fec74dedb66663 Mon Sep 17 00:00:00 2001 From: Nik V Date: Fri, 27 Nov 2020 19:18:51 -0800 Subject: [PATCH] make journals in roam date format be recognized by journals page --- app/db.py | 4 ++-- app/util.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/db.py b/app/db.py index d0b0bc5..25a9d69 100644 --- a/app/db.py +++ b/app/db.py @@ -187,7 +187,7 @@ def all_nodes(include_journals=True): # remove journals if so desired. if not include_journals: - nodes = [node for node in nodes if not re.match('[0-9]+?-[0-9]+?-[0-9]+?', node.wikilink)] + nodes = [node for node in nodes if not util.is_journal(node.wikilink)] # TODO: experiment with other ranking. # return sorted(nodes, key=lambda x: -x.size()) @@ -201,7 +201,7 @@ def all_users(): def all_journals(): # hack hack. nodes = all_nodes() - nodes = [node for node in nodes if re.match('[0-9]+?-[0-9]+?-[0-9]+?', node.wikilink)] + nodes = [node for node in nodes if util.is_journal(node.wikilink)] return sorted(nodes, key=attrgetter('wikilink'), reverse=True) def nodes_by_wikilink(wikilink): diff --git a/app/util.py b/app/util.py index 4c6bf7a..259fa97 100644 --- a/app/util.py +++ b/app/util.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import re def canonical_wikilink(wikilink): # hack hack @@ -22,3 +23,19 @@ def canonical_wikilink(wikilink): .replace('/', '-') ) return wikilink + + +def is_journal(wikilink): + date_regexes = [ + # iso format + '[0-9]{4}-[0-9]{2}-[0-9]{2}', + # roam format (what a monstrosity!) + '(January|February|March|April|May|June|July|August|September|October|November|December) [0-9]{1,2}(st|nd|th), [0-9]{4}', + # roam format (sanitzed for filenames) + '(january|february|march|april|may|june|july|august|september|october|november|december)-[0-9]{1,2}(st|nd|th)-[0-9]{4}', + ] + + # combine all the date regexes into one super regex + combined_date_regex = re.compile(f'^({"|".join(date_regexes)})$') + + return combined_date_regex.match(wikilink)