1 import os, re, time, urllib
2 from Cheetah.Template import Template
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/]"
8 return '<a href="%s">%s</a>' % (url, url)
10 full_dirs = len(os.getcwd().split('/')) + 1
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)
20 root = "../"*dirs_to_root
24 path = '/'.join(directories) + '/' + \
25 self._fileBaseName.replace(".tmpl", ".html")
26 if path[0] <> '/': path = '/' + path
29 def copyright(self, start_year):
30 this_year = time.localtime()[0]
31 if start_year >= 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)
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()
46 body = re.sub(url_re, _url2href, self.body_text())
48 paragraphs = body.split("\n\n")
52 parts = p.split("\n ")
53 parts[0] = parts[0].strip()
54 new_paras.append('\n</p>\n<p>\n'.join(parts))
57 title = "<h1>%s</h1>\n\n" % self.Title
61 body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
62 return "%s<p>%s</p>" % (title, body)
65 from docutils.core import publish_parts
66 from locale import getpreferredencoding
67 encoding = getpreferredencoding()
69 parts = publish_parts(self.body_rst(), writer_name="html")
71 title = parts["title"].encode(encoding) or self.Title
73 title = "<h1>%s</h1>" % title
75 subtitle = parts["subtitle"].encode(encoding)
77 subtitle = "<h2>%s</h2>" % subtitle
79 body = parts["body"].encode(encoding)
80 parts = [part for part in (title, subtitle, body) if part]
81 return "\n\n".join(parts)
83 def img_thumbnail_800_1024(self, img_name):
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}
88 def wikipedia(self, query):
89 return "http://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
91 def wikipedia_ru(self, query):
92 return "http://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
94 def nigma(self, query):
95 return "http://www.nigma.ru/index.php?s=%s" % quote_string(query)
99 def yandex(self, query):
100 return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
102 def google(self, query):
103 return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
105 def google_ru(self, query):
106 return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
108 def quote_string(s, to_encoding="utf-8", ext_safe=''):
109 return urllib.quote(unicode(s, "koi8-r").encode(to_encoding), '/' + ext_safe)