diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2016-07-03 15:45:01 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2016-07-07 00:02:57 +0900 |
commit | bb1e6f9044567fcd05a27bb1cc55f727c86fe2bd (patch) | |
tree | 0fb14c5bdc1bdaf777d2d97f0a276286cb686297 | |
parent | 829b5631a34a7da67e6dee18853c82c12e1aa59d (diff) | |
download | sphinx-git-bb1e6f9044567fcd05a27bb1cc55f727c86fe2bd.tar.gz |
Refactor: Replace copy_extra_entry() with copy_asset()
-rw-r--r-- | sphinx/builders/html.py | 16 | ||||
-rw-r--r-- | sphinx/util/__init__.py | 31 | ||||
-rw-r--r-- | sphinx/util/matching.py | 18 | ||||
-rw-r--r-- | tests/roots/test-html_extra_path/extra/subdir/.htaccess | 0 | ||||
-rw-r--r-- | tests/roots/test-html_extra_path/extra/subdir/.htpasswd | 0 | ||||
-rw-r--r-- | tests/test_build_html.py | 2 | ||||
-rw-r--r-- | tests/test_util_matching.py | 10 |
7 files changed, 38 insertions, 39 deletions
diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 1f1811778..25cdc04bd 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -27,12 +27,13 @@ from docutils.frontend import OptionParser from docutils.readers.doctree import Reader as DoctreeReader from sphinx import package_dir, __display_version__ -from sphinx.util import jsonimpl, copy_static_entry, copy_extra_entry +from sphinx.util import jsonimpl, copy_static_entry from sphinx.util.i18n import format_date from sphinx.util.osutil import SEP, os_path, relative_uri, ensuredir, \ movefile, copyfile from sphinx.util.nodes import inline_all_toctrees -from sphinx.util.matching import patmatch, compile_matchers +from sphinx.util.fileutil import copy_asset +from sphinx.util.matching import patmatch, compile_matchers, Matcher from sphinx.config import string_classes from sphinx.locale import _, l_ from sphinx.search import js_index @@ -650,14 +651,15 @@ class StandaloneHTMLBuilder(Builder): def copy_extra_files(self): # copy html_extra_path files self.info(bold('copying extra files... '), nonl=True) - extraentries = [path.join(self.confdir, epath) - for epath in self.config.html_extra_path] - matchers = compile_matchers(self.config.exclude_patterns) - for entry in extraentries: + excluded = Matcher(self.config.exclude_patterns) + + for extra_path in self.config.html_extra_path: + entry = path.join(self.confdir, extra_path) if not path.exists(entry): self.warn('html_extra_path entry %r does not exist' % entry) continue - copy_extra_entry(entry, self.outdir, matchers) + + copy_asset(entry, self.outdir, excluded) self.info('done') def write_buildinfo(self): diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index e1a65d950..036e57ac0 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -173,37 +173,6 @@ def copy_static_entry(source, targetdir, builder, context={}, builder, context, level=level+1, exclude_matchers=exclude_matchers) - -def copy_extra_entry(source, targetdir, exclude_matchers=()): - """Copy a HTML builder extra_path entry from source to targetdir. - - Handles all possible cases of files, directories and subdirectories. - """ - def excluded(path): - relpath = relative_path(os.path.dirname(source), path) - return any(matcher(relpath) for matcher in exclude_matchers) - - def copy_extra_file(source_, targetdir_): - if not excluded(source_): - target = path.join(targetdir_, os.path.basename(source_)) - copyfile(source_, target) - - if os.path.isfile(source): - copy_extra_file(source, targetdir) - return - - for root, dirs, files in os.walk(source): - reltargetdir = os.path.join(targetdir, relative_path(source, root)) - for dir in dirs[:]: - if excluded(os.path.join(root, dir)): - dirs.remove(dir) - else: - target = os.path.join(reltargetdir, dir) - if not path.exists(target): - os.mkdir(target) - for file in files: - copy_extra_file(os.path.join(root, file), reltargetdir) - _DEBUG_HEADER = '''\ # Sphinx version: %s # Python version: %s (%s) diff --git a/sphinx/util/matching.py b/sphinx/util/matching.py index 91fda6378..8f17980ca 100644 --- a/sphinx/util/matching.py +++ b/sphinx/util/matching.py @@ -62,6 +62,24 @@ def compile_matchers(patterns): return [re.compile(_translate_pattern(pat)).match for pat in patterns] +class Matcher(object): + """A pattern matcher for Multiple shell-style glob patterns. + + Note: this modifies the patterns to work with copy_asset(). + For example, "**/index.rst" matches with "index.rst" + """ + + def __init__(self, patterns): + expanded = [pat[3:] for pat in patterns if pat.startswith('**/')] + self.patterns = compile_matchers(patterns + expanded) + + def __call__(self, string): + return self.match(string) + + def match(self, string): + return any(pat(string) for pat in self.patterns) + + _pat_cache = {} diff --git a/tests/roots/test-html_extra_path/extra/subdir/.htaccess b/tests/roots/test-html_extra_path/extra/subdir/.htaccess new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/roots/test-html_extra_path/extra/subdir/.htaccess diff --git a/tests/roots/test-html_extra_path/extra/subdir/.htpasswd b/tests/roots/test-html_extra_path/extra/subdir/.htpasswd new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/roots/test-html_extra_path/extra/subdir/.htpasswd diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 173ad6cf5..78ef1770c 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -987,6 +987,8 @@ def test_html_extra_path(app, status, warning): assert (app.outdir / 'rimg.png').exists() assert not (app.outdir / '_build/index.html').exists() assert (app.outdir / 'background.png').exists() + assert (app.outdir / 'subdir' / '.htaccess').exists() + assert not (app.outdir / 'subdir' / '.htpasswd').exists() @with_app(buildername='html', confoverrides={'html_sourcelink_suffix': ''}) diff --git a/tests/test_util_matching.py b/tests/test_util_matching.py index fe86c3bbc..9e99a5322 100644 --- a/tests/test_util_matching.py +++ b/tests/test_util_matching.py @@ -8,7 +8,7 @@ :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ -from sphinx.util.matching import compile_matchers +from sphinx.util.matching import compile_matchers, Matcher def test_compile_matchers(): @@ -81,3 +81,11 @@ def test_compile_matchers(): pat = compile_matchers(['hello[!].py']).pop() assert pat('hello[!].py') assert not pat('hello.py') + + +def test_Matcher(): + matcher = Matcher(['hello.py', '**/world.py']) + assert matcher('hello.py') + assert not matcher('subdir/hello.py') + assert matcher('world.py') + assert matcher('subdir/world.py') |