diff options
| author | goodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2005-12-12 04:12:02 +0000 |
|---|---|---|
| committer | goodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2005-12-12 04:12:02 +0000 |
| commit | 2bc026a7ba733a87edd2019d50ced36171de210a (patch) | |
| tree | 7adfb623c5afdab355309ff5c2e75d4bb87e6542 /docutils | |
| parent | bac1f89c4a022d16ad519580b22f97aada6f9368 (diff) | |
| download | docutils-2bc026a7ba733a87edd2019d50ced36171de210a.tar.gz | |
Added the universal.StripComments transform, the "strip_comments" setting, and the --strip-comments/--leave-comments options.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@4183 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils')
| -rw-r--r-- | docutils/HISTORY.txt | 2 | ||||
| -rw-r--r-- | docutils/docs/dev/todo.txt | 7 | ||||
| -rw-r--r-- | docutils/docs/ref/transforms.txt | 2 | ||||
| -rw-r--r-- | docutils/docs/user/config.txt | 6 | ||||
| -rw-r--r-- | docutils/docutils/frontend.py | 9 | ||||
| -rw-r--r-- | docutils/docutils/readers/__init__.py | 2 | ||||
| -rw-r--r-- | docutils/docutils/transforms/universal.py | 15 | ||||
| -rwxr-xr-x | docutils/test/test_transforms/test_strip_comments.py | 49 |
8 files changed, 84 insertions, 8 deletions
diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index 9f9cd5169..217099e4b 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -155,6 +155,8 @@ Changes Since 0.3.9 several new transforms). - Fixed bug with the "expose_internals" setting and Text nodes (exposed by the "rawsource" internal attribute). + - Added the universal.StripComments transform, implementation of the + "strip_comments" setting. * docutils/transforms/writer_aux.py: Added to project; auxiliary transforms for writers. diff --git a/docutils/docs/dev/todo.txt b/docutils/docs/dev/todo.txt index 1074daf33..dbe28d2ca 100644 --- a/docutils/docs/dev/todo.txt +++ b/docutils/docs/dev/todo.txt @@ -1,4 +1,3 @@ - ====================== Docutils_ To Do List ====================== @@ -116,12 +115,6 @@ General * Change the docutils-update script (in sandbox/infrastructure), to support arbitrary branch snapshots. -* Add a --strip-comments command-line option (& strip_comments runtime - setting), declared in docutils/frontend.py, and implemented as a - transform. This feature isn't specific to rst2html.py, or to the - HTML Writer, or to the reST parser; it applies to Docutils as a - whole. See <http://thread.gmane.org/gmane.text.docutils.devel/3216>. - * Add a generic "container" element, equivalent to "inline", to which a "class" attribute can be attached. Will require a reST directive also. diff --git a/docutils/docs/ref/transforms.txt b/docutils/docs/ref/transforms.txt index 0595175b4..54446f8dd 100644 --- a/docutils/docs/ref/transforms.txt +++ b/docutils/docs/ref/transforms.txt @@ -63,6 +63,8 @@ parts.SectNum "sectnum" (d/p) 710 parts.Contents "contents" (d/p), 720 peps.Contents (t/p) +universal.StripComments Reader (r) 740 + peps.PEPZero peps.Headers (t/p) 760 components.Filter "meta" (d/p) 780 diff --git a/docutils/docs/user/config.txt b/docutils/docs/user/config.txt index c5ec73492..a3b79b510 100644 --- a/docutils/docs/user/config.txt +++ b/docutils/docs/user/config.txt @@ -369,6 +369,12 @@ _`strict_visitor` Default: disabled (None). Option: ``--strict-visitor`` (hidden, for development use only). +_`strip_comments` + Enable the removal of comment elements from the document tree. + + Default: disabled (None). Options: ``--strip-comment``, + ``--leave-comments``. + _`title` The document title as metadata, which does not become part of the document body. It overrides a document-supplied title. For diff --git a/docutils/docutils/frontend.py b/docutils/docutils/frontend.py index 43750c30c..46fcaab89 100644 --- a/docutils/docutils/frontend.py +++ b/docutils/docutils/frontend.py @@ -346,6 +346,15 @@ class OptionParser(optparse.OptionParser, docutils.SettingsSpec): ['--no-section-numbering'], {'action': 'store_false', 'dest': 'sectnum_xform', 'validator': validate_boolean}), + ('Remove comment elements from the document tree ' + '(default: leave them).', + ['--strip-comments'], + {'action': 'store_true', 'validator': validate_boolean}), + ('Leave comment elements in the document tree ' + '(this is the default).', + ['--leave-comments'], + {'action': 'store_false', 'dest': 'strip_comments', + 'validator': validate_boolean}), ('Set verbosity threshold; report system messages at or higher than ' '<level> (by name or number: "info" or "1", warning/2, error/3, ' 'severe/4; also, "none" or "5"). Default is 2 (warning).', diff --git a/docutils/docutils/readers/__init__.py b/docutils/docutils/readers/__init__.py index ae5b3e6bc..b3a7e64eb 100644 --- a/docutils/docutils/readers/__init__.py +++ b/docutils/docutils/readers/__init__.py @@ -33,7 +33,7 @@ class Reader(Component): return Component.get_transforms(self) + [ universal.Decorations, universal.ExposeInternals, - ] + universal.StripComments,] def __init__(self, parser=None, parser_name=None): """ diff --git a/docutils/docutils/transforms/universal.py b/docutils/docutils/transforms/universal.py index a6a0462cb..b31648632 100644 --- a/docutils/docutils/transforms/universal.py +++ b/docutils/docutils/transforms/universal.py @@ -154,3 +154,18 @@ class TestMessages(Transform): for msg in self.document.transform_messages: if not msg.parent: self.document += msg + + +class StripComments(Transform): + + """ + Remove comment elements from the document tree (only if the + ``strip_comments`` setting is enabled). + """ + + default_priority = 740 + + def apply(self): + if self.document.settings.strip_comments: + for node in self.document.traverse(nodes.comment): + node.parent.remove(node) diff --git a/docutils/test/test_transforms/test_strip_comments.py b/docutils/test/test_transforms/test_strip_comments.py new file mode 100755 index 000000000..e3efc1e14 --- /dev/null +++ b/docutils/test/test_transforms/test_strip_comments.py @@ -0,0 +1,49 @@ +#! /usr/bin/env python + +# Author: David Goodger +# Contact: goodger@python.org +# Revision: $Revision$ +# Date: $Date$ +# Copyright: This module has been placed in the public domain. + +""" +Tests for docutils.transforms.universal.StripComments. +""" + +from __init__ import DocutilsTestSupport +from docutils.transforms.universal import StripComments +from docutils.parsers.rst import Parser + + +def suite(): + parser = Parser() + s = DocutilsTestSupport.TransformTestSuite( + parser, suite_settings={'strip_comments': 1}) + s.generateTests(totest) + return s + +totest = {} + +totest['strip_comments'] = ((StripComments,), [ +["""\ +.. this is a comment + +Title +===== + +Paragraph. +""", +"""\ +<document source="test data"> + <section ids="title" names="title"> + <title> + Title + <paragraph> + Paragraph. +"""], +]) + + +if __name__ == '__main__': + import unittest + unittest.main(defaultTest='suite') |
