annotate bitsyblog/utils.py @ 84:e5a23f5ea14e

make cooked bodies depend on file mtime
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 17 Nov 2011 13:18:51 -0800
parents 4df927b0d847
children 8f30c2f702bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """utlity functions for bitsyblog"""
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
2
75
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
3 import cgi
0
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
4 import datetime
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
5 import os
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
6 import urllib
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
7 import urllib2
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
8
79
4df927b0d847 fix unicode error in titles (i hope)
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
9 import docutils
4df927b0d847 fix unicode error in titles (i hope)
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
10 import docutils.core
4df927b0d847 fix unicode error in titles (i hope)
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
11
4df927b0d847 fix unicode error in titles (i hope)
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
12 from docutils.utils import SystemMessage
4df927b0d847 fix unicode error in titles (i hope)
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
13
0
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 # format to uniquely label blog posts
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
15 timeformat = ( 'YYYY', 'MM', 'DD', 'HH', 'MM', 'SS' )
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
16 timestamp = '%Y%m%d%H%M%S' # strftime representation
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
17
75
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
18 def ReST2html(string):
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
19 """renders a string with restructured text"""
84
e5a23f5ea14e make cooked bodies depend on file mtime
Jeff Hammel <jhammel@mozilla.com>
parents: 79
diff changeset
20
75
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
21 settings = { 'report_level': 5 }
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
22 string = string.strip()
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
23 try:
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
24 parts = docutils.core.publish_parts(string,
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
25 writer_name='html',
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
26 settings_overrides=settings)
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
27 body = parts['body']
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
28 except (SystemMessage, UnicodeError), e:
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
29 lines = [ cgi.escape(i.strip()) for i in string.split('\n') ]
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
30 body = '<br/>\n'.join(lines)
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
31 return body
6b8ccf6ec819 move restructured text string rendering to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
32
0
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
33 def validate_css(css):
84
e5a23f5ea14e make cooked bodies depend on file mtime
Jeff Hammel <jhammel@mozilla.com>
parents: 79
diff changeset
34 """use a webservice to determine if the argument is valid css"""
0
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
35 url = 'http://jigsaw.w3.org/css-validator/validator?text=%s'
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
36 url = url % urllib.quote_plus(css)
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
37 foo = urllib2.urlopen(url)
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
38 text = foo.read()
5
794dac65c9b6 comment about how brittle the CSS validation is
k0s <k0scist@gmail.com>
parents: 0
diff changeset
39 return not 'We found the following errors' in text # XXX hacky
0
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
40
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
41 def date(datestamp):
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
42 datestamp = os.path.split(datestamp)[-1]
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
43 retval = []
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
44 for i in timeformat:
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
45 retval.append(int(datestamp[:len(i)]))
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
46 datestamp = datestamp[len(i):]
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
47 return datetime.datetime(*retval)
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
48
45
c228832db770 fix blogme
k0s <k0scist@gmail.com>
parents: 5
diff changeset
49 def datestamp(date=None):
84
e5a23f5ea14e make cooked bodies depend on file mtime
Jeff Hammel <jhammel@mozilla.com>
parents: 79
diff changeset
50 if isinstance(date, float):
e5a23f5ea14e make cooked bodies depend on file mtime
Jeff Hammel <jhammel@mozilla.com>
parents: 79
diff changeset
51 date = datetime.datetime.fromtimestamp(date)
45
c228832db770 fix blogme
k0s <k0scist@gmail.com>
parents: 5
diff changeset
52 if date is None:
c228832db770 fix blogme
k0s <k0scist@gmail.com>
parents: 5
diff changeset
53 date = datetime.datetime.now()
0
e3823be6a423 initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
54 return date.strftime(timestamp)