]> git.phdru.name Git - phdru.name/phdru.name.git/blob - phd.py
Execute sitemap_gen.py
[phdru.name/phdru.name.git] / phd.py
1 import os, re, time
2 try:
3     from urllib import quote as url_quote
4 except ImportError:
5     from urllib.parse import quote as url_quote
6 from Cheetah.Template import Template
7 from Cheetah.compat import PY2
8
9
10 url_re = r"(((https?|ftp|gopher|telnet)://|(mailto|file|news|about|ed2k|irc|sip|magnet):)[^' \t<>\"]+|(www|web|w3)[A-Za-z0-9_-]*\.[A-Za-z0-9._-]+\.[^' \t<>\"]+)[A-Za-z0-9/]"
11
12 def _url2href(match):
13     url = match.group(0)
14     return '<a href="%s">%s</a>' % (url, url)
15
16
17 full_dirs = len(os.getcwd().split('/')) + 1
18
19 class phd(Template):
20     def __init__(self, *args, **kw):
21         if not hasattr(self, "_fileBaseName"):
22             self._fileDirName, self._fileBaseName = os.path.split(os.path.abspath(self._CHEETAH_src))
23         Template.__init__(self, *args, **kw)
24         directories = self._fileDirName.split('/')[full_dirs:] # remove directories up to "./files"
25         dirs_to_root = len(directories)
26         if dirs_to_root:
27             root = "../"*dirs_to_root
28         else:
29             root = ''
30         self.root = root
31         path = '/'.join(directories) + '/' + \
32             self._fileBaseName.replace(".tmpl", ".html")
33         if path[0] != '/': path = '/' + path
34         self.path = path
35
36     def copyright(self, start_year):
37         this_year = time.localtime()[0]
38         if start_year >= this_year:
39             return this_year
40         if start_year == this_year - 1:
41             return "%s, %s" % (start_year, this_year)
42         return "%s-%s" % (start_year, this_year)
43
44
45     def body(self):
46         if hasattr(self, "body_html"):
47             return self.body_html()
48         if hasattr(self, "body_text"):
49             return self.text2html()
50         if hasattr(self, "body_rst"):
51             return self.rst2html()
52         if hasattr(self, "body_mkd"):
53             return self.mkd2html()
54
55     def text2html(self):
56         body = re.sub(url_re, _url2href, self.body_text())
57
58         paragraphs = body.split("\n\n")
59
60         new_paras = []
61         for p in paragraphs:
62             if PY2 and not isinstance(p, bytes):
63                 p = p.encode('utf-8')
64             parts = p.split("\n  ")
65             parts[0] = parts[0].strip()
66             new_paras.append('\n</p>\n<p>\n'.join(parts))
67
68         if self.Title:
69             title = "<h1>%s</h1>\n\n" % self.Title
70         else:
71             title = ''
72
73         body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
74         return "%s<p>%s</p>" % (title, body)
75
76     def rst2html(self):
77         from docutils.core import publish_parts
78
79         parts = publish_parts(self.body_rst(), writer_name="html")
80
81         title = parts["title"] or self.Title
82         if title:
83             title = "<h1>%s</h1>" % title
84
85         subtitle = parts["subtitle"]
86         if subtitle:
87             subtitle = "<h2>%s</h2>" % subtitle
88
89         body = parts["body"]
90         parts = []
91         for part in (title, subtitle, body):
92              if not part:
93                   continue
94              if PY2 and not isinstance(part, bytes):
95                   part = part.encode('utf-8')
96              parts.append(part)
97         return "\n\n".join(parts)
98
99     def mkd2html(self):
100         from markdown import markdown
101         return markdown(self.body_mkd(), output_format="html")
102
103     def img_thumbnail_800_1024(self, img_name):
104         return """\
105 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
106 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
107
108     def wikipedia(self, query):
109         return "https://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
110
111     def wikipedia_ru(self, query):
112         return "https://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
113
114     def startpage(self, query):
115          return "https://startpage.com/do/search?q=%s" % quote_string(query)
116
117     search = startpage
118
119     def nigma(self, query):
120          return "http://www.nigma.ru/index.php?s=%s" % quote_string(query)
121
122     def yandex(self, query):
123         return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
124
125     def google(self, query):
126         return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
127
128     def google_ru(self, query):
129         return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
130
131     def lurkmoar(self, query):
132          return "https://lurkmore.to/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
133
134 def quote_string(s, to_encoding="utf-8", ext_safe=''):
135     if isinstance(s, bytes):
136         s = s.decode("utf-8")
137         if PY2:
138             s = s.encode(to_encoding)
139     return url_quote(s, '/' + ext_safe)