diff --git a/app/converter/wordpress_markdown.py b/app/converter/wordpress_markdown.py index 2f9e875..e90657d 100644 --- a/app/converter/wordpress_markdown.py +++ b/app/converter/wordpress_markdown.py @@ -43,8 +43,10 @@ class WordpressMarkdownConverter: with key_error_silence(): del header[field] # rewrite header fields - header["guid"] = header["guid"].replace("http://localhost", "") - header["author"] = self.configurator.converter_options.author_rewrite + with key_error_silence(): + header["guid"] = header["guid"].replace("http://localhost", "") + with key_error_silence(): + header["author"] = self.configurator.converter_options.author_rewrite return header def remove_html_tags(self, post_lines): @@ -53,7 +55,7 @@ class WordpressMarkdownConverter: if line == "": fixed_lines.append("\n") continue - soup = BeautifulSoup(line) + soup = BeautifulSoup(line, features="html.parser") for content in soup.contents: if isinstance(content, Tag): # Check if it is a youtube video and add it as a shortcode. diff --git a/app/tests/converter/__init__.py b/app/tests/converter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/tests/converter/wordpress_markdown_test.py b/app/tests/converter/wordpress_markdown_test.py new file mode 100644 index 0000000..8f751de --- /dev/null +++ b/app/tests/converter/wordpress_markdown_test.py @@ -0,0 +1,27 @@ +import pytest + +from app.config import ConverterOptions +from app.converter import WordpressMarkdownConverter +from app.tests.utils import make_fake_configurator + + +@pytest.mark.parametrize( + "author_rewrite, input_header, expected_header", + [ + ("", {"author": "author"}, {"author": ""}), + ("", {}, {"author": ""}), + ("", {"a": 1}, {"a": 1, "author": ""}), + ("NucuLabs.dev", {"author": "Denis"}, {"author": "NucuLabs.dev"}), + ], +) +def test_fix_hugo_header_rewrite_author(author_rewrite, input_header, expected_header): + configurator = make_fake_configurator( + "wordpress_markdown_converter", + ConverterOptions( + author_rewrite=author_rewrite, + links_rewrite=[], + header_fields_drop=[], + ), + ) + converter = WordpressMarkdownConverter(configurator) + assert converter.fix_hugo_header(input_header) == expected_header diff --git a/app/tests/utils.py b/app/tests/utils.py new file mode 100644 index 0000000..910e365 --- /dev/null +++ b/app/tests/utils.py @@ -0,0 +1,30 @@ +from app.config import ConverterOptions, Configurator + + +def make_fake_configurator(converter: str, converter_options: ConverterOptions): + class FakeConfigurator(Configurator): + logging_level: str = "INFO" + source_path: str = "" + output_path: str = "" + converter: str = "" + converter_options = ConverterOptions( + author_rewrite="", + links_rewrite=[], + header_fields_drop=[], + ) + + class Config: + env_file_encoding = "utf-8" + + @classmethod + def customise_sources( + cls, + init_settings, + env_settings, + file_secret_settings, + ): + return (init_settings,) + + configurator = FakeConfigurator() + configurator.converter_options = converter_options + return configurator