diff --git a/v7/import_jekyll/import_jekyll.py b/v7/import_jekyll/import_jekyll.py index 90837748..0cf375d9 100644 --- a/v7/import_jekyll/import_jekyll.py +++ b/v7/import_jekyll/import_jekyll.py @@ -80,6 +80,7 @@ def _execute(self, options, args): self._read_config() self._write_site() self._import_posts() + self._import_pages() except JekyllImportError as e: LOGGER.error('ERROR: %s' % e) @@ -110,6 +111,13 @@ def _write_site(self): ("posts/*.html", "blog", "post.tmpl"), )""" + context['PAGES'] = """( + ("pages/*.md", "", "page.tmpl"), + ("pages/*.rst", "", "page.tmpl"), + ("pages/*.txt", "", "page.tmpl"), + ("pages/*.html", "", "page.tmpl"), + )""" + if 'disqus_short_name' in self._jekyll_config: context['COMMENT_SYSTEM'] = 'disqus' context['COMMENT_SYSTEM_ID'] = self._jekyll_config[ @@ -147,24 +155,43 @@ def _import_posts(self): fd.write(nikola_post) LOGGER.info('Writing post %s' % output_file) + def _import_pages(self): + rel_path = self._jekyll_config.get('source', '') + source_path = os.path.join(self._jekyll_path, rel_path) + importer = JekyllPageImport(source_path) + + for dirpath, dirnames, filenames in os.walk(source_path): + basepath = os.path.relpath(dirpath, source_path) + if basepath != '.' and basepath[0] in ('.', '_'): + continue + for filename in filenames: + if filename[0] in ('.', '_'): + continue + filepath = os.path.join(dirpath, filename) + if not filepath.lower().endswith( + ('.md', '.markdown', '.html', 'rst', '.textile')): + continue + LOGGER.info('Importing page %s' % filepath) + output_relfile, nikola_page = importer.import_file(filepath) + if not nikola_page: + LOGGER.info('Page is empty, skipping') + continue + output_file = os.path.join(self.output_folder, 'pages', + output_relfile) + utils.makedirs(os.path.dirname(output_file)) + with codecs.open(output_file, 'w', encoding='utf-8') as fd: + fd.write(nikola_page) + LOGGER.info('Writing page %s' % output_file) -class JekyllPostImport(object): - def import_file(self, path): - def new_filename(filename): - _, ext = os.path.splitext(filename) - return '{0}{1}'.format(slugify_file(filename), ext) +class JekyllImport(object): + def import_file(self, path): jmetadata, jcontent = self._split_metadata(path) metadata = self._import_metadata(path, jmetadata) doc = self._import_content(path, jcontent) - filename = os.path.basename(path) - date = metadata['date'] - output_file = os.path.join(str(date.year), str(date.month), - str(date.day), new_filename(filename)) - - content = self._serialize(metadata, doc, is_html(path)) - return output_file, content + content = self._serialize(metadata, doc, is_html(path)) if doc else None + return metadata, content def _serialize(self, metadata, doc, is_html): header = utils.write_metadata(metadata) @@ -191,6 +218,9 @@ def extract_date(): return raw_date if isinstance(raw_date, str): return dateutil.parser.parse(raw_date) + timestamp = jmetadata.get('created') + if isinstance(timestamp, int): + return datetime.date.fromtimestamp(timestamp) # date not in metadata or unreadable. Trying from filename. raw_date = re.findall(r'\d+\-\d+\-\d+', path) @@ -283,6 +313,36 @@ def link_repl(matchobj): return content +class JekyllPageImport(JekyllImport): + def __init__(self, source_path): + self.source_path = source_path + + def import_file(self, path): + _, content = super().import_file(path) + name, ext = os.path.splitext(path) + if ext == '.markdown': path = name + '.md' + return os.path.relpath(path, self.source_path), content + + +class JekyllPostImport(JekyllImport): + def import_file(self, path): + def new_filename(filename): + _, ext = os.path.splitext(filename) + if ext == '.markdown': ext = 'md' + return '{0}{1}'.format(slugify_file(filename), ext) + + metadata, content = super().import_file(path) + + filename = os.path.basename(path) + date = metadata['date'] + output_file = os.path.join(str(date.year), + '{:02d}'.format(date.month), + '{:02d}'.format(date.day), + new_filename(filename)) + + return output_file, content + + def slugify_file(filename): name, _ = os.path.splitext(os.path.basename(filename)) m = re.match('\d+\-\d+\-\d+\-(?P.*)', name)