From 1cc5017fb49deecfc0edcd321a3ff776a9a90ea0 Mon Sep 17 00:00:00 2001 From: FELD Boris Date: Mon, 24 Jan 2011 23:50:29 +0100 Subject: [PATCH 1/4] Correct bug in date format importation. --- tools/wp2pelican.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/wp2pelican.py b/tools/wp2pelican.py index 9a52a8f8..27678126 100755 --- a/tools/wp2pelican.py +++ b/tools/wp2pelican.py @@ -15,7 +15,11 @@ def wp2html(xml): title = item.title.contents[0] content = item.fetch('content:encoded')[0].contents[0] filename = item.fetch('wp:post_name')[0].contents[0] - date = item.fetch('wp:post_date')[0].contents[0] + + raw_date = item.fetch('wp:post_date')[0].contents[0] + date_object = time.strptime(raw_date, "%Y-%m-%d %H:%M:%S") + date = time.strftime("%Y-%m-%d %H:%M", date_object) + author = item.fetch('dc:creator')[0].contents[0].title() yield (title, content, filename, date, author) From f4450b0091fc24cb68be3aa13adcc456fca08c98 Mon Sep 17 00:00:00 2001 From: FELD Boris Date: Mon, 24 Jan 2011 23:57:53 +0100 Subject: [PATCH 2/4] Add extraction of categories and tags, put rst files in category subdir. --- tools/wp2pelican.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tools/wp2pelican.py b/tools/wp2pelican.py index 27678126..b00662d3 100755 --- a/tools/wp2pelican.py +++ b/tools/wp2pelican.py @@ -21,7 +21,11 @@ def wp2html(xml): date = time.strftime("%Y-%m-%d %H:%M", date_object) author = item.fetch('dc:creator')[0].contents[0].title() - yield (title, content, filename, date, author) + categories = [(cat['nicename'],cat.contents[0]) for cat in item.fetch(domain='category')] + + tags = [tag.contents[0].title() for tag in item.fetch(domain='tag', nicename=None)] + + yield (title, content, filename, date, author, categories, tags) if __name__ == '__main__': parser = argparse.ArgumentParser(description="""Transform a wordpress xml export into rst files """) @@ -30,14 +34,22 @@ if __name__ == '__main__': parser.add_argument('-o', '--output', dest='output', default='output', help='Output path') args = parser.parse_args() - for title, content, filename, date, author in wp2html(args.xml): + for title, content, filename, date, author, categories in wp2html(args.xml): html_filename = os.path.join(args.output, filename+'.html') - rst_filename = os.path.join(args.output, filename+'.rst') + + if(len(categories) == 1): + rst_filename = os.path.join(args.output, categories[0][0], filename+'.rst.dr') + if not os.path.isdir(os.path.join(args.output, categories[0][0])): + os.mkdir(os.path.join(args.output, categories[0][0])) + else: + rst_filename = os.path.join(args.output, filename+'.rst.dr') with open(html_filename, 'w', encoding='utf-8') as fp: fp.write(content) + os.system('pandoc --from=html --to=rst -o %s %s' % (rst_filename, html_filename)) + with open(rst_filename, 'r', encoding='utf-8') as fs: content = fs.read() with open(rst_filename, 'w', encoding='utf-8') as fs: From 246753c6d4607dd146bf70e3535fc61201ac5ddb Mon Sep 17 00:00:00 2001 From: FELD Boris Date: Mon, 24 Jan 2011 23:59:15 +0100 Subject: [PATCH 3/4] Remove temporary html files after their utilisation. --- tools/wp2pelican.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/wp2pelican.py b/tools/wp2pelican.py index b00662d3..f4b3988a 100755 --- a/tools/wp2pelican.py +++ b/tools/wp2pelican.py @@ -49,6 +49,8 @@ if __name__ == '__main__': os.system('pandoc --from=html --to=rst -o %s %s' % (rst_filename, html_filename)) + + os.remove(html_filename) with open(rst_filename, 'r', encoding='utf-8') as fs: content = fs.read() From a5203ae3634b1a6b486673635ad4793368690183 Mon Sep 17 00:00:00 2001 From: FELD Boris Date: Tue, 25 Jan 2011 00:02:00 +0100 Subject: [PATCH 4/4] Refactor building of header part. --- tools/wp2pelican.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tools/wp2pelican.py b/tools/wp2pelican.py index f4b3988a..0407175c 100755 --- a/tools/wp2pelican.py +++ b/tools/wp2pelican.py @@ -4,6 +4,7 @@ from BeautifulSoup import BeautifulStoneSoup from codecs import open import os import argparse +import time def wp2html(xml): xmlfile = open(xml, encoding='utf-8').read() @@ -27,6 +28,17 @@ def wp2html(xml): yield (title, content, filename, date, author, categories, tags) +def buildHeader(title, date, author, categories, tags): + header = '%s\n%s\n' % (title, '#' * len(title)) + if (date != None): + header += ':date: %s\n' % (date) + if (categories != []): + header += ':category: %s\n' % (', '.join(categories)) + if (tags != []): + header += ':tags: %s\n' % (', '.join(tags)) + header += '\n' + return header + if __name__ == '__main__': parser = argparse.ArgumentParser(description="""Transform a wordpress xml export into rst files """) @@ -55,5 +67,6 @@ if __name__ == '__main__': with open(rst_filename, 'r', encoding='utf-8') as fs: content = fs.read() with open(rst_filename, 'w', encoding='utf-8') as fs: - header = '%s\n%s\n\n:date: %s\n:author: %s\n\n' % (title, '#' * len(title) ,date, author) - fs.write(header+content) + categories = [x[1] for x in categories] + header = buildHeader(title, date, author, categories, tags) + fs.write(header+content) \ No newline at end of file