Skip to content

Commit

Permalink
Add MDList class
Browse files Browse the repository at this point in the history
  • Loading branch information
didix21 committed May 3, 2020
1 parent d72585a commit b1351ca
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
68 changes: 68 additions & 0 deletions mdutils/tools/MDList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Python
#
# This module implements tests for Header class.
#
# This file is part of mdutils. https://github.com/didix21/mdutils
#
# MIT License: (C) 2020 Dídac Coll
import re


class MDListHelper:
def __init__(self):
self.n_tabs = 0

def get_unordered_markdown_list(self, items, marker: str) -> str:
md_list = ""
for item in items:
if isinstance(item, list):
self.n_tabs += 1
md_list += self.get_unordered_markdown_list(item, marker)
self.n_tabs -= 1
else:
md_list += self.__add_new_item(item, marker)

return md_list

def get_ordered_markdown_list(self, items) -> str:
md_list = ""
n_marker = 1
for item in items:
if isinstance(item, list):
self.n_tabs += 1
md_list += self.get_ordered_markdown_list(items=item)
self.n_tabs -= 1
else:
md_list += self.__add_new_item(item, f"{n_marker}.")
n_marker += 1
return md_list

def __add_new_item(self, item: str, marker: str):
item_with_hyphen = item if self.is_there_marker_in_item(item) else self.__add_hyphen(item, marker)
return self.__n_spaces(4 * self.n_tabs) + item_with_hyphen + '\n'

@classmethod
def is_there_marker_in_item(cls, item: str) -> bool:
if item.startswith('-') or item.startswith('*') or item.startswith('+') or re.search(r"^(\d\.)", item):
return True
return False

@classmethod
def __add_hyphen(cls, item: str, marker: str):
return f"{marker} {item}"

@classmethod
def __n_spaces(cls, number_spaces: int = 1):
return " " * number_spaces


class MDList(MDListHelper):

def __init__(self, items: [str], marked_with: str = '-'):
super().__init__()
self.md_list = self.get_ordered_markdown_list(items) if marked_with == '1' else \
self.get_unordered_markdown_list(items, marked_with)

def get_md(self) -> str:
return self.md_list

111 changes: 111 additions & 0 deletions tests/test_tools/test_mdlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Python
#
# This module implements tests for Header class.
#
# This file is part of mdutils. https://github.com/didix21/mdutils
#
# MIT License: (C) 2020 Dídac Coll

from unittest import TestCase
from mdutils.tools.MDList import MDList


class TestLink(TestCase):

def setUp(self):
self.basic_list = "- Item 1\n" \
"- Item 2\n" \
"- Item 3\n"
self.complex_list = self.basic_list + \
"- Item 4\n" \
" - Item 4.1\n" \
" - Item 4.2\n" \
" - Item 4.2.1\n" \
" - Item 4.2.2\n" \
" - Item 4.3\n" \
" - Item 4.3.1\n" \
"- Item 5\n"
self.basic_items = ["Item 1", "Item 2", "Item 3"]
self.complex_items = self.basic_items + ["Item 4",
["Item 4.1", "Item 4.2",
["Item 4.2.1", "Item 4.2.2"],
"Item 4.3", ["Item 4.3.1"]],
"Item 5"]

def test_basic_list(self):
expected_list = self.basic_list
mdlist = MDList(items=self.basic_items)
actual_list = mdlist.get_md()

self.assertEqual(expected_list, actual_list)

def test_complex_list(self):
expected_list = self.complex_list
mdlist = MDList(items=self.complex_items)
actual_list = mdlist.get_md()

self.assertEqual(expected_list, actual_list)

def test_another_marker(self):
marker = "*"
expected_list = self.complex_list.replace("-", marker)
mdlist = MDList(items=self.complex_items, marked_with=marker)
actual_list = mdlist.get_md()

self.assertEqual(expected_list, actual_list)

def test_ordered_list(self):
marker = "1"
expected_list = "1. Item 1\n" \
"2. Item 2\n" \
"3. Item 3\n" \
"4. Item 4\n" \
" 1. Item 4.1\n" \
" 2. Item 4.2\n" \
" 1. Item 4.2.1\n" \
" 2. Item 4.2.2\n" \
" 3. Item 4.3\n" \
" 1. Item 4.3.1\n" \
"5. Item 5\n"
mdlist = MDList(items=self.complex_items, marked_with=marker)
actual_list = mdlist.get_md()

self.assertEqual(expected_list, actual_list)

def test_unordered_with_mixed_ordered_list(self):
expected_list = self.basic_list + \
"- Item 4\n" \
" 1. Item 4.1\n" \
" 2. Item 4.2\n" \
"- Item 5\n"
items = self.basic_items + ["Item 4", ["1. Item 4.1", "2. Item 4.2"], "Item 5"]
mdlist = MDList(items)
actual_list = mdlist.get_md()

self.assertEqual(expected_list, actual_list)

def test_ordered_with_mixed_unordered_list(self):
expected_list = "1. Item 1\n" \
"2. Item 2\n" \
"3. Item 3\n" \
"4. Item 4\n" \
" + Item 4.1\n" \
" + Item 4.2\n" \
" + Item 4.2.1\n" \
" + Item 4.2.2\n" \
" + Item 4.3\n" \
" 1. Item 4.3.1\n" \
"5. Item 5\n"

items = ["Item 1", "Item 2", "Item 3", "Item 4",
["+ Item 4.1", "+ Item 4.2",
["+ Item 4.2.1", "+ Item 4.2.2"],
"+ Item 4.3", ["Item 4.3.1"]
],
"Item 5"
]

mdlist = MDList(items, marked_with="1")
actual_list = mdlist.get_md()

self.assertEqual(expected_list, actual_list)

0 comments on commit b1351ca

Please sign in to comment.