|
66 | 66 | # The encoding of source files.
|
67 | 67 | #source_encoding = 'utf-8-sig'
|
68 | 68 |
|
| 69 | +import logging |
| 70 | +from glob import glob |
69 | 71 | from shutil import copyfile
|
70 | 72 |
|
71 |
| -SUPPORTED_FORMATS = { fmt: os.path.abspath("index-{}.rst".format(fmt)) |
72 |
| - for fmt in ["html", "latex"] } |
73 |
| -MASTER_DOC = os.path.abspath("index.rst") |
74 |
| - |
75 | 73 | # Add extra cases here to support more formats
|
76 |
| -def copy_master_doc(app): |
77 |
| - for fmt, rst_path in SUPPORTED_FORMATS.items(): |
| 74 | + |
| 75 | +SUPPORTED_FORMATS = ["html", "latex"] |
| 76 | + |
| 77 | +def current_format(app): |
| 78 | + for fmt in SUPPORTED_FORMATS: |
78 | 79 | if app.tags.has(fmt):
|
79 |
| - copyfile(rst_path, MASTER_DOC) |
80 |
| - break |
81 |
| - else: |
82 |
| - MSG = """Unexpected builder; expected one of {!r}. |
83 |
| -Add support for this new builder in `copy_master_doc` in conf.py.""" |
84 |
| - from sphinx.errors import ConfigError |
85 |
| - raise ConfigError(MSG.format(list(SUPPORTED_FORMATS.keys()))) |
| 80 | + return fmt |
| 81 | + MSG = """Unexpected builder ({}); expected one of {!r}. |
| 82 | +Add support for this new builder in `copy_formatspecific_files` in conf.py.""" |
| 83 | + from sphinx.errors import ConfigError |
| 84 | + raise ConfigError(MSG.format(list(app.tags.tags.keys()), SUPPORTED_FORMATS)) |
| 85 | + |
| 86 | +def copy_formatspecific_files(app): |
| 87 | + ext = ".{}.rst".format(current_format(app)) |
| 88 | + for fname in sorted(os.listdir(app.srcdir)): |
| 89 | + if fname.endswith(ext): |
| 90 | + src = os.path.join(app.srcdir, fname) |
| 91 | + dst = os.path.join(app.srcdir, fname[:-len(ext)] + ".rst") |
| 92 | + app.info("Copying {} to {}".format(src, dst)) |
| 93 | + copyfile(src, dst) |
86 | 94 |
|
87 | 95 | def setup(app):
|
88 |
| - app.connect('builder-inited', copy_master_doc) |
| 96 | + app.connect('builder-inited', copy_formatspecific_files) |
89 | 97 |
|
90 | 98 | # The master toctree document.
|
91 | 99 | # We create this file in `copy_master_doc` above.
|
@@ -126,11 +134,10 @@ def setup(app):
|
126 | 134 | 'Thumbs.db',
|
127 | 135 | '.DS_Store',
|
128 | 136 | 'introduction.rst',
|
129 |
| - 'index-*.rst', # One of these gets copied to index.rst in `copy_master_doc` |
130 | 137 | 'refman-preamble.rst',
|
131 | 138 | 'README.rst',
|
132 | 139 | 'README.template.rst'
|
133 |
| -] |
| 140 | +] + ["*.{}.rst".format(fmt) for fmt in SUPPORTED_FORMATS] |
134 | 141 |
|
135 | 142 | # The reST default role (used for this markup: `text`) to use for all
|
136 | 143 | # documents.
|
|
0 commit comments