Skip to content

Commit

Permalink
Added support for multiple categories, WARNING: this will break the
Browse files Browse the repository at this point in the history
current themes, just did it because I needed it.
  • Loading branch information
fitoria committed Jan 6, 2013
1 parent 355842c commit 697c627
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pelican/contents.py
Expand Up @@ -236,7 +236,7 @@ def relative_dir(self):


class Article(Page):
mandatory_properties = ('title', 'date', 'category')
mandatory_properties = ('title', 'date', 'categories')
default_template = 'article'


Expand Down Expand Up @@ -266,7 +266,7 @@ def __unicode__(self):
return self.name

def _from_settings(self, key, get_page_name=False):
"""Returns URL information as defined in settings.
"""Returns URL information as defined in settings.
When get_page_name=True returns URL without anything after {slug}
e.g. if in settings: CATEGORY_URL="cat/{slug}.html" this returns "cat/{slug}"
Useful for pagination."""
Expand Down
9 changes: 5 additions & 4 deletions pelican/generators.py
Expand Up @@ -238,7 +238,7 @@ def generate_articles(self, write):
"""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, categories=article.categories)

def generate_direct_templates(self, write):
"""Generate direct templates pages"""
Expand Down Expand Up @@ -328,7 +328,7 @@ def generate_context(self):
continue

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

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

if category != '':
metadata['category'] = Category(category, self.settings)
metadata['categories'] = [Category(category, self.settings)]

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

for article in self.articles:
# only main articles are listed in categories, not translations
self.categories[article.category].append(article)
for category in article.categories:
self.categories[category].append(article)
# ignore blank authors as well as undefined
if hasattr(article,'author') and article.author.name != '':
self.authors[article.author].append(article)
Expand Down
2 changes: 1 addition & 1 deletion pelican/readers.py
Expand Up @@ -29,7 +29,7 @@
'tags': lambda x, y: [Tag(tag, y) for tag in unicode(x).split(',')],
'date': lambda x, y: get_date(x),
'status': lambda x, y: unicode.strip(x),
'category': Category,
'categories': lambda x, y: [Category(cat, y) for cat in unicode(x).split(',')],
'author': Author,
}

Expand Down
12 changes: 10 additions & 2 deletions pelican/tools/pelican_import.py
Expand Up @@ -203,9 +203,10 @@ def build_markdown_header(title, date, author, categories, tags, slug):
if date:
header += 'Date: %s\n' % date
if author:
header += 'Author: %s\n' % author
#header += 'Author: %s\n' % author
header += 'Author: fitoria\n'
if categories:
header += 'Category: %s\n' % ', '.join(categories)
header += 'Categories: %s\n' % ','.join(categories)
if tags:
header += 'Tags: %s\n' % ', '.join(tags)
if slug:
Expand All @@ -226,12 +227,19 @@ def fields2pelican(fields, out_markup, output_path, dircat=False, strip_raw=Fals

filename = os.path.basename(filename)

year, month = date.split('-')[:2]

# option to put files in directories with categories names
if dircat and (len(categories) > 0):
catname = slugify(categories[0])
out_filename = os.path.join(output_path, catname, filename+ext)
if not os.path.isdir(os.path.join(output_path, catname)):
os.mkdir(os.path.join(output_path, catname))
elif year and month:
month_dir = os.path.join(output_path, year, month)
if not os.path.exists(month_dir):
os.makedirs(month_dir)
out_filename = os.path.join(month_dir, filename+ext)
else:
out_filename = os.path.join(output_path, filename+ext)

Expand Down

0 comments on commit 697c627

Please sign in to comment.