Source code for mdutils.tools.tools

# 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
#
# MIT License: (C) 2018 Dídac Coll


# noinspection SpellCheckingInspection



[docs]class TableOfContents(object): def _loop_through(self, elements, tab='', depth=1): """Method that go through a list of elements that contain strings and other list and return a string. The returned string is ready to be written inside a markdown file in order to create a table of contents. :param elements: contain all the headers defined on the main class. :param tab: Inserts tabulations. :param depth: allows to include Headers 1 and 2 or only Headers of level 1. Possibly values 1 or 2. :type elements: list :type tab: str :type depth: int :rtype: str """ elements_to_string = "" for item in elements: if isinstance(item, list): if item and depth >= 2: if tab == '\t': elements_to_string += self._loop_through(item, tab='\t\t') else: elements_to_string += self._loop_through(item, tab='\t') else: elements_to_string += '\n' + tab + '* [' + item + '](#' + item.lower().replace(' ', '-') + ')' return elements_to_string
[docs] def create_table_of_contents(self, array_of_title_contents, depth=1): """This method can create a table of contents using an array of the different titles. The deep can be changed. Only accepts 1 or 2. :param array_of_title_contents: a string list with the different headers. :type array_of_title_contents: list :param depth: allows to include Headers 1 and 2 or only Headers of level 1. Possibly values 1 or 2. :type depth: int :return: return a string ready to be written to a Markdown file. :rtype: str """ table_of_contents = "" table_of_contents += self._loop_through(array_of_title_contents, depth=depth) table_of_contents += '\n' return table_of_contents
[docs]class Table(object): def __init__(self): self.rows = 0 self.columns = 0 @staticmethod def _align(columns, text_align): """ This private method it is in charfe of aligning text of a table. - notes: more information about `text_align` :param columns: it is the number of columns. :param text_align: it is a string giving information about the alignment that is wanted. text_align can take the following parameters: ``'center'``, ``'right'`` and ``'left'``. :return: returns a string of type ``'| :---: | :---: | :---: |'``. :type columns: int :type text_align: str :rtype: str .. note: """ 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
[docs] def create_table(self, columns, rows, text, text_align='center'): self.columns = columns self.rows = rows table = '\n' column_align_string = self._align(columns, text_align) index = 0 for r in range(rows + 2): if r == 1: table += column_align_string # Row align, Example: '| :---: | :---: | ... | \n' else: table += '|' for c in range(columns): table += text[index] + '|' index += 1 table += '\n' return table
[docs]class TextUtils(object): """This class helps to create bold, italics and change color text. """
[docs] @staticmethod def bold(text): """Bold text converter. :param text: a text string. :type text: str :return: a string like this example: ``'**text**'`` :rtype: str""" return '**' + text + '**'
[docs] @staticmethod def italics(text): """Italics text converter. :param text: a text string. :type text: str :return: a string like this example: ``'_text_'`` :rtype: str""" return '_' + text + '_'
[docs] @staticmethod def inline_code(text): """Inline code text converter. :param text: a text string. :type text: str :return: a string like this example: ``'`text`'`` :rtype: str""" return '`' + text + '`'
[docs] @staticmethod def center_text(text): """Place a text string to center. :param text: a text string. :type text: str :return: a string like this exampple: ``'<center>text</center>'`` """ return '<center>' + text + '</center>'
[docs] @staticmethod def text_color(text, color="black"): """Change text color. :param text: it is the text that will be changed its color. :param color: it is the text color: ``'orange'``, ``'blue'``, ``'red'``... or a **RGB** color such as ``'#ffce00'``. :return: a string like this one: ``'<font color='color'> 'text' </font>'`` :type text: str :type color: str :rtype: str """ return '<font color="' + color + '"> ' + text + ' </font>'
[docs] def text_format(self, text, bold_italics_code='', color='black', align=''): """Text format helps to write multiple text format such as bold, italics and color. :param text: it is a string in which will be added the mew format :param bold_italics_code: using `'b'`: **bold**, `'i'`: _italics_ and `'c'`: `inline_code`. :param color: Can change text color. For example: 'red', 'green, 'orange'... :param align: Using this parameter you can align text. :return: return a string with the new text format. :type text: str :type bold_italics_code: str :type color: str :type align: str :rtype: str :Example: >>> from mdutils.tools.tools import TextUtils >>> textUtils = TextUtils() >>> textUtils.text_format(text='Some Text Here', bold_italics_code='bi', color='red', align='center') '_**<center><font color="red"> Some Text Here </font></center>**_' """ if color != 'black': new_text_format = self.text_color(text, color) else: new_text_format = text if align == 'center': new_text_format = self.center_text(new_text_format) if bold_italics_code: for i in range(len(bold_italics_code)): char = bold_italics_code[i] if char == 'b': new_text_format = self.bold(new_text_format) elif char == 'c': new_text_format = self.inline_code(new_text_format) elif char == 'i': new_text_format = self.italics(new_text_format) return new_text_format
if __name__ == "__main__": import doctest doctest.testmod()