]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blobdiff - search-tags.py
HTML-escape expression in the output (to quote &)
[phdru.name/cgi-bin/blog-ru/search-tags.git] / search-tags.py
index 9bdfc8bb5bc1e4cd6711e6f7f4caad5fe5bc464c..e40f05c0500e5104d88e0948da57a7196ba85dbc 100755 (executable)
@@ -1,14 +1,15 @@
 #! /usr/bin/env python
+# coding: koi8-r
 """Search tags CGI"""
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2014 PhiloSoft Design"
+__copyright__ = "Copyright (C) 2014-2016 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-import cgi
+import cgi, sys
 from ply.lex import LexError
 
-from html.response import response
+from html.response import redirect, response
 from parser.parser import parser
 
 form = cgi.FieldStorage()
@@ -27,5 +28,34 @@ else:
         status = "400 Bad request"
         title = "Error!"
         body = "Bad query syntax!"
+    elif tree[0] == 'NAME': # Single tag - just do redirect
+        tag = tree[1]
+        assert isinstance(tag, str)
+        from tags import tag_exists
+        if tag_exists(tag):
+            redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved")
+            sys.exit()
+        status = "404 Tag not found"
+        title = "Ошибка!"
+        body = "Тег %s не существует!" % tag
+    else: # Process tree
+        from tags import find_tags
+        posts = find_tags(tree)
+        status = None
+        title = "Записи, найденные для выражения " + cgi.escape(q)
+        if posts:
+            _posts = ["""\
+<p class="head">
+<ul>
+"""]
+            for year, month, day, suburl, _title in posts:
+                _posts.append('<li>%s-%s-%s <a href="../../../../Russian/blog/%s">%s</a></li>\n' % (year, month, day, suburl, _title))
+            _posts .append("""\
+</ul>
+</p>
+""")
+            body = ''.join(_posts)
+        else:
+            body = "Не найдено ни одной записи."
 
 response(title, body, status)