diff --git a/pelican/tests/output/custom/feeds/all-en.atom.xml b/pelican/tests/output/custom/feeds/all-en.atom.xml
index 49d45cde..49b0de79 100644
--- a/pelican/tests/output/custom/feeds/all-en.atom.xml
+++ b/pelican/tests/output/custom/feeds/all-en.atom.xml
@@ -27,4 +27,5 @@ YEAH !</p>
Unbelievable !2010-10-15T20:30:00+02:00Alexis Métaireautag:blog.notmyidea.org,2010-10-15:unbelievable.html<p>Or completely awesome. Depends the needs.</p>
<p><a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a root-relative link to markdown-article</a>
<a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a file-relative link to markdown-article</a></p>
+The baz tag2010-03-14T00:00:00+01:00Alexis Métaireautag:blog.notmyidea.org,2010-03-14:tag/baz.html<p>This article overrides the listening of the articles under the <em>baz</em> tag.</p>
\ No newline at end of file
diff --git a/pelican/tests/output/custom/feeds/all.atom.xml b/pelican/tests/output/custom/feeds/all.atom.xml
index 3187c2aa..a9e67acd 100644
--- a/pelican/tests/output/custom/feeds/all.atom.xml
+++ b/pelican/tests/output/custom/feeds/all.atom.xml
@@ -29,4 +29,5 @@ YEAH !</p>
Unbelievable !2010-10-15T20:30:00+02:00Alexis Métaireautag:blog.notmyidea.org,2010-10-15:unbelievable.html<p>Or completely awesome. Depends the needs.</p>
<p><a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a root-relative link to markdown-article</a>
<a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a file-relative link to markdown-article</a></p>
+The baz tag2010-03-14T00:00:00+01:00Alexis Métaireautag:blog.notmyidea.org,2010-03-14:tag/baz.html<p>This article overrides the listening of the articles under the <em>baz</em> tag.</p>
\ No newline at end of file
diff --git a/pelican/tests/output/custom/feeds/all.rss.xml b/pelican/tests/output/custom/feeds/all.rss.xml
index 8d07bec7..7d7890a9 100644
--- a/pelican/tests/output/custom/feeds/all.rss.xml
+++ b/pelican/tests/output/custom/feeds/all.rss.xml
@@ -29,4 +29,5 @@ YEAH !</p>
Alexis MétaireauWed, 20 Oct 2010 10:14:00 +0200tag:blog.notmyidea.org,2010-10-20:oh-yeah.htmlohbaryeahUnbelievable !http://blog.notmyidea.org/unbelievable.html<p>Or completely awesome. Depends the needs.</p>
<p><a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a root-relative link to markdown-article</a>
<a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a file-relative link to markdown-article</a></p>
-Alexis MétaireauFri, 15 Oct 2010 20:30:00 +0200tag:blog.notmyidea.org,2010-10-15:unbelievable.html
\ No newline at end of file
+Alexis MétaireauFri, 15 Oct 2010 20:30:00 +0200tag:blog.notmyidea.org,2010-10-15:unbelievable.htmlThe baz taghttp://blog.notmyidea.org/tag/baz.html<p>This article overrides the listening of the articles under the <em>baz</em> tag.</p>
+Alexis MétaireauSun, 14 Mar 2010 00:00:00 +0100tag:blog.notmyidea.org,2010-03-14:tag/baz.html
\ No newline at end of file
diff --git a/pelican/tests/output/custom/feeds/misc.atom.xml b/pelican/tests/output/custom/feeds/misc.atom.xml
index 45c996f3..029184b0 100644
--- a/pelican/tests/output/custom/feeds/misc.atom.xml
+++ b/pelican/tests/output/custom/feeds/misc.atom.xml
@@ -4,4 +4,5 @@
Unbelievable !2010-10-15T20:30:00+02:00Alexis Métaireautag:blog.notmyidea.org,2010-10-15:unbelievable.html<p>Or completely awesome. Depends the needs.</p>
<p><a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a root-relative link to markdown-article</a>
<a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a file-relative link to markdown-article</a></p>
+The baz tag2010-03-14T00:00:00+01:00Alexis Métaireautag:blog.notmyidea.org,2010-03-14:tag/baz.html<p>This article overrides the listening of the articles under the <em>baz</em> tag.</p>
\ No newline at end of file
diff --git a/pelican/tests/output/custom/feeds/misc.rss.xml b/pelican/tests/output/custom/feeds/misc.rss.xml
index 1d295abc..ab2639cf 100644
--- a/pelican/tests/output/custom/feeds/misc.rss.xml
+++ b/pelican/tests/output/custom/feeds/misc.rss.xml
@@ -4,4 +4,5 @@
Alexis MétaireauWed, 29 Feb 2012 00:00:00 +0100tag:blog.notmyidea.org,2012-02-29:second-article.htmlfoobarbazUnbelievable !http://blog.notmyidea.org/unbelievable.html<p>Or completely awesome. Depends the needs.</p>
<p><a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a root-relative link to markdown-article</a>
<a class="reference external" href="http://blog.notmyidea.org/a-markdown-powered-article.html">a file-relative link to markdown-article</a></p>
-Alexis MétaireauFri, 15 Oct 2010 20:30:00 +0200tag:blog.notmyidea.org,2010-10-15:unbelievable.html
\ No newline at end of file
+Alexis MétaireauFri, 15 Oct 2010 20:30:00 +0200tag:blog.notmyidea.org,2010-10-15:unbelievable.htmlThe baz taghttp://blog.notmyidea.org/tag/baz.html<p>This article overrides the listening of the articles under the <em>baz</em> tag.</p>
+Alexis MétaireauSun, 14 Mar 2010 00:00:00 +0100tag:blog.notmyidea.org,2010-03-14:tag/baz.html
\ No newline at end of file
diff --git a/pelican/tests/output/custom/filename_metadata-example.html b/pelican/tests/output/custom/filename_metadata-example.html
index a9c0f5c1..83a4c43f 100644
--- a/pelican/tests/output/custom/filename_metadata-example.html
+++ b/pelican/tests/output/custom/filename_metadata-example.html
@@ -19,6 +19,7 @@
diff --git a/samples/content/article_tag_baz.rst b/samples/content/article_tag_baz.rst
new file mode 100644
index 00000000..9a33a8d3
--- /dev/null
+++ b/samples/content/article_tag_baz.rst
@@ -0,0 +1,8 @@
+The baz tag
+###########
+
+:date: 2010-03-14
+:url: tag/baz.html
+:save_as: tag/baz.html
+
+This article overrides the listening of the articles under the *baz* tag.
diff --git a/samples/content/pages/override_tag_oh.rst b/samples/content/pages/override_tag_oh.rst
new file mode 100644
index 00000000..c3160349
--- /dev/null
+++ b/samples/content/pages/override_tag_oh.rst
@@ -0,0 +1,8 @@
+Oh Oh Oh
+########
+
+:date: 2010-03-14
+:url: tag/oh.html
+:save_as: tag/oh.html
+
+This page overrides the listening of the articles under the *oh* tag.
From a495527e2bd4d0853d18ada90b45cc7d16dc0be4 Mon Sep 17 00:00:00 2001
From: Rogdham
Date: Sat, 22 Jun 2013 14:49:48 +0100
Subject: [PATCH 2/2] Delib. overriding and overwrite detect. Fixes #938
Make deliberate overriding (*) works with overwrites detection.
(*) first introduced by d0e9c52410e70e14008d2ef827ba7ac306243e72
The following are decided to be deliberate override:
- articles using the `save_as` metadata
- pages using the `save_as` metadata
- template pages (always)
Pelican now exits in the following 2 cases:
- at least 2 not deliberate writes to the same file name (behaviour introduced
by the overwrite detection feature ff7410ce2ada85b486a67ae11874d60d135ff939)
- at least 2 deliberate writes to the same file name (new behaviour)
Also added info logging when deliberate overrides are performed.
Switched to StandardError instead of IOError, thanks to @ametaireau and
@russkel.
---
pelican/generators.py | 9 ++++++---
pelican/writers.py | 36 +++++++++++++++++++++++++++---------
2 files changed, 33 insertions(+), 12 deletions(-)
diff --git a/pelican/generators.py b/pelican/generators.py
index dea22e17..c294f3e1 100644
--- a/pelican/generators.py
+++ b/pelican/generators.py
@@ -168,7 +168,8 @@ class TemplatePagesGenerator(Generator):
try:
template = self.env.get_template(source)
rurls = self.settings['RELATIVE_URLS']
- writer.write_file(dest, template, self.context, rurls)
+ writer.write_file(dest, template, self.context, rurls,
+ override_output=True)
finally:
del self.env.loader.loaders[0]
@@ -262,7 +263,8 @@ class ArticlesGenerator(Generator):
"""Generate the articles."""
for article in chain(self.translations, self.articles):
write(article.save_as, self.get_template(article.template),
- self.context, article=article, category=article.category)
+ self.context, article=article, category=article.category,
+ override_output=hasattr(article, 'override_save_as'))
def generate_period_archives(self, write):
"""Generate per-year, per-month, and per-day archives."""
@@ -533,7 +535,8 @@ class PagesGenerator(Generator):
self.hidden_translations, self.hidden_pages):
writer.write_file(page.save_as, self.get_template(page.template),
self.context, page=page,
- relative_urls=self.settings['RELATIVE_URLS'])
+ relative_urls=self.settings['RELATIVE_URLS'],
+ override_output=hasattr(page, 'override_save_as'))
class StaticGenerator(Generator):
diff --git a/pelican/writers.py b/pelican/writers.py
index 25f49aeb..59642cd9 100644
--- a/pelican/writers.py
+++ b/pelican/writers.py
@@ -24,6 +24,7 @@ class Writer(object):
self.reminder = dict()
self.settings = settings or {}
self._written_files = set()
+ self._overridden_files = set()
def _create_new_feed(self, feed_type, context):
feed_class = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed
@@ -49,13 +50,26 @@ class Writer(object):
pubdate=set_date_tzinfo(item.date,
self.settings.get('TIMEZONE', None)))
- def _open_w(self, filename, encoding):
+ def _open_w(self, filename, encoding, override=False):
"""Open a file to write some content to it.
- Exit if we have already written to that file.
+ Exit if we have already written to that file, unless one (and no more
+ than one) of the writes has the override parameter set to True.
"""
- if filename in self._written_files:
- raise IOError('File %s is to be overwritten' % filename)
+ if filename in self._overridden_files:
+ if override:
+ raise StandardError('File %s is set to be overridden twice'
+ % filename)
+ else:
+ logger.info('skipping %s' % filename)
+ filename = os.devnull
+ elif filename in self._written_files:
+ if override:
+ logger.info('overwriting %s' % filename)
+ else:
+ raise StandardError('File %s is to be overwritten' % filename)
+ if override:
+ self._overridden_files.add(filename)
self._written_files.add(filename)
return open(filename, 'w', encoding=encoding)
@@ -103,7 +117,7 @@ class Writer(object):
locale.setlocale(locale.LC_ALL, old_locale)
def write_file(self, name, template, context, relative_urls=False,
- paginated=None, **kwargs):
+ paginated=None, override_output=False, **kwargs):
"""Render the template and write the file.
:param name: name of the file to output
@@ -112,6 +126,9 @@ class Writer(object):
:param relative_urls: use relative urls or absolutes ones
:param paginated: dict of article list to paginate - must have the
same length (same list in different orders)
+ :param override_output: boolean telling if we can override previous
+ output with the same name (and if next files written with the same
+ name should be skipped to keep that one)
:param **kwargs: additional variables to pass to the templates
"""
@@ -121,7 +138,7 @@ class Writer(object):
# other stuff, just return for now
return
- def _write_file(template, localcontext, output_path, name):
+ def _write_file(template, localcontext, output_path, name, override):
"""Render the template write the file."""
old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
@@ -134,7 +151,7 @@ class Writer(object):
os.makedirs(os.path.dirname(path))
except Exception:
pass
- with self._open_w(path, 'utf-8') as f:
+ with self._open_w(path, 'utf-8', override=override) as f:
f.write(output)
logger.info('writing {}'.format(path))
@@ -180,7 +197,8 @@ class Writer(object):
'%s_next_page' % key: next_page})
_write_file(template, paginated_localcontext, self.output_path,
- page.save_as)
+ page.save_as, override_output)
else:
# no pagination
- _write_file(template, localcontext, self.output_path, name)
+ _write_file(template, localcontext, self.output_path, name,
+ override_output)