comparison bitsyblog/utils.py @ 75:6b8ccf6ec819

move restructured text string rendering to its own method
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 24 Sep 2010 19:01:07 -0700
parents c228832db770
children 4df927b0d847
comparison
equal deleted inserted replaced
73:e6055bf127eb 75:6b8ccf6ec819
1 """utlity functions for bitsyblog""" 1 """utlity functions for bitsyblog"""
2 2
3 import cgi
3 import datetime 4 import datetime
4 import os 5 import os
5 import urllib 6 import urllib
6 import urllib2 7 import urllib2
7 8
8 # format to uniquely label blog posts 9 # format to uniquely label blog posts
9 timeformat = ( 'YYYY', 'MM', 'DD', 'HH', 'MM', 'SS' ) 10 timeformat = ( 'YYYY', 'MM', 'DD', 'HH', 'MM', 'SS' )
10 timestamp = '%Y%m%d%H%M%S' # strftime representation 11 timestamp = '%Y%m%d%H%M%S' # strftime representation
12
13 def ReST2html(string):
14 """renders a string with restructured text"""
15
16 settings = { 'report_level': 5 }
17 string = string.strip()
18 try:
19
20 parts = docutils.core.publish_parts(string,
21 writer_name='html',
22 settings_overrides=settings)
23 body = parts['body']
24 except (SystemMessage, UnicodeError), e:
25 lines = [ cgi.escape(i.strip()) for i in string.split('\n') ]
26 body = '<br/>\n'.join(lines)
27 return body
11 28
12 def validate_css(css): 29 def validate_css(css):
13 """use a webservice to determine if the argument is valid css""" 30 """use a webservice to determine if the argument is valid css"""
14 url = 'http://jigsaw.w3.org/css-validator/validator?text=%s' 31 url = 'http://jigsaw.w3.org/css-validator/validator?text=%s'
15 url = url % urllib.quote_plus(css) 32 url = url % urllib.quote_plus(css)