1 import os, re, time, urllib
2 from HTMLParser import HTMLParseError
3 from Cheetah.Template import Template
4 from m_lib.net.www.html import HTMLParser as _HTMLParser
7 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/]"
11 return '<a href="%s">%s</a>' % (url, url)
14 full_dirs = len(os.getcwd().split('/')) + 1
16 class phd_pp(Template):
17 def __init__(self, *args, **kw):
18 if not hasattr(self, "_fileBaseName"):
19 self._fileDirName, self._fileBaseName = os.path.split(os.path.abspath(self._CHEETAH_src))
20 Template.__init__(self, *args, **kw)
21 directories = self._fileDirName.split('/')[full_dirs:] # remove directories up to "./files"
22 dirs_to_root = len(directories)
24 root = "../"*dirs_to_root
28 path = '/'.join(directories) + '/' + \
29 self._fileBaseName.replace(".tmpl", ".html")
30 if path[0] <> '/': path = '/' + path
33 def copyright(self, start_year):
34 this_year = time.localtime()[0]
35 if start_year >= this_year:
37 if start_year == this_year - 1:
38 return "%s, %s" % (start_year, this_year)
39 return "%s-%s" % (start_year, this_year)
42 if hasattr(self, "body_html"):
43 body = self.body_html()
44 if hasattr(self, "body_text"):
45 body = self.text2html()
46 if hasattr(self, "body_rst"):
47 body = self.rst2html()
52 body = re.sub(url_re, _url2href, self.body_text())
54 paragraphs = body.split("\n\n")
58 parts = p.split("\n ")
59 parts[0] = parts[0].strip()
60 new_paras.append('\n</p>\n<p>\n'.join(parts))
63 title = "<h1>%s</h1>\n\n" % self.Title
67 body = '\n</p>\n\n<p class="head">\n'.join(new_paras)
68 return "%s<p>%s</p>" % (title, body)
71 from docutils.core import publish_parts
72 from m_lib.defenc import default_encoding as encoding
74 parts = publish_parts(self.body_rst(), writer_name="html")
76 title = parts["title"].encode(encoding) or self.Title
78 title = "<h1>%s</h1>" % title
80 subtitle = parts["subtitle"].encode(encoding)
82 subtitle = "<h2>%s</h2>" % subtitle
84 body = parts["body"].encode(encoding)
85 parts = [part for part in (title, subtitle, body) if part]
86 return "\n\n".join(parts)
88 def get_first_p(self):
92 parser.feed(self.body())
93 except (HTMLParseError, HTMLHeadDone):
98 except (HTMLParseError, HTMLHeadDone):
101 return parser.first_p
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 "http://en.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
111 def wikipedia_ru(self, query):
112 return "http://ru.wikipedia.org/wiki/%s" % quote_string(query.replace(' ', '_'), ext_safe=',')
114 def nigma(self, query):
115 return "http://www.nigma.ru/index.php?s=%s" % quote_string(query)
119 def yandex(self, query):
120 return "http://www.yandex.ru/yandsearch?text=%s&rpt=rad" % quote_string(query, "cp1251")
122 def google(self, query):
123 return "http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
125 def google_ru(self, query):
126 return "http://www.google.ru/search?hl=ru&ie=utf-8&oe=utf-8&q=%s" % quote_string(query)
128 def quote_string(s, to_encoding="utf-8", ext_safe=''):
129 return urllib.quote(unicode(s, "koi8-r").encode(to_encoding), '/' + ext_safe)
132 class HTMLHeadDone(Exception): pass
134 class HTMLParser(_HTMLParser):
135 def __init__(self, charset=None):
136 _HTMLParser.__init__(self)
139 def start_p(self, attrs):
140 self.accumulator = '<p>'
143 self.first_p = self.accumulator + '</p>'