From 652eb3686f15a24f8e8830f2d4345ab8c50cf761 Mon Sep 17 00:00:00 2001
From: Stefan hr Berder
Date: Thu, 26 Dec 2013 19:30:55 +0100
Subject: [PATCH] add lang support for drafts (#826 & #1107)
Fix #826
Fix #1107
---
docs/settings.rst | 10 +++++--
pelican/__init__.py | 12 ++++++---
pelican/contents.py | 5 ++++
pelican/generators.py | 27 +++++++++++++------
pelican/settings.py | 4 +++
.../output/custom/drafts/a-draft-article.html | 4 +--
6 files changed, 46 insertions(+), 16 deletions(-)
diff --git a/docs/settings.rst b/docs/settings.rst
index ca92a19e..a8e96d71 100644
--- a/docs/settings.rst
+++ b/docs/settings.rst
@@ -237,12 +237,18 @@ posts for the month at ``posts/2011/Aug/index.html``.
==================================================== =====================================================
Setting name (default value) What does it do?
==================================================== =====================================================
-`ARTICLE_URL` (``'{slug}.html'``) The URL to refer to an ARTICLE.
+`ARTICLE_URL` (``'{slug}.html'``) The URL to refer to an article.
`ARTICLE_SAVE_AS` (``'{slug}.html'``) The place where we will save an article.
-`ARTICLE_LANG_URL` (``'{slug}-{lang}.html'``) The URL to refer to an ARTICLE which doesn't use the
+`ARTICLE_LANG_URL` (``'{slug}-{lang}.html'``) The URL to refer to an article which doesn't use the
default language.
`ARTICLE_LANG_SAVE_AS` (``'{slug}-{lang}.html'``) The place where we will save an article which
doesn't use the default language.
+`DRAFT_URL` (``'drafts/{slug}.html'``) The URL to refer to an article draft.
+`DRAFT_SAVE_AS` (``'drafts/{slug}.html'``) The place where we will save an article draft.
+`DRAFT_LANG_URL` (``'drafts/{slug}-{lang}.html'``) The URL to refer to an article draft which doesn't
+ use the default language.
+`DRAFT_LANG_SAVE_AS` (``'drafts/{slug}-{lang}.html'``) The place where we will save an article draft which
+ doesn't use the default language.
`PAGE_URL` (``'pages/{slug}.html'``) The URL we will use to link to a page.
`PAGE_SAVE_AS` (``'pages/{slug}.html'``) The location we will save the page. This value has to be
the same as PAGE_URL or you need to use a rewrite in
diff --git a/pelican/__init__.py b/pelican/__init__.py
index 47260551..08dd484e 100644
--- a/pelican/__init__.py
+++ b/pelican/__init__.py
@@ -114,9 +114,10 @@ class Pelican(object):
structure = re.sub('^/', '', structure)
for setting in ('ARTICLE_URL', 'ARTICLE_LANG_URL', 'PAGE_URL',
- 'PAGE_LANG_URL', 'ARTICLE_SAVE_AS',
- 'ARTICLE_LANG_SAVE_AS', 'PAGE_SAVE_AS',
- 'PAGE_LANG_SAVE_AS'):
+ 'PAGE_LANG_URL', 'DRAFT_URL', 'DRAFT_LANG_URL',
+ 'ARTICLE_SAVE_AS', 'ARTICLE_LANG_SAVE_AS',
+ 'DRAFT_SAVE_AS', 'DRAFT_LANG_SAVE_AS',
+ 'PAGE_SAVE_AS', 'PAGE_LANG_SAVE_AS'):
self.settings[setting] = os.path.join(structure,
self.settings[setting])
logger.warning("%s = '%s'" % (setting, self.settings[setting]))
@@ -174,8 +175,11 @@ class Pelican(object):
pages_generator = next(g for g in generators
if isinstance(g, PagesGenerator))
- print('Done: Processed {} articles and {} pages in {:.2f} seconds.'.format(
+ print('Done: Processed {} article(s), {} draft(s) and {} page(s) in ' \
+ '{:.2f} seconds.'.format(
len(articles_generator.articles) + len(articles_generator.translations),
+ len(articles_generator.drafts) + \
+ len(articles_generator.drafts_translations),
len(pages_generator.pages) + len(pages_generator.translations),
time.time() - start_time))
diff --git a/pelican/contents.py b/pelican/contents.py
index 69b7fa69..2ba81c1d 100644
--- a/pelican/contents.py
+++ b/pelican/contents.py
@@ -328,6 +328,11 @@ class Article(Page):
default_template = 'article'
+class Draft(Page):
+ mandatory_properties = ('title', 'category')
+ default_template = 'article'
+
+
class Quote(Page):
base_properties = ('author', 'date')
diff --git a/pelican/generators.py b/pelican/generators.py
index d1034eb0..bfdac1a5 100644
--- a/pelican/generators.py
+++ b/pelican/generators.py
@@ -18,7 +18,7 @@ from operator import attrgetter, itemgetter
from jinja2 import (Environment, FileSystemLoader, PrefixLoader, ChoiceLoader,
BaseLoader, TemplateNotFound)
-from pelican.contents import Article, Page, Static, is_valid_content
+from pelican.contents import Article, Draft, Page, Static, is_valid_content
from pelican.readers import Readers
from pelican.utils import copy, process_translations, mkdir_p, DateFormatter
from pelican import signals
@@ -190,7 +190,8 @@ class ArticlesGenerator(Generator):
self.categories = defaultdict(list)
self.related_posts = []
self.authors = defaultdict(list)
- self.drafts = []
+ self.drafts = [] # only drafts in default language
+ self.drafts_translations = []
super(ArticlesGenerator, self).__init__(*args, **kwargs)
signals.article_generator_init.send(self)
@@ -376,11 +377,11 @@ class ArticlesGenerator(Generator):
def generate_drafts(self, write):
"""Generate drafts pages."""
- for article in self.drafts:
- write(os.path.join('drafts', '%s.html' % article.slug),
- self.get_template(article.template), self.context,
- article=article, category=article.category,
- all_articles=self.articles)
+ for draft in chain(self.drafts_translations, self.drafts):
+ write(draft.save_as, self.get_template(draft.template),
+ self.context, article=draft, category=draft.category,
+ override_output=hasattr(draft, 'override_save_as'),
+ all_articles=self.articles)
def generate_pages(self, writer):
"""Generate the pages on the disk"""
@@ -403,6 +404,7 @@ class ArticlesGenerator(Generator):
"""Add the articles into the shared context"""
all_articles = []
+ all_drafts = []
for f in self.get_files(
self.settings['ARTICLE_DIR'],
exclude=self.settings['ARTICLE_EXCLUDES']):
@@ -426,13 +428,22 @@ class ArticlesGenerator(Generator):
if article.status.lower() == "published":
all_articles.append(article)
elif article.status.lower() == "draft":
- self.drafts.append(article)
+ draft = self.readers.read_file(
+ base_path=self.path, path=f, content_class=Draft,
+ context=self.context,
+ preread_signal=signals.article_generator_preread,
+ preread_sender=self,
+ context_signal=signals.article_generator_context,
+ context_sender=self)
+ all_drafts.append(draft)
else:
logger.warning("Unknown status %s for file %s, skipping it." %
(repr(article.status),
repr(f)))
self.articles, self.translations = process_translations(all_articles)
+ self.drafts, self.drafts_translations = \
+ process_translations(all_drafts)
signals.article_generator_pretaxonomy.send(self)
diff --git a/pelican/settings.py b/pelican/settings.py
index 99828935..225a1e9d 100644
--- a/pelican/settings.py
+++ b/pelican/settings.py
@@ -59,6 +59,10 @@ DEFAULT_CONFIG = {
'ARTICLE_SAVE_AS': '{slug}.html',
'ARTICLE_LANG_URL': '{slug}-{lang}.html',
'ARTICLE_LANG_SAVE_AS': '{slug}-{lang}.html',
+ 'DRAFT_URL': 'drafts/{slug}.html',
+ 'DRAFT_SAVE_AS': os.path.join('drafts', '{slug}.html'),
+ 'DRAFT_LANG_URL': 'drafts/{slug}-{lang}.html',
+ 'DRAFT_LANG_SAVE_AS': os.path.join('drafts', '{slug}-{lang}.html'),
'PAGE_URL': 'pages/{slug}.html',
'PAGE_SAVE_AS': os.path.join('pages', '{slug}.html'),
'PAGE_LANG_URL': 'pages/{slug}-{lang}.html',
diff --git a/pelican/tests/output/custom/drafts/a-draft-article.html b/pelican/tests/output/custom/drafts/a-draft-article.html
index 9050c04e..57eea18c 100644
--- a/pelican/tests/output/custom/drafts/a-draft-article.html
+++ b/pelican/tests/output/custom/drafts/a-draft-article.html
@@ -32,7 +32,7 @@
@@ -97,4 +97,4 @@ listed anywhere else.
}());