X-Git-Url: https://git.phdru.name/?p=phdru.name%2Fcgi-bin%2Fblog-ru%2Fsearch-tags.git;a=blobdiff_plain;f=search-tags.py;h=c2884fab2942aeeeb7374451770cbd556d5c0581;hp=9bdfc8bb5bc1e4cd6711e6f7f4caad5fe5bc464c;hb=HEAD;hpb=805b4f5ed00d30d3e39a19fc2d6d0afa65e86e92 diff --git a/search-tags.py b/search-tags.py index 9bdfc8b..c2884fa 100755 --- a/search-tags.py +++ b/search-tags.py @@ -1,15 +1,15 @@ #! /usr/bin/env python +# coding: koi8-r """Search tags CGI""" __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2014 PhiloSoft Design" +__copyright__ = "Copyright (C) 2014-2017 PhiloSoft Design" __license__ = "GNU GPL" -import cgi -from ply.lex import LexError - -from html.response import response -from parser.parser import parser +import cgi, sys +from lark import ParseError +from html.response import redirect, response +from parser import parser form = cgi.FieldStorage() if not form.has_key('q'): @@ -21,11 +21,39 @@ else: q = form['q'].value try: tree = parser.parse(q) - except LexError: - tree = None - if tree is None: + except ParseError: status = "400 Bad request" title = "Error!" body = "Bad query syntax!" + 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): + 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 = ["""\ +

+

+

+ """) + body = ''.join(_posts) + else: + body = "îÅ ÎÁÊÄÅÎÏ ÎÉ ÏÄÎÏÊ ÚÁÐÉÓÉ." response(title, body, status)