1 import os, re, time, urllib
2 from Cheetah.Template import Template
5 # Copied from ZWiki and Gajim
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)' % \
14 return '<a href="%s">%s</a>' % (url, url)
17 full_dirs = len(os.getcwd().split('/')) + 1
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)
27 root = "../"*dirs_to_root
31 path = '/'.join(directories) + '/' + \
32 self._fileBaseName.replace(".tmpl", ".html")
33 if path[0] <> '/': path = '/' + path
36 def copyright(self, start_year):
37 this_year = time.localtime()[0]
38 if start_year >= 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)
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()
53 body = re.sub(url, _url2href, self.body_text())
55 paragraphs = body.split("\n\n")
59 parts = p.split("\n ")
60 parts[0] = parts[0].strip()
61 new_paras.append('\n</p>\n<p>\n'.join(parts))
64 title = "<h1>%s</h1>\n\n" % self.Title
68 body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
69 return "%s<p>%s</p>" % (title, body)
72 from docutils.core import publish_parts
73 from locale import getpreferredencoding
74 encoding = getpreferredencoding()
76 parts = publish_parts(self.body_rst(), writer_name="html")
78 title = parts["title"].encode(encoding) or self.Title
80 title = "<h1>%s</h1>" % title
82 subtitle = parts["subtitle"].encode(encoding)
84 subtitle = "<h2>%s</h2>" % subtitle
86 body = parts["body"].encode(encoding)
87 parts = [part for part in (title, subtitle, body) if part]
88 return "\n\n".join(parts)
90 def img_thumbnail_800_1024(self, img_name):
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}
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)
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)
101 def yandex(self, query):
102 return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
104 def wikipedia(self, query):
105 return "http://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'))
107 def wikipedia_ru(self, query):
108 return "http://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'))
111 def quote_string(s, to_encoding="utf-8"):
112 return urllib.quote(unicode(s, "koi8-r").encode(to_encoding))