From 31fb56edacd89393211a5b6c6bb7493bfd309630 Mon Sep 17 00:00:00 2001 From: didix21 Date: Sat, 7 Apr 2018 19:09:07 +0200 Subject: [PATCH 01/50] Created NewFile class. --- mdutils/fileutils/__init__.py | 0 mdutils/fileutils/fileutils.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 mdutils/fileutils/__init__.py create mode 100644 mdutils/fileutils/fileutils.py diff --git a/mdutils/fileutils/__init__.py b/mdutils/fileutils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mdutils/fileutils/fileutils.py b/mdutils/fileutils/fileutils.py new file mode 100644 index 0000000..1e5105f --- /dev/null +++ b/mdutils/fileutils/fileutils.py @@ -0,0 +1,34 @@ +# Python +# +# This module implements a main class that allows to create markdown files, write in them or read. +# +# This file is part of mdutils. https://github.com/didix21/mdutils +# +# (C) 2018 Dídac Coll +# +# SPDX-License-Identifier: BSD-3-Clause + +class NewFile(object): + """NewFile class creates a new file of MarkDown extension. + + Features available are: + - Create a file. + - Rewrite all the file with new data. + - Write at the end of the file.""" + def __init__(self, name): + self.file_name = name + '.md' + self.file = open(self.file_name, 'w+', encoding='UTF-8') + self.file.close() + + def rewrite_all_file(self, data): + with open(self.file_name, 'w', encoding='utf-8') as self.file: + self.file.write(data) + + def append_end(self, data): + with open(self.file_name, 'a') as self.file: + self.file.write(data) + + +if __name__ == '__main__': + new_file = NewFile('Example') + new_file.add(data="# Some Text Example") \ No newline at end of file From e952f9d57980f9a22c8b4bf09d3b4a9802ad616e Mon Sep 17 00:00:00 2001 From: didix21 Date: Sat, 7 Apr 2018 21:26:39 +0200 Subject: [PATCH 02/50] Python code inspection done. --- mdutils/fileutils/fileutils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mdutils/fileutils/fileutils.py b/mdutils/fileutils/fileutils.py index 1e5105f..b758740 100644 --- a/mdutils/fileutils/fileutils.py +++ b/mdutils/fileutils/fileutils.py @@ -8,6 +8,7 @@ # # SPDX-License-Identifier: BSD-3-Clause + class NewFile(object): """NewFile class creates a new file of MarkDown extension. @@ -15,6 +16,7 @@ class NewFile(object): - Create a file. - Rewrite all the file with new data. - Write at the end of the file.""" + def __init__(self, name): self.file_name = name + '.md' self.file = open(self.file_name, 'w+', encoding='UTF-8') @@ -31,4 +33,4 @@ def append_end(self, data): if __name__ == '__main__': new_file = NewFile('Example') - new_file.add(data="# Some Text Example") \ No newline at end of file + new_file.rewrite_all_file(data="# Some Text Example") From 47bb68c255f52bb29467779c525774f58a203fbc Mon Sep 17 00:00:00 2001 From: didix21 Date: Sat, 7 Apr 2018 21:27:36 +0200 Subject: [PATCH 03/50] new classes created Headers and MdUtils. --- mdutils/__init__.py | 1 + mdutils/mdutils.py | 84 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 mdutils/__init__.py create mode 100644 mdutils/mdutils.py diff --git a/mdutils/__init__.py b/mdutils/__init__.py new file mode 100644 index 0000000..23905df --- /dev/null +++ b/mdutils/__init__.py @@ -0,0 +1 @@ +from mdutils.fileutils.fileutils import NewFile diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py new file mode 100644 index 0000000..e6df544 --- /dev/null +++ b/mdutils/mdutils.py @@ -0,0 +1,84 @@ +# ! Python +# +# This file is part of mdutils. https://github.com/didix21/mdutils +# (C) 2018 Dídac Coll +"""Module **mdutils** + +The different features availables are: + * Create Heades, Til 6 sub-lvels. + * Auto generate a table of contents. + * Create List and sub-list. + * Create paragraph. + * Generate tables of different sizes. + * Insert Links. + * Insert Images. + * Insert Code. +""" +from mdutils.fileutils.fileutils import NewFile + + +class Header(object): + @staticmethod + def level_1(title): + return '# ' + title + + @staticmethod + def level_2(title): + return '## ' + title + + @staticmethod + def level_3(title): + return '### ' + title + + @staticmethod + def level_4(title): + return '#### ' + title + + @staticmethod + def level_5(title): + return '##### ' + title + + @staticmethod + def level_6(title): + return '###### ' + title + + +class MdUtils: + def __init__(self, file_name, title="", author=""): + self.file_name = file_name + self.title = title + self.author = author + self.header = Header() + self.create_md_file() + self._table_titles = [] + + def create_md_file(self): + md_file = NewFile(self.file_name) + md_file.rewrite_all_file(data=self.header.level_1(self.title)) + + def new_header(self, level, title): + if level == 1: + return self.header.level_1(title) + elif level == 2: + self._add_new_item_table_of_content(title) + return self.header.level_2(title) + elif level == 3: + self._add_new_item_table_of_content(title) + return self.header.level_3(title) + elif level == 4: + self._add_new_item_table_of_content(title) + return self.header.level_4(title) + elif level == 5: + self._add_new_item_table_of_content(title) + return self.header.level_5(title) + elif level == 6: + self._add_new_item_table_of_content(title) + return self.header.level_6(title) + + def _add_new_item_table_of_content(self, item): + self._table_titles.append(item) + self._table_titles[-1].append([]) + + +if __name__ == '__main__': + heeader = Header() From dca343bdf2c8045e1dc0ec54dec5269d55e44972 Mon Sep 17 00:00:00 2001 From: didix21 Date: Tue, 10 Apr 2018 21:41:33 +0200 Subject: [PATCH 04/50] Added new Class TableOfContents --- mdutils/mdutils.py | 82 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py index e6df544..e0b9527 100644 --- a/mdutils/mdutils.py +++ b/mdutils/mdutils.py @@ -24,23 +24,49 @@ def level_1(title): @staticmethod def level_2(title): - return '## ' + title + return '\n\n## ' + title @staticmethod def level_3(title): - return '### ' + title + return '\n\n### ' + title @staticmethod def level_4(title): - return '#### ' + title + return '\n\n#### ' + title @staticmethod def level_5(title): - return '##### ' + title + return '\n\n##### ' + title @staticmethod def level_6(title): - return '###### ' + title + return '\n\n###### ' + title + + + + + +class TableOfContents(object): + def _loop_trought(self, elements, tab=''): + elements_to_string = "" + for item in elements: + if isinstance(item, list): + if item != []: + if tab == '\t': + elements_to_string += self._loop_trought(item, tab='\t\t') + else: + elements_to_string += self._loop_trought(item, tab='\t') + else: + elements_to_string += '\n' + tab + '* [' + item + '](#' + item.lower().replace(' ', '-') + ')' + + return elements_to_string + + def create_table_of_contents(self, array_of_title_contents): + table_of_contents = "\n\n" + table_of_contents += self._loop_trought(array_of_title_contents) + + return table_of_contents + class MdUtils: @@ -49,35 +75,51 @@ def __init__(self, file_name, title="", author=""): self.title = title self.author = author self.header = Header() - self.create_md_file() + self.markdown_file = self.create_md_file() self._table_titles = [] def create_md_file(self): md_file = NewFile(self.file_name) md_file.rewrite_all_file(data=self.header.level_1(self.title)) + return md_file def new_header(self, level, title): - if level == 1: + if level == 1: # Check header's level return self.header.level_1(title) elif level == 2: - self._add_new_item_table_of_content(title) - return self.header.level_2(title) + self._add_new_item_table_of_content(level, title) # Add Header level 2 to print in a table of contents + self.markdown_file.append_end(self.header.level_2(title)) elif level == 3: - self._add_new_item_table_of_content(title) - return self.header.level_3(title) + self._add_new_item_table_of_content(level, title) # Add Header level 3 to print in a table of contents + self.markdown_file.append_end(self.header.level_3(title)) elif level == 4: - self._add_new_item_table_of_content(title) - return self.header.level_4(title) + self._add_new_item_table_of_content(level, title) # Add Header level 4 to print in a table of contents + self.markdown_file.append_end(self.header.level_4(title)) elif level == 5: - self._add_new_item_table_of_content(title) - return self.header.level_5(title) + self.markdown_file.append_end(self.header.level_5(title)) elif level == 6: - self._add_new_item_table_of_content(title) - return self.header.level_6(title) + self.markdown_file.append_end(self.header.level_6(title)) + + def _add_new_item_table_of_content(self, level, item): + if level == 2: + self._table_titles.append(item) + self._table_titles.append([]) + elif level == 3: + self._table_titles[-1].append(item) + self._table_titles[-1].append([]) + elif level == 4: + self._table_titles[-1][-1].append(item) + self._table_titles[-1][-1].append([]) + elif level == 5: + self._table_titles[-1][-1][-1].append(item) + self._table_titles[-1][-1][-1].append([]) + elif level == 6: + self._table_titles[-1][-1][-1][-1].append(item) + + def new_table_of_contents(self): + table = TableOfContents().create_table_of_contents(self._table_titles) + self.markdown_file.append_end(table) - def _add_new_item_table_of_content(self, item): - self._table_titles.append(item) - self._table_titles[-1].append([]) if __name__ == '__main__': From 4000926c39cf6054b39c461451bd33d05c5f283d Mon Sep 17 00:00:00 2001 From: didix21 Date: Wed, 11 Apr 2018 00:19:54 +0200 Subject: [PATCH 05/50] It has been added comment on the different methods --- mdutils/fileutils/fileutils.py | 6 ++++++ mdutils/mdutils.py | 13 +++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/mdutils/fileutils/fileutils.py b/mdutils/fileutils/fileutils.py index b758740..2ff541f 100644 --- a/mdutils/fileutils/fileutils.py +++ b/mdutils/fileutils/fileutils.py @@ -23,10 +23,16 @@ def __init__(self, name): self.file.close() def rewrite_all_file(self, data): + """Rewrite all the data of a Markdown file by ``data``. + + - **data:** is a string containing all the data that is written in the markdown file.""" with open(self.file_name, 'w', encoding='utf-8') as self.file: self.file.write(data) def append_end(self, data): + """Write at the last position of a Markdown file. + + - **data:** is a string.""" with open(self.file_name, 'a') as self.file: self.file.write(data) diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py index e0b9527..aa17331 100644 --- a/mdutils/mdutils.py +++ b/mdutils/mdutils.py @@ -18,6 +18,7 @@ class Header(object): + """Contain the main methods to define Headers on a Markdown file.""" @staticmethod def level_1(title): return '# ' + title @@ -43,11 +44,13 @@ def level_6(title): return '\n\n###### ' + title - - - class TableOfContents(object): def _loop_trought(self, elements, tab=''): + """Method that go trough a list of elements that contain strings and other list and return a string \ + reade to be written inside a markdown file in order to create a table of contents. + + - **elements**: contain all the headers defined on the main class. + - **tab:** Inserts tabulations.""" elements_to_string = "" for item in elements: if isinstance(item, list): @@ -68,7 +71,6 @@ def create_table_of_contents(self, array_of_title_contents): return table_of_contents - class MdUtils: def __init__(self, file_name, title="", author=""): self.file_name = file_name @@ -84,7 +86,7 @@ def create_md_file(self): return md_file def new_header(self, level, title): - if level == 1: # Check header's level + if level == 1: # Check header's level return self.header.level_1(title) elif level == 2: self._add_new_item_table_of_content(level, title) # Add Header level 2 to print in a table of contents @@ -121,6 +123,5 @@ def new_table_of_contents(self): self.markdown_file.append_end(table) - if __name__ == '__main__': heeader = Header() From d7faa15b356e4b637de8917dcba9d5464e54f233 Mon Sep 17 00:00:00 2001 From: didix21 Date: Thu, 12 Apr 2018 16:08:55 +0200 Subject: [PATCH 06/50] Added new method append_after_first_line and added new documentation --- mdutils/fileutils/fileutils.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mdutils/fileutils/fileutils.py b/mdutils/fileutils/fileutils.py index 2ff541f..72f695d 100644 --- a/mdutils/fileutils/fileutils.py +++ b/mdutils/fileutils/fileutils.py @@ -25,17 +25,28 @@ def __init__(self, name): def rewrite_all_file(self, data): """Rewrite all the data of a Markdown file by ``data``. - - **data:** is a string containing all the data that is written in the markdown file.""" + :param data: is a string containing all the data that is written in the markdown file.""" with open(self.file_name, 'w', encoding='utf-8') as self.file: self.file.write(data) def append_end(self, data): """Write at the last position of a Markdown file. - - **data:** is a string.""" - with open(self.file_name, 'a') as self.file: + :param data: is a string containing all the data that is written in the markdown file.""" + with open(self.file_name, 'a', encoding='utf-8') as self.file: self.file.write(data) + def append_after_first_line(self, data): + """Write after the file's first line. + + :param data: is a string containing all the data that is written in the markdown file.""" + with open(self.file_name, 'r+', encoding='utf-8') as self.file: + file_data = self.file.read() # Save all the file's content + self.file.seek(0, 0) # Place file pointer at the beginning + first_line = self.file.readline() # Read the first line + self.file.seek(len(first_line), 0) # Place file pointer at the end of the first line + self.file.write(data) # Write data + self.file.write('\n' + file_data[len(first_line):]) if __name__ == '__main__': new_file = NewFile('Example') From caa1ab0fe81748e75d29bcad18630361532d44ce Mon Sep 17 00:00:00 2001 From: didix21 Date: Thu, 12 Apr 2018 16:24:40 +0200 Subject: [PATCH 07/50] Added new documentation on each method MdUtils Class method. --- mdutils/mdutils.py | 122 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 94 insertions(+), 28 deletions(-) diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py index aa17331..308eb8b 100644 --- a/mdutils/mdutils.py +++ b/mdutils/mdutils.py @@ -1,7 +1,13 @@ -# ! Python +# Python +# +# This module implements a main class that allows to create markdown files, write in them or read. +# +# This file is part of mdutils. https://github.com/didix21/mdutils # -# This file is part of mdutils. https://github.com/didix21/mdutils # (C) 2018 Dídac Coll +# +# SPDX-License-Identifier: BSD-3-Clause + """Module **mdutils** The different features availables are: @@ -18,31 +24,83 @@ class Header(object): - """Contain the main methods to define Headers on a Markdown file.""" + """Contain the main methods to define Headers on a Markdown file. + + """ + # ******************************************************************** + # * Atx-Style * + # ******************************************************************** @staticmethod - def level_1(title): + def atx_level_1(title): return '# ' + title @staticmethod - def level_2(title): + def atx_level_2(title): return '\n\n## ' + title @staticmethod - def level_3(title): + def atx_level_3(title): return '\n\n### ' + title @staticmethod - def level_4(title): + def atx_level_4(title): return '\n\n#### ' + title @staticmethod - def level_5(title): + def atx_level_5(title): return '\n\n##### ' + title @staticmethod - def level_6(title): + def atx_level_6(title): return '\n\n###### ' + title + # ******************************************************************** + # * Setext-Style * + # ******************************************************************** + @staticmethod + def setext_level_1(title): + return title + '\n' + ''.join(['=' for _ in title]) + + @staticmethod + def setext_level_2(title): + return title + '\n' + ''.join(['-' for _ in title]) + + def choose_header(self, level, title, style='atx'): + """ This method choose the style and the header level. + + :Examples: + >>> import Header + >>> createHeaders = Header() + >>> createHeaders.choose_header(level=1, title='New Header', style='atx') + "# New Header" + + >>> createHeaders.choose_header(level=1, title='Another Header 1', style=setext) + "Another Header 1\\n----------------" + + :param level: Header Level, For Atx-style 1 til 6. For Setext-style 1 and 2 header levels. + :param title: Header Title. + :param style: Header Style atx or setext. + :return: + """ + if style.lower() == 'atx': + if level == 1: + return self.atx_level_1(title) + elif level == 2: + return self.atx_level_2(title) + elif level == 3: + return self.atx_level_3(title) + elif level == 4: + return self.atx_level_4(title) + elif level == 5: + return self.atx_level_5(title) + elif level == 6: + return self.atx_level_6(title) + elif style.lower() == 'setext': + if level == 1: + return self.setext_level_1(title) + elif level == 2: + return self.setext_level_2(title) + class TableOfContents(object): def _loop_trought(self, elements, tab=''): @@ -65,13 +123,18 @@ def _loop_trought(self, elements, tab=''): return elements_to_string def create_table_of_contents(self, array_of_title_contents): - table_of_contents = "\n\n" + table_of_contents = "" table_of_contents += self._loop_trought(array_of_title_contents) return table_of_contents class MdUtils: + """This class give some basic methods that helps the creation of Markdown files. + + Long description will be written. + + """ def __init__(self, file_name, title="", author=""): self.file_name = file_name self.title = title @@ -81,26 +144,28 @@ def __init__(self, file_name, title="", author=""): self._table_titles = [] def create_md_file(self): + """ It creates a new Markdown file""" md_file = NewFile(self.file_name) - md_file.rewrite_all_file(data=self.header.level_1(self.title)) + md_file.rewrite_all_file(data=self.header.choose_header(level=1, title=self.title)) return md_file - def new_header(self, level, title): - if level == 1: # Check header's level - return self.header.level_1(title) - elif level == 2: - self._add_new_item_table_of_content(level, title) # Add Header level 2 to print in a table of contents - self.markdown_file.append_end(self.header.level_2(title)) - elif level == 3: - self._add_new_item_table_of_content(level, title) # Add Header level 3 to print in a table of contents - self.markdown_file.append_end(self.header.level_3(title)) - elif level == 4: - self._add_new_item_table_of_content(level, title) # Add Header level 4 to print in a table of contents - self.markdown_file.append_end(self.header.level_4(title)) - elif level == 5: - self.markdown_file.append_end(self.header.level_5(title)) - elif level == 6: - self.markdown_file.append_end(self.header.level_6(title)) + def new_header(self, level, title, style='atx'): + """ Add a new header to the Markdown file. + **Examples:** + ``new_header(level=2, title='Header Title', style='atx')`` + This will write a new level 2 Atx-style header on file_name.md: + * ## Header Title + ``new_header(level=2, title='Header Title', style='setext)`` + This will write a new level 2 Setext-style header on file_name.md: + * Header Title + '-------------' + + :param level: Header level + :param title: Header title + :param style: Header style, could be ``'atx'`` or ``'setext'``. By default **atx* style is chosen. + """ + self._add_new_item_table_of_content(level, title) + self.markdown_file.append_end(self.header.choose_header(level, title, style)) def _add_new_item_table_of_content(self, level, item): if level == 2: @@ -119,8 +184,9 @@ def _add_new_item_table_of_content(self, level, item): self._table_titles[-1][-1][-1][-1].append(item) def new_table_of_contents(self): + """ Add a table of contents of the Header Levels 2, 3 and 4.""" table = TableOfContents().create_table_of_contents(self._table_titles) - self.markdown_file.append_end(table) + self.markdown_file.append_after_first_line(table) if __name__ == '__main__': From 96ec0c0aa37a3c7b9b962fa7a604c6fc08dd9d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Thu, 12 Apr 2018 19:27:49 +0200 Subject: [PATCH 08/50] Renamed append_after_first_line by append_after_second_line. It has been changed the behaviour of this method. --- mdutils/fileutils/fileutils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mdutils/fileutils/fileutils.py b/mdutils/fileutils/fileutils.py index 72f695d..cb61c34 100644 --- a/mdutils/fileutils/fileutils.py +++ b/mdutils/fileutils/fileutils.py @@ -36,17 +36,18 @@ def append_end(self, data): with open(self.file_name, 'a', encoding='utf-8') as self.file: self.file.write(data) - def append_after_first_line(self, data): + def append_after_second_line(self, data): """Write after the file's first line. :param data: is a string containing all the data that is written in the markdown file.""" with open(self.file_name, 'r+', encoding='utf-8') as self.file: - file_data = self.file.read() # Save all the file's content - self.file.seek(0, 0) # Place file pointer at the beginning - first_line = self.file.readline() # Read the first line - self.file.seek(len(first_line), 0) # Place file pointer at the end of the first line - self.file.write(data) # Write data - self.file.write('\n' + file_data[len(first_line):]) + file_data = self.file.read() # Save all the file's content + self.file.seek(0, 0) # Place file pointer at the beginning + first_line = self.file.readline() # Read the first line + second_line = self.file.readline() # Read the second line + self.file.seek(len(first_line + second_line), 0) # Place file pointer at the end of the first line + self.file.write(data) # Write data + self.file.write('\n' + file_data[len(first_line + second_line):]) if __name__ == '__main__': new_file = NewFile('Example') From bf1ef0d900afbb72b8a63ca92163f49427bb70f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Thu, 12 Apr 2018 19:28:58 +0200 Subject: [PATCH 09/50] Now the table of contents will be written at the second line of Markdown file. --- mdutils/mdutils.py | 132 ++++++--------------------------------------- 1 file changed, 15 insertions(+), 117 deletions(-) diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py index 308eb8b..75c7556 100644 --- a/mdutils/mdutils.py +++ b/mdutils/mdutils.py @@ -1,6 +1,6 @@ # Python # -# This module implements a main class that allows to create markdown files, write in them or read. +# This module implements the Main Markdown class. # # This file is part of mdutils. https://github.com/didix21/mdutils # @@ -21,112 +21,10 @@ * Insert Code. """ from mdutils.fileutils.fileutils import NewFile +from mdutils.tools.tools import Header +from mdutils.tools.tools import TableOfContents -class Header(object): - """Contain the main methods to define Headers on a Markdown file. - - """ - # ******************************************************************** - # * Atx-Style * - # ******************************************************************** - @staticmethod - def atx_level_1(title): - return '# ' + title - - @staticmethod - def atx_level_2(title): - return '\n\n## ' + title - - @staticmethod - def atx_level_3(title): - return '\n\n### ' + title - - @staticmethod - def atx_level_4(title): - return '\n\n#### ' + title - - @staticmethod - def atx_level_5(title): - return '\n\n##### ' + title - - @staticmethod - def atx_level_6(title): - return '\n\n###### ' + title - - # ******************************************************************** - # * Setext-Style * - # ******************************************************************** - @staticmethod - def setext_level_1(title): - return title + '\n' + ''.join(['=' for _ in title]) - - @staticmethod - def setext_level_2(title): - return title + '\n' + ''.join(['-' for _ in title]) - - def choose_header(self, level, title, style='atx'): - """ This method choose the style and the header level. - - :Examples: - >>> import Header - >>> createHeaders = Header() - >>> createHeaders.choose_header(level=1, title='New Header', style='atx') - "# New Header" - - >>> createHeaders.choose_header(level=1, title='Another Header 1', style=setext) - "Another Header 1\\n----------------" - - :param level: Header Level, For Atx-style 1 til 6. For Setext-style 1 and 2 header levels. - :param title: Header Title. - :param style: Header Style atx or setext. - :return: - """ - if style.lower() == 'atx': - if level == 1: - return self.atx_level_1(title) - elif level == 2: - return self.atx_level_2(title) - elif level == 3: - return self.atx_level_3(title) - elif level == 4: - return self.atx_level_4(title) - elif level == 5: - return self.atx_level_5(title) - elif level == 6: - return self.atx_level_6(title) - elif style.lower() == 'setext': - if level == 1: - return self.setext_level_1(title) - elif level == 2: - return self.setext_level_2(title) - - -class TableOfContents(object): - def _loop_trought(self, elements, tab=''): - """Method that go trough a list of elements that contain strings and other list and return a string \ - reade to be written inside a markdown file in order to create a table of contents. - - - **elements**: contain all the headers defined on the main class. - - **tab:** Inserts tabulations.""" - elements_to_string = "" - for item in elements: - if isinstance(item, list): - if item != []: - if tab == '\t': - elements_to_string += self._loop_trought(item, tab='\t\t') - else: - elements_to_string += self._loop_trought(item, tab='\t') - else: - elements_to_string += '\n' + tab + '* [' + item + '](#' + item.lower().replace(' ', '-') + ')' - - return elements_to_string - - def create_table_of_contents(self, array_of_title_contents): - table_of_contents = "" - table_of_contents += self._loop_trought(array_of_title_contents) - - return table_of_contents class MdUtils: @@ -146,7 +44,7 @@ def __init__(self, file_name, title="", author=""): def create_md_file(self): """ It creates a new Markdown file""" md_file = NewFile(self.file_name) - md_file.rewrite_all_file(data=self.header.choose_header(level=1, title=self.title)) + md_file.rewrite_all_file(data=self.header.choose_header(level=1, title=self.title, style='setext')) return md_file def new_header(self, level, title, style='atx'): @@ -168,25 +66,25 @@ def new_header(self, level, title, style='atx'): self.markdown_file.append_end(self.header.choose_header(level, title, style)) def _add_new_item_table_of_content(self, level, item): - if level == 2: + if level == 1: self._table_titles.append(item) self._table_titles.append([]) - elif level == 3: + elif level == 2: self._table_titles[-1].append(item) self._table_titles[-1].append([]) - elif level == 4: - self._table_titles[-1][-1].append(item) - self._table_titles[-1][-1].append([]) - elif level == 5: - self._table_titles[-1][-1][-1].append(item) - self._table_titles[-1][-1][-1].append([]) - elif level == 6: - self._table_titles[-1][-1][-1][-1].append(item) + # elif level == 3: + # self._table_titles[-1][-1].append(item) + # self._table_titles[-1][-1].append([]) + # elif level == 4: + # self._table_titles[-1][-1][-1].append(item) + # self._table_titles[-1][-1][-1].append([]) + # elif level == 5: + # self._table_titles[-1][-1][-1][-1].append(item) def new_table_of_contents(self): """ Add a table of contents of the Header Levels 2, 3 and 4.""" table = TableOfContents().create_table_of_contents(self._table_titles) - self.markdown_file.append_after_first_line(table) + self.markdown_file.append_after_second_line(table) if __name__ == '__main__': From 0b4503f15c4868da8c7d86a81a35cabc04be1ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Thu, 12 Apr 2018 19:29:22 +0200 Subject: [PATCH 10/50] "" --- mdutils/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mdutils/__init__.py b/mdutils/__init__.py index 23905df..6e45703 100644 --- a/mdutils/__init__.py +++ b/mdutils/__init__.py @@ -1 +1,2 @@ from mdutils.fileutils.fileutils import NewFile + From d900cf3587fc86ca0d1d8238819711bdc61b2b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Thu, 12 Apr 2018 21:08:27 +0200 Subject: [PATCH 11/50] Firs commit of tools.py: Contain classes Header, TableOfCOntents, Table and TextUtils. --- mdutils/tools/tools.py | 181 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 mdutils/tools/tools.py diff --git a/mdutils/tools/tools.py b/mdutils/tools/tools.py new file mode 100644 index 0000000..7f87fd2 --- /dev/null +++ b/mdutils/tools/tools.py @@ -0,0 +1,181 @@ +# Python +# +# This module implements a text class that allows to modify and create text on Markdown files. +# +# This file is part of mdutils. https://github.com/didix21/mdutils +# +# (C) 2018 Dídac Coll +# +# SPDX-License-Identifier: BSD-3-Clause + + +class Header(object): + """Contain the main methods to define Headers on a Markdown file. + + """ + + # ******************************************************************** + # * Atx-Style * + # ******************************************************************** + @staticmethod + def atx_level_1(title): + return '\n\n# ' + title + + @staticmethod + def atx_level_2(title): + return '\n\n## ' + title + + @staticmethod + def atx_level_3(title): + return '\n\n### ' + title + + @staticmethod + def atx_level_4(title): + return '\n\n#### ' + title + + @staticmethod + def atx_level_5(title): + return '\n\n##### ' + title + + @staticmethod + def atx_level_6(title): + return '\n\n###### ' + title + + # ******************************************************************** + # * Setext-Style * + # ******************************************************************** + @staticmethod + def setext_level_1(title): + return title + '\n' + ''.join(['=' for _ in title]) + + @staticmethod + def setext_level_2(title): + return title + '\n' + ''.join(['-' for _ in title]) + + def choose_header(self, level, title, style='atx'): + """ This method choose the style and the header level. + + :Examples: + >>> from mdutils.tools.tools import Header + >>> createHeaders = Header() + >>> createHeaders.choose_header(level=1, title='New Header', style='atx') + "# New Header" + + >>> createHeaders.choose_header(level=1, title='Another Header 1', style='setext') + "Another Header 1\\n----------------" + + :param level: Header Level, For Atx-style 1 til 6. For Setext-style 1 and 2 header levels. + :param title: Header Title. + :param style: Header Style atx or setext. + :return: + """ + if style.lower() == 'atx': + if level == 1: + return self.atx_level_1(title) + elif level == 2: + return self.atx_level_2(title) + elif level == 3: + return self.atx_level_3(title) + elif level == 4: + return self.atx_level_4(title) + elif level == 5: + return self.atx_level_5(title) + elif level == 6: + return self.atx_level_6(title) + elif style.lower() == 'setext': + if level == 1: + return self.setext_level_1(title) + elif level == 2: + return self.setext_level_2(title) + + +class TableOfContents(object): + def _loop_trought(self, elements, tab=''): + """Method that go trough a list of elements that contain strings and other list and return a string \ + reade to be written inside a markdown file in order to create a table of contents. + + - **elements**: contain all the headers defined on the main class. + - **tab:** Inserts tabulations.""" + elements_to_string = "" + for item in elements: + if isinstance(item, list): + if item: + if tab == '\t': + elements_to_string += self._loop_trought(item, tab='\t\t') + else: + elements_to_string += self._loop_trought(item, tab='\t') + else: + elements_to_string += '\n' + tab + '* [' + item + '](#' + item.lower().replace(' ', '-') + ')' + + return elements_to_string + + def create_table_of_contents(self, array_of_title_contents): + table_of_contents = "" + table_of_contents += self._loop_trought(array_of_title_contents) + + return table_of_contents + + +class Table(object): + + def __init__(self): + self.rows = 0 + self.columns = 0 + + @staticmethod + def _align(columns, text_align): + + column_align_string = '' + + if text_align == 'center': + + column_align_string = '|' + ''.join([' :---: |' for _ in range(columns)]) + + elif text_align == 'left': + + column_align_string = '|' + ''.join([' :--- |' for _ in range(columns)]) + + elif text_align == 'right': + + column_align_string = '|' + ''.join([' ---: |' for _ in range(columns)]) + + return column_align_string + + def create_table(self, columns, rows, text, text_align='center'): + self.columns = columns + self.rows = rows + table = '' + column_align_string = self._align(columns, text_align) + + for r in range(rows): + for c in range(columns): + if r == 1: + table += column_align_string + else: + if c != columns - 1: + table += '| ' + text[c + r] + else: + table += ' |' + table += '\n' + + +class TextUtils(object): + """This class helps to create bold, italics and change color text. + + """ + + @staticmethod + def bold(text): + return '**' + text + '**' + + @staticmethod + def italics(text): + return '_' + text + '_' + + @staticmethod + def inline_code(text): + return '`' + text + '`' + + @staticmethod + def text_color(text, color="black"): + return '' + text + '' From 0656303cabc42d0c9b7c35f98801153a25b3b6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Thu, 12 Apr 2018 21:08:56 +0200 Subject: [PATCH 12/50] Adding __init__.py file --- mdutils/tools/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 mdutils/tools/__init__.py diff --git a/mdutils/tools/__init__.py b/mdutils/tools/__init__.py new file mode 100644 index 0000000..e69de29 From b8e4a2a6d153ca10222e01bb08034286083d7069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Thu, 12 Apr 2018 21:09:12 +0200 Subject: [PATCH 13/50] Ignore Test.md file. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 7db61e6..0eef1cd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ __pycache__/ # C extensions *.so +# Others +Test.md + # Distribution / packaging .Python build/ From 32cb7d3e0b5cb4e09615f4e18ca43ab8f797871f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Thu, 12 Apr 2018 21:14:42 +0200 Subject: [PATCH 14/50] Renamed _llop_trough --- mdutils/tools/tools.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/mdutils/tools/tools.py b/mdutils/tools/tools.py index 7f87fd2..a603319 100644 --- a/mdutils/tools/tools.py +++ b/mdutils/tools/tools.py @@ -9,6 +9,7 @@ # SPDX-License-Identifier: BSD-3-Clause +# noinspection SpellCheckingInspection class Header(object): """Contain the main methods to define Headers on a Markdown file. @@ -53,22 +54,23 @@ def setext_level_2(title): return title + '\n' + ''.join(['-' for _ in title]) def choose_header(self, level, title, style='atx'): + # noinspection SpellCheckingInspection """ This method choose the style and the header level. - :Examples: - >>> from mdutils.tools.tools import Header - >>> createHeaders = Header() - >>> createHeaders.choose_header(level=1, title='New Header', style='atx') - "# New Header" + :Examples: + >>> from mdutils.tools.tools import Header + >>> createHeaders = Header() + >>> createHeaders.choose_header(level=1, title='New Header', style='atx') + "# New Header" - >>> createHeaders.choose_header(level=1, title='Another Header 1', style='setext') - "Another Header 1\\n----------------" + >>> createHeaders.choose_header(level=1, title='Another Header 1', style='setext') + "Another Header 1\\n----------------" - :param level: Header Level, For Atx-style 1 til 6. For Setext-style 1 and 2 header levels. - :param title: Header Title. - :param style: Header Style atx or setext. - :return: - """ + :param level: Header Level, For Atx-style 1 til 6. For Setext-style 1 and 2 header levels. + :param title: Header Title. + :param style: Header Style atx or setext. + :return: + """ if style.lower() == 'atx': if level == 1: return self.atx_level_1(title) @@ -90,7 +92,7 @@ def choose_header(self, level, title, style='atx'): class TableOfContents(object): - def _loop_trought(self, elements, tab=''): + def _loop_through(self, elements, tab=''): """Method that go trough a list of elements that contain strings and other list and return a string \ reade to be written inside a markdown file in order to create a table of contents. @@ -101,9 +103,9 @@ def _loop_trought(self, elements, tab=''): if isinstance(item, list): if item: if tab == '\t': - elements_to_string += self._loop_trought(item, tab='\t\t') + elements_to_string += self._loop_through(item, tab='\t\t') else: - elements_to_string += self._loop_trought(item, tab='\t') + elements_to_string += self._loop_through(item, tab='\t') else: elements_to_string += '\n' + tab + '* [' + item + '](#' + item.lower().replace(' ', '-') + ')' @@ -111,7 +113,7 @@ def _loop_trought(self, elements, tab=''): def create_table_of_contents(self, array_of_title_contents): table_of_contents = "" - table_of_contents += self._loop_trought(array_of_title_contents) + table_of_contents += self._loop_through(array_of_title_contents) return table_of_contents From 226c6d577e205198a4852213e5ae217251f4aa1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll?= Date: Fri, 13 Apr 2018 14:37:06 +0200 Subject: [PATCH 15/50] Ignore .idea folder. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0eef1cd..85630cc 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ __pycache__/ # Others Test.md +.idea* # Distribution / packaging .Python From 16627770d6f4d50915381bc1105f46e50264a890 Mon Sep 17 00:00:00 2001 From: didix21 Date: Fri, 13 Apr 2018 16:05:52 +0200 Subject: [PATCH 16/50] " Now you can create tables" --- mdutils/mdutils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py index 75c7556..9faa5a6 100644 --- a/mdutils/mdutils.py +++ b/mdutils/mdutils.py @@ -23,8 +23,7 @@ from mdutils.fileutils.fileutils import NewFile from mdutils.tools.tools import Header from mdutils.tools.tools import TableOfContents - - +from mdutils.tools.tools import Table class MdUtils: @@ -86,6 +85,11 @@ def new_table_of_contents(self): table = TableOfContents().create_table_of_contents(self._table_titles) self.markdown_file.append_after_second_line(table) + def create_table(self, columns, rows, text, text_align='center'): + new_table = Table() + text_table = new_table.create_table(columns, rows, text, text_align) + self.markdown_file.append_end(text_table) + if __name__ == '__main__': heeader = Header() From 6bd28be71707e3c556603edb92c941f78cf66ad6 Mon Sep 17 00:00:00 2001 From: didix21 Date: Fri, 13 Apr 2018 16:06:19 +0200 Subject: [PATCH 17/50] Now tables are correctly created --- mdutils/tools/tools.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/mdutils/tools/tools.py b/mdutils/tools/tools.py index a603319..ec60405 100644 --- a/mdutils/tools/tools.py +++ b/mdutils/tools/tools.py @@ -148,18 +148,17 @@ def create_table(self, columns, rows, text, text_align='center'): self.rows = rows table = '' column_align_string = self._align(columns, text_align) - + n_row = 0 # New Row, its value is 1 when is set row align for r in range(rows): - for c in range(columns): - if r == 1: - table += column_align_string - else: - if c != columns - 1: - table += '| ' + text[c + r] - else: - table += ' |' + if r == 1: + table += column_align_string # Row align, Example: '| :---: | :---: | ... | \n' + n_row = 1 + else: + table += '|' + ''.join([text[c + r + n_row] + '|' for c in range(columns)]) table += '\n' + return table + class TextUtils(object): """This class helps to create bold, italics and change color text. From fb1fe32b3bf9a2f4bb6192458914f2464c842ed3 Mon Sep 17 00:00:00 2001 From: didix21 Date: Fri, 13 Apr 2018 16:30:46 +0200 Subject: [PATCH 18/50] Adding Readme.md --- Readme.md | 1 + Testing_md.py | 0 2 files changed, 1 insertion(+) create mode 100644 Readme.md create mode 100644 Testing_md.py diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..264a7ca --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ +['Test', 'Descripción', 'Estado', 'Test 1', 'Carga de configuración correcta', 'OK', 'Test 2', 'Lectura de Configuración', '', 'Test 3', 'Lectura de Soporte', '', 'Test 4', 'Modificación de entradas y lectura de salidas de cantón', '', 'Test 5', 'Lectura de estados de Pedal de Rearme y Aviso', '', 'Test 6', 'Actualización de datos de unidades de vía', '', 'Test 7', 'Fallos en carga de configuración - Campo IdApp Erróneo', '', 'Test 8', 'Fallos en carga de configuración - Campo VersTAbla Erróneo', '', 'Test 9', 'Fallos en carga de configuración - Campo IdUc Erróneo', '', 'Test 10', 'Fallos en carga de configuración - Campo Addresses Erróneo', '', 'Test 11', 'Fallos en carga de configuración - Campo NumTc Erróneo', '', 'Test 12', 'Fallos en carga de configuración - Campo NumUv Erróneo', '', 'Test 13', 'Fallos en carga de configuración - Campo CRC Erróneo', '', 'Test 14', 'Fallos en carga de configuración - Campo IdTc Erróneo', '', 'Test 15', 'Fallos en carga de configuración - Campo TC - Type erróneo', '', 'Test 16', 'Fallos en carga de configuración - Campo TC - NumUVs erróneo', '', 'Test 17', 'Fallos en carga de configuración - Campo TC - NumUVsNormalizacion erróneo', '', 'Test 18', 'Fallos en carga de configuración - Campo TC - IdUVsNormalizacion erróneo', '', 'Test 19', 'Fallos en carga de configuración - Campo TC - NormType erróneo', '', 'Test 20', 'Fallos en carga de configuración - Campo TC - RtType erróneo', '', 'Test 21', 'Fallos en carga de configuración - Campo TC - AveriaType erróneo', '', 'Test 23', 'Fallos en carga de configuración - Campo UV - IdUv Erróneo', '', 'Test 24', 'Fallos en carga de configuración - Campo UV - UvType Erróneo', '', 'Test 25', 'Fallos en carga de configuración - Campo UV - Tc no configurado', '', 'Test 26', 'Fallos en las comunicaciones - Mensaje GetSop no válido', '', 'Test 27', 'Fallos en las comunicaciones - Mensaje SetCnf no válido', '', 'Test 28', 'Fallos en las comunicaciones - Mensaje GetCnf no válido', '', 'Test 29', 'Fallos en las comunicaciones - Mensaje SetGetTcIO no válido', '', 'Test 30', 'Fallos en las comunicaciones - Mensaje GetPrAv no válido', '', 'Test 31', 'Fallos en las comunicaciones - Mensaje SetUvData no válido', '', 'Test 32', 'Fallos en Procesos - Activar proceso de Normalización desde Avería', '', 'Test 33', 'Fallos en Procesos - Activar proceso de Normalización desde Servicio', ''] diff --git a/Testing_md.py b/Testing_md.py new file mode 100644 index 0000000..e69de29 From 41fd444ea8e58593a6765cefe8b8f173da4ebdd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Coll=20Pujals?= Date: Sun, 15 Apr 2018 18:03:57 +0200 Subject: [PATCH 19/50] Changed some features, now all data will be saved previously in a string and then will be written to Markdown file. Now, create_md_file() is called at the end of the code --- .idea/dictionaries/didix21.xml | 9 + .idea/mdutils_develop.iml | 13 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 623 +++++++++++++++++++++++++++++++++ Test/__init__.py | 0 Test/test_mdUtils.py | 0 Test/test_tools/__init__.py | 0 Test/test_tools/test_header.py | 0 mdutils/mdutils.py | 55 +-- mdutils/tools/tools.py | 28 +- test.py | 37 ++ 13 files changed, 745 insertions(+), 38 deletions(-) create mode 100644 .idea/dictionaries/didix21.xml create mode 100644 .idea/mdutils_develop.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 Test/__init__.py create mode 100644 Test/test_mdUtils.py create mode 100644 Test/test_tools/__init__.py create mode 100644 Test/test_tools/test_header.py create mode 100644 test.py diff --git a/.idea/dictionaries/didix21.xml b/.idea/dictionaries/didix21.xml new file mode 100644 index 0000000..290dd3a --- /dev/null +++ b/.idea/dictionaries/didix21.xml @@ -0,0 +1,9 @@ + + + + dídac + mdutils + spdx + + + \ No newline at end of file diff --git a/.idea/mdutils_develop.iml b/.idea/mdutils_develop.iml new file mode 100644 index 0000000..57bab0f --- /dev/null +++ b/.idea/mdutils_develop.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b8f0eb1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..be34e12 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..e120e15 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,623 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + new_header + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + - - + + - + - + + + - - - - + + + + + + + + - - - - + + + + + + @@ -342,23 +384,110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + - + - + + - - - + + @@ -384,7 +513,16 @@ - @@ -400,12 +538,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -422,21 +604,10 @@ - - - - - - - - - - - - + @@ -450,27 +621,16 @@ - + - - - - - - - - - - - + - @@ -487,7 +647,7 @@ - + @@ -505,7 +665,7 @@ - + @@ -514,21 +674,10 @@ - - - - - - - - - - - - + @@ -560,61 +709,133 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + - + - - + + + - + - - + + - + - + - - + + - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + diff --git a/Test/test_tools/test_table.py b/Test/test_tools/test_table.py index 8e8b5c8..24db5e1 100644 --- a/Test/test_tools/test_table.py +++ b/Test/test_tools/test_table.py @@ -53,9 +53,7 @@ def test_create_table(self): 'Test 12', 'Fallos en carga de configuración - Campo NumUv Erróneo', md_file.textUtils.text_color("NOK", 'red'), 'Test 13', 'Fallos en carga de configuración - Campo CRC Erróneo', - md_file.textUtils.text_color("NOK", 'red'), - 'Test 14', 'Fallos en carga de configuración - Campo IdTc Erróneo', md_file.textUtils.text_color("NOK", 'red')] - self.assertEqual(table.create_table(columns=3, rows=15, text=text_array), result_table) + self.assertEqual(table.create_table(columns=3, rows=13, text=text_array), result_table) diff --git a/Testing_md.py b/Testing_md.py index e69de29..6dd5fcd 100644 --- a/Testing_md.py +++ b/Testing_md.py @@ -0,0 +1,10 @@ +from mdutils.mdutils import MdUtils + +md_file = MdUtils("Testing_md", title="Testing Tables") +text_array = ["Test", "Description", "State", "Test 1", "Carga de configuración correcta", "OK"] + +md_file.create_md_file() +md_file.markdown_file.append_end('\n\n') +md_file.create_table(3, 3, text_array) + + diff --git a/mdutils/tools/tools.py b/mdutils/tools/tools.py index c31e245..7bf9d5a 100644 --- a/mdutils/tools/tools.py +++ b/mdutils/tools/tools.py @@ -169,7 +169,7 @@ def create_table(self, columns, rows, text, text_align='center'): table = '\n' column_align_string = self._align(columns, text_align) index = 0 - for r in range(rows): + for r in range(rows + 2): if r == 1: table += column_align_string # Row align, Example: '| :---: | :---: | ... | \n' else: @@ -267,7 +267,7 @@ def text_format(self, text, bold_italics_code='', color='black'): '_** Some Text Here **_' """ if color != 'black': - new_text_format = self.text_color(color) + new_text_format = self.text_color(text, color) else: new_text_format = text From 9ce8948da55d46f13aed33211f611a433793c8bb Mon Sep 17 00:00:00 2001 From: didix21 Date: Mon, 16 Apr 2018 12:19:02 +0200 Subject: [PATCH 39/50] Added methods create_marker and place_text_using_marker. Methods implemented on create_table. --- mdutils/mdutils.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py index 9c98280..7eeede8 100644 --- a/mdutils/mdutils.py +++ b/mdutils/mdutils.py @@ -95,10 +95,13 @@ def new_table_of_contents(self, table_title="Table of contents"): return self.table_of_contents - def create_table(self, columns, rows, text, text_align='center'): + def create_table(self, columns, rows, text, text_align='center', marker=''): new_table = tools.Table() text_table = new_table.create_table(columns, rows, text, text_align) - self.file_data_text += text_table + if marker: + self.file_data_text = self.place_text_using_marker(text_table, marker) + else: + self.file_data_text += text_table return text_table @@ -110,6 +113,32 @@ def add_new_paragraph(self, text=''): self.file_data_text += '\n' + text + '\n' return self.file_data_text + def create_marker(self, text_marker): + """This will add a marker to file_data_text in order to add after some text. + + :param text_marker: marker name. + :type text_marker: str + :return return the marker. + :rtype: str + """ + + new_marker = '##--[' + text_marker + ']--##' + self.file_data_text += new_marker + return new_marker + + def place_text_using_marker(self, text, marker): + """It replace a previous marker created with ``create_marker`` with a text string. + + :param text: the new string that will replace the marker. + :type text: str + :param marker: the marker that has to be replaced. + :type marker: str + :return: return a new file_data_text with the replace marker. + :rtype: str + """ + return self.file_data_text.replace(marker, text) + + if __name__ == '__main__': header = tools.Header() From 824378bdcba5d1ede7afcbe25139067b1d3e1d19 Mon Sep 17 00:00:00 2001 From: didix21 Date: Mon, 16 Apr 2018 12:42:17 +0200 Subject: [PATCH 40/50] Deleted create_itnernal_link method and created a new one named header_anchor --- mdutils/tools/tools.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/mdutils/tools/tools.py b/mdutils/tools/tools.py index 7bf9d5a..76b3b09 100644 --- a/mdutils/tools/tools.py +++ b/mdutils/tools/tools.py @@ -53,6 +53,27 @@ def setext_level_1(title): def setext_level_2(title): return '\n' + title + '\n' + ''.join(['-' for _ in title]) + '\n' + @staticmethod + def header_anchor(text, link=""): + """Creates an internal link of a defined Header level 1 or level 2 in the markdown file. + + Giving a text string an text link you can create an internal link of already existing header. If the ``link`` + string does not contain '#', it will creates an automatic link of the type ``#title-1``. + + :param text: it is the text that will be displayed. + :param link: it is the internal link. + :return: ``'[text](#link)'`` + :type text: str + :type link: str + :rtype: string + + **Example:** [Title 1](#title-1) + """ + if link[0] != '#': + link = link.lower().replace(' ', '-') + + return '[' + text + '](#' + link + ')' + def choose_header(self, level, title, style='atx'): # noinspection SpellCheckingInspection """ This method choose the style and the header level. @@ -214,27 +235,6 @@ def text_color(text, color="black"): return ' ' + text + ' ' - @staticmethod - def text_internal_link(text, link=""): - """Creates an internal link of a defined Header level 1 or level 2 in the markdown file. - - Giving a text string an text link you can create an internal link of already existing header. If the ``link`` - string does not contain '#', it will creates an automatic link of the type ``#title-1``. - - :param text: it is the text that will be displayed. - :param link: it is the internal link. - :return: ``'[text](#link)'`` - :type text: str - :type link: str - :rtype: string - - **Example:** [Title 1](#title-1) - """ - if link[0] != '#': - link = link.lower().replace(' ', '-') - - return '[' + text + '](#' + link + ')' - @staticmethod def text_external_link(text, link=''): """ Using this method can be created an external link of a file or a web page. From 5158ddf7b54d88756ab6ddf2bc9c368182fb84b3 Mon Sep 17 00:00:00 2001 From: didix21 Date: Mon, 16 Apr 2018 15:38:36 +0200 Subject: [PATCH 41/50] It is not necessary to give a link value. Anchor can be directly created using the text's value --- mdutils/tools/tools.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mdutils/tools/tools.py b/mdutils/tools/tools.py index 76b3b09..8102dda 100644 --- a/mdutils/tools/tools.py +++ b/mdutils/tools/tools.py @@ -69,10 +69,15 @@ def header_anchor(text, link=""): **Example:** [Title 1](#title-1) """ - if link[0] != '#': - link = link.lower().replace(' ', '-') + if link: + if link[0] != '#': + link = link.lower().replace(' ', '-') + else: + link = '#' + link + else: + link = '#' + text.lower().replace(' ', '-') - return '[' + text + '](#' + link + ')' + return '[' + text + '](' + link + ')' def choose_header(self, level, title, style='atx'): # noinspection SpellCheckingInspection From 3ef0d15fdb564c07c63b26813c19d9b0df323ccb Mon Sep 17 00:00:00 2001 From: didix21 Date: Mon, 16 Apr 2018 16:34:43 +0200 Subject: [PATCH 42/50] Added new methods to align text. --- .idea/workspace.xml | 176 +++++++++++++++++++++-------------------- mdutils/mdutils.py | 17 +++- mdutils/tools/tools.py | 16 +++- 3 files changed, 118 insertions(+), 91 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d17fe10..43eff76 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,11 +5,8 @@ - - - - + - + - - - - - + + + @@ -36,29 +31,25 @@ - - - + + + + + + + - - + + - + - - - - - - - - - - - + + + @@ -94,7 +85,6 @@ @@ -138,34 +129,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - -