2 from urllib.parse import quote as url_quote
4 from Cheetah.Template import Template
5 from Cheetah.compat import PY2
7 from blog_db import load_blog
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/]"
15 return '<a href="%s">%s</a>' % (url, url)
18 full_dirs = len(os.getcwd().split('/')) + 1
22 def __init__(self, *args, **kw):
23 if not hasattr(self, "_fileBaseName"):
24 self._fileDirName, self._fileBaseName = \
25 os.path.split(os.path.abspath(self._CHEETAH_src))
26 Template.__init__(self, *args, **kw)
27 # remove directories up to "./files"
28 directories = self._fileDirName.split('/')[full_dirs:]
29 dirs_to_root = len(directories)
31 root = "../"*dirs_to_root
35 path = '/'.join(directories) + '/' + \
36 self._fileBaseName.replace(".tmpl", ".html")
37 if path[0] != '/': path = '/' + path
40 def copyright(self, start_year):
41 this_year = time.localtime()[0]
42 if start_year >= this_year:
44 if start_year == this_year - 1:
45 return "%s, %s" % (start_year, this_year)
46 return "%s-%s" % (start_year, this_year)
49 if hasattr(self, "body_html"):
50 return self.body_html()
51 if hasattr(self, "body_text"):
52 return self.text2html()
53 if hasattr(self, "body_rst"):
54 return self.rst2html()
55 if hasattr(self, "body_mkd"):
56 return self.mkd2html()
59 body = re.sub(url_re, _url2href, self.body_text())
61 paragraphs = body.split("\n\n")
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):
96 return "\n\n".join(parts)
99 from markdown import markdown
100 return markdown(self.body_mkd(), output_format="html")
102 def find_near_blog_posts(self):
103 if not self.path.startswith("/Russian/blog/"):
104 return None, None, None, None
105 dirs = self.path.split('/')
106 # blog post is ['', 'Russian', 'blog', year, month, day, filename]
107 if len(dirs) != 7: # Not a blog post
108 return None, None, None, None
109 ymd = tuple(dirs[3:6])
110 filename = self._fileBaseName
112 prev_key = prev_blog_post = current_key = current_blog_post = \
113 next_key = next_blog_post = None
115 for key in sorted(blog):
116 for blog_post in sorted(blog[key]):
117 if current_blog_post:
118 prev_key = current_key
119 prev_blog_post = current_blog_post
121 current_key = next_key
122 current_blog_post = next_blog_post
124 next_blog_post = blog_post
125 if current_blog_post and (current_key == ymd) and \
126 (current_blog_post[0] == filename): # Found!
127 return prev_key, prev_blog_post, next_key, next_blog_post
128 return current_key, current_blog_post, None, None
130 def img_thumbnail_800_1024(self, img_name):
132 <img src="%(img_name)s-thumbnail.jpg" alt="%(img_name)s-thumbnail.jpg" /><br />
133 <a href="%(img_name)s-800x600.jpg">800x600</a>, <a href="%(img_name)s-1024x800.jpg">1024x800</a>""" % {"img_name": img_name}
135 def wikipedia(self, query):
136 return "https://en.wikipedia.org/wiki/%s" % quote_string(
137 query.replace(' ', '_'), ext_safe=',')
139 def wikipedia_ru(self, query):
140 return "https://ru.wikipedia.org/wiki/%s" % quote_string(
141 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" \
153 % quote_string(query, "cp1251")
155 def google(self, query):
156 return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" \
157 % quote_string(query)
159 def google_ru(self, query):
160 return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" \
161 % quote_string(query)
163 def lurkmoar(self, query):
164 return "http://lurklurk.com/%s" % quote_string(
165 query.replace(' ', '_'), ext_safe=',')
168 def quote_string(s, to_encoding="utf-8", ext_safe=''):
169 return url_quote(s, '/' + ext_safe)