diff options
| author | Georg Brandl <georg@python.org> | 2009-06-04 17:57:58 +0200 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2009-06-04 17:57:58 +0200 |
| commit | 7ed7231c41f290d056686ceccc0410a3e7cf1305 (patch) | |
| tree | 5b9f9f5804820f8f2a7cfd8e9515baea8b0eee62 | |
| parent | b71e52171b5d3d8cb581711dd30ca955be3c0019 (diff) | |
| download | sphinx-7ed7231c41f290d056686ceccc0410a3e7cf1305.tar.gz | |
Added the ``prepend`` and ``append`` options to the
``literalinclude`` directive.
| -rw-r--r-- | CHANGES | 3 | ||||
| -rw-r--r-- | doc/markup/code.rst | 6 | ||||
| -rw-r--r-- | sphinx/directives/code.py | 11 | ||||
| -rw-r--r-- | tests/root/includes.txt | 4 | ||||
| -rw-r--r-- | tests/test_build.py | 4 |
5 files changed, 26 insertions, 2 deletions
@@ -1,6 +1,9 @@ Release 1.0 (in development) ============================ +* Added the ``prepend`` and ``append`` options to the + ``literalinclude`` directive. + * Added the ``latex_docclass`` config value and made the "twoside" documentclass option overridable by "oneside". diff --git a/doc/markup/code.rst b/doc/markup/code.rst index 93cd127b..8c223297 100644 --- a/doc/markup/code.rst +++ b/doc/markup/code.rst @@ -143,11 +143,17 @@ Includes string option, only lines that precede the first lines containing that string are included. + You can prepend and/or append a line to the included code, using the + ``prepend`` and ``append`` option, respectively. This is useful e.g. for + highlighting PHP code that doesn't include the ``<?php``/``?>`` markers. + .. versionadded:: 0.4.3 The ``encoding`` option. .. versionadded:: 0.6 The ``pyobject``, ``lines``, ``start-after`` and ``end-before`` options, as well as support for absolute filenames. + .. versionadded:: 1.0 + The ``prepend`` and ``append`` options. .. rubric:: Footnotes diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 645bc784..2c70ada8 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -87,6 +87,8 @@ class LiteralInclude(Directive): 'lines': directives.unchanged_required, 'start-after': directives.unchanged_required, 'end-before': directives.unchanged_required, + 'prepend': directives.unchanged_required, + 'append': directives.unchanged_required, } def run(self): @@ -144,7 +146,9 @@ class LiteralInclude(Directive): lines = [lines[i] for i in linelist] startafter = self.options.get('start-after') - endbefore = self.options.get('end-before') + endbefore = self.options.get('end-before') + prepend = self.options.get('prepend') + append = self.options.get('append') if startafter is not None or endbefore is not None: use = not startafter res = [] @@ -158,6 +162,11 @@ class LiteralInclude(Directive): res.append(line) lines = res + if prepend: + lines.insert(0, prepend + '\n') + if append: + lines.append(append + '\n') + text = ''.join(lines) retnode = nodes.literal_block(text, text, source=fn) retnode.line = 1 diff --git a/tests/root/includes.txt b/tests/root/includes.txt index 44e33af0..b8a6bee8 100644 --- a/tests/root/includes.txt +++ b/tests/root/includes.txt @@ -37,6 +37,10 @@ Literalinclude options :start-after: coding: utf-8 :end-before: class Foo +.. cssclass:: inc-preappend +.. literalinclude:: literal.inc + :prepend: START CODE + :append: END CODE Testing downloadable files ========================== diff --git a/tests/test_build.py b/tests/test_build.py index 72c821d0..d81e496e 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -43,7 +43,7 @@ ENV_WARNINGS = """\ http://www.python.org/logo.png %(root)s/includes.txt:: (WARNING/2) Encoding 'utf-8-sig' used for reading \ included file u'wrongenc.inc' seems to be wrong, try giving an :encoding: option -%(root)s/includes.txt:56: WARNING: download file not readable: nonexisting.png +%(root)s/includes.txt:60: WARNING: download file not readable: nonexisting.png """ HTML_WARNINGS = ENV_WARNINGS + """\ @@ -134,6 +134,8 @@ if pygments: r'^class Foo:\n pass\nclass Bar:\n$', ".//div[@class='inc-startend highlight-text']/div/pre": ur'^foo = u"Including Unicode characters: üöä"\n$', + ".//div[@class='inc-preappend highlight-text']/div/pre": + r'(?m)^START CODE$', }) HTML_XPATH['subdir/includes.html'].update({ ".//pre/span": 'line 1', |
