Makefile: Correctly exclude htmlize-hack.el from byte-compilation.
[muse-el.git] / contrib / pyblosxom / metadate.py
bloba2a85d0268f89b6ead8498220b1cc7dc36c7d1c2
1 """
2 Purpose:
4 If an entry contains a #postdate metatag, metadate.py makes
5 pyblosxom use that as the entry's date instead of the file's mtime.
7 To use:
9 Place this file in your pyblosxom plugins directory.
11 If you have previously assigned a value to py['load_plugins'] in
12 your config.py file, add this module to that list.
14 Tag an entry with a metadata line like so:
15 #postdate YYYY-MM-DD HH-MM
17 Developer Info:
19 There really isn't a clean way to implement this (as far as I can
20 tell). The cb_filestat callback isn't passed the entry, and none of
21 the other callbacks are passed the mtime, and we really need them
22 both to do this properly.
24 The ugly--but functional--solution to this problem is to go into the
25 file and dig the metadata out directly. That is what I have done
26 here.
28 Since the entries are now being opened twice each, this could result
29 in decently-sized (relatively speaking) performance hit. Consider
30 yourself warned.
32 License:
34 Permission is hereby granted, free of charge, to any person
35 obtaining a copy of this software and associated documentation files
36 (the"Software"), to deal in the Software without restriction,
37 including without limitation the rights to use, copy, modify, merge,
38 publish, distribute, sublicense, and/or sell copies of the Software,
39 and to permit persons to whom the Software is furnished to do so,
40 subject to the following conditions:
42 The above copyright notice and this permission notice shall be
43 included in all copies or substantial portions of the Software.
45 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
49 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
50 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
51 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
52 SOFTWARE.
54 Copyright 2005 Cameron Desautels
55 """
57 import os, re, time
59 __author__ = 'Cameron Desautels <cam@apt2324.com>'
60 __version__ = 'metadate.py, v 0.1'
61 __description__ = 'Implements overriding of mtime with #postdate metadata.'
63 DATEFORMAT = re.compile('([0-9]{4})-([0-1][0-9])-([0-3][0-9]) ([0-2][0-9]):([0-5][0-9])')
65 def cb_filestat(args):
66 stattuple = args['mtime']
68 try:
69 story = open(args['filename']).readlines()
70 except IOError:
71 raise IOError
73 if story:
74 story.pop(0) #throw away the title
76 while story:
77 metamatch = re.match(r'#(\w+)\s+(.*)', story[0])
78 if metamatch:
79 if metamatch.groups()[0] == 'postdate':
80 datematch = DATEFORMAT.match(metamatch.groups()[1].strip())
81 if datematch:
82 # year, month, day, hour, minute
83 mtime = time.mktime((int(datematch.groups()[0]),int(datematch.groups()[1]), \
84 int(datematch.groups()[2]),int(datematch.groups()[3]), \
85 int(datematch.groups()[4]),0,0,0,-1))
87 args['mtime'] = tuple(list(stattuple[:8]) + [mtime] + list(stattuple[9:]))
89 # whether the postdate line was correct or malformed, we found it, so quit
90 break
91 else:
92 # that wasn't the right metadata line, so chuck it and keep going
93 story.pop(0)
94 else:
95 # quit when all metadata has been examined
96 break
98 return args