Release 0.5: set version number to 0.6
[docutils.git] / docutils / readers / __init__.py
blob74db893f94da7cc205881dbc0ffc75b4928a363e
1 # $Id$
2 # Authors: David Goodger <goodger@python.org>; Ueli Schlaepfer
3 # Copyright: This module has been placed in the public domain.
5 """
6 This package contains Docutils Reader modules.
7 """
9 __docformat__ = 'reStructuredText'
12 from docutils import utils, parsers, Component
13 from docutils.transforms import universal
16 class Reader(Component):
18 """
19 Abstract base class for docutils Readers.
21 Each reader module or package must export a subclass also called 'Reader'.
23 The two steps of a Reader's responsibility are `scan()` and
24 `parse()`. Call `read()` to process a document.
25 """
27 component_type = 'reader'
28 config_section = 'readers'
30 def get_transforms(self):
31 return Component.get_transforms(self) + [
32 universal.Decorations,
33 universal.ExposeInternals,
34 universal.StripComments,]
36 def __init__(self, parser=None, parser_name=None):
37 """
38 Initialize the Reader instance.
40 Several instance attributes are defined with dummy initial values.
41 Subclasses may use these attributes as they wish.
42 """
44 self.parser = parser
45 """A `parsers.Parser` instance shared by all doctrees. May be left
46 unspecified if the document source determines the parser."""
48 if parser is None and parser_name:
49 self.set_parser(parser_name)
51 self.source = None
52 """`docutils.io` IO object, source of input data."""
54 self.input = None
55 """Raw text input; either a single string or, for more complex cases,
56 a collection of strings."""
58 def set_parser(self, parser_name):
59 """Set `self.parser` by name."""
60 parser_class = parsers.get_parser_class(parser_name)
61 self.parser = parser_class()
63 def read(self, source, parser, settings):
64 self.source = source
65 if not self.parser:
66 self.parser = parser
67 self.settings = settings
68 self.input = self.source.read()
69 self.parse()
70 return self.document
72 def parse(self):
73 """Parse `self.input` into a document tree."""
74 self.document = document = self.new_document()
75 self.parser.parse(self.input, document)
76 document.current_source = document.current_line = None
78 def new_document(self):
79 """Create and return a new empty document tree (root node)."""
80 document = utils.new_document(self.source.source_path, self.settings)
81 return document
84 class ReReader(Reader):
86 """
87 A reader which rereads an existing document tree (e.g. a
88 deserializer).
90 Often used in conjunction with `writers.UnfilteredWriter`.
91 """
93 def get_transforms(self):
94 # Do not add any transforms. They have already been applied
95 # by the reader which originally created the document.
96 return Component.get_transforms(self)
99 _reader_aliases = {}
101 def get_reader_class(reader_name):
102 """Return the Reader class from the `reader_name` module."""
103 reader_name = reader_name.lower()
104 if _reader_aliases.has_key(reader_name):
105 reader_name = _reader_aliases[reader_name]
106 module = __import__(reader_name, globals(), locals())
107 return module.Reader