summaryrefslogtreecommitdiffstats
path: root/nikola/plugins/basic_import.py
diff options
context:
space:
mode:
Diffstat (limited to 'nikola/plugins/basic_import.py')
-rw-r--r--nikola/plugins/basic_import.py61
1 files changed, 38 insertions, 23 deletions
diff --git a/nikola/plugins/basic_import.py b/nikola/plugins/basic_import.py
index 073a539..3e6e21e 100644
--- a/nikola/plugins/basic_import.py
+++ b/nikola/plugins/basic_import.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright © 2012-2015 Roberto Alsina and others.
+# Copyright © 2012-2020 Roberto Alsina and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
@@ -26,21 +26,15 @@
"""Mixin for importer plugins."""
-from __future__ import unicode_literals, print_function
import io
import csv
import datetime
import os
-import sys
-from pkg_resources import resource_filename
-
-try:
- from urlparse import urlparse
-except ImportError:
- from urllib.parse import urlparse # NOQA
+from urllib.parse import urlparse
from lxml import etree, html
from mako.template import Template
+from pkg_resources import resource_filename
from nikola import utils
@@ -48,7 +42,6 @@ links = {}
class ImportMixin(object):
-
"""Mixin with common used methods."""
name = "import_mixin"
@@ -77,8 +70,11 @@ class ImportMixin(object):
return channel
@staticmethod
- def configure_redirections(url_map):
+ def configure_redirections(url_map, base_dir=''):
"""Configure redirections from an url_map."""
+ index = base_dir + 'index.html'
+ if index.startswith('/'):
+ index = index[1:]
redirections = []
for k, v in url_map.items():
if not k[-1] == '/':
@@ -87,11 +83,10 @@ class ImportMixin(object):
# remove the initial "/" because src is a relative file path
src = (urlparse(k).path + 'index.html')[1:]
dst = (urlparse(v).path)
- if src == 'index.html':
- utils.LOGGER.warn("Can't do a redirect for: {0!r}".format(k))
+ if src == index:
+ utils.LOGGER.warning("Can't do a redirect for: {0!r}".format(k))
else:
redirections.append((src, dst))
-
return redirections
def generate_base_site(self):
@@ -100,8 +95,8 @@ class ImportMixin(object):
os.system('nikola init -q ' + self.output_folder)
else:
self.import_into_existing_site = True
- utils.LOGGER.notice('The folder {0} already exists - assuming that this is a '
- 'already existing Nikola site.'.format(self.output_folder))
+ utils.LOGGER.warning('The folder {0} already exists - assuming that this is a '
+ 'already existing Nikola site.'.format(self.output_folder))
filename = resource_filename('nikola', 'conf.py.in')
# The 'strict_undefined=True' will give the missing symbol name if any,
@@ -126,9 +121,12 @@ class ImportMixin(object):
def write_content(cls, filename, content, rewrite_html=True):
"""Write content to file."""
if rewrite_html:
- doc = html.document_fromstring(content)
- doc.rewrite_links(replacer)
- content = html.tostring(doc, encoding='utf8')
+ try:
+ doc = html.document_fromstring(content)
+ doc.rewrite_links(replacer)
+ content = html.tostring(doc, encoding='utf8')
+ except etree.ParserError:
+ content = content.encode('utf-8')
else:
content = content.encode('utf-8')
@@ -136,8 +134,25 @@ class ImportMixin(object):
with open(filename, "wb+") as fd:
fd.write(content)
- @staticmethod
- def write_metadata(filename, title, slug, post_date, description, tags, **kwargs):
+ @classmethod
+ def write_post(cls, filename, content, headers, compiler, rewrite_html=True):
+ """Ask the specified compiler to write the post to disk."""
+ if rewrite_html:
+ try:
+ doc = html.document_fromstring(content)
+ doc.rewrite_links(replacer)
+ content = html.tostring(doc, encoding='utf8')
+ except etree.ParserError:
+ pass
+ if isinstance(content, bytes):
+ content = content.decode('utf-8')
+ compiler.create_post(
+ filename,
+ content=content,
+ onefile=True,
+ **headers)
+
+ def write_metadata(self, filename, title, slug, post_date, description, tags, **kwargs):
"""Write metadata to meta file."""
if not description:
description = ""
@@ -146,13 +161,13 @@ class ImportMixin(object):
with io.open(filename, "w+", encoding="utf8") as fd:
data = {'title': title, 'slug': slug, 'date': post_date, 'tags': ','.join(tags), 'description': description}
data.update(kwargs)
- fd.write(utils.write_metadata(data))
+ fd.write(utils.write_metadata(data, site=self.site, comment_wrap=False))
@staticmethod
def write_urlmap_csv(output_file, url_map):
"""Write urlmap to csv file."""
utils.makedirs(os.path.dirname(output_file))
- fmode = 'wb+' if sys.version_info[0] == 2 else 'w+'
+ fmode = 'w+'
with io.open(output_file, fmode) as fd:
csv_writer = csv.writer(fd)
for item in url_map.items():