Clean up trailing whitespace.
[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 python-markdown into your pyblosxom plugins dir alongside this
10 plugin. Your plugins dir should look like this:
11
12     plugins/ <-- your pyblosxom plugins dir
13         markdown-plugin.py <-- this file
14         markdown.py <-- the python-markdown command line script
15         markdown/ <-- directory containing python-markdown's files
16         ... <-- (any other pyblosxom plugins)
17
18 Now any posts with filenames ending in `.mkdn` will be passed through
19 python-markdown.
20
21 Copyright (C) Benjamin Mako Hill, 2005
22 Updated for python-markdown 2 by seanh 2009
23
24 This program is free software; you can redistribute it and/or modify
25 it under the terms of the GNU General Public License as published by
26 the Free Software Foundation; either version 2 of the License, or (at
27 your option) any later versi
28
29 This program is distributed in the hope that it will be useful, but
30 WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32 General Public License for more details.
33
34 You should have received a copy of the GNU General Public License
35 along with this program; if not, write to the Free Software
36 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 USA.
38
39 """
40 PREFORMATTER_ID = 'markdown'
41 FILE_EXT = 'mkdn'
42 _version__ = '0.2'
43 __author__ = 'Benjamin Mako Hill <mako@atdot.cc>'
44 __author__ = 'seanh'
45
46 import re,codecs
47 import markdown
48 from Pyblosxom import tools
49
50 md = markdown.Markdown(
51     #safe_mode=True,
52     output_format='html4',
53     extensions=[ 'codehilite', # Requires python-pygments
54                  'extra', # Abbreviations, definition lists, fenced code blocks,
55                           # footnotes, headerid and tables.
56                  #'html_tidy', # Enable this if you have libtidy and uTidylib
57                  #'imagelinks', # Broken?
58                  #'meta',
59                  #'rss',
60                  'toc',
61                  #'wikilinks'
62                ]
63 )
64
65 def cb_entryparser(args):
66     args[FILE_EXT] = readfile
67     return args
68
69 def cb_preformat(args):
70     if args['parser'] == PREFORMATTER_ID:
71         return parse(''.join(args['story']))
72
73 def to_ascii(char):
74     """Return char if char is an ASCII character, '?' otherwise."""
75     if ord(char) < 128:
76         return char
77     else:
78         return '?'
79
80 def parse(story):
81     # Replace any non-ascii characters in the story with '?', so that
82     # python-markdown doesn't crash.
83     ascii = "".join([to_ascii(x) for x in story])
84     # Convert the ASCII text to HTML with python-markdown.
85     html = md.convert(ascii)
86     # Reset python-markdown ready for next time.
87     md.reset()
88     return html
89
90 def readfile(filename, request):
91     entryData = {}
92     lines = codecs.open(filename, mode="r", encoding="utf8").readlines()
93     title = lines.pop(0).strip()
94     while lines and lines[0].startswith("#"):
95         meta = lines.pop(0)
96         meta = meta[1:].strip()
97         meta = meta.split(" ", 1)
98         entryData[meta[0].strip()] = meta[1].strip()
99     entryData['title'] = title
100     entryData['body'] = parse(''.join(lines))
101     # Call the postformat callbacks
102     tools.run_callback('postformat',
103             {'request': request,
104              'entry_data': entryData})
105     return entryData

Benjamin Mako Hill || Want to submit a patch?