]> 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 cbac10c6bbdac3eee8d9b171667938976afdfeeb..e40f05c0500e5104d88e0948da57a7196ba85dbc 100755 (executable)
@@ -1,13 +1,16 @@
 #! /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
-from parse_query import parser
-from html.response import response
+import cgi, sys
+from ply.lex import LexError
+
+from html.response import redirect, response
+from parser.parser import parser
 
 form = cgi.FieldStorage()
 if not form.has_key('q'):
@@ -15,4 +18,44 @@ if not form.has_key('q'):
     title = "Error!"
     body = "Required parameter is missing!"
 
+else:
+    q = form['q'].value
+    try:
+        tree = parser.parse(q)
+    except LexError:
+        tree = None
+    if tree is None:
+        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)