1 import os, re, time, urllib
2 from Cheetah.Template import Template
5 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/]"
9 return '<a href="%s">%s</a>' % (url, url)
12 full_dirs = len(os.getcwd().split('/')) + 1
15 def __init__(self, *args, **kw):
16 if not hasattr(self, "_fileBaseName"):
17 self._fileDirName, self._fileBaseName = os.path.split(os.path.abspath(self._CHEETAH_src))
18 Template.__init__(self, *args, **kw)
19 directories = self._fileDirName.split('/')[full_dirs:] # remove directories up to "./files"
20 dirs_to_root = len(directories)
22 root = "../"*dirs_to_root
26 path = '/'.join(directories) + '/' + \
27 self._fileBaseName.replace(".tmpl", ".html")
28 if path[0] <> '/': path = '/' + path
31 def copyright(self, start_year):
32 this_year = time.localtime()[0]
33 if start_year >= this_year:
35 if start_year == this_year - 1:
36 return "%s, %s" % (start_year, this_year)
37 return "%s-%s" % (start_year, this_year)
41 if hasattr(self, "body_html"):
42 return self.body_html().encode('utf-8')
43 if hasattr(self, "body_text"):
44 return self.text2html()
45 if hasattr(self, "body_rst"):
46 return self.rst2html()
47 if hasattr(self, "body_mkd"):
48 return self.mkd2html()
51 body = re.sub(url_re, _url2href, self.body_text())
53 paragraphs = body.split("\n\n")
57 if isinstance(p, unicode):
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
74 parts = publish_parts(self.body_rst(), writer_name="html")
76 title = parts["title"] or self.Title
78 title = "<h1>%s</h1>" % title
80 subtitle = parts["subtitle"]
82 subtitle = "<h2>%s</h2>" % subtitle
86 for part in (title, subtitle, body):
89 if isinstance(part, unicode):
90 part = part.encode('utf-8')
92 return "\n\n".join(parts)
95 from markdown import markdown
96 return markdown(self.body_mkd(), output_format="html")
98 def img_thumbnail_800_1024(self, img_name):
100 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
101 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
103 def wikipedia(self, query):
104 return "https://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
106 def wikipedia_ru(self, query):
107 return "https://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
109 def startpage(self, query):
110 return "https://startpage.com/do/search?q=%s" % quote_string(query)
114 def nigma(self, query):
115 return "http://www.nigma.ru/index.php?s=%s" % quote_string(query)
117 def yandex(self, query):
118 return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
120 def google(self, query):
121 return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
123 def google_ru(self, query):
124 return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
126 def quote_string(s, to_encoding="utf-8", ext_safe=''):
127 return urllib.quote(unicode(s, "utf-8").encode(to_encoding), '/' + ext_safe)