From d29fe1bc6d5f05acd38d1e2b7b6e344d79e47f22 Mon Sep 17 00:00:00 2001 From: Antony Nevis Date: Sat, 6 Apr 2024 16:48:38 +1100 Subject: [PATCH] Support for List --- README.md | 2 +- setup.py | 2 +- src/yaml_to_markdown/md_converter.py | 15 +++++++++++++-- src/yaml_to_markdown/md_converter_test.py | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d46dc03..172a27d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # YAML to Markdown Converter -A Python utility to take a JSON / YAML file or a python dict and create a Markdown file. +A Python utility to take a JSON / YAML file or a python dict / list and create a Markdown file. ## Usage diff --git a/setup.py b/setup.py index aadb19f..5d804b7 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="yaml-to-markdown", version=f"0.1.{ts}", - description="Converts a YAML/JSON file or python Dict to a Markdown file", + description="Converts a YAML/JSON file or python Dict/List to a Markdown file", packages=find_packages(where="src"), package_dir={"": "src"}, url="https://anevis.github.io/yaml-to-markdown/", diff --git a/src/yaml_to_markdown/md_converter.py b/src/yaml_to_markdown/md_converter.py index 6833f8f..82f23a8 100644 --- a/src/yaml_to_markdown/md_converter.py +++ b/src/yaml_to_markdown/md_converter.py @@ -44,17 +44,28 @@ def set_custom_section_processors( def convert( self, - data: Dict[str, Union[List[Any], Dict[str, Any], str]], + data: Union[Dict[str, Union[List[Any], Dict[str, Any], str]], List[Any]], output_writer: IO[str], ) -> None: """ Convert the given JSON object into Markdown. Args: - data (Dict[str, Union[List[Any], Dict[str, Any], str]]): + data (Union[Dict[str, Union[List[Any], Dict[str, Any], str]], List[Any]]): + The JSON object to convert, either a dictionary or a list. output_writer (IO[str]): The output stream object to write the Markdown to. """ + if isinstance(data, dict): + self._process_dict(data, output_writer) + elif isinstance(data, list): + self._process_dict({"": data}, output_writer) + + def _process_dict( + self, + data: Dict[str, Any], + output_writer: IO[str], + ) -> None: for section in self._sections if self._sections is not None else data.keys(): if section in data: output_writer.write(self.process_section(section, data.get(section))) diff --git a/src/yaml_to_markdown/md_converter_test.py b/src/yaml_to_markdown/md_converter_test.py index 902ed62..a581cac 100644 --- a/src/yaml_to_markdown/md_converter_test.py +++ b/src/yaml_to_markdown/md_converter_test.py @@ -21,6 +21,23 @@ class TestMDConverter: + def test_process_list(self) -> None: + output_writer = StringIO() + md_converter = MDConverter() + data = [{"section1": "data1"}] + + md_converter.convert(data, output_writer) + output = output_writer.getvalue() + + assert ( + output + == """## +| Section1 | +| --- | +| data1 | +""" + ) + def test_process_section_with_str(self) -> None: output_writer = StringIO() md_converter = MDConverter()