]> git.phdru.name Git - phdru.name/phdru.name.git/blob - phd_pp.py
Added .profile, .shellrc.
[phdru.name/phdru.name.git] / phd_pp.py
1 import os, re, time, urllib
2 from Cheetah.Template import Template
3
4 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/]"
5
6 def _url2href(match):
7    url = match.group(0)
8    return '<a href="%s">%s</a>' % (url, url)
9
10 full_dirs = len(os.getcwd().split('/')) + 1
11
12 class phd_pp(Template):
13    def __init__(self, *args, **kw):
14       if not hasattr(self, "_fileBaseName"):
15          self._fileDirName, self._fileBaseName = os.path.split(os.path.abspath(self._CHEETAH_src))
16       Template.__init__(self, *args, **kw)
17       directories = self._fileDirName.split('/')[full_dirs:] # remove directories up to "./files"
18       dirs_to_root = len(directories)
19       if dirs_to_root:
20          root = "../"*dirs_to_root
21       else:
22          root = ''
23       self.root = root
24       path = '/'.join(directories) + '/' + \
25          self._fileBaseName.replace(".tmpl", ".html")
26       if path[0] <> '/': path = '/' + path
27       self.path = path
28
29    def copyright(self, start_year):
30       this_year = time.localtime()[0]
31       if start_year >= this_year:
32          return this_year
33       if start_year == this_year - 1:
34          return "%s, %s" % (start_year, this_year)
35       return "%s-%s" % (start_year, this_year)
36
37    def body(self):
38       if hasattr(self, "body_html"):
39          return self.body_html()
40       if hasattr(self, "body_text"):
41          return self.text2html()
42       if hasattr(self, "body_rst"):
43          return self.rst2html()
44
45    def text2html(self):
46       body = re.sub(url_re, _url2href, self.body_text())
47
48       paragraphs = body.split("\n\n")
49
50       new_paras = []
51       for p in paragraphs:
52          parts = p.split("\n   ")
53          parts[0] = parts[0].strip()
54          new_paras.append('\n</p>\n<p>\n'.join(parts))
55
56       if self.Title:
57          title = "<h1>%s</h1>\n\n" % self.Title
58       else:
59          title = ''
60
61       body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
62       return "%s<p>%s</p>" % (title, body)
63
64    def rst2html(self):
65       from docutils.core import publish_parts
66       from locale import getpreferredencoding
67       encoding = getpreferredencoding()
68
69       parts = publish_parts(self.body_rst(), writer_name="html")
70
71       title = parts["title"].encode(encoding) or self.Title
72       if title:
73          title = "<h1>%s</h1>" % title
74
75       subtitle = parts["subtitle"].encode(encoding)
76       if subtitle:
77          subtitle = "<h2>%s</h2>" % subtitle
78
79       body = parts["body"].encode(encoding)
80       parts = [part for part in (title, subtitle, body) if part]
81       return "\n\n".join(parts)
82
83    def img_thumbnail_800_1024(self, img_name):
84       return """\
85 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
86 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
87
88    def google(self, query):
89       return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
90
91    def google_ru(self, query):
92       return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
93
94    def yandex(self, query):
95       return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
96
97    def wikipedia(self, query):
98       return "http://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
99
100    def wikipedia_ru(self, query):
101       return "http://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
102
103 def quote_string(s, to_encoding="utf-8", ext_safe=''):
104    return urllib.quote(unicode(s, "koi8-r").encode(to_encoding), '/' + ext_safe)