~ubuntu-branches/ubuntu/hardy/python-docutils/hardy

« back to all changes in this revision

Viewing changes to tools/buildhtml.py

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
# Author: David Goodger
4
4
# Contact: goodger@users.sourceforge.net
5
 
# Revision: $Revision: 1.23 $
6
 
# Date: $Date: 2004/03/28 15:39:27 $
 
5
# Revision: $Revision: 2504 $
 
6
# Date: $Date: 2004-07-29 06:36:53 +0200 (Thu, 29 Jul 2004) $
7
7
# Copyright: This module has been placed in the public domain.
8
8
 
9
9
"""
10
10
Generates .html from all the .txt files in a directory.
11
11
 
12
12
Ordinary .txt files are understood to be standalone reStructuredText.
13
 
Files named ``pep-*.txt`` are interpreted as PEPs (either old-style or
14
 
new reStructuredText PEPs).
 
13
Files named ``pep-*.txt`` are interpreted as reStructuredText PEPs.
15
14
"""
16
15
# Once PySource is here, build .html from .py as well.
17
16
 
37
36
 
38
37
 
39
38
usage = '%prog [options] [<directory> ...]'
40
 
description = ('Generates .html from all the .txt files (including PEPs) '
41
 
               'in each <directory> (default is the current directory).')
 
39
description = ('Generates .html from all the reStructuredText .txt files '
 
40
               '(including PEPs) in each <directory> '
 
41
               '(default is the current directory).')
42
42
 
43
43
 
44
44
class SettingsSpec(docutils.SettingsSpec):
72
72
    config_section = 'buildhtml application'
73
73
    config_section_dependencies = ('applications',)
74
74
 
75
 
    
 
75
 
76
76
class OptionParser(frontend.OptionParser):
77
77
 
78
78
    """
108
108
            '': Struct(components=(pep.Reader, rst.Parser, pep_html.Writer,
109
109
                                   SettingsSpec)),
110
110
            '.txt': Struct(components=(rst.Parser, standalone.Reader,
111
 
                                       html4css1.Writer, SettingsSpec)),
 
111
                                       html4css1.Writer, SettingsSpec),
 
112
                           reader_name='standalone',
 
113
                           writer_name='html'),
112
114
            'PEPs': Struct(components=(rst.Parser, pep.Reader,
113
 
                                       pep_html.Writer, SettingsSpec))}
 
115
                                       pep_html.Writer, SettingsSpec),
 
116
                           reader_name='pep',
 
117
                           writer_name='pep_html')}
114
118
        """Publisher-specific settings.  Key '' is for the front-end script
115
119
        itself.  ``self.publishers[''].components`` must contain a superset of
116
120
        all components used by individual publishers."""
129
133
        """
130
134
        for name, publisher in self.publishers.items():
131
135
            option_parser = OptionParser(
132
 
                components=publisher.components,
 
136
                components=publisher.components, read_config_files=1,
133
137
                usage=usage, description=description)
134
138
            publisher.option_parser = option_parser
135
139
            publisher.setting_defaults = option_parser.get_default_values()
136
140
            frontend.make_paths_absolute(publisher.setting_defaults.__dict__,
137
141
                                         option_parser.relative_path_settings)
138
 
        self.config_settings = option_parser.get_standard_config_settings()
 
142
            publisher.config_settings = (
 
143
                option_parser.get_standard_config_settings())
139
144
        self.settings_spec = self.publishers[''].option_parser.parse_args(
140
145
            values=frontend.Values())   # no defaults; just the cmdline opts
141
146
        self.initial_settings = self.get_settings('')
150
155
        """
151
156
        publisher = self.publishers[publisher_name]
152
157
        settings = frontend.Values(publisher.setting_defaults.__dict__)
153
 
        settings.update(self.config_settings, publisher.option_parser)
 
158
        settings.update(publisher.config_settings, publisher.option_parser)
154
159
        if directory:
155
160
            local_config = publisher.option_parser.get_config_file_settings(
156
161
                os.path.join(directory, 'docutils.conf'))
182
187
        if not self.initial_settings.silent:
183
188
            print >>sys.stderr, '/// Processing directory:', directory
184
189
            sys.stderr.flush()
185
 
        peps_found = 0
186
190
        prune = 0
187
191
        for name in names:
188
192
            if name.endswith('.txt'):
189
 
                if name.startswith('pep-'):
190
 
                    peps_found = 1
191
 
                else:
192
 
                    prune = self.process_txt(directory, name)
193
 
                    if prune:
194
 
                        break
195
 
        if peps_found and not prune:
196
 
            self.process_peps(directory)
 
193
                prune = self.process_txt(directory, name)
 
194
                if prune:
 
195
                    break
197
196
        if not recurse:
198
197
            del names[:]
199
198
 
200
199
    def process_txt(self, directory, name):
201
 
        settings = self.get_settings('.txt', directory)
 
200
        if name.startswith('pep-'):
 
201
            publisher = 'PEPs'
 
202
        else:
 
203
            publisher = '.txt'
 
204
        settings = self.get_settings(publisher, directory)
 
205
        pub_struct = self.publishers[publisher]
202
206
        if settings.prune and (directory in settings.prune):
203
207
            return 1
204
208
        settings._source = os.path.normpath(os.path.join(directory, name))
205
209
        settings._destination = settings._source[:-4]+'.html'
206
210
        if not self.initial_settings.silent:
207
 
            print >>sys.stderr, '    ::: Processing .txt:', name
 
211
            print >>sys.stderr, '    ::: Processing:', name
208
212
            sys.stderr.flush()
209
213
        try:
210
214
            core.publish_file(source_path=settings._source,
211
215
                              destination_path=settings._destination,
212
 
                              reader_name='standalone',
 
216
                              reader_name=pub_struct.reader_name,
213
217
                              parser_name='restructuredtext',
214
 
                              writer_name='html',
 
218
                              writer_name=pub_struct.writer_name,
215
219
                              settings=settings)
216
220
        except ApplicationError, error:
217
221
            print >>sys.stderr, ('        Error (%s): %s'
218
222
                                 % (error.__class__.__name__, error))
219
223
 
220
 
    def process_peps(self, directory):
221
 
        # only import PEP module/script if we need it.
222
 
        import pep2html
223
 
 
224
 
        settings = self.get_settings('PEPs', directory)
225
 
        if settings.prune and (directory in settings.prune):
226
 
            return 1
227
 
        old_directory = os.getcwd()
228
 
        os.chdir(directory)
229
 
        if self.initial_settings.silent:
230
 
            argv = ['-q']
231
 
        else:
232
 
            print >>sys.stderr, '    ::: Processing PEPs:'
233
 
            sys.stderr.flush()
234
 
            argv = []
235
 
        pep2html.docutils_settings = settings
236
 
        try:
237
 
            pep2html.main(argv)
238
 
        except Exception, error:
239
 
            print >>sys.stderr, ('        Error (%s): %s'
240
 
                                 % (error.__class__.__name__, error))
241
 
        os.chdir(old_directory)
242
 
 
243
224
 
244
225
if __name__ == "__main__":
245
226
    Builder().run()