source_suffix can now be a list.

This commit is contained in:
Georg Brandl 2015-02-08 19:18:38 +01:00
parent 4b396d4232
commit bf3bdcc7f5
12 changed files with 51 additions and 29 deletions

View File

@ -15,6 +15,8 @@ Features added
as an iterable of additional docnames that need to be rewritten.
* Add ``todo_include_todos`` config option to quickstart conf file, handled as
described in documentation.
* The :confval:`source_suffix` config value can now be a list of multiple
suffixes.
Bugs fixed
----------

View File

@ -78,8 +78,11 @@ General configuration
.. confval:: source_suffix
The file name extension of source files. Only files with this suffix will be
read as sources. Default is ``'.rst'``.
The file name extension, or list of extensions, of source files. Only files
with this suffix will be read as sources. Default is ``'.rst'``.
.. versionchanged:: 1.3
Can now be a list of extensions.
.. confval:: source_encoding

View File

@ -19,6 +19,7 @@ except ImportError:
multiprocessing = threading = None
from docutils import nodes
from six import string_types
from sphinx.util import i18n, path_stabilize
from sphinx.util.osutil import SEP, relative_uri, find_catalog
@ -205,20 +206,23 @@ class Builder(object):
# relative to the source directory and without source_suffix.
dirlen = len(self.srcdir) + 1
to_write = []
suffix = self.config.source_suffix
suffixes = tuple(self.config.source_suffix)
for filename in filenames:
filename = path.normpath(path.abspath(filename))
if not filename.startswith(self.srcdir):
self.warn('file %r given on command line is not under the '
'source directory, ignoring' % filename)
continue
if not (path.isfile(filename) or path.isfile(filename + suffix)):
if not (path.isfile(filename) or
any(path.isfile(filename + suffix) for suffix in suffixes)):
self.warn('file %r given on command line does not exist, '
'ignoring' % filename)
continue
filename = filename[dirlen:]
if filename.endswith(suffix):
filename = filename[:-len(suffix)]
for suffix in suffixes:
if filename.endswith(suffix):
filename = filename[:-len(suffix)]
break
filename = filename.replace(path.sep, SEP)
to_write.append(filename)
self.build(to_write, method='specific',

View File

@ -51,7 +51,7 @@ class Config(object):
locale_dirs = ([], 'env'),
master_doc = ('contents', 'env'),
source_suffix = ('.rst', 'env'),
source_suffix = (['.rst'], 'env'),
source_encoding = ('utf-8-sig', 'env'),
exclude_patterns = ([], 'env'),
default_role = (None, 'env'),
@ -321,6 +321,8 @@ class Config(object):
for name in config:
if name in self.values:
self.__dict__[name] = config[name]
if isinstance(self.source_suffix, string_types):
self.source_suffix = [self.source_suffix]
def __getattr__(self, name):
if name.startswith('_'):

View File

@ -7,6 +7,7 @@
:license: BSD, see LICENSE for details.
"""
from six import string_types
from six.moves import range
from docutils import nodes
from docutils.parsers.rst import Directive, directives
@ -48,7 +49,7 @@ class TocTree(Directive):
def run(self):
env = self.state.document.settings.env
suffix = env.config.source_suffix
suffixes = env.config.source_suffix
glob = 'glob' in self.options
ret = []
@ -84,8 +85,10 @@ class TocTree(Directive):
ref = docname = entry
title = None
# remove suffixes (backwards compatibility)
if docname.endswith(suffix):
docname = docname[:-len(suffix)]
for suffix in suffixes:
if docname.endswith(suffix):
docname = docname[:-len(suffix)]
break
# absolutize filenames
docname = docname_join(env.docname, docname)
if url_re.match(ref) or ref == 'self':

View File

@ -23,7 +23,7 @@ from os import path
from glob import glob
from itertools import groupby
from six import iteritems, itervalues, text_type, class_types
from six import iteritems, itervalues, text_type, class_types, string_types
from six.moves import cPickle as pickle, zip
from docutils import nodes
from docutils.io import FileInput, NullOutput
@ -389,7 +389,15 @@ class BuildEnvironment:
If *suffix* is not None, add it instead of config.source_suffix.
"""
docname = docname.replace(SEP, path.sep)
suffix = suffix or self.config.source_suffix
if suffix is None:
for candidate_suffix in self.config.source_suffix:
if path.isfile(path.join(self.srcdir, docname) +
candidate_suffix):
suffix = candidate_suffix
break
else:
# document does not exist
suffix = self.config.source_suffix[0]
if base is True:
return path.join(self.srcdir, docname) + suffix
elif base is None:

View File

@ -60,7 +60,7 @@ import inspect
import posixpath
from types import ModuleType
from six import text_type
from six import text_type, string_types
from docutils.parsers.rst import directives
from docutils.statemachine import ViewList
from docutils import nodes
@ -534,11 +534,9 @@ def autolink_role(typ, rawtext, etext, lineno, inliner,
def process_generate_options(app):
genfiles = app.config.autosummary_generate
ext = app.config.source_suffix
if genfiles and not hasattr(genfiles, '__len__'):
env = app.builder.env
genfiles = [x + ext for x in env.found_docs
genfiles = [env.doc2path(x, base=None) for x in env.found_docs
if os.path.isfile(env.doc2path(x))]
if not genfiles:
@ -546,6 +544,7 @@ def process_generate_options(app):
from sphinx.ext.autosummary.generate import generate_autosummary_docs
ext = app.config.source_suffix[0]
genfiles = [genfile + (not genfile.endswith(ext) and ext or '')
for genfile in genfiles]

View File

@ -105,8 +105,8 @@ extensions = [%(extensions)s]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['%(dot)stemplates']
# The suffix of source filenames.
source_suffix = '%(suffix)s'
# The suffix(es) of source filenames.
source_suffix = ['%(suffix)s']
# The encoding of source files.
#source_encoding = 'utf-8-sig'

View File

@ -21,7 +21,7 @@ from os import path
from codecs import open, BOM_UTF8
from collections import deque
from six import iteritems, text_type, binary_type
from six import iteritems, text_type, binary_type, string_types
from six.moves import range
import docutils
from docutils.utils import relative_path
@ -87,17 +87,18 @@ def get_matching_files(dirname, exclude_matchers=()):
yield filename
def get_matching_docs(dirname, suffix, exclude_matchers=()):
"""Get all file names (without suffix) matching a suffix in a directory,
def get_matching_docs(dirname, suffixes, exclude_matchers=()):
"""Get all file names (without suffixes) matching a suffix in a directory,
recursively.
Exclude files and dirs matching a pattern in *exclude_patterns*.
"""
suffixpattern = '*' + suffix
suffixpatterns = ['*' + s for s in suffixes]
for filename in get_matching_files(dirname, exclude_matchers):
if not fnmatch.fnmatch(filename, suffixpattern):
continue
yield filename[:-len(suffix)]
for suffixpattern in suffixpatterns:
if fnmatch.fnmatch(filename, suffixpattern):
yield filename[:-len(suffixpattern)+1]
break
class FilenameUniqDict(dict):

View File

@ -12,10 +12,10 @@ jsmath_path = 'dummy.js'
templates_path = ['_templates']
master_doc = 'contents'
source_suffix = '.txt'
source_suffix = ['.txt', '.add']
project = 'Sphinx <Tests>'
copyright = '2010-2014, Georg Brandl & Team'
copyright = '2010-2015, Georg Brandl & Team'
# If this is changed, remember to update the versionchanges!
version = '0.6'
release = '0.6alpha1'

View File

@ -144,7 +144,7 @@ def test_quickstart_defaults(tempdir):
execfile_(conffile, ns)
assert ns['extensions'] == []
assert ns['templates_path'] == ['_templates']
assert ns['source_suffix'] == '.rst'
assert ns['source_suffix'] == ['.rst']
assert ns['master_doc'] == 'index'
assert ns['project'] == 'Sphinx Test'
assert ns['copyright'] == '%s, Georg Brandl' % time.strftime('%Y')
@ -203,7 +203,7 @@ def test_quickstart_all_answers(tempdir):
'sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.todo'
]
assert ns['templates_path'] == ['.templates']
assert ns['source_suffix'] == '.txt'
assert ns['source_suffix'] == ['.txt']
assert ns['master_doc'] == 'contents'
assert ns['project'] == u'STASI™'
assert ns['copyright'] == u'%s, Wolfgang Schäuble & G\'Beckstein' % \