diff options
| author | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2005-08-17 13:59:12 +0000 |
|---|---|---|
| committer | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2005-08-17 13:59:12 +0000 |
| commit | a4bc0d09f3d1f633a5780976d1f913e083c9564a (patch) | |
| tree | 1777475e88463b08438f0719be97cc087554eadd /docutils/readers/__init__.py | |
| parent | 60c69f8cf97125135948a3d35b7698ee80921137 (diff) | |
| parent | 2818db9a234cc37008c5f2bb0e821f9b1fa92de2 (diff) | |
| download | docutils-0.3.9.tar.gz | |
re-added docutils-0.3.9 tag one level deeper (without web/ and sandboxes/)docutils-0.3.9
git-svn-id: http://svn.code.sf.net/p/docutils/code/tags/docutils-0.3.9@3815 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/readers/__init__.py')
| -rw-r--r-- | docutils/readers/__init__.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/docutils/readers/__init__.py b/docutils/readers/__init__.py new file mode 100644 index 000000000..3ff069a28 --- /dev/null +++ b/docutils/readers/__init__.py @@ -0,0 +1,89 @@ +# Authors: David Goodger; Ueli Schlaepfer +# Contact: goodger@users.sourceforge.net +# Revision: $Revision$ +# Date: $Date$ +# Copyright: This module has been placed in the public domain. + +""" +This package contains Docutils Reader modules. +""" + +__docformat__ = 'reStructuredText' + + +import sys +from docutils import utils, parsers, Component +from docutils.transforms import universal + + +class Reader(Component): + + """ + Abstract base class for docutils Readers. + + Each reader module or package must export a subclass also called 'Reader'. + + The three steps of a Reader's responsibility are defined: `scan()`, + `parse()`, and `transform()`. Call `read()` to process a document. + """ + + component_type = 'reader' + config_section = 'readers' + + def __init__(self, parser=None, parser_name='restructuredtext'): + """ + Initialize the Reader instance. + + Several instance attributes are defined with dummy initial values. + Subclasses may use these attributes as they wish. + """ + + self.parser = parser + """A `parsers.Parser` instance shared by all doctrees. May be left + unspecified if the document source determines the parser.""" + + if parser is None and parser_name: + self.set_parser(parser_name) + + self.source = None + """`docutils.io` IO object, source of input data.""" + + self.input = None + """Raw text input; either a single string or, for more complex cases, + a collection of strings.""" + + def set_parser(self, parser_name): + """Set `self.parser` by name.""" + parser_class = parsers.get_parser_class(parser_name) + self.parser = parser_class() + + def read(self, source, parser, settings): + self.source = source + if not self.parser: + self.parser = parser + self.settings = settings + self.input = self.source.read() + self.parse() + return self.document + + def parse(self): + """Parse `self.input` into a document tree.""" + self.document = document = self.new_document() + self.parser.parse(self.input, document) + document.current_source = document.current_line = None + + def new_document(self): + """Create and return a new empty document tree (root node).""" + document = utils.new_document(self.source.source_path, self.settings) + return document + + +_reader_aliases = {} + +def get_reader_class(reader_name): + """Return the Reader class from the `reader_name` module.""" + reader_name = reader_name.lower() + if _reader_aliases.has_key(reader_name): + reader_name = _reader_aliases[reader_name] + module = __import__(reader_name, globals(), locals()) + return module.Reader |
