jekyll-to-hugo/app/converter/converter.py

62 lines
1.8 KiB
Python
Raw Normal View History

2023-05-29 18:58:32 +00:00
import logging
2023-05-29 18:35:38 +00:00
import os
from pathlib import Path
from app import utils
2023-05-29 18:58:32 +00:00
from app.config import Configurator
2023-05-29 18:35:38 +00:00
from app.converter.wordpress_markdown import WordpressMarkdownConverter
from app.io.reader import FileReader
from app.io.writer import FileWriter
2023-05-29 18:35:38 +00:00
class Converter:
"""
Convert Jekyll posts to Hugo posts
"""
2023-05-29 18:58:32 +00:00
def __init__(self, configurator: Configurator):
2023-05-29 18:35:38 +00:00
"""
Initializes the converter
Parameters
----------
2023-05-29 18:58:32 +00:00
configurator : Configurator
The configurator instance.
2023-05-29 18:35:38 +00:00
"""
2023-05-29 18:58:32 +00:00
utils.guard_against_none(configurator, "configurator")
2023-05-29 18:35:38 +00:00
2023-05-29 18:58:32 +00:00
self._logger = logging.getLogger(__name__)
self._jekyll_posts_path = configurator.source_path
self._hugo_posts_path = configurator.output_path
self._logger.info("Converting posts, please wait")
2023-05-29 18:58:32 +00:00
self._logger.info(
f"Using source: {self._jekyll_posts_path} output: {self._hugo_posts_path}"
)
2023-05-29 18:35:38 +00:00
# The converter that converts the markdown
2023-05-29 18:58:32 +00:00
self.markdown_converter = WordpressMarkdownConverter(configurator)
2023-05-29 18:35:38 +00:00
def convert(self):
"""
Converts the Jekyll posts to Hugo posts
"""
source_path = self._jekyll_posts_path
output_path = Path(self._hugo_posts_path)
_, _, files = next(os.walk(source_path))
posts_converted_count = 0
2023-05-29 18:35:38 +00:00
for file in files:
source_abs_path = source_path / Path(file)
file_reader = FileReader(str(source_abs_path))
file_writer = FileWriter(output_path.joinpath(source_abs_path.name))
2023-05-29 18:35:38 +00:00
self.markdown_converter.convert_jekyll_to_hugo(
file_reader,
file_writer,
2023-05-29 18:35:38 +00:00
)
posts_converted_count += 1
self._logger.info(f"Converted {posts_converted_count} posts! 🚀")