X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=search-tags.py;h=33d141f33107c9f7fcdcc1728f4f160ba74d106d;hb=85b18364aea83224a966dc129c9e0b9afc832a9a;hp=8d322d12057ed3290646c67c3c99b94846a6e877;hpb=ed552696f81f160ff233a268cbc9b506e76edce9;p=phdru.name%2Fcgi-bin%2Fblog-ru%2Fsearch-tags.git diff --git a/search-tags.py b/search-tags.py index 8d322d1..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, sys -from ply.lex import LexError +from grako.exceptions import FailedParse from html.response import redirect, response -from parser.parser import parser form = cgi.FieldStorage() if not form.has_key('q'): @@ -20,22 +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!" - 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 = "Error!" - body = "Tag does not exist!" + 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)