]> git.phdru.name Git - phdru.name/phdru.name.git/blob - phd_pp.py
Only process vimrc if it exists.
[phdru.name/phdru.name.git] / phd_pp.py
1 import os, re, time, urllib
2 from Cheetah.Template import Template
3
4
5 # Copied from ZWiki and Gajim
6
7 urlchars         = r'[A-Za-z0-9/:@_%~#=&\.\-\?\+\$,]+'
8 urlendchar       = r'[A-Za-z0-9/]'
9 url              = r'["=]?((about|file|ftp|gopher|http|https|mailto|news|telnet|ed2k|irc|sip|magnet):%s)' % \
10                    (urlchars+urlendchar)
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_pp(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    def body(self):
45       if hasattr(self, "body_html"):
46          return self.body_html()
47       if hasattr(self, "body_text"):
48          return self.text2html()
49       if hasattr(self, "body_rst"):
50          return self.rst2html()
51
52    def text2html(self):
53       body = re.sub(url, _url2href, self.body_text())
54
55       paragraphs = body.split("\n\n")
56
57       new_paras = []
58       for p in paragraphs:
59          parts = p.split("\n   ")
60          parts[0] = parts[0].strip()
61          new_paras.append('\n</p>\n<p>\n'.join(parts))
62
63       if self.Title:
64          title = "<h1>%s</h1>\n\n" % self.Title
65       else:
66          title = ''
67
68       body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
69       return "%s<p>%s</p>" % (title, body)
70
71    def rst2html(self):
72       from docutils.core import publish_parts
73       from locale import getpreferredencoding
74       encoding = getpreferredencoding()
75
76       parts = publish_parts(self.body_rst(), writer_name="html")
77
78       title = parts["title"].encode(encoding) or self.Title
79       if title:
80          title = "<h1>%s</h1>" % title
81
82       subtitle = parts["subtitle"].encode(encoding)
83       if subtitle:
84          subtitle = "<h2>%s</h2>" % subtitle
85
86       body = parts["body"].encode(encoding)
87       parts = [part for part in (title, subtitle, body) if part]
88       return "\n\n".join(parts)
89
90    def img_thumbnail_800_1024(self, img_name):
91       return """\
92 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
93 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
94
95    def google(self, query):
96       return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
97
98    def google_ru(self, query):
99       return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
100
101    def yandex(self, query):
102       return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
103
104    def wikipedia(self, query):
105       return "http://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'))
106
107    def wikipedia_ru(self, query):
108       return "http://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'))
109
110
111 def quote_string(s, to_encoding="utf-8"):
112    return urllib.quote(unicode(s, "koi8-r").encode(to_encoding))