mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
some improvements for importers:
- for dotclear: remove seconds in date, remove \\n - fix categories for wordpress (categories is a list a string) - refactor a bit the code between markdown and rst
This commit is contained in:
parent
79e675b911
commit
3b37b42633
1 changed files with 33 additions and 26 deletions
|
|
@ -27,7 +27,9 @@ def wp2fields(xml):
|
||||||
date = time.strftime("%Y-%m-%d %H:%M", date_object)
|
date = time.strftime("%Y-%m-%d %H:%M", date_object)
|
||||||
|
|
||||||
author = item.fetch('dc:creator')[0].contents[0].title()
|
author = item.fetch('dc:creator')[0].contents[0].title()
|
||||||
categories = [(cat['nicename'],cat.contents[0]) for cat in item.fetch(domain='category')]
|
|
||||||
|
categories = [cat.contents[0] for cat in item.fetch(domain='category')]
|
||||||
|
# caturl = [cat['nicename'] for cat in item.fetch(domain='category')]
|
||||||
|
|
||||||
tags = [tag.contents[0].title() for tag in item.fetch(domain='tag', nicename=None)]
|
tags = [tag.contents[0].title() for tag in item.fetch(domain='tag', nicename=None)]
|
||||||
|
|
||||||
|
|
@ -101,16 +103,22 @@ def dc2fields(file):
|
||||||
# post_meta = fields[27]
|
# post_meta = fields[27]
|
||||||
# redirect_url = fields[28][:-1]
|
# redirect_url = fields[28][:-1]
|
||||||
|
|
||||||
|
# remove seconds
|
||||||
|
post_creadt = ':'.join(post_creadt.split(':')[0:2])
|
||||||
|
|
||||||
author = ""
|
author = ""
|
||||||
categories = ""
|
categories = []
|
||||||
|
tags = []
|
||||||
|
|
||||||
if cat_id:
|
if cat_id:
|
||||||
categories = category_list[cat_id]
|
categories = [category_list[id].strip() for id in cat_id.split(',')]
|
||||||
tags = ""
|
|
||||||
|
|
||||||
if post_format == "markdown":
|
if post_format == "markdown":
|
||||||
content = post_excerpt + post_content
|
content = post_excerpt + post_content
|
||||||
else:
|
else:
|
||||||
content = post_excerpt_xhtml + post_content_xhtml
|
content = post_excerpt_xhtml + post_content_xhtml
|
||||||
|
content = content.replace('\\n', '')
|
||||||
|
post_format = "html"
|
||||||
|
|
||||||
yield (post_title, content, post_url, post_creadt, author, categories, tags, post_format)
|
yield (post_title, content, post_url, post_creadt, author, categories, tags, post_format)
|
||||||
|
|
||||||
|
|
@ -135,7 +143,7 @@ def build_header(title, date, author, categories, tags):
|
||||||
if date:
|
if date:
|
||||||
header += ':date: %s\n' % date
|
header += ':date: %s\n' % date
|
||||||
if categories:
|
if categories:
|
||||||
header += ':category: %s\n' % categories
|
header += ':category: %s\n' % ', '.join(categories)
|
||||||
if tags:
|
if tags:
|
||||||
header += ':tags: %s\n' % ', '.join(tags)
|
header += ':tags: %s\n' % ', '.join(tags)
|
||||||
header += '\n'
|
header += '\n'
|
||||||
|
|
@ -147,7 +155,7 @@ def build_markdown_header(title, date, author, categories, tags):
|
||||||
if date:
|
if date:
|
||||||
header += 'Date: %s\n' % date
|
header += 'Date: %s\n' % date
|
||||||
if categories:
|
if categories:
|
||||||
header += 'Category: %s\n' % categories
|
header += 'Category: %s\n' % ', '.join(categories)
|
||||||
if tags:
|
if tags:
|
||||||
header += 'Tags: %s\n' % ', '.join(tags)
|
header += 'Tags: %s\n' % ', '.join(tags)
|
||||||
header += '\n'
|
header += '\n'
|
||||||
|
|
@ -156,40 +164,39 @@ def build_markdown_header(title, date, author, categories, tags):
|
||||||
def fields2pelican(fields, output_path):
|
def fields2pelican(fields, output_path):
|
||||||
for title, content, filename, date, author, categories, tags, markup in fields:
|
for title, content, filename, date, author, categories, tags, markup in fields:
|
||||||
if markup == "markdown":
|
if markup == "markdown":
|
||||||
md_filename = os.path.join(output_path, filename+'.md')
|
ext = '.md'
|
||||||
header = build_markdown_header(title, date, author, categories, tags)
|
header = build_markdown_header(title, date, author, categories, tags)
|
||||||
|
|
||||||
# content.replace('\r\n', '\n')
|
|
||||||
|
|
||||||
with open(md_filename, 'w', encoding='utf-8') as fp:
|
|
||||||
fp.write(header+content)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
filename = os.path.basename(filename)
|
ext = '.rst'
|
||||||
html_filename = os.path.join(output_path, filename+'.html')
|
header = build_header(title, date, author, categories, tags)
|
||||||
|
|
||||||
# if(len(categories) == 1):
|
# TODO: add options to put files in directories by categories
|
||||||
# rst_filename = os.path.join(output_path, categories[0][0], filename+'.rst')
|
# if(len(categories) == 1):
|
||||||
# if not os.path.isdir(os.path.join(output_path, categories[0][0])):
|
# out_filename = os.path.join(output_path, categories[0], filename+'.rst')
|
||||||
# os.mkdir(os.path.join(output_path, categories[0][0]))
|
# if not os.path.isdir(os.path.join(output_path, categories[0])):
|
||||||
# else:
|
# os.mkdir(os.path.join(output_path, categories[0]))
|
||||||
rst_filename = os.path.join(output_path, filename+'.rst')
|
# else:
|
||||||
|
|
||||||
|
filename = os.path.basename(filename)
|
||||||
|
out_filename = os.path.join(output_path, filename+ext)
|
||||||
|
print out_filename
|
||||||
|
|
||||||
|
if markup == "html":
|
||||||
|
html_filename = os.path.join(output_path, filename+'.html')
|
||||||
|
|
||||||
with open(html_filename, 'w', encoding='utf-8') as fp:
|
with open(html_filename, 'w', encoding='utf-8') as fp:
|
||||||
fp.write(content)
|
fp.write(content)
|
||||||
|
|
||||||
print rst_filename
|
os.system('pandoc --normalize --reference-links --from=html --to=rst -o %s %s' % (out_filename,
|
||||||
os.system('pandoc --normalize --reference-links --from=html --to=rst -o %s %s' % (rst_filename,
|
|
||||||
html_filename))
|
html_filename))
|
||||||
|
|
||||||
os.remove(html_filename)
|
os.remove(html_filename)
|
||||||
|
|
||||||
with open(rst_filename, 'r', encoding='utf-8') as fs:
|
with open(out_filename, 'r', encoding='utf-8') as fs:
|
||||||
content = fs.read()
|
content = fs.read()
|
||||||
with open(rst_filename, 'w', encoding='utf-8') as fs:
|
|
||||||
# categories = [x[1] for x in categories]
|
with open(out_filename, 'w', encoding='utf-8') as fs:
|
||||||
header = build_header(title, date, author, categories, tags)
|
fs.write(header + content)
|
||||||
fs.write(header + content)
|
|
||||||
|
|
||||||
|
|
||||||
def main(input_type, input, output_path):
|
def main(input_type, input, output_path):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue