Skip to content

Commit

Permalink
Support for List
Browse files Browse the repository at this point in the history
  • Loading branch information
anevis committed Apr 6, 2024
1 parent 29c4d55 commit d29fe1b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
15 changes: 13 additions & 2 deletions src/yaml_to_markdown/md_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
17 changes: 17 additions & 0 deletions src/yaml_to_markdown/md_converter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d29fe1b

Please sign in to comment.