From a269183254b03e72131d9060adad7ea0ee40ac56 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 16 Jun 2024 16:22:33 +0300 Subject: [PATCH] Feat: Search tags ignoring case --- search-tags.py | 7 ++++--- tags.py | 27 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/search-tags.py b/search-tags.py index 6281a1b..63f4241 100755 --- a/search-tags.py +++ b/search-tags.py @@ -33,10 +33,11 @@ else: if tree[0] == 'NAME': # Single tag - just do redirect tag = tree[1] assert isinstance(tag, str) - from tags import tag_exists - if tag_exists(tag): + from tags import real_tag + rtag = real_tag(tag) + if rtag: redirect( - "/Russian/blog/tags/%s.html" % tag, status="301 Moved") + "/Russian/blog/tags/%s.html" % rtag, status="301 Moved") sys.exit() status = "404 Tag not found" title = "ïÛÉÂËÁ!" diff --git a/tags.py b/tags.py index 3fe860b..a4ae88b 100644 --- a/tags.py +++ b/tags.py @@ -17,12 +17,26 @@ else: # blog_dict is a mapping # (year, month, day) => [list of (file, title, lead, tags)] -def tag_exists(tag): +# Add lower-case tags +_new_dict = {} +for (year, month, day), posts in blog_dict.items(): + _new_dict[year, month, day] = _posts = [] + for _file, _title, _lead, _tags in posts: + tags_lower = [tag.lower() for tag in _tags] + _posts.append((_file, _title, _lead, _tags, tags_lower)) +blog_dict = _new_dict + + +def real_tag(tag): + ltag = tag.lower() for posts in blog_dict.values(): - for _file, _title, _lead, _tags in posts: - if tag in _tags: - return True - return False + for _file, _title, _lead, _tags, _tags_lower in posts: + try: + ix = _tags_lower.index(ltag) + except ValueError: + continue + else: + return _tags[ix] def _test_post(post, tree): @@ -35,8 +49,7 @@ def _test_post(post, tree): if op == 'NAME': tag = tree[1] assert isinstance(tag, str) - _tags = post[3] - return tag in _tags + return tag.lower() in post[4] elif op in ('AND', 'OR'): value1 = _test_post(post, tree[1]) value2 = _test_post(post, tree[2]) -- 2.39.2