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

64 lines
2 KiB
Python
Raw Permalink 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)
posts_converted_count = 0
try:
_, _, files = next(os.walk(source_path))
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))
self.markdown_converter.convert_jekyll_to_hugo(
file_reader,
file_writer,
)
posts_converted_count += 1
2023-06-05 08:52:23 +00:00
self._logger.info(f"Converted {posts_converted_count} posts! 🚀")
except StopIteration:
self._logger.fatal(f"Source path {source_path} does not exist!")