diff --git a/src/markdown_helper/__init__.py b/src/markdown_helper/__init__.py
index 8e924f9..8b54754 100644
--- a/src/markdown_helper/__init__.py
+++ b/src/markdown_helper/__init__.py
@@ -28,7 +28,7 @@ def __init__(self, text: str, level: int = 1):
def __str__(self):
"""Return the header as a string."""
return "#" * self.level + " " + self.text
-
+
def __repr__(self):
"""Return the header as a string."""
return "#" * self.level + " " + self.text
@@ -45,9 +45,6 @@ def __init__(self, headers: list[str], **kwargs): # type: ignore
Keyword Args:
title (str): Title for the table
flexible_headers (bool): If True, allow headers to be added dynamically
- total_row (bool): If True, add a total row to the table
- total_row_label (str): Label for the total row
- sort (bool): If True, sort the table by the sort_key
sort_reverse (bool): If True, sort the table in reverse order
sort_key (str): Key to sort the table by
custom_map (dict): Custom map to remap values in the table
@@ -55,9 +52,6 @@ def __init__(self, headers: list[str], **kwargs): # type: ignore
self.headers = headers
self.rows: list[dict[str, str | int | float | bool]] = []
self.flexible_headers = kwargs.get("flexible_headers", False)
- self.total_row = kwargs.get("total_row", False)
- self.total_row_label = kwargs.get("total_row_label", "Total")
- self.sort = kwargs.get("sort", False)
self.sort_reverse = kwargs.get("sort_reverse", False)
self.sort_key = kwargs.get("sort_key", "")
self.custom_map: dict = kwargs.get("custom_map", False)
@@ -94,7 +88,7 @@ def sort_table(self):
# If multiple sort keys are provided, prioritize the first one, then the second, etc.
sort_keys = self.sort_key.split(",")
for sort_key in sort_keys:
- if all(
+ if all( # pylint: disable=use-a-generator
[
row.get(sort_key, "")
in [1, 0, False, True, "False", "True", "false", "true"]
@@ -139,7 +133,7 @@ def sort_table(self):
def get_table(self) -> str:
"""Generate the table."""
- if self.sort:
+ if self.sort_key:
self.sort_table()
if self.custom_map:
self.remap()
@@ -151,19 +145,19 @@ def get_table(self) -> str:
table += f"| {' | '.join(['---' for _ in self.headers])} |\n"
for row in self.rows:
table += f"| {' | '.join([str(row.get(header, '')) for header in self.headers])} |\n"
- if self.total_row:
- total_row = {}
- for header in self.headers:
- if header == self.total_row_label:
- total_row[header] = "Total"
- else:
- try:
- total_row[header] = sum(
- [int(row.get(header, 0)) for row in self.rows]
- )
- except ValueError:
- total_row[header] = ""
- table += f"| {' | '.join([str(total_row.get(header, '')) for header in self.headers])} |\n" # pylint: disable=line-too-long
+ # if self.total_row:
+ # total_row = {}
+ # for header in self.headers:
+ # if header == self.total_row_label:
+ # total_row[header] = "Total"
+ # else:
+ # try:
+ # total_row[header] = sum(
+ # [int(row.get(header, 0)) for row in self.rows]
+ # )
+ # except ValueError:
+ # total_row[header] = ""
+ # table += f"| {' | '.join([str(total_row.get(header, '')) for header in self.headers])} |\n" # pylint: disable=line-too-long
return table
def __str__(self):
@@ -205,7 +199,7 @@ def html(self):
if self.caption:
image += f'
{self.caption}'
return image
-
+
def markdown(self):
"""Generate the markdown for the image."""
image = ""
@@ -219,8 +213,7 @@ def markdown(self):
def __str__(self):
if any([self.width, self.height, self.align]):
return self.html()
- else:
- return self.markdown()
+ return self.markdown()
def __repr__(self):
return f"""Image(url={self.url}, title={self.title}, alt={self.alt},
width={self.width}, height={self.height}, align={self.align}, caption={self.caption})"""
diff --git a/tests/test_markdown.py b/tests/test_markdown.py
index 8160c79..94553e3 100644
--- a/tests/test_markdown.py
+++ b/tests/test_markdown.py
@@ -1,184 +1,264 @@
"""
This file contains the pytest tests for the markdown_helper.py file.
"""
-import markdown_helper as markdown # pylint: disable=import-error
+import markdown_helper as markdown # pylint: disable=import-error
+
def test_ordered_list():
"""
This function tests the markdown.List class.
"""
- list_1:markdown.List = markdown.List(['item 1', 'item 2', 'item 3'], ordered=True)
- assert str(list_1) == '1. item 1\n2. item 2\n3. item 3\n', "String representation of ordered list is incorrect."
- assert list_1.items == ['item 1', 'item 2', 'item 3'], "List items are incorrect."
+ list_1: markdown.List = markdown.List(["item 1", "item 2", "item 3"], ordered=True)
+ assert (
+ str(list_1) == "1. item 1\n2. item 2\n3. item 3\n"
+ ), "String representation of ordered list is incorrect."
+ assert list_1.items == ["item 1", "item 2", "item 3"], "List items are incorrect."
+
def test_modify_list():
"""
This function tests the markdown.List class.
"""
- list_1 = markdown.List(['item 1', 'item 2', 'item 3'], ordered=True)
- list_1.add('item 4')
- assert str(list_1) == '1. item 1\n2. item 2\n3. item 3\n4. item 4\n', "String representation of ordered list is incorrect."
- assert list_1.items == ['item 1', 'item 2', 'item 3', 'item 4'], "List items are incorrect."
+ list_1 = markdown.List(["item 1", "item 2", "item 3"], ordered=True)
+ list_1.add("item 4")
+ assert (
+ str(list_1) == "1. item 1\n2. item 2\n3. item 3\n4. item 4\n"
+ ), "String representation of ordered list is incorrect."
+ assert list_1.items == [
+ "item 1",
+ "item 2",
+ "item 3",
+ "item 4",
+ ], "List items are incorrect."
+
def test_unordered_list():
"""
This function tests the markdown.List class.
"""
- list_1 = markdown.List(['item 1', 'item 2', 'item 3'], ordered=False)
- assert str(list_1) == '- item 1\n- item 2\n- item 3\n', "String representation of unordered list is incorrect."
+ list_1 = markdown.List(["item 1", "item 2", "item 3"], ordered=False)
+ assert (
+ str(list_1) == "- item 1\n- item 2\n- item 3\n"
+ ), "String representation of unordered list is incorrect."
+
def test_named_list():
"""
This function tests the markdown.List class.
"""
- list_1 = markdown.List(['item 1', 'item 2', 'item 3'], ordered=False, title='my_list')
- assert str(list_1) == '### my_list\n- item 1\n- item 2\n- item 3\n', "String representation of unordered list is incorrect."
- assert list_1.title == 'my_list', "List name is incorrect."
+ list_1 = markdown.List(
+ ["item 1", "item 2", "item 3"], ordered=False, title="my_list"
+ )
+ assert (
+ str(list_1) == "### my_list\n- item 1\n- item 2\n- item 3\n"
+ ), "String representation of unordered list is incorrect."
+ assert list_1.title == "my_list", "List name is incorrect."
def test_link():
"""
This function tests the markdown.Link class.
"""
- link_1 = markdown.Link('http://www.google.com', 'Google')
- assert str(link_1) == '[Google](http://www.google.com)\n', "String representation of link is incorrect."
- assert link_1.url == 'http://www.google.com', "Link URL is incorrect."
- assert link_1.text == 'Google', "Link text is incorrect."
+ link_1 = markdown.Link("http://www.google.com", "Google")
+ assert (
+ str(link_1) == "[Google](http://www.google.com)\n"
+ ), "String representation of link is incorrect."
+ assert link_1.url == "http://www.google.com", "Link URL is incorrect."
+ assert link_1.text == "Google", "Link text is incorrect."
+
def test_link_no_trailing():
"""
This function tests the markdown.Link class.
"""
- link_1 = markdown.Link('http://www.google.com', 'Google', trailing=False)
- assert str(link_1) == '[Google](http://www.google.com)', "String representation of link is incorrect."
- assert link_1.url == 'http://www.google.com', "Link URL is incorrect."
- assert link_1.text == 'Google', "Link text is incorrect."
+ link_1 = markdown.Link("http://www.google.com", "Google", trailing=False)
+ assert (
+ str(link_1) == "[Google](http://www.google.com)"
+ ), "String representation of link is incorrect."
+ assert link_1.url == "http://www.google.com", "Link URL is incorrect."
+ assert link_1.text == "Google", "Link text is incorrect."
+
def test_link_no_text():
"""
This function tests the markdown.Link class.
"""
- link_1 = markdown.Link('http://www.google.com')
- assert str(link_1) == '[http://www.google.com](http://www.google.com)\n', "String representation of link is incorrect."
- assert link_1.url == 'http://www.google.com', "Link URL is incorrect."
- assert link_1.text == 'http://www.google.com', "Link text is incorrect."
+ link_1 = markdown.Link("http://www.google.com")
+ assert (
+ str(link_1) == "[http://www.google.com](http://www.google.com)\n"
+ ), "String representation of link is incorrect."
+ assert link_1.url == "http://www.google.com", "Link URL is incorrect."
+ assert link_1.text == "http://www.google.com", "Link text is incorrect."
+
def test_link_new_tab():
"""
This function tests the markdown.Link class.
"""
- link_1 = markdown.Link('http://www.google.com', 'Google', new_tab=True)
- assert str(link_1) == '[Google](http://www.google.com target=_blank)\n', "String representation of link is incorrect."
- assert link_1.url == 'http://www.google.com', "Link URL is incorrect."
- assert link_1.text == 'Google', "Link text is incorrect."
+ link_1 = markdown.Link("http://www.google.com", "Google", new_tab=True)
+ assert (
+ str(link_1) == "[Google](http://www.google.com target=_blank)\n"
+ ), "String representation of link is incorrect."
+ assert link_1.url == "http://www.google.com", "Link URL is incorrect."
+ assert link_1.text == "Google", "Link text is incorrect."
def test_image():
"""
This function tests the markdown.Image class.
"""
- image_1 = markdown.Image('http://www.google.com', alt='Google')
- assert str(image_1) == '![Google](http://www.google.com)\n', "String representation of image is incorrect."
- assert image_1.url == 'http://www.google.com', "Image URL is incorrect."
- assert image_1.alt == 'Google', "Image text is incorrect."
-
+ image_1 = markdown.Image("http://www.google.com", alt="Google")
+ assert (
+ str(image_1) == "![Google](http://www.google.com)\n"
+ ), "String representation of image is incorrect."
+ assert image_1.url == "http://www.google.com", "Image URL is incorrect."
+ assert image_1.alt == "Google", "Image text is incorrect."
+
def test_image_size():
"""
This function tests the markdown.Image class.
"""
- image_1 = markdown.Image('http://www.google.com', alt='Google', width=100, height=100)
- assert str(image_1) == '', "String representation of image is incorrect."
- assert image_1.url == 'http://www.google.com', "Image URL is incorrect."
- assert image_1.alt == 'Google', "Image text is incorrect."
+ image_1 = markdown.Image(
+ "http://www.google.com", alt="Google", width=100, height=100
+ )
+ assert (
+ str(image_1)
+ == ''
+ ), "String representation of image is incorrect."
+ assert image_1.url == "http://www.google.com", "Image URL is incorrect."
+ assert image_1.alt == "Google", "Image text is incorrect."
assert image_1.width == 100, "Image width is incorrect."
assert image_1.height == 100, "Image height is incorrect."
+
def test_image_no_alt():
"""
This function tests the markdown.Image class.
"""
- image_1 = markdown.Image('http://www.google.com')
- assert str(image_1) == '![http://www.google.com](http://www.google.com)\n', "String representation of image is incorrect."
- assert image_1.url == 'http://www.google.com', "Image URL is incorrect."
- assert image_1.alt == 'http://www.google.com', "Image text is incorrect."
+ image_1 = markdown.Image("http://www.google.com")
+ assert (
+ str(image_1) == "![http://www.google.com](http://www.google.com)\n"
+ ), "String representation of image is incorrect."
+ assert image_1.url == "http://www.google.com", "Image URL is incorrect."
+ assert image_1.alt == "http://www.google.com", "Image text is incorrect."
def test_table():
"""
This function tests the markdown.Table class.
"""
- table_1 = markdown.Table(['col 1', 'col 2', 'col 3'])
- assert str(table_1) == '| col 1 | col 2 | col 3 |\n| --- | --- | --- |\n', "String representation of table is incorrect."
- assert table_1.headers == ['col 1', 'col 2', 'col 3'], "Table columns are incorrect."
+ table_1 = markdown.Table(["col 1", "col 2", "col 3"])
+ assert (
+ str(table_1) == "| col 1 | col 2 | col 3 |\n| --- | --- | --- |\n"
+ ), "String representation of table is incorrect."
+ assert table_1.headers == [
+ "col 1",
+ "col 2",
+ "col 3",
+ ], "Table columns are incorrect."
+
def test_table_add_row():
"""
This function tests the markdown.Table class.
"""
- table_1 = markdown.Table(['col 1', 'col 2', 'col 3'])
- table_1.add_row({'col 1': 'item 1', 'col 2': 'item 2', 'col 3': 'item 3'})
- assert str(table_1) == '| col 1 | col 2 | col 3 |\n| --- | --- | --- |\n| item 1 | item 2 | item 3 |\n', "String representation of table is incorrect."
- assert table_1.headers == ['col 1', 'col 2', 'col 3'], "Table columns are incorrect."
- assert table_1.rows == [{'col 1': 'item 1', 'col 2': 'item 2', 'col 3': 'item 3'}], "Table rows are incorrect."
-
+ table_1 = markdown.Table(["col 1", "col 2", "col 3"])
+ table_1.add_row({"col 1": "item 1", "col 2": "item 2", "col 3": "item 3"})
+ assert (
+ str(table_1)
+ == "| col 1 | col 2 | col 3 |\n| --- | --- | --- |\n| item 1 | item 2 | item 3 |\n"
+ ), "String representation of table is incorrect."
+ assert table_1.headers == [
+ "col 1",
+ "col 2",
+ "col 3",
+ ], "Table columns are incorrect."
+ assert table_1.rows == [
+ {"col 1": "item 1", "col 2": "item 2", "col 3": "item 3"}
+ ], "Table rows are incorrect."
+
def test_table_sort():
"""
This function tests the markdown.Table class.
"""
- table_1 = markdown.Table(['Name', 'Value'], sort=True, sort_key='Value')
- table_1.add_row({'Name': 'First', 'Value':1})
- table_1.add_row({'Name': 'Second', 'Value':2})
- table_1.add_row({'Name': 'Fourth', 'Value':4})
- table_1.add_row({'Name': 'Third', 'Value':3})
- assert str(table_1) == '| Name | Value |\n| --- | --- |\n| First | 1 |\n| Second | 2 |\n| Third | 3 |\n| Fourth | 4 |\n', "Sorted Table is incorrect."
- assert table_1.headers == ['Name', 'Value'], "Table columns are incorrect."
+ table_1 = markdown.Table(["Name", "Value"], sort_key="Value")
+ table_1.add_row({"Name": "First", "Value": 1})
+ table_1.add_row({"Name": "Second", "Value": 2})
+ table_1.add_row({"Name": "Fourth", "Value": 4})
+ table_1.add_row({"Name": "Third", "Value": 3})
+ assert (
+ str(table_1)
+ == "| Name | Value |\n| --- | --- |\n| First | 1 |\n| Second | 2 |\n| Third | 3 |\n| Fourth | 4 |\n" # pylint: disable=line-too-long
+ ), "Sorted Table is incorrect."
+ assert table_1.headers == ["Name", "Value"], "Table columns are incorrect."
table_1.sort_reverse = True
- assert str(table_1) == '| Name | Value |\n| --- | --- |\n| Fourth | 4 |\n| Third | 3 |\n| Second | 2 |\n| First | 1 |\n', "Reverse sorted Table is incorrect."
+ assert (
+ str(table_1)
+ == "| Name | Value |\n| --- | --- |\n| Fourth | 4 |\n| Third | 3 |\n| Second | 2 |\n| First | 1 |\n" # pylint: disable=line-too-long
+ ), "Reverse sorted Table is incorrect."
table_1.sort_reverse = False
- table_1.sort_key = 'Name'
- assert str(table_1) == '| Name | Value |\n| --- | --- |\n| First | 1 |\n| Fourth | 4 |\n| Second | 2 |\n| Third | 3 |\n', "Second sorted Table is incorrect."
+ table_1.sort_key = "Name"
+ assert (
+ str(table_1)
+ == "| Name | Value |\n| --- | --- |\n| First | 1 |\n| Fourth | 4 |\n| Second | 2 |\n| Third | 3 |\n" # pylint: disable=line-too-long
+ ), "Second sorted Table is incorrect."
def test_table_flexible():
"""
This function tests the markdown.Table class.
"""
- table_1 = markdown.Table(['Name', 'Value'], flexible_headers=True)
- table_1.add_row({'Name': 'First', 'Value':1})
- table_1.add_row({'Name': 'Second', 'Value':2})
- table_1.add_row({'Name': 'Third', 'Value':3, "Extra": "Extra Value"})
- table_1.add_row({'Name': 'Fourth', 'Value':4})
- assert str(table_1) == '| Name | Value | Extra |\n| --- | --- | --- |\n| First | 1 | |\n| Second | 2 | |\n| Third | 3 | Extra Value |\n| Fourth | 4 | |\n', "Flexible Table is incorrect."
- assert table_1.headers == ['Name', 'Value', 'Extra'], "Table columns are incorrect."
+ table_1 = markdown.Table(["Name", "Value"], flexible_headers=True)
+ table_1.add_row({"Name": "First", "Value": 1})
+ table_1.add_row({"Name": "Second", "Value": 2})
+ table_1.add_row({"Name": "Third", "Value": 3, "Extra": "Extra Value"})
+ table_1.add_row({"Name": "Fourth", "Value": 4})
+ assert (
+ str(table_1)
+ == "| Name | Value | Extra |\n| --- | --- | --- |\n| First | 1 | |\n| Second | 2 | |\n| Third | 3 | Extra Value |\n| Fourth | 4 | |\n" # pylint: disable=line-too-long
+ ), "Flexible Table is incorrect."
+ assert table_1.headers == ["Name", "Value", "Extra"], "Table columns are incorrect."
+
def test_section():
"""
This function tests the markdown.Section class.
"""
- section_1 = markdown.Section('Section 1')
- assert str(section_1) == '# Section 1\n\n', "String representation of section is incorrect."
- assert section_1.title.text == 'Section 1', "Section title is incorrect."
+ section_1 = markdown.Section("Section 1")
+ assert (
+ str(section_1) == "# Section 1\n\n"
+ ), "String representation of section is incorrect."
+ assert section_1.title.text == "Section 1", "Section title is incorrect."
+
def test_section_add():
"""
This function tests the markdown.Section class.
"""
- section_1 = markdown.Section('Section 1')
+ section_1 = markdown.Section("Section 1")
section_1.add("This is a paragraph.")
- assert str(section_1) == '# Section 1\nThis is a paragraph. \n', "String representation of section is incorrect."
+ assert (
+ str(section_1) == "# Section 1\nThis is a paragraph. \n"
+ ), "String representation of section is incorrect."
+
def test_document():
"""
This function tests the markdown.Document class.
"""
document_1 = markdown.Document("Document 1", filename="document_1.md")
- document_1.add_section(markdown.Section(markdown.Header("Section 1",2)))
- assert document_1.sections['Section 1'].title.text == 'Section 1', "Section title is incorrect."
- section = document_1.sections['Section 1']
+ document_1.add_section(markdown.Section(markdown.Header("Section 1", 2)))
+ assert (
+ document_1.sections["Section 1"].title.text == "Section 1"
+ ), "Section title is incorrect."
+ section = document_1.sections["Section 1"]
section.add("This is a paragraph.")
- assert str(document_1) == '# Document 1\n## Section 1\nThis is a paragraph. \n', "String representation of document is incorrect."
+ assert (
+ str(document_1) == "# Document 1\n## Section 1\nThis is a paragraph. \n"
+ ), "String representation of document is incorrect."
+
def test_document_save(tmp_path):
"""
@@ -187,11 +267,14 @@ def test_document_save(tmp_path):
filename = tmp_path / "document_1.md"
document_1 = markdown.Document("Document 1", filename=str(filename))
print(filename)
- document_1.add_section(markdown.Section(markdown.Header("Section 1",2)))
- assert document_1.sections['Section 1'].title.text == 'Section 1', "Section title is incorrect."
- section = document_1.sections['Section 1']
+ document_1.add_section(markdown.Section(markdown.Header("Section 1", 2)))
+ assert (
+ document_1.sections["Section 1"].title.text == "Section 1"
+ ), "Section title is incorrect."
+ section = document_1.sections["Section 1"]
section.add("This is a paragraph.")
document_1.save()
assert filename.exists(), "File was not saved."
- assert filename.read_text() == '# Document 1\n## Section 1\nThis is a paragraph. \n', "File contents are incorrect."
-
\ No newline at end of file
+ assert (
+ filename.read_text() == "# Document 1\n## Section 1\nThis is a paragraph. \n"
+ ), "File contents are incorrect."