Created
February 11, 2020 14:48
-
-
Save siddhantgoel/e4647886832f06f4990425068c24fb84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from argparse import ArgumentParser | |
import os | |
from textwrap import dedent | |
import warnings | |
def parse_args(): | |
parser = ArgumentParser() | |
parser.add_argument( | |
'--pelican-content', | |
type=str, | |
required=True, | |
help='Path to Pelican content directory', | |
) | |
parser.add_argument( | |
'--zola-content', | |
type=str, | |
required=True, | |
help='Path to Zola content directory', | |
) | |
return parser.parse_args() | |
def main(): | |
args = parse_args() | |
pelican_content_dir = os.path.expanduser(args.pelican_content) | |
zola_posts_dir = os.path.join( | |
os.path.expanduser(args.zola_content), 'posts' | |
) | |
if not os.path.exists(zola_posts_dir): | |
os.mkdir(zola_posts_dir) | |
content = {} | |
for file_name in os.listdir(pelican_content_dir): | |
if not file_name.endswith('md'): | |
warnings.warn('Ignoring {}'.format(file_name)) | |
continue | |
with open(os.path.join(pelican_content_dir, file_name)) as fd: | |
content = fd.read() | |
slug = file_name[: -len('.md')] | |
title = extract_title(content) | |
date = extract_date(content) | |
tags = extract_tags(content) | |
target_post_dir = os.path.join( | |
zola_posts_dir, '{}-{}'.format(date, slug) | |
) | |
if not os.path.exists(target_post_dir): | |
os.mkdir(target_post_dir) | |
target_file_name = os.path.join(target_post_dir, 'index.md') | |
without_tags = dedent( | |
'''\ | |
+++ | |
title = "{title}" | |
date = {date} | |
+++ | |
{content} | |
''' | |
) | |
with_tags = dedent( | |
'''\ | |
+++ | |
title = "{title}" | |
date = {date} | |
[taxonomies] | |
tags = [{tags}] | |
+++ | |
{content} | |
''' | |
) | |
with open(target_file_name, 'w') as fd: | |
if not tags: | |
fd.write( | |
without_tags.format( | |
title=title, | |
date=date, | |
content=extract_content(content), | |
) | |
) | |
else: | |
fd.write( | |
with_tags.format( | |
title=title, | |
date=date, | |
tags=extract_tags(content), | |
content=extract_content(content), | |
) | |
) | |
def extract_title(data): | |
return _extract_meta(data, 'Title') | |
def extract_date(data): | |
return _extract_meta(data, 'Date') | |
def extract_tags(data): | |
tags = _extract_meta(data, 'Tags') | |
if not tags: | |
return tags | |
return ', '.join(['"{}"'.format(tag.lower()) for tag in tags.split(', ')]) | |
def _extract_meta(data, key): | |
to_strip = '{}: '.format(key) | |
for line in data.split('\n')[:5]: | |
if line.startswith(to_strip): | |
return line[len(to_strip) :] | |
def extract_content(data): | |
lines = data.split('\n') | |
for index, line in enumerate(lines): | |
if line.strip() == '': | |
return '\n'.join(lines[index + 1 :]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment