comparison pylons_app/lib/helpers.py @ 98:01d0f363f36d

added pygments webhelper
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 25 Apr 2010 01:18:38 +0200
parents be0096a02772
children 2dc0c8e4f384
comparison
equal deleted inserted replaced
97:be0096a02772 98:01d0f363f36d
4 available to Controllers. This module is available to both as 'h'. 4 available to Controllers. This module is available to both as 'h'.
5 """ 5 """
6 from pylons import url 6 from pylons import url
7 from pylons.i18n.translation import _, ungettext 7 from pylons.i18n.translation import _, ungettext
8 from webhelpers.html import (literal, HTML, escape) 8 from webhelpers.html import (literal, HTML, escape)
9 from webhelpers.html.builder import make_tag
9 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate 10 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate
10 , mail_to, strip_links, strip_tags, tag_re) 11 , mail_to, strip_links, strip_tags, tag_re)
11 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes, 12 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes,
12 end_form, file, form, hidden, image, 13 end_form, file, form, hidden, image,
13 javascript_link, link_to, link_to_if, 14 javascript_link, link_to, link_to_if,
19 convert_misc_entities, lchop, plural, rchop, 20 convert_misc_entities, lchop, plural, rchop,
20 remove_formatting, replace_whitespace, urlify) 21 remove_formatting, replace_whitespace, urlify)
21 22
22 from webhelpers.pylonslib import Flash as _Flash 23 from webhelpers.pylonslib import Flash as _Flash
23 from webhelpers.pylonslib.secure_form import secure_form 24 from webhelpers.pylonslib.secure_form import secure_form
25
26 from pygments import highlight
27 from pygments.formatters import HtmlFormatter
28 from pygments.lexers import guess_lexer
29 from pygments.lexers import get_lexer_by_name
24 30
25 #Custom helper here :) 31 #Custom helper here :)
26 class _Link(object): 32 class _Link(object):
27 ''' 33 '''
28 Make a url based on label and url with help of url_for 34 Make a url based on label and url with help of url_for
42 def __call__(self, field_name, form_errors): 48 def __call__(self, field_name, form_errors):
43 tmpl = """<span class="error_msg">%s</span>""" 49 tmpl = """<span class="error_msg">%s</span>"""
44 if form_errors and form_errors.has_key(field_name): 50 if form_errors and form_errors.has_key(field_name):
45 return literal(tmpl % form_errors.get(field_name)) 51 return literal(tmpl % form_errors.get(field_name))
46 52
47 class _FileSizeFormat(): 53 class _FileSizeFormat(object):
48 """ 54 """
49 Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 55 Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
50 102 bytes, etc). 56 102 bytes, etc).
51 """ 57 """
52 def __call__(self, bytes): 58 def __call__(self, bytes):
62 if bytes < 1024 * 1024 * 1024: 68 if bytes < 1024 * 1024 * 1024:
63 return _("%.1f MB") % (bytes / (1024 * 1024)) 69 return _("%.1f MB") % (bytes / (1024 * 1024))
64 return _("%.1f GB") % (bytes / (1024 * 1024 * 1024)) 70 return _("%.1f GB") % (bytes / (1024 * 1024 * 1024))
65 71
66 72
73
74 def pygmentize(code, **kwargs):
75 '''
76 Filter for chunks of html to replace code tags with pygmented code
77 '''
78 return literal(highlight(code, guess_lexer(code), HtmlFormatter(**kwargs)))
79
80
81
67 filesizeformat = _FileSizeFormat() 82 filesizeformat = _FileSizeFormat()
68 link = _Link() 83 link = _Link()
69 flash = _Flash() 84 flash = _Flash()
70 get_error = _GetError() 85 get_error = _GetError()