From e84e5782f0aa17b1cddc36f6c19052dea039ce97 Mon Sep 17 00:00:00 2001 From: carlcarl Date: Sun, 6 Oct 2013 14:57:35 +0800 Subject: [PATCH 1/4] Support embed post id in article meta, and used in url structure --- pelican/tools/pelican_import.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pelican/tools/pelican_import.py b/pelican/tools/pelican_import.py index 46410c13..d50c298b 100755 --- a/pelican/tools/pelican_import.py +++ b/pelican/tools/pelican_import.py @@ -124,13 +124,14 @@ 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 raw_date = item.find('post_date').string 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')] @@ -140,7 +141,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): @@ -440,13 +441,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: @@ -459,14 +462,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' From 1621f6c8e7cd78b62e0b47ce3a92039314e084e3 Mon Sep 17 00:00:00 2001 From: carlcarl Date: Sun, 6 Oct 2013 19:50:22 +0800 Subject: [PATCH 2/4] Add post_id to the metadata in test_importer --- pelican/tests/test_importer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pelican/tests/test_importer.py b/pelican/tests/test_importer.py index 53809e7e..c2a20ddb 100644 --- a/pelican/tests/test_importer.py +++ b/pelican/tests/test_importer.py @@ -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)) From 27ef34c19f898f79bc8046ec515b743ff14377b0 Mon Sep 17 00:00:00 2001 From: carlcarl Date: Sun, 6 Oct 2013 19:51:29 +0800 Subject: [PATCH 3/4] Add article output path testing include post_id --- pelican/tests/test_contents.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index 437d0228..68830bbe 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -293,6 +293,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): From 9447e0991b7f58dd6bcc8486fa626799e4ceed40 Mon Sep 17 00:00:00 2001 From: carlcarl Date: Sun, 6 Oct 2013 19:52:26 +0800 Subject: [PATCH 4/4] Add post_id field in settings.rst --- docs/settings.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/settings.rst b/docs/settings.rst index 82752436..669b899f 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -201,6 +201,7 @@ Also, you can use other file metadata attributes as well: * lang * author * category +* post_id Example usage: