]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/commitdiff
Feat: Search tags ignoring case
authorOleg Broytman <phd@phdru.name>
Sun, 16 Jun 2024 13:22:33 +0000 (16:22 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 16 Jun 2024 13:22:33 +0000 (16:22 +0300)
search-tags.py
tags.py

index 6281a1bc6919f7814bd04b2a86cc7b955ae41dde..63f4241ae94c40cd255adac64daf94578020d566 100755 (executable)
@@ -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 3fe860bdfab7fcfaa196726a11fc9bcae95ca7b0..a4ae88b15ce64e980f95c1577c491dcbdd8d0453 100644 (file)
--- 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])