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

Benjamin Mako Hill || Want to submit a patch?