1
0
Fork 0
forked from github/pelican
pelican-theme/pelican/tools/pelican_import.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1126 lines
37 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2015-06-16 09:25:09 +02:00
import argparse
import datetime
2015-06-16 09:25:09 +02:00
import logging
import os
import re
import subprocess
import sys
import tempfile
import time
from collections import defaultdict
2019-11-17 19:19:37 +03:00
from html import unescape
from urllib.error import URLError
from urllib.parse import quote, urlparse, urlsplit, urlunsplit
from urllib.request import urlretrieve
# because logging.setLoggerClass has to be called before logging.getLogger
from pelican.log import init
from pelican.settings import DEFAULT_CONFIG
2015-06-16 09:25:09 +02:00
from pelican.utils import SafeDatetime, slugify
logger = logging.getLogger(__name__)
def decode_wp_content(content, br=True):
pre_tags = {}
if content.strip() == "":
return ""
content += "\n"
if "<pre" in content:
pre_parts = content.split("</pre>")
last_pre = pre_parts.pop()
content = ""
pre_index = 0
for pre_part in pre_parts:
start = pre_part.find("<pre")
if start == -1:
content = content + pre_part
continue
name = "<pre wp-pre-tag-{}></pre>".format(pre_index)
pre_tags[name] = pre_part[start:] + "</pre>"
content = content + pre_part[0:start] + name
pre_index += 1
content = content + last_pre
content = re.sub(r"<br />\s*<br />", "\n\n", content)
allblocks = (
"(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|"
"td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|"
"map|area|blockquote|address|math|style|p|h[1-6]|hr|"
"fieldset|noscript|samp|legend|section|article|aside|"
"hgroup|header|footer|nav|figure|figcaption|details|"
"menu|summary)"
)
content = re.sub(r"(<" + allblocks + r"[^>]*>)", "\n\\1", content)
content = re.sub(r"(</" + allblocks + r">)", "\\1\n\n", content)
# content = content.replace("\r\n", "\n")
if "<object" in content:
# no <p> inside object/embed
content = re.sub(r"\s*<param([^>]*)>\s*", "<param\\1>", content)
content = re.sub(r"\s*</embed>\s*", "</embed>", content)
# content = re.sub(r'/\n\n+/', '\n\n', content)
pgraphs = filter(lambda s: s != "", re.split(r"\n\s*\n", content))
content = ""
for p in pgraphs:
content = content + "<p>" + p.strip() + "</p>\n"
2015-06-16 09:25:09 +02:00
# under certain strange conditions it could create
# a P of entirely whitespace
content = re.sub(r"<p>\s*</p>", "", content)
2015-06-16 09:25:09 +02:00
content = re.sub(r"<p>([^<]+)</(div|address|form)>", "<p>\\1</p></\\2>", content)
# don't wrap tags
2015-06-16 09:25:09 +02:00
content = re.sub(r"<p>\s*(</?" + allblocks + r"[^>]*>)\s*</p>", "\\1", content)
# problem with nested lists
content = re.sub(r"<p>(<li.*)</p>", "\\1", content)
content = re.sub(r"<p><blockquote([^>]*)>", "<blockquote\\1><p>", content)
content = content.replace("</blockquote></p>", "</p></blockquote>")
content = re.sub(r"<p>\s*(</?" + allblocks + "[^>]*>)", "\\1", content)
content = re.sub(r"(</?" + allblocks + r"[^>]*>)\s*</p>", "\\1", content)
if br:
def _preserve_newline(match):
return match.group(0).replace("\n", "<WPPreserveNewline />")
2015-06-16 09:25:09 +02:00
content = re.sub(r"/<(script|style).*?<\/\\1>/s", _preserve_newline, content)
# optionally make line breaks
content = re.sub(r"(?<!<br />)\s*\n", "<br />\n", content)
content = content.replace("<WPPreserveNewline />", "\n")
2015-06-16 09:25:09 +02:00
content = re.sub(r"(</?" + allblocks + r"[^>]*>)\s*<br />", "\\1", content)
content = re.sub(
r"<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)", "\\1", content
)
content = re.sub(r"\n</p>", "</p>", content)
if pre_tags:
def _multi_replace(dic, string):
pattern = r"|".join(map(re.escape, dic.keys()))
return re.sub(pattern, lambda m: dic[m.group()], string)
content = _multi_replace(pre_tags, content)
# convert [caption] tags into <figure>
content = re.sub(
r"\[caption(?:.*?)(?:caption=\"(.*?)\")?\]"
r"((?:\<a(?:.*?)\>)?(?:\<img.*?\>)(?:\<\/a\>)?)\s?(.*?)\[\/caption\]",
r"<figure>\n\2\n<figcaption>\1\3</figcaption>\n</figure>",
content,
)
return content
2015-06-16 09:25:09 +02:00
2018-08-07 14:06:46 +02:00
def xml_to_soup(xml):
"""Opens an xml file"""
try:
from bs4 import BeautifulSoup
except ImportError:
2015-06-16 09:25:09 +02:00
error = (
'Missing dependency "BeautifulSoup4" and "lxml" required to '
2018-08-07 14:06:46 +02:00
"import XML files."
)
sys.exit(error)
with open(xml, encoding="utf-8") as infile:
xmlfile = infile.read()
2014-02-09 08:45:06 -08:00
soup = BeautifulSoup(xmlfile, "xml")
2018-08-07 14:06:46 +02:00
return soup
2015-06-16 09:25:09 +02:00
def get_filename(post_name, post_id):
if post_name is None or post_name.isspace():
return post_id
else:
return post_name
2015-06-16 09:25:09 +02:00
def wp2fields(xml, wp_custpost=False):
"""Opens a wordpress XML file, and yield Pelican fields"""
2013-07-05 01:08:45 +02:00
2018-08-07 14:06:46 +02:00
soup = xml_to_soup(xml)
items = soup.rss.channel.findAll("item")
for item in items:
if item.find("status").string in ["publish", "draft"]:
try:
# Use HTMLParser due to issues with BeautifulSoup 3
title = unescape(item.title.contents[0])
except IndexError:
title = "No title [%s]" % item.find("post_name").string
logger.warning('Post "%s" is lacking a proper title', title)
post_name = item.find("post_name").string
post_id = item.find("post_id").string
filename = get_filename(post_name, post_id)
2013-07-05 01:08:45 +02:00
content = item.find("encoded").string
raw_date = item.find("post_date").string
if raw_date == "0000-00-00 00:00:00":
date = None
else:
2018-08-07 14:06:46 +02:00
date_object = SafeDatetime.strptime(raw_date, "%Y-%m-%d %H:%M:%S")
date = date_object.strftime("%Y-%m-%d %H:%M")
author = item.find("creator").string
2015-06-16 09:25:09 +02:00
categories = [
cat.string for cat in item.findAll("category", {"domain": "category"})
]
2015-06-16 09:25:09 +02:00
tags = [
tag.string for tag in item.findAll("category", {"domain": "post_tag"})
]
# To publish a post the status should be 'published'
2015-06-16 09:25:09 +02:00
status = (
"published"
if item.find("status").string == "publish"
else item.find("status").string
)
kind = "article"
post_type = item.find("post_type").string
if post_type == "page":
kind = "page"
elif wp_custpost:
if post_type == "post":
pass
2015-06-16 09:25:09 +02:00
# Old behaviour was to name everything not a page as an
# article.Theoretically all attachments have status == inherit
# so no attachments should be here. But this statement is to
# maintain existing behaviour in case that doesn't hold true.
elif post_type == "attachment":
pass
else:
kind = post_type
2015-06-16 09:25:09 +02:00
yield (
title,
content,
filename,
date,
author,
categories,
tags,
status,
kind,
"wp-html",
)
2018-08-07 14:06:46 +02:00
def blogger2fields(xml):
"""Opens a blogger XML file, and yield Pelican fields"""
soup = xml_to_soup(xml)
entries = soup.feed.findAll("entry")
for entry in entries:
raw_kind = entry.find(
"category", {"scheme": "http://schemas.google.com/g/2005#kind"}
).get("term")
if raw_kind == "http://schemas.google.com/blogger/2008/kind#post":
kind = "article"
elif raw_kind == "http://schemas.google.com/blogger/2008/kind#comment":
kind = "comment"
elif raw_kind == "http://schemas.google.com/blogger/2008/kind#page":
kind = "page"
else:
continue
try:
assert kind != "comment"
filename = entry.find("link", {"rel": "alternate"})["href"]
filename = os.path.splitext(os.path.basename(filename))[0]
except (AssertionError, TypeError, KeyError):
filename = entry.find("id").string.split(".")[-1]
title = entry.find("title").string or ""
content = entry.find("content").string
raw_date = entry.find("published").string
if hasattr(SafeDatetime, "fromisoformat"):
date_object = SafeDatetime.fromisoformat(raw_date)
else:
date_object = SafeDatetime.strptime(raw_date[:23], "%Y-%m-%dT%H:%M:%S.%f")
date = date_object.strftime("%Y-%m-%d %H:%M")
author = entry.find("author").find("name").string
# blogger posts only have tags, no category
tags = [
tag.get("term")
for tag in entry.findAll(
"category", {"scheme": "http://www.blogger.com/atom/ns#"}
)
2018-08-07 14:06:46 +02:00
]
# Drafts have <app:control><app:draft>yes</app:draft></app:control>
status = "published"
try:
if entry.find("control").find("draft").string == "yes":
status = "draft"
except AttributeError:
pass
yield (title, content, filename, date, author, None, tags, status, kind, "html")
def dc2fields(file):
"""Opens a Dotclear export file, and yield pelican fields"""
try:
from bs4 import BeautifulSoup
except ImportError:
2012-04-18 22:14:53 -07:00
error = (
"Missing dependency "
2015-06-16 09:25:09 +02:00
'"BeautifulSoup4" and "lxml" required '
"to import Dotclear files."
)
sys.exit(error)
in_cat = False
in_post = False
category_list = {}
posts = []
with open(file, encoding="utf-8") as f:
for line in f:
# remove final \n
line = line[:-1]
if line.startswith("[category"):
in_cat = True
elif line.startswith("[post"):
in_post = True
elif in_cat:
fields = line.split('","')
if not line:
in_cat = False
else:
# remove 1st and last ""
fields[0] = fields[0][1:]
# fields[-1] = fields[-1][:-1]
2015-06-16 09:25:09 +02:00
category_list[fields[0]] = fields[2]
elif in_post:
if not line:
in_post = False
break
else:
posts.append(line)
2012-03-07 12:14:13 +00:00
print("%i posts read." % len(posts))
subs = DEFAULT_CONFIG["SLUG_REGEX_SUBSTITUTIONS"]
for post in posts:
fields = post.split('","')
# post_id = fields[0][1:]
# blog_id = fields[1]
# user_id = fields[2]
cat_id = fields[3]
# post_dt = fields[4]
# post_tz = fields[5]
post_creadt = fields[6]
# post_upddt = fields[7]
# post_password = fields[8]
# post_type = fields[9]
post_format = fields[10]
# post_url = fields[11]
# post_lang = fields[12]
post_title = fields[13]
post_excerpt = fields[14]
post_excerpt_xhtml = fields[15]
post_content = fields[16]
post_content_xhtml = fields[17]
# post_notes = fields[18]
# post_words = fields[19]
# post_status = fields[20]
# post_selected = fields[21]
# post_position = fields[22]
# post_open_comment = fields[23]
# post_open_tb = fields[24]
# nb_comment = fields[25]
# nb_trackback = fields[26]
post_meta = fields[27]
# redirect_url = fields[28][:-1]
# remove seconds
post_creadt = ":".join(post_creadt.split(":")[0:2])
2015-06-16 09:25:09 +02:00
author = ""
categories = []
tags = []
if cat_id:
2015-06-16 09:25:09 +02:00
categories = [category_list[id].strip() for id in cat_id.split(",")]
2011-09-30 22:48:16 +02:00
# Get tags related to a post
2015-06-16 09:25:09 +02:00
tag = (
post_meta.replace("{", "")
.replace("}", "")
.replace('a:1:s:3:\\"tag\\";a:', "")
.replace("a:0:", "")
)
if len(tag) > 1:
if int(len(tag[:1])) == 1:
newtag = tag.split('"')[1]
tags.append(
BeautifulSoup(newtag, "xml")
# bs4 always outputs UTF-8
.decode("utf-8")
)
else:
2015-06-16 09:25:09 +02:00
i = 1
j = 1
while i <= int(tag[:1]):
2015-06-16 09:25:09 +02:00
newtag = tag.split('"')[j].replace("\\", "")
tags.append(
BeautifulSoup(newtag, "xml")
# bs4 always outputs UTF-8
.decode("utf-8")
)
2015-06-16 09:25:09 +02:00
i = i + 1
if j < int(tag[:1]) * 2:
j = j + 2
2011-08-30 22:27:43 +02:00
"""
2015-06-16 09:25:09 +02:00
dotclear2 does not use markdown by default unless
you use the markdown plugin
2011-09-30 22:48:16 +02:00
Ref: http://plugins.dotaddict.org/dc2/details/formatting-markdown
2011-08-30 22:27:43 +02:00
"""
if post_format == "markdown":
content = post_excerpt + post_content
else:
content = post_excerpt_xhtml + post_content_xhtml
content = content.replace("\\n", "")
post_format = "html"
kind = "article" # TODO: Recognise pages
status = "published" # TODO: Find a way for draft posts
yield (
post_title,
content,
slugify(post_title, regex_subs=subs),
post_creadt,
author,
categories,
tags,
status,
kind,
post_format,
)
def _get_tumblr_posts(api_key, blogname, offset=0):
import json
import urllib.request as urllib_request
url = (
"https://api.tumblr.com/v2/blog/%s.tumblr.com/"
"posts?api_key=%s&offset=%d&filter=raw"
) % (blogname, api_key, offset)
request = urllib_request.Request(url)
handle = urllib_request.urlopen(request)
posts = json.loads(handle.read().decode("utf-8"))
return posts.get("response").get("posts")
def tumblr2fields(api_key, blogname):
"""Imports Tumblr posts (API v2)"""
offset = 0
posts = _get_tumblr_posts(api_key, blogname, offset)
subs = DEFAULT_CONFIG["SLUG_REGEX_SUBSTITUTIONS"]
while len(posts) > 0:
for post in posts:
2015-06-16 09:25:09 +02:00
title = (
post.get("title")
or post.get("source_title")
or post.get("type").capitalize()
)
slug = post.get("slug") or slugify(title, regex_subs=subs)
tags = post.get("tags")
timestamp = post.get("timestamp")
date = SafeDatetime.fromtimestamp(
int(timestamp), tz=datetime.timezone.utc
).strftime("%Y-%m-%d %H:%M:%S%z")
slug = (
SafeDatetime.fromtimestamp(
int(timestamp), tz=datetime.timezone.utc
).strftime("%Y-%m-%d-")
+ slug
)
format = post.get("format")
content = post.get("body")
type = post.get("type")
if type == "photo":
if format == "markdown":
fmtstr = "![%s](%s)"
else:
fmtstr = '<img alt="%s" src="%s" />'
content = "\n".join(
fmtstr
% (photo.get("caption"), photo.get("original_size").get("url"))
for photo in post.get("photos")
)
elif type == "quote":
if format == "markdown":
fmtstr = "\n\n&mdash; %s"
else:
fmtstr = "<p>&mdash; %s</p>"
content = post.get("text") + fmtstr % post.get("source")
elif type == "link":
if format == "markdown":
fmtstr = "[via](%s)\n\n"
else:
fmtstr = '<p><a href="%s">via</a></p>\n'
content = fmtstr % post.get("url") + post.get("description")
elif type == "audio":
if format == "markdown":
fmtstr = "[via](%s)\n\n"
else:
fmtstr = '<p><a href="%s">via</a></p>\n'
2015-06-16 09:25:09 +02:00
content = (
fmtstr % post.get("source_url")
+ post.get("caption")
+ post.get("player")
)
elif type == "video":
if format == "markdown":
fmtstr = "[via](%s)\n\n"
else:
fmtstr = '<p><a href="%s">via</a></p>\n'
2015-06-16 09:25:09 +02:00
source = fmtstr % post.get("source_url")
caption = post.get("caption")
players = [
# If embed_code is False, couldn't get the video
player.get("embed_code") or None
for player in post.get("player")
]
# If there are no embeddable players, say so, once
if len(players) > 0 and all(player is None for player in players):
players = "<p>(This video isn't available anymore.)</p>\n"
else:
players = "\n".join(players)
2015-06-16 09:25:09 +02:00
content = source + caption + players
elif type == "answer":
title = post.get("question")
2015-06-16 09:25:09 +02:00
content = (
"<p>"
'<a href="%s" rel="external nofollow">%s</a>'
": %s"
"</p>\n"
" %s"
% (
post.get("asking_name"),
post.get("asking_url"),
post.get("question"),
post.get("answer"),
)
)
content = content.rstrip() + "\n"
kind = "article"
status = "published" # TODO: Find a way for draft posts
yield (
title,
content,
slug,
date,
post.get("blog_name"),
[type],
tags,
status,
kind,
format,
)
offset += len(posts)
posts = _get_tumblr_posts(api_key, blogname, offset)
2015-06-16 09:25:09 +02:00
def feed2fields(file):
"""Read a feed and yield pelican fields"""
import feedparser
d = feedparser.parse(file)
subs = DEFAULT_CONFIG["SLUG_REGEX_SUBSTITUTIONS"]
for entry in d.entries:
date = (
time.strftime("%Y-%m-%d %H:%M", entry.updated_parsed)
2015-06-16 09:25:09 +02:00
if hasattr(entry, "updated_parsed")
else None
)
author = entry.author if hasattr(entry, "author") else None
tags = [e["term"] for e in entry.tags] if hasattr(entry, "tags") else None
slug = slugify(entry.title, regex_subs=subs)
kind = "article"
2015-06-16 09:25:09 +02:00
yield (
entry.title,
entry.description,
slug,
date,
author,
[],
tags,
None,
kind,
"html",
)
2015-06-16 09:25:09 +02:00
def build_header(
title, date, author, categories, tags, slug, status=None, attachments=None
):
"""Build a header from a list of fields"""
2015-06-16 09:25:09 +02:00
from docutils.utils import column_width
header = "{}\n{}\n".format(title, "#" * column_width(title))
if date:
header += ":date: %s\n" % date
if author:
2012-09-26 19:59:47 +01:00
header += ":author: %s\n" % author
if categories:
header += ":category: %s\n" % ", ".join(categories)
if tags:
header += ":tags: %s\n" % ", ".join(tags)
if slug:
header += ":slug: %s\n" % slug
if status:
header += ":status: %s\n" % status
if attachments:
header += ":attachments: %s\n" % ", ".join(attachments)
header += "\n"
return header
def build_asciidoc_header(
title, date, author, categories, tags, slug, status=None, attachments=None
):
"""Build a header from a list of fields"""
header = "= %s\n" % title
if author:
header += "%s\n" % author
if date:
header += "%s\n" % date
if categories:
header += ":category: %s\n" % ", ".join(categories)
if tags:
header += ":tags: %s\n" % ", ".join(tags)
if slug:
header += ":slug: %s\n" % slug
if status:
header += ":status: %s\n" % status
if attachments:
header += ":attachments: %s\n" % ", ".join(attachments)
header += "\n"
return header
2015-06-16 09:25:09 +02:00
2020-04-16 08:10:30 +02:00
2015-06-16 09:25:09 +02:00
def build_markdown_header(
title, date, author, categories, tags, slug, status=None, attachments=None
):
"""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 categories:
header += "Category: %s\n" % ", ".join(categories)
if tags:
header += "Tags: %s\n" % ", ".join(tags)
if slug:
header += "Slug: %s\n" % slug
if status:
header += "Status: %s\n" % status
if attachments:
header += "Attachments: %s\n" % ", ".join(attachments)
header += "\n"
return header
2015-06-16 09:25:09 +02:00
def get_ext(out_markup, in_markup="html"):
if out_markup == "asciidoc":
ext = ".adoc"
elif in_markup == "markdown" or out_markup == "markdown":
ext = ".md"
else:
ext = ".rst"
return ext
2013-07-05 01:08:45 +02:00
2015-06-16 09:25:09 +02:00
2013-07-05 01:08:45 +02:00
def get_out_filename(
output_path,
filename,
ext,
kind,
dirpage,
dircat,
categories,
wp_custpost,
slug_subs,
):
filename = os.path.basename(filename)
# Enforce filename restrictions for various filesystems at once; see
# https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
# we do not need to filter words because an extension will be appended
2015-06-16 09:25:09 +02:00
filename = re.sub(r'[<>:"/\\|?*^% ]', "-", filename) # invalid chars
filename = filename.lstrip(".") # should not start with a dot
if not filename:
filename = "_"
2015-06-16 09:25:09 +02:00
filename = filename[:249] # allow for 5 extra characters
2015-06-16 09:25:09 +02:00
out_filename = os.path.join(output_path, filename + ext)
# option to put page posts in pages/ subdirectory
if dirpage and kind == "page":
pages_dir = os.path.join(output_path, "pages")
if not os.path.isdir(pages_dir):
os.mkdir(pages_dir)
2015-06-16 09:25:09 +02:00
out_filename = os.path.join(pages_dir, filename + ext)
elif not dirpage and kind == "page":
2013-07-05 01:08:45 +02:00
pass
# option to put wp custom post types in directories with post type
# names. Custom post types can also have categories so option to
# create subdirectories with category names
elif kind != "article":
if wp_custpost:
typename = slugify(kind, regex_subs=slug_subs)
else:
typename = ""
kind = "article"
if dircat and (len(categories) > 0):
catname = slugify(categories[0], regex_subs=slug_subs, preserve_case=True)
else:
catname = ""
2013-07-05 01:08:45 +02:00
out_filename = os.path.join(output_path, typename, catname, filename + ext)
if not os.path.isdir(os.path.join(output_path, typename, catname)):
os.makedirs(os.path.join(output_path, typename, catname))
# option to put files in directories with categories names
elif dircat and (len(categories) > 0):
catname = slugify(categories[0], regex_subs=slug_subs, preserve_case=True)
2015-06-16 09:25:09 +02:00
out_filename = os.path.join(output_path, catname, filename + ext)
if not os.path.isdir(os.path.join(output_path, catname)):
os.mkdir(os.path.join(output_path, catname))
return out_filename
2015-06-16 09:25:09 +02:00
def get_attachments(xml):
2013-07-05 01:08:45 +02:00
"""returns a dictionary of posts that have attachments with a list
of the attachment_urls
"""
2018-08-07 14:06:46 +02:00
soup = xml_to_soup(xml)
items = soup.rss.channel.findAll("item")
names = {}
attachments = []
2013-07-05 01:08:45 +02:00
for item in items:
kind = item.find("post_type").string
post_name = item.find("post_name").string
post_id = item.find("post_id").string
if kind == "attachment":
2013-07-05 01:08:45 +02:00
attachments.append(
(item.find("post_parent").string, item.find("attachment_url").string)
2015-06-16 09:25:09 +02:00
)
else:
filename = get_filename(post_name, post_id)
names[post_id] = filename
attachedposts = defaultdict(set)
for parent, url in attachments:
try:
parent_name = names[parent]
except KeyError:
2015-06-16 09:25:09 +02:00
# attachment's parent is not a valid post
parent_name = None
2013-07-05 01:08:45 +02:00
attachedposts[parent_name].add(url)
return attachedposts
2015-06-16 09:25:09 +02:00
def download_attachments(output_path, urls):
2015-05-24 13:04:35 +01:00
"""Downloads WordPress attachments and returns a list of paths to
2013-07-05 01:08:45 +02:00
attachments that can be associated with a post (relative path to output
directory). Files that fail to download, will not be added to posts"""
locations = {}
for url in urls:
path = urlparse(url).path
2015-06-16 09:25:09 +02:00
# teardown path and rebuild to negate any errors with
# os.path.join and leading /'s
path = path.split("/")
filename = path.pop(-1)
localpath = ""
for item in path:
if sys.platform != "win32" or ":" not in item:
localpath = os.path.join(localpath, item)
full_path = os.path.join(output_path, localpath)
# Generate percent-encoded URL
scheme, netloc, path, query, fragment = urlsplit(url)
if scheme != "file":
path = quote(path)
url = urlunsplit((scheme, netloc, path, query, fragment))
if not os.path.exists(full_path):
os.makedirs(full_path)
print("downloading {}".format(filename))
try:
urlretrieve(url, os.path.join(full_path, filename))
locations[url] = os.path.join(localpath, filename)
except (URLError, OSError) as e:
2015-06-16 09:25:09 +02:00
# Python 2.7 throws an IOError rather Than URLError
logger.warning("No file could be downloaded from %s\n%s", url, e)
return locations
def is_pandoc_needed(in_markup):
return in_markup in ("html", "wp-html")
def get_pandoc_version():
cmd = ["pandoc", "--version"]
try:
output = subprocess.check_output(cmd, universal_newlines=True)
except (subprocess.CalledProcessError, OSError) as e:
logger.warning("Pandoc version unknown: %s", e)
return ()
return tuple(int(i) for i in output.split()[1].split("."))
def update_links_to_attached_files(content, attachments):
for old_url, new_path in attachments.items():
# url may occur both with http:// and https://
http_url = old_url.replace("https://", "http://")
https_url = old_url.replace("http://", "https://")
for url in [http_url, https_url]:
2018-07-11 15:54:47 +02:00
content = content.replace(url, "{static}" + new_path)
return content
2015-06-16 09:25:09 +02:00
def fields2pelican(
fields,
out_markup,
output_path,
dircat=False,
strip_raw=False,
disable_slugs=False,
dirpage=False,
filename_template=None,
filter_author=None,
wp_custpost=False,
wp_attach=False,
attachments=None,
):
pandoc_version = get_pandoc_version()
posts_require_pandoc = []
slug_subs = DEFAULT_CONFIG["SLUG_REGEX_SUBSTITUTIONS"]
for (
title,
content,
filename,
date,
author,
categories,
tags,
status,
kind,
in_markup,
) in fields:
if filter_author and filter_author != author:
continue
if is_pandoc_needed(in_markup) and not pandoc_version:
posts_require_pandoc.append(filename)
slug = not disable_slugs and filename or None
if wp_attach and attachments:
try:
urls = attachments[filename]
links = download_attachments(output_path, urls)
except KeyError:
links = None
2013-07-05 01:08:45 +02:00
else:
links = None
ext = get_ext(out_markup, in_markup)
if ext == ".adoc":
header = build_asciidoc_header(
title, date, author, categories, tags, slug, status, attachments
)
elif ext == ".md":
2015-06-16 09:25:09 +02:00
header = build_markdown_header(
title,
date,
author,
categories,
tags,
slug,
status,
links.values() if links else None,
)
else:
2015-06-16 09:25:09 +02:00
out_markup = "rst"
2013-07-05 01:08:45 +02:00
header = build_header(
title,
date,
author,
categories,
tags,
slug,
status,
links.values() if links else None,
)
2015-06-16 09:25:09 +02:00
out_filename = get_out_filename(
output_path,
filename,
ext,
kind,
dirpage,
dircat,
categories,
wp_custpost,
slug_subs,
)
2012-03-07 12:14:13 +00:00
print(out_filename)
2015-06-16 09:25:09 +02:00
if in_markup in ("html", "wp-html"):
with tempfile.TemporaryDirectory() as tmpdir:
html_filename = os.path.join(tmpdir, "pandoc-input.html")
# Replace newlines with paragraphs wrapped with <p> so
# HTML is valid before conversion
2015-06-16 09:25:09 +02:00
if in_markup == "wp-html":
new_content = decode_wp_content(content)
else:
paragraphs = content.splitlines()
paragraphs = ["<p>{}</p>".format(p) for p in paragraphs]
new_content = "".join(paragraphs)
with open(html_filename, "w", encoding="utf-8") as fp:
fp.write(new_content)
if pandoc_version < (2,):
parse_raw = "--parse-raw" if not strip_raw else ""
wrap_none = (
"--wrap=none" if pandoc_version >= (1, 16) else "--no-wrap"
)
cmd = (
"pandoc --normalize {0} --from=html"
' --to={1} {2} -o "{3}" "{4}"'
)
cmd = cmd.format(
parse_raw,
out_markup if out_markup != "markdown" else "gfm",
wrap_none,
out_filename,
html_filename,
)
else:
from_arg = "-f html+raw_html" if not strip_raw else "-f html"
cmd = 'pandoc {0} --to={1}-smart --wrap=none -o "{2}" "{3}"'
cmd = cmd.format(
from_arg,
out_markup if out_markup != "markdown" else "gfm",
out_filename,
html_filename,
)
try:
rc = subprocess.call(cmd, shell=True)
if rc < 0:
error = "Child was terminated by signal %d" % -rc
exit(error)
elif rc > 0:
error = "Please, check your Pandoc installation."
exit(error)
except OSError as e:
error = "Pandoc execution failed: %s" % e
2012-06-10 13:27:36 +02:00
exit(error)
with open(out_filename, encoding="utf-8") as fs:
content = fs.read()
2015-06-16 09:25:09 +02:00
if out_markup == "markdown":
# In markdown, to insert a <br />, end a line with two
# or more spaces & then a end-of-line
content = content.replace("\\\n ", " \n")
content = content.replace("\\\n", " \n")
if wp_attach and links:
content = update_links_to_attached_files(content, links)
with open(out_filename, "w", encoding="utf-8") as fs:
fs.write(header + content)
if posts_require_pandoc:
logger.error(
"Pandoc must be installed to import the following posts:" "\n {}".format(
"\n ".join(posts_require_pandoc)
)
)
if wp_attach and attachments and None in attachments:
print("downloading attachments that don't have a parent post")
urls = attachments[None]
2015-06-16 09:25:09 +02:00
download_attachments(output_path, urls)
2013-07-05 01:08:45 +02:00
def main():
parser = argparse.ArgumentParser(
description="Transform feed, Blogger, Dotclear, Tumblr, or "
2018-08-07 14:06:46 +02:00
"WordPress files into reST (rst) or Markdown (md) files. "
2015-06-16 09:25:09 +02:00
"Be sure to have pandoc installed.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
2015-06-16 09:25:09 +02:00
parser.add_argument(dest="input", help="The input file to read")
parser.add_argument(
2018-08-07 14:06:46 +02:00
"--blogger", action="store_true", dest="blogger", help="Blogger XML export"
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--dotclear", action="store_true", dest="dotclear", help="Dotclear export"
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--tumblr", action="store_true", dest="tumblr", help="Tumblr export"
)
2018-08-07 14:06:46 +02:00
parser.add_argument(
"--wpfile", action="store_true", dest="wpfile", help="Wordpress XML export"
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--feed", action="store_true", dest="feed", help="Feed to parse"
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"-o", "--output", dest="output", default="content", help="Output path"
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"-m",
"--markup",
dest="markup",
default="rst",
help="Output markup format (supports rst & markdown)",
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--dir-cat",
action="store_true",
dest="dircat",
help="Put files in directories with categories name",
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--dir-page",
action="store_true",
dest="dirpage",
help=(
'Put files recognised as pages in "pages/" sub-directory'
2018-08-07 14:06:46 +02:00
" (blogger and wordpress import only)"
),
2018-08-07 14:06:46 +02:00
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--filter-author",
dest="author",
help="Import only post from the specified author",
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--strip-raw",
action="store_true",
dest="strip_raw",
help="Strip raw HTML code that can't be converted to "
"markup such as flash embeds or iframes (wordpress import only)",
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--wp-custpost",
action="store_true",
dest="wp_custpost",
help="Put wordpress custom post types in directories. If used with "
"--dir-cat option directories will be created as "
"/post_type/category/ (wordpress import only)",
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--wp-attach",
action="store_true",
dest="wp_attach",
help="(wordpress import only) Download files uploaded to wordpress as "
"attachments. Files will be added to posts as a list in the post "
"header. All files will be downloaded, even if "
"they aren't associated with a post. Files will be downloaded "
"with their original path inside the output directory. "
"e.g. output/wp-uploads/date/postname/file.jpg "
"-- Requires an internet connection --",
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"--disable-slugs",
action="store_true",
2013-01-11 18:44:35 +01:00
dest="disable_slugs",
help="Disable storing slugs from imported posts within output. "
"With this disabled, your Pelican URLs may not be consistent "
"with your original posts.",
)
2015-06-16 09:25:09 +02:00
parser.add_argument(
"-b", "--blogname", dest="blogname", help="Blog name (Tumblr import only)"
)
args = parser.parse_args()
input_type = None
2018-08-07 14:06:46 +02:00
if args.blogger:
input_type = "blogger"
elif args.dotclear:
input_type = "dotclear"
elif args.tumblr:
input_type = "tumblr"
2018-08-07 14:06:46 +02:00
elif args.wpfile:
input_type = "wordpress"
elif args.feed:
input_type = "feed"
else:
2018-08-07 14:06:46 +02:00
error = (
"You must provide either --blogger, --dotclear, "
"--tumblr, --wpfile or --feed options"
)
2012-06-10 13:27:36 +02:00
exit(error)
2011-10-24 00:30:58 +05:30
if not os.path.exists(args.output):
try:
os.mkdir(args.output)
except OSError:
2015-06-16 09:25:09 +02:00
error = "Unable to create the output folder: " + args.output
2012-06-10 13:27:36 +02:00
exit(error)
2011-10-24 00:30:58 +05:30
if args.wp_attach and input_type != "wordpress":
2015-06-16 09:25:09 +02:00
error = "You must be importing a wordpress xml " "to use the --wp-attach option"
exit(error)
2013-07-05 01:08:45 +02:00
2018-08-07 14:06:46 +02:00
if input_type == "blogger":
fields = blogger2fields(args.input)
elif input_type == "dotclear":
fields = dc2fields(args.input)
elif input_type == "tumblr":
fields = tumblr2fields(args.input, args.blogname)
2018-08-07 14:06:46 +02:00
elif input_type == "wordpress":
fields = wp2fields(args.input, args.wp_custpost or False)
elif input_type == "feed":
fields = feed2fields(args.input)
if args.wp_attach:
attachments = get_attachments(args.input)
else:
attachments = None
2015-06-16 09:25:09 +02:00
# init logging
init()
fields2pelican(
fields,
args.markup,
args.output,
dircat=args.dircat or False,
dirpage=args.dirpage or False,
strip_raw=args.strip_raw or False,
disable_slugs=args.disable_slugs or False,
filter_author=args.author,
2015-06-16 09:25:09 +02:00
wp_custpost=args.wp_custpost or False,
wp_attach=args.wp_attach or False,
attachments=attachments or None,
)