diff --git a/custom_article_urls/README.md b/custom_article_urls/README.md new file mode 100644 index 0000000..2b87d18 --- /dev/null +++ b/custom_article_urls/README.md @@ -0,0 +1,36 @@ +#Custom Article URLs# + +Adds support for defining different default urls for different categories, or +different subcategories if using the subcategory plugin. + +##Usage## + +After adding `custom_article_urls` to your `PLUGINS` add a `CUSTOM_ARTICLE_URLS` +setting, which is a dictionary of rules. The rules are also a dictionary, +consisting of the `URL` and the `SAVE_AS` values. + +For example, if you had two categories, Category 1 and Category 2 and you +would like Category 1 saved as category-1/article-slug/ and Category 2 saved as +/year/month/article-slug/ you would add: + + CUSTOM_ARTICLE_URLS = { + 'Category 1': {'URL': '{category}/{slug}/, + 'SAVE_AS': '{category}/{slug}/index.html}, + 'Category 2': {'URL': '{date:%Y}/{date:%B}/{slug}/, + 'SAVE_AS': '{date:%Y}/{date:%B}/{slug}/index.html}, + } + +If had any other categories they would use the default `ARTICLE_SAVE_AS` +and `ARTICLE_URL` + +If you are using the subcategory plugin, you can define them the same way. +For example if Category 1 had a subcategory Sub Category you could define +it's rules with + + 'Category 1/Sub Category`: ... + +##Other Usage: Article Metadata## + +If you define a url and save_as in your articles metadata, then this plugin +will not alter that value. So you can still specify special one off urls as +normal. diff --git a/custom_article_urls/__init__.py b/custom_article_urls/__init__.py new file mode 100644 index 0000000..ed876ab --- /dev/null +++ b/custom_article_urls/__init__.py @@ -0,0 +1 @@ +from .custom_article_urls import * diff --git a/custom_article_urls/custom_article_urls.py b/custom_article_urls/custom_article_urls.py new file mode 100644 index 0000000..7b8a4e8 --- /dev/null +++ b/custom_article_urls/custom_article_urls.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +""" +@Author: Alistair Magee + +Adds ability to specify custom urls for different categories +(or subcategories if using subcategory plugin) of article +using a dictionary stored in pelican settings file as +{category: {article_url_structure: stirng, article_save_as: string}} +""" +from pelican import signals +from pelican.contents import Content, Category +from six import text_type + +def recursive_name(self): + if type(self) is Category: + return self.name + else: + return '{}/{}'.format(recursive_name(self.parent), self.name) + +def custom_url(generator, metadata): + if 'CUSTOM_ARTICLE_URLS' in generator.settings: + custom_urls = generator.settings['CUSTOM_ARTICLE_URLS'] + category = text_type(metadata['category']) + pattern_matched = {} + + if category in custom_urls: + pattern_matched = custom_urls[category] + + if 'subcategories' in metadata: #using subcategory plugin + for subcategory in metadata['subcategories']: + subcategory_name = recursive_name(subcategory) + if subcategory_name in custom_urls: + pattern_matched = custom_urls[subcategory_name] + + if pattern_matched: + #only alter url if hasn't been set in the metdata + if ('url', 'save_as') in metadata: + """ if both url and save_as are set in the metadata already + then there is already a custom url set, skip this one + """ + pass + else: + temp_article = Content(None, metadata=metadata) + url_format = pattern_matched['URL'] + save_as_format = pattern_matched['SAVE_AS'] + url = url_format.format(**temp_article.url_format) + save_as = save_as_format.format(**temp_article.url_format) + metadata.update({'url': url, 'save_as': save_as}) + + +def register(): + signals.article_generator_context.connect(custom_url)