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
8 from blog_db import load_blog
11 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/]"
15 return '<a href="%s">%s</a>' % (url, url)
18 full_dirs = len(os.getcwd().split('/')) + 1
21 def __init__(self, *args, **kw):
22 if not hasattr(self, "_fileBaseName"):
23 self._fileDirName, self._fileBaseName = os.path.split(os.path.abspath(self._CHEETAH_src))
24 Template.__init__(self, *args, **kw)
25 directories = self._fileDirName.split('/')[full_dirs:] # remove directories up to "./files"
26 dirs_to_root = len(directories)
28 root = "../"*dirs_to_root
32 path = '/'.join(directories) + '/' + \
33 self._fileBaseName.replace(".tmpl", ".html")
34 if path[0] != '/': path = '/' + path
37 def copyright(self, start_year):
38 this_year = time.localtime()[0]
39 if start_year >= this_year:
41 if start_year == this_year - 1:
42 return "%s, %s" % (start_year, this_year)
43 return "%s-%s" % (start_year, this_year)
47 if hasattr(self, "body_html"):
48 return self.body_html()
49 if hasattr(self, "body_text"):
50 return self.text2html()
51 if hasattr(self, "body_rst"):
52 return self.rst2html()
53 if hasattr(self, "body_mkd"):
54 return self.mkd2html()
57 body = re.sub(url_re, _url2href, self.body_text())
59 paragraphs = body.split("\n\n")
63 if PY2 and not isinstance(p, bytes):
65 parts = p.split("\n ")
66 parts[0] = parts[0].strip()
67 new_paras.append('\n</p>\n<p>\n'.join(parts))
70 title = "<h1>%s</h1>\n\n" % self.Title
74 body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
75 return "%s<p>%s</p>" % (title, body)
78 from docutils.core import publish_parts
80 parts = publish_parts(self.body_rst(), writer_name="html")
82 title = parts["title"] or self.Title
84 title = "<h1>%s</h1>" % title
86 subtitle = parts["subtitle"]
88 subtitle = "<h2>%s</h2>" % subtitle
92 for part in (title, subtitle, body):
95 if PY2 and not isinstance(part, bytes):
96 part = part.encode('utf-8')
98 return "\n\n".join(parts)
101 from markdown import markdown
102 return markdown(self.body_mkd(), output_format="html")
105 def find_near_blog_posts(self):
106 if not self.path.startswith("/Russian/blog/"):
107 return None, None, None, None
108 dirs = self.path.split('/')
109 # blog post is ['', 'Russian', 'blog', year, month, day, filename]
110 if len(dirs) != 7: # Not a blog post
111 return None, None, None, None
112 ymd = tuple(dirs[3:-1])
113 filename = self._fileBaseName
115 prev_key = prev_blog_post = current_key = current_blog_post = \
116 next_key = next_blog_post = None
117 old_blog = load_blog()
118 for key in sorted(old_blog):
119 for blog_post in sorted(old_blog[key]):
120 if current_blog_post:
121 prev_key, prev_blog_post = current_key, current_blog_post
123 current_key, current_blog_post = next_key, next_blog_post
125 next_blog_post = blog_post
126 if current_blog_post and (current_key == ymd) and \
127 (current_blog_post[0] == filename): # Found!
128 return prev_key, prev_blog_post, next_key, next_blog_post
129 return current_key, current_blog_post, None, None
132 def img_thumbnail_800_1024(self, img_name):
134 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
135 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
137 def wikipedia(self, query):
138 return "https://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
140 def wikipedia_ru(self, query):
141 return "https://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
143 def startpage(self, query):
144 return "https://startpage.com/do/search?q=%s" % quote_string(query)
148 def nigma(self, query):
149 return "http://www.nigma.ru/index.php?s=%s" % quote_string(query)
151 def yandex(self, query):
152 return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
154 def google(self, query):
155 return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
157 def google_ru(self, query):
158 return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
160 def lurkmoar(self, query):
161 return "https://lurkmore.to/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
163 def quote_string(s, to_encoding="utf-8", ext_safe=''):
164 if isinstance(s, bytes):
165 s = s.decode("utf-8")
167 s = s.encode(to_encoding)
168 return url_quote(s, '/' + ext_safe)