root / pycommunity / pycommunity / __init__.py

Revision 77:0cce3971aff9, 1.6 kB (checked in by Tarek Ziad?? <tarek@…>, 15 months ago)

added pygments support

Line 
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3#
4# Copyright (c) 2006 Tarek Ziadé <tarek@ziade.org>
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19# $Id: $
20""" Setup pygment extension for reST
21"""
22from docutils import nodes
23from docutils.parsers.rst import directives
24from pygments import highlight
25from pygments.lexers import get_lexer_by_name
26from pygments.formatters import HtmlFormatter
27
28pygments_formatter = HtmlFormatter()
29
30def pygments_directive(name, arguments, options, content, lineno,
31                       content_offset, block_text, state, state_machine):
32    try:
33        lexer = get_lexer_by_name(arguments[0])
34    except ValueError:
35        # no lexer found - use the text one instead of an exception
36        lexer = get_lexer_by_name('text')
37    parsed = highlight(u'\n'.join(content), lexer, pygments_formatter)
38    return [nodes.raw('', parsed, format='html')]
39pygments_directive.arguments = (1, 0, 1)
40pygments_directive.content = 1
41directives.register_directive('sourcecode', pygments_directive)
Note: See TracBrowser for help on using the browser.