This commit is contained in:
Chien-Wei Huang 2013-12-29 05:58:33 -08:00
commit 027bf0eb5d
4 changed files with 26 additions and 7 deletions

View file

@ -201,6 +201,7 @@ Also, you can use other file metadata attributes as well:
* lang
* author
* category
* post_id
Example usage:

View file

@ -371,6 +371,21 @@ class TestArticle(TestPage):
self.assertEqual(article.url, 'obrien/csharp-stuff/fnord/')
self.assertEqual(article.save_as, 'obrien/csharp-stuff/fnord/index.html')
def test_slugify_post_id(self):
settings = get_settings()
settings['SLUG_SUBSTITUTIONS'] = [ ('C#', 'csharp') ]
settings['ARTICLE_URL'] = '{post_id}/{slug}/'
settings['ARTICLE_SAVE_AS'] = '{post_id}/{slug}/index.html'
article_kwargs = self._copy_page_kwargs()
article_kwargs['metadata']['author'] = "O'Brien"
article_kwargs['metadata']['category'] = 'C# & stuff'
article_kwargs['metadata']['title'] = 'fnord'
article_kwargs['metadata']['post_id'] = '10'
article_kwargs['settings'] = settings
article = Article(**article_kwargs)
self.assertEqual(article.url, '10/fnord/')
self.assertEqual(article.save_as, '10/fnord/index.html')
class TestURLWrapper(unittest.TestCase):
def test_comparisons(self):

View file

@ -32,7 +32,7 @@ class TestWordpressXmlImporter(unittest.TestCase):
def test_ignore_empty_posts(self):
self.assertTrue(self.posts)
for title, content, fname, date, author, categ, tags, kind, format in self.posts:
for title, content, fname, date, author, post_id, categ, tags, kind, format in self.posts:
self.assertTrue(title.strip())
def test_recognise_page_kind(self):
@ -40,7 +40,7 @@ class TestWordpressXmlImporter(unittest.TestCase):
self.assertTrue(self.posts)
# Collect (title, filename, kind) of non-empty posts recognised as page
pages_data = []
for title, content, fname, date, author, categ, tags, kind, format in self.posts:
for title, content, fname, date, author, post_id, categ, tags, kind, format in self.posts:
if kind == 'page':
pages_data.append((title, fname))
self.assertEqual(2, len(pages_data))

View file

@ -124,6 +124,7 @@ def wp2fields(xml):
logger.warn('Post "%s" is lacking a proper title' % title)
content = item.find('encoded').string
post_id = item.find('post_id').string
filename = item.find('post_name').string
if filename is None:
@ -133,7 +134,7 @@ def wp2fields(xml):
date_object = time.strptime(raw_date, "%Y-%m-%d %H:%M:%S")
date = time.strftime("%Y-%m-%d %H:%M", date_object)
author = item.find('creator').string
categories = [cat.string for cat in item.findAll('category', {'domain' : 'category'})]
# caturl = [cat['nicename'] for cat in item.find(domain='category')]
@ -143,7 +144,7 @@ def wp2fields(xml):
if item.find('post_type').string == 'page':
kind = 'page'
yield (title, content, filename, date, author, categories, tags,
yield (title, content, filename, date, author, post_id, categories, tags,
kind, "wp-html")
def dc2fields(file):
@ -445,13 +446,15 @@ def build_header(title, date, author, categories, tags, slug):
header += '\n'
return header
def build_markdown_header(title, date, author, categories, tags, slug):
def build_markdown_header(title, date, author, post_id, categories, tags, slug):
"""Build a header from a list of fields"""
header = 'Title: %s\n' % title
if date:
header += 'Date: %s\n' % date
if author:
header += 'Author: %s\n' % author
if post_id:
header += 'Post_ID: %s\n' % post_id
if categories:
header += 'Category: %s\n' % ', '.join(categories)
if tags:
@ -464,14 +467,14 @@ def build_markdown_header(title, date, author, categories, tags, slug):
def fields2pelican(fields, out_markup, output_path,
dircat=False, strip_raw=False, disable_slugs=False,
dirpage=False, filename_template=None, filter_author=None):
for (title, content, filename, date, author, categories, tags,
for (title, content, filename, date, author, post_id, categories, tags,
kind, in_markup) in fields:
if filter_author and filter_author != author:
continue
slug = not disable_slugs and filename or None
if (in_markup == "markdown") or (out_markup == "markdown") :
ext = '.md'
header = build_markdown_header(title, date, author, categories, tags, slug)
header = build_markdown_header(title, date, author, post_id, categories, tags, slug)
else:
out_markup = "rst"
ext = '.rst'