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