ed01d66aa277089c02b722fe6c7a0dfc10bdb361
[pyblosxom-markdown2] / markdown-plugin.py
1 #!/usr/bin/env python
2 """
3 markdown-plugin.py -- A Python Markdown v2.x plugin for PyBlosxom.
4
5 This plugin requires Python Markdown v2.x, which you can download from:
6
7         http://www.freewisdom.org/projects/python-markdown/
8
9 Extract the 'markdown' directory from the Python Markdown tarball (the
10 directory containing __init__.py, not the Markdown-2.x.y directory)  into your
11 pyblosxom plugins dir alongside this plugin. Your plugins dir should look like
12 this:
13
14         plugins/ <-- your pyblosxom plugins dir
15                 markdown-plugin.py <-- this file
16                 markdown/ <-- the Python Markdown module
17                 ... <-- (any other pyblosxom plugins)
18
19 Now any posts with filenames ending in one of the FILENAME_EXTENSIONS defined
20 below will be passed through python-markdown.
21
22 Copyright (C) Benjamin Mako Hill, 2005
23 Updated for python-markdown 2 by seanh 2009
24
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2 of the License, or (at
28 your option) any later versi
29
30 This program is distributed in the hope that it will be useful, but
31 WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
33 General Public License for more details.
34
35 You should have received a copy of the GNU General Public License
36 along with this program; if not, write to the Free Software
37 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 USA.
39
40 """
41 PREFORMATTER_ID = 'markdown'
42 FILENAME_EXTENSIONS = ('txt','text','mkdn','markdown','md','mdown','markdn','mkd')
43 _version__ = '0.2'
44 __author__ = 'Benjamin Mako Hill <mako@atdot.cc>'
45 __author__ = 'seanh <snhmnd@gmail.com>'
46
47 import re,codecs
48 import markdown
49 from Pyblosxom import tools
50
51 md = markdown.Markdown(
52         #safe_mode=True,
53         output_format='html4',
54         extensions=[ #'codehilite', # Requires python-pygments
55                                 'extra',
56                                  #'html_tidy', # Requires libtidy and uTidylib
57                                  #'imagelinks', # Broken?
58                                  #'meta',
59                                  #'rss',
60                                  #'toc',
61                                  #'wikilinks'
62                            ]
63 )
64
65 def cb_entryparser(args):
66         for FILENAME_EXTENSION in FILENAME_EXTENSIONS:
67                 args[FILENAME_EXTENSION] = readfile
68         return args
69
70 def cb_preformat(args):
71         if args['parser'] == PREFORMATTER_ID:
72                 return parse(''.join(args['story']))
73
74 def parse(story):
75         # Convert the ASCII text to HTML with python-markdown.
76         html = md.convert(story)
77         # Reset python-markdown ready for next time.
78         md.reset()
79         return html
80
81 def readfile(filename, request):
82         entryData = {}
83         lines = codecs.open(filename, mode="r", encoding="utf8").readlines()
84         title = lines.pop(0).strip()
85         while lines and lines[0].startswith("#"):
86                 meta = lines.pop(0)
87                 meta = meta[1:].strip()
88                 meta = meta.split(" ", 1)
89                 entryData[meta[0].strip()] = meta[1].strip()
90         entryData['title'] = title
91         entryData['body'] = parse(''.join(lines))
92         # Call the postformat callbacks
93         tools.run_callback('postformat',
94                         {'request': request,
95                          'entry_data': entryData})
96         return entryData

Benjamin Mako Hill || Want to submit a patch?