Skip to content

Commit 697c627

Browse files
committed
Added support for multiple categories, WARNING: this will break the
current themes, just did it because I needed it.
1 parent 355842c commit 697c627

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

Diff for: pelican/contents.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def relative_dir(self):
236236

237237

238238
class Article(Page):
239-
mandatory_properties = ('title', 'date', 'category')
239+
mandatory_properties = ('title', 'date', 'categories')
240240
default_template = 'article'
241241

242242

@@ -266,7 +266,7 @@ def __unicode__(self):
266266
return self.name
267267

268268
def _from_settings(self, key, get_page_name=False):
269-
"""Returns URL information as defined in settings.
269+
"""Returns URL information as defined in settings.
270270
When get_page_name=True returns URL without anything after {slug}
271271
e.g. if in settings: CATEGORY_URL="cat/{slug}.html" this returns "cat/{slug}"
272272
Useful for pagination."""

Diff for: pelican/generators.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def generate_articles(self, write):
238238
"""Generate the articles."""
239239
for article in chain(self.translations, self.articles):
240240
write(article.save_as, self.get_template(article.template),
241-
self.context, article=article, category=article.category)
241+
self.context, article=article, categories=article.categories)
242242

243243
def generate_direct_templates(self, write):
244244
"""Generate direct templates pages"""
@@ -328,7 +328,7 @@ def generate_context(self):
328328
continue
329329

330330
# if no category is set, use the name of the path as a category
331-
if 'category' not in metadata:
331+
if 'categories' not in metadata:
332332

333333
if (self.settings['USE_FOLDER_AS_CATEGORY']
334334
and os.path.dirname(f) != article_path):
@@ -340,7 +340,7 @@ def generate_context(self):
340340
category = self.settings['DEFAULT_CATEGORY']
341341

342342
if category != '':
343-
metadata['category'] = Category(category, self.settings)
343+
metadata['categories'] = [Category(category, self.settings)]
344344

345345
if 'date' not in metadata and self.settings.get('DEFAULT_DATE'):
346346
if self.settings['DEFAULT_DATE'] == 'fs':
@@ -374,7 +374,8 @@ def generate_context(self):
374374

375375
for article in self.articles:
376376
# only main articles are listed in categories, not translations
377-
self.categories[article.category].append(article)
377+
for category in article.categories:
378+
self.categories[category].append(article)
378379
# ignore blank authors as well as undefined
379380
if hasattr(article,'author') and article.author.name != '':
380381
self.authors[article.author].append(article)

Diff for: pelican/readers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'tags': lambda x, y: [Tag(tag, y) for tag in unicode(x).split(',')],
3030
'date': lambda x, y: get_date(x),
3131
'status': lambda x, y: unicode.strip(x),
32-
'category': Category,
32+
'categories': lambda x, y: [Category(cat, y) for cat in unicode(x).split(',')],
3333
'author': Author,
3434
}
3535

Diff for: pelican/tools/pelican_import.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ def build_markdown_header(title, date, author, categories, tags, slug):
203203
if date:
204204
header += 'Date: %s\n' % date
205205
if author:
206-
header += 'Author: %s\n' % author
206+
#header += 'Author: %s\n' % author
207+
header += 'Author: fitoria\n'
207208
if categories:
208-
header += 'Category: %s\n' % ', '.join(categories)
209+
header += 'Categories: %s\n' % ','.join(categories)
209210
if tags:
210211
header += 'Tags: %s\n' % ', '.join(tags)
211212
if slug:
@@ -226,12 +227,19 @@ def fields2pelican(fields, out_markup, output_path, dircat=False, strip_raw=Fals
226227

227228
filename = os.path.basename(filename)
228229

230+
year, month = date.split('-')[:2]
231+
229232
# option to put files in directories with categories names
230233
if dircat and (len(categories) > 0):
231234
catname = slugify(categories[0])
232235
out_filename = os.path.join(output_path, catname, filename+ext)
233236
if not os.path.isdir(os.path.join(output_path, catname)):
234237
os.mkdir(os.path.join(output_path, catname))
238+
elif year and month:
239+
month_dir = os.path.join(output_path, year, month)
240+
if not os.path.exists(month_dir):
241+
os.makedirs(month_dir)
242+
out_filename = os.path.join(month_dir, filename+ext)
235243
else:
236244
out_filename = os.path.join(output_path, filename+ext)
237245

0 commit comments

Comments
 (0)