X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=search-tags.py;h=33d141f33107c9f7fcdcc1728f4f160ba74d106d;hb=18b709482675cb51ba6fb405c17d60d782ecbc86;hp=9bdfc8bb5bc1e4cd6711e6f7f4caad5fe5bc464c;hpb=805b4f5ed00d30d3e39a19fc2d6d0afa65e86e92;p=phdru.name%2Fcgi-bin%2Fblog-ru%2Fsearch-tags.git diff --git a/search-tags.py b/search-tags.py index 9bdfc8b..33d141f 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-2016 PhiloSoft Design" __license__ = "GNU GPL" -import cgi -from ply.lex import LexError +import cgi, sys +from grako.exceptions import FailedParse -from html.response import response -from parser.parser import parser +from html.response import redirect, response form = cgi.FieldStorage() if not form.has_key('q'): @@ -20,12 +20,43 @@ if not form.has_key('q'): else: q = form['q'].value try: - tree = parser.parse(q) - except LexError: - tree = None - if tree is None: + from parser.parser import TagsParser + from parser.build_ast import TagsSemantics + parser = TagsParser(parseinfo=False) + tree = parser.parse(q, semantics=TagsSemantics()) + except FailedParse: 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)