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/]"
16 return '<a href="%s">%s</a>' % (url, url)
19 full_dirs = len(os.getcwd().split('/')) + 1
23 def __init__(self, *args, **kw):
24 if not hasattr(self, "_fileBaseName"):
25 self._fileDirName, self._fileBaseName = \
26 os.path.split(os.path.abspath(self._CHEETAH_src))
27 Template.__init__(self, *args, **kw)
28 # remove directories up to "./files"
29 directories = self._fileDirName.split('/')[full_dirs:]
30 dirs_to_root = len(directories)
32 root = "../"*dirs_to_root
36 path = '/'.join(directories) + '/' + \
37 self._fileBaseName.replace(".tmpl", ".html")
38 if path[0] != '/': path = '/' + path
41 def copyright(self, start_year):
42 this_year = time.localtime()[0]
43 if start_year >= this_year:
45 if start_year == this_year - 1:
46 return "%s, %s" % (start_year, this_year)
47 return "%s-%s" % (start_year, this_year)
50 if hasattr(self, "body_html"):
51 return self.body_html()
52 if hasattr(self, "body_text"):
53 return self.text2html()
54 if hasattr(self, "body_rst"):
55 return self.rst2html()
56 if hasattr(self, "body_mkd"):
57 return self.mkd2html()
60 body = re.sub(url_re, _url2href, self.body_text())
62 paragraphs = body.split("\n\n")
66 if PY2 and not isinstance(p, bytes):
68 parts = p.split("\n ")
69 parts[0] = parts[0].strip()
70 new_paras.append('\n</p>\n<p>\n'.join(parts))
73 title = "<h1>%s</h1>\n\n" % self.Title
77 body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
78 return "%s<p>%s</p>" % (title, body)
81 from docutils.core import publish_parts
83 parts = publish_parts(self.body_rst(), writer_name="html")
85 title = parts["title"] or self.Title
87 title = "<h1>%s</h1>" % title
89 subtitle = parts["subtitle"]
91 subtitle = "<h2>%s</h2>" % subtitle
95 for part in (title, subtitle, body):
98 if PY2 and not isinstance(part, bytes):
99 part = part.encode('utf-8')
101 return "\n\n".join(parts)
104 from markdown import markdown
105 return markdown(self.body_mkd(), output_format="html")
107 def find_near_blog_posts(self):
108 if not self.path.startswith("/Russian/blog/"):
109 return None, None, None, None
110 dirs = self.path.split('/')
111 # blog post is ['', 'Russian', 'blog', year, month, day, filename]
112 if len(dirs) != 7: # Not a blog post
113 return None, None, None, None
114 ymd = tuple(dirs[3:6])
115 filename = self._fileBaseName
117 prev_key = prev_blog_post = current_key = current_blog_post = \
118 next_key = next_blog_post = None
120 for key in sorted(blog):
121 for blog_post in sorted(blog[key]):
122 if current_blog_post:
123 prev_key = current_key
124 prev_blog_post = current_blog_post
126 current_key = next_key
127 current_blog_post = next_blog_post
129 next_blog_post = blog_post
130 if current_blog_post and (current_key == ymd) and \
131 (current_blog_post[0] == filename): # Found!
132 return prev_key, prev_blog_post, next_key, next_blog_post
133 return current_key, current_blog_post, None, None
135 def img_thumbnail_800_1024(self, img_name):
137 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
138 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
140 def wikipedia(self, query):
141 return "https://en.wikipedia.org/wiki/%s" % quote_string(
142 query.replace(' ', '_'), ext_safe=',')
144 def wikipedia_ru(self, query):
145 return "https://ru.wikipedia.org/wiki/%s" % quote_string(
146 query.replace(' ', '_'), ext_safe=',')
148 def startpage(self, query):
149 return "https://startpage.com/do/search?q=%s" % quote_string(query)
153 def nigma(self, query):
154 return "http://www.nigma.ru/index.php?s=%s" % quote_string(query)
156 def yandex(self, query):
157 return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" \
158 % quote_string(query, "cp1251")
160 def google(self, query):
161 return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" \
162 % quote_string(query)
164 def google_ru(self, query):
165 return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" \
166 % quote_string(query)
168 def lurkmoar(self, query):
169 return "https://lurkmore.to/%s" % quote_string(
170 query.replace(' ', '_'), ext_safe=',')
173 def quote_string(s, to_encoding="utf-8", ext_safe=''):
174 if isinstance(s, bytes):
175 s = s.decode("utf-8")
177 s = s.encode(to_encoding)
178 return url_quote(s, '/' + ext_safe)