|
@@ -17,68 +17,6 @@ Include Pygments highlighted code with reStructuredText
|
|
|
Use this plugin to make writing coding tutorials easier! You can
|
|
|
maintain the example source files separately from the actual article.
|
|
|
|
|
|
-
|
|
|
-Based heavily on ``docutils.parsers.rst.directives.Include``. Include
|
|
|
-a file and output as a code block formatted with pelican's Pygments
|
|
|
-directive.
|
|
|
-
|
|
|
-Note that this is broken with the Docutils 0.10 release, there's a
|
|
|
-circular import. It was fixed in trunk:
|
|
|
-http://sourceforge.net/p/docutils/bugs/214/
|
|
|
-
|
|
|
-Directives
|
|
|
-----------
|
|
|
-
|
|
|
-.. code:: rst
|
|
|
-
|
|
|
- .. code-include:: incfile.py
|
|
|
- :lexer: string, name of the Pygments lexer to use, default 'text'
|
|
|
- :encoding: string, encoding with which to open the file
|
|
|
- :tab-width: integer, hard tabs are replaced with `tab-width` spaces
|
|
|
- :start-line: integer, starting line to begin reading include file
|
|
|
- :end-line: integer, last line from include file to display
|
|
|
-
|
|
|
-``start-line``, and ``end-line`` have the same meaning as in the
|
|
|
-docutils ``include`` directive, that is, they index from zero.
|
|
|
-
|
|
|
-Example
|
|
|
--------
|
|
|
-
|
|
|
-./incfile.py:
|
|
|
-
|
|
|
-.. code:: python
|
|
|
-
|
|
|
- # These two comment lines will not
|
|
|
- # be included in the output
|
|
|
- import random
|
|
|
-
|
|
|
- insults = ['I fart in your general direction',
|
|
|
- 'your mother was a hampster',
|
|
|
- 'your father smelt of elderberries']
|
|
|
-
|
|
|
- def insult():
|
|
|
- print random.choice(insults)
|
|
|
- # This comment line will be included
|
|
|
- # ...but this one won't
|
|
|
-
|
|
|
-./yourfile.rst:
|
|
|
-
|
|
|
-.. code:: rst
|
|
|
-
|
|
|
- How to Insult the English
|
|
|
- =========================
|
|
|
-
|
|
|
- :author: Pierre Devereaux
|
|
|
-
|
|
|
- A function to help insult those silly English knnnnnnniggets:
|
|
|
-
|
|
|
- .. code-include:: incfile.py
|
|
|
- :lexer: python
|
|
|
- :encoding: utf-8
|
|
|
- :tab-width: 4
|
|
|
- :start-line: 3
|
|
|
- :end-line: 11
|
|
|
-
|
|
|
"""
|
|
|
|
|
|
class CodeInclude(Directive):
|
|
@@ -123,13 +61,13 @@ class CodeInclude(Directive):
|
|
|
include_file = io.FileInput(source_path=path,
|
|
|
encoding=encoding,
|
|
|
error_handler=e_handler)
|
|
|
- except UnicodeEncodeError, error:
|
|
|
- raise self.severe(u'Problems with "%s" directive path:\n'
|
|
|
+ except UnicodeEncodeError as error:
|
|
|
+ raise self.severe('Problems with "%s" directive path:\n'
|
|
|
'Cannot encode input file path "%s" '
|
|
|
'(wrong locale?).' %
|
|
|
(self.name, SafeString(path)))
|
|
|
- except IOError, error:
|
|
|
- raise self.severe(u'Problems with "%s" directive path:\n%s.' %
|
|
|
+ except IOError as error:
|
|
|
+ raise self.severe('Problems with "%s" directive path:\n%s.' %
|
|
|
(self.name, ErrorString(error)))
|
|
|
startline = self.options.get('start-line', None)
|
|
|
endline = self.options.get('end-line', None)
|
|
@@ -139,8 +77,8 @@ class CodeInclude(Directive):
|
|
|
rawtext = ''.join(lines[startline:endline])
|
|
|
else:
|
|
|
rawtext = include_file.read()
|
|
|
- except UnicodeError, error:
|
|
|
- raise self.severe(u'Problem with "%s" directive:\n%s' %
|
|
|
+ except UnicodeError as error:
|
|
|
+ raise self.severe('Problem with "%s" directive:\n%s' %
|
|
|
(self.name, ErrorString(error)))
|
|
|
|
|
|
include_lines = statemachine.string2lines(rawtext, tab_width,
|