diff options
Diffstat (limited to 'sphinx/builders/changes.py')
-rw-r--r-- | sphinx/builders/changes.py | 47 |
1 files changed, 19 insertions, 28 deletions
diff --git a/sphinx/builders/changes.py b/sphinx/builders/changes.py index c077b7dd2..1bccb67d9 100644 --- a/sphinx/builders/changes.py +++ b/sphinx/builders/changes.py @@ -15,12 +15,12 @@ from os import path from six import iteritems from sphinx import package_dir -from sphinx.util import copy_static_entry from sphinx.locale import _ from sphinx.theming import Theme from sphinx.builders import Builder from sphinx.util.osutil import ensuredir, os_path from sphinx.util.console import bold +from sphinx.util.fileutil import copy_asset_file from sphinx.util.pycompat import htmlescape @@ -101,16 +101,10 @@ class ChangesBuilder(Builder): 'show_copyright': self.config.html_show_copyright, 'show_sphinx': self.config.html_show_sphinx, } - f = codecs.open(path.join(self.outdir, 'index.html'), 'w', 'utf8') - try: + with codecs.open(path.join(self.outdir, 'index.html'), 'w', 'utf8') as f: f.write(self.templates.render('changes/frameset.html', ctx)) - finally: - f.close() - f = codecs.open(path.join(self.outdir, 'changes.html'), 'w', 'utf8') - try: + with codecs.open(path.join(self.outdir, 'changes.html'), 'w', 'utf8') as f: f.write(self.templates.render('changes/versionchanges.html', ctx)) - finally: - f.close() hltext = ['.. versionadded:: %s' % version, '.. versionchanged:: %s' % version, @@ -126,35 +120,28 @@ class ChangesBuilder(Builder): self.info(bold('copying source files...')) for docname in self.env.all_docs: - f = codecs.open(self.env.doc2path(docname), 'r', - self.env.config.source_encoding) - try: - lines = f.readlines() - except UnicodeDecodeError: - self.warn('could not read %r for changelog creation' % docname) - continue - finally: - f.close() + with codecs.open(self.env.doc2path(docname), 'r', + self.env.config.source_encoding) as f: + try: + lines = f.readlines() + except UnicodeDecodeError: + self.warn('could not read %r for changelog creation' % docname) + continue targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html' ensuredir(path.dirname(targetfn)) - f = codecs.open(targetfn, 'w', 'utf-8') - try: + with codecs.open(targetfn, 'w', 'utf-8') as f: text = ''.join(hl(i+1, line) for (i, line) in enumerate(lines)) ctx = { 'filename': self.env.doc2path(docname, None), 'text': text } f.write(self.templates.render('changes/rstsource.html', ctx)) - finally: - f.close() themectx = dict(('theme_' + key, val) for (key, val) in iteritems(self.theme.get_options({}))) - copy_static_entry(path.join(package_dir, 'themes', 'default', - 'static', 'default.css_t'), - self.outdir, self, themectx) - copy_static_entry(path.join(package_dir, 'themes', 'basic', - 'static', 'basic.css'), - self.outdir, self) + copy_asset_file(path.join(package_dir, 'themes', 'default', 'static', 'default.css_t'), + self.outdir, context=themectx, renderer=self.templates) + copy_asset_file(path.join(package_dir, 'themes', 'basic', 'static', 'basic.css'), + self.outdir) def hl(self, text, version): text = htmlescape(text) @@ -165,3 +152,7 @@ class ChangesBuilder(Builder): def finish(self): pass + + +def setup(app): + app.add_builder(ChangesBuilder) |