-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Description
We have a similar problem as bug #823 with files such as CMakeLists.txt and Makefile. The reader will get confused when we tell them to download a demo file along with a CMakeLists.txt file and then they get CMakeLists5.txt which is not recognized by the cmake program, so they must remember to rename the file before running the demo.
We do not require as large a change as #823 (which is why I created a new issue), only that the file base names remain unchanged and unique prefix directories are created. A quick and ugly fix is stick this at the end of conf.py, but hotpatching internal Sphinx APIs is not very maintainable in the long run ...
# Hack to support avoid renaming download files with same names
# coming from different directories (CMakeLists.txt, Makefile etc)
from sphinx.util import FilenameUniqDict
from os import path
def add_file(self, docname, newfile):
if newfile in self:
self[newfile][0].add(docname)
return self[newfile][1]
uniquename = path.basename(newfile)
i = 0
while uniquename in self._existing:
i += 1
uniquename = path.join('subdir%s' % i, path.basename(newfile))
self[newfile] = (set([docname]), uniquename)
self._existing.add(uniquename)
return uniquename
FilenameUniqDict.add_file = add_file
# Hack to support sub-directories within the download directory
from sphinx.builders.html import StandaloneHTMLBuilder, ensuredir, copyfile
from docutils.utils import relative_path
from sphinx.util.console import brown
def copy_download_files(self):
def to_relpath(f):
return relative_path(self.srcdir, f)
# copy downloadable files
if self.env.dlfiles:
ensuredir(path.join(self.outdir, '_downloads'))
for src in self.status_iterator(self.env.dlfiles, # self.app.status_iterator in master ...
'copying downloadable files... ',
brown, len(self.env.dlfiles),
stringify_func=to_relpath):
dest = self.env.dlfiles[src][1]
subdirs, filename = os.path.split(dest)
if subdirs:
ensuredir(path.join(self.outdir, '_downloads', subdirs))
try:
copyfile(path.join(self.srcdir, src),
path.join(self.outdir, '_downloads', dest))
except Exception as err:
self.warn('cannot copy downloadable file %r: %s' %
(path.join(self.srcdir, src), err))
StandaloneHTMLBuilder.copy_download_files = copy_download_files
This works-for-me! Would something similar be appreciated as a patch? I guess more builders would need to be changed?