3 from urllib import quote as url_quote
5 from urllib.parse import quote as url_quote
6 from Cheetah.Template import Template
7 from Cheetah.compat import PY2
10 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/]"
14 return '<a href="%s">%s</a>' % (url, url)
17 full_dirs = len(os.getcwd().split('/')) + 1
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)
46 if hasattr(self, "body_html"):
47 return self.body_html()
48 if hasattr(self, "body_text"):
49 return self.text2html()
50 if hasattr(self, "body_rst"):
51 return self.rst2html()
52 if hasattr(self, "body_mkd"):
53 return self.mkd2html()
56 body = re.sub(url_re, _url2href, self.body_text())
58 paragraphs = body.split("\n\n")
62 if PY2 and not isinstance(p, bytes):
64 parts = p.split("\n ")
65 parts[0] = parts[0].strip()
66 new_paras.append('\n</p>\n<p>\n'.join(parts))
69 title = "<h1>%s</h1>\n\n" % self.Title
73 body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
74 return "%s<p>%s</p>" % (title, body)
77 from docutils.core import publish_parts
79 parts = publish_parts(self.body_rst(), writer_name="html")
81 title = parts["title"] or self.Title
83 title = "<h1>%s</h1>" % title
85 subtitle = parts["subtitle"]
87 subtitle = "<h2>%s</h2>" % subtitle
91 for part in (title, subtitle, body):
94 if PY2 and not isinstance(part, bytes):
95 part = part.encode('utf-8')
97 return "\n\n".join(parts)
100 from markdown import markdown
101 return markdown(self.body_mkd(), output_format="html")
103 def img_thumbnail_800_1024(self, img_name):
105 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
106 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
108 def wikipedia(self, query):
109 return "https://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
111 def wikipedia_ru(self, query):
112 return "https://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
114 def startpage(self, query):
115 return "https://startpage.com/do/search?q=%s" % quote_string(query)
119 def nigma(self, query):
120 return "http://www.nigma.ru/index.php?s=%s" % quote_string(query)
122 def yandex(self, query):
123 return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
125 def google(self, query):
126 return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
128 def google_ru(self, query):
129 return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
131 def lurkmoar(self, query):
132 return "https://lurkmore.to/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
134 def quote_string(s, to_encoding="utf-8", ext_safe=''):
135 if isinstance(s, bytes):
136 s = s.decode("utf-8")
138 s = s.encode(to_encoding)
139 return url_quote(s, '/' + ext_safe)