]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/commitdiff
Process tree (parsed expression)
authorOleg Broytman <phd@phdru.name>
Tue, 20 May 2014 04:19:21 +0000 (08:19 +0400)
committerOleg Broytman <phd@phdru.name>
Tue, 20 May 2014 04:31:56 +0000 (08:31 +0400)
Recursively evaluate the tree for every post and return a list
of matched posts.

search-tags.py
tags.py

index 8e047e1b400fb46fdd79bb1c0f5652f249091433..27f14f0865f895f893349651784ff059b9114ba7 100755 (executable)
@@ -38,5 +38,17 @@ else:
         status = "404 Tag not found"
         title = "Ошибка!"
         body = "Тег %s не существует!" % tag
         status = "404 Tag not found"
         title = "Ошибка!"
         body = "Тег %s не существует!" % tag
+    else: # Process tree
+        from tags import calc_tree
+        posts = calc_tree(tree)
+        status = None
+        title = "Записи, найденные для выражения " + q
+        if posts:
+            _posts = []
+            for suburl, title in posts:
+                _posts.append('<a href="/Russian/blog/%s">%s</a>' % (suburl, title))
+            body = "<br>\n".join(_posts)
+        else:
+            body = "Не найдено ни одной записи."
 
 response(title, body, status)
 
 response(title, body, status)
diff --git a/tags.py b/tags.py
index 7ff886d544ec0a70bc9f8eb6926633671ab872f4..35c4634ca53702fe4791bc70237fe13aa6018a2b 100644 (file)
--- a/tags.py
+++ b/tags.py
@@ -19,7 +19,37 @@ else:
 
 def tag_exists(tag):
     for posts in blog_dict.itervalues():
 
 def tag_exists(tag):
     for posts in blog_dict.itervalues():
-        for _file, _title, _lead, tags in posts:
-            if tag in tags:
+        for _file, _title, _lead, _tags in posts:
+            if tag in _tags:
                 return True
     return False
                 return True
     return False
+
+def _test_post(post, tree):
+    op = tree[0]
+    if op == 'NAME':
+        tag = tree[1]
+        assert isinstance(tag, str)
+        _tags = post[3]
+        return tag in _tags
+    elif op in ('AND', 'OR'):
+        value1 = _test_post(post, tree[1])
+        value2 = _test_post(post, tree[2])
+        if op == 'AND':
+            return value1 and value2
+        if op == 'OR':
+            return value1 or value2
+    elif op == 'NOT':
+        return not _test_post(post, tree[1])
+    else:
+        raise ValueError("Cannot get there")
+
+def calc_tree(tree):
+    _posts = []
+    for (year, month, day), posts in blog_dict.iteritems():
+        for post in posts:
+            if _test_post(post, tree):
+                _posts.append((
+                    '/'.join((year, month, day, post[0].replace('.tmpl', '.html'))),
+                    post[1]))
+    _posts.sort()
+    return _posts