]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blob - search-tags.py
HTML-escape expression in the output (to quote &)
[phdru.name/cgi-bin/blog-ru/search-tags.git] / search-tags.py
1 #! /usr/bin/env python
2 # coding: koi8-r
3 """Search tags CGI"""
4
5 __author__ = "Oleg Broytman <phd@phdru.name>"
6 __copyright__ = "Copyright (C) 2014-2016 PhiloSoft Design"
7 __license__ = "GNU GPL"
8
9 import cgi, sys
10 from ply.lex import LexError
11
12 from html.response import redirect, response
13 from parser.parser import parser
14
15 form = cgi.FieldStorage()
16 if not form.has_key('q'):
17     status = "400 Bad request"
18     title = "Error!"
19     body = "Required parameter is missing!"
20
21 else:
22     q = form['q'].value
23     try:
24         tree = parser.parse(q)
25     except LexError:
26         tree = None
27     if tree is None:
28         status = "400 Bad request"
29         title = "Error!"
30         body = "Bad query syntax!"
31     elif tree[0] == 'NAME': # Single tag - just do redirect
32         tag = tree[1]
33         assert isinstance(tag, str)
34         from tags import tag_exists
35         if tag_exists(tag):
36             redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved")
37             sys.exit()
38         status = "404 Tag not found"
39         title = "Ошибка!"
40         body = "Тег %s не существует!" % tag
41     else: # Process tree
42         from tags import find_tags
43         posts = find_tags(tree)
44         status = None
45         title = "Записи, найденные для выражения " + cgi.escape(q)
46         if posts:
47             _posts = ["""\
48 <p class="head">
49 <ul>
50 """]
51             for year, month, day, suburl, _title in posts:
52                 _posts.append('<li>%s-%s-%s <a href="../../../../Russian/blog/%s">%s</a></li>\n' % (year, month, day, suburl, _title))
53             _posts .append("""\
54 </ul>
55 </p>
56 """)
57             body = ''.join(_posts)
58         else:
59             body = "Не найдено ни одной записи."
60
61 response(title, body, status)