CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mdutils

A comprehensive Python library for programmatically creating and manipulating Markdown files with support for headers, tables, lists, images, links, and text formatting.

80

1.90x
Overview
Eval results
Files

text-formatting.mddocs/

Text Formatting

Comprehensive text styling capabilities including bold, italics, colors, alignment, inline code, and advanced formatting combinations. The TextUtils class provides static methods for applying various text formatting options that can be used independently or combined for complex styling needs.

Capabilities

Basic Text Styling

Apply fundamental text formatting including bold, italics, and inline code styling.

class TextUtils:
    @staticmethod
    def bold(text: str) -> str:
        """
        Apply bold formatting to text.

        Parameters:
        - text (str): Text to make bold

        Returns:
        str: Text wrapped in markdown bold syntax (**text**)
        """

    @staticmethod
    def italics(text: str) -> str:
        """
        Apply italic formatting to text.

        Parameters:
        - text (str): Text to make italic

        Returns:
        str: Text wrapped in markdown italic syntax (*text*)
        """

    @staticmethod
    def inline_code(text: str) -> str:
        """
        Apply inline code formatting to text.

        Parameters:
        - text (str): Text to format as inline code

        Returns:
        str: Text wrapped in markdown code syntax (`text`)
        """

Usage Example:

from mdutils import MdUtils
from mdutils.tools import TextUtils

md = MdUtils(file_name='formatting_example')

# Basic styling
bold_text = TextUtils.bold('This is bold text')
italic_text = TextUtils.italics('This is italic text') 
code_text = TextUtils.inline_code('print("Hello")')

md.write(f"{bold_text}, {italic_text}, and {code_text}")

# Or use directly in document methods
md.new_paragraph('Important note', bold_italics_code='b')  # 'b' for bold
md.new_paragraph('Emphasized text', bold_italics_code='i')  # 'i' for italics
md.new_paragraph('Code example', bold_italics_code='c')    # 'c' for code

Text Color and Alignment

Apply color styling and text alignment for enhanced visual presentation.

class TextUtils:
    @staticmethod
    def text_color(text: str, color: str = "black") -> str:
        """
        Apply color formatting to text using HTML/CSS color names.

        Parameters:
        - text (str): Text to colorize
        - color (str): Color name or specification (e.g., 'red', 'blue', '#FF0000')

        Returns:
        str: Text wrapped in HTML color tags
        """

    @staticmethod
    def center_text(text: str) -> str:
        """
        Center-align text using HTML alignment.

        Parameters:
        - text (str): Text to center

        Returns:
        str: Text wrapped in HTML center alignment tags
        """

Usage Example:

from mdutils import MdUtils
from mdutils.tools import TextUtils

md = MdUtils(file_name='color_alignment_example')

# Colored text
red_text = TextUtils.text_color('Error message', 'red')
blue_text = TextUtils.text_color('Information', 'blue')
green_text = TextUtils.text_color('Success', 'green')

md.write(f"{red_text} | {blue_text} | {green_text}")

# Centered text
centered = TextUtils.center_text('This text is centered')
md.new_paragraph(centered)

# Using color and alignment in document methods
md.new_paragraph('Red centered text', color='red', align='center')
md.new_line('Blue right-aligned text', color='blue', align='right')

Advanced Text Formatting

Comprehensive text formatting with multiple style combinations and external link integration.

class TextUtils:
    @staticmethod
    def text_format(text: str, bold_italics_code: str = "", color: str = "black", align: str = "") -> str:
        """
        Apply comprehensive text formatting with multiple style options.

        Parameters:
        - text (str): Text to format  
        - bold_italics_code (str): Combination of format codes:
          - 'b' for bold
          - 'i' for italics  
          - 'c' for inline code
          - Can combine: 'bi' for bold italic, 'bc' for bold code, etc.
        - color (str): Text color specification
        - align (str): Text alignment ('left', 'center', 'right')

        Returns:
        str: Text with all specified formatting applied
        """

    @staticmethod
    def text_external_link(text: str, link: str = "") -> str:
        """
        Create an external link with specified text.

        Parameters:
        - text (str): Display text for the link
        - link (str): URL for the external link

        Returns:
        str: Markdown-formatted external link
        """

    @staticmethod
    def add_tooltip(link: str, tip: str) -> str:
        """
        Add tooltip to a link.

        Parameters:
        - link (str): The link URL
        - tip (str): Tooltip text to display

        Returns:
        str: Link with tooltip attribute
        """

Usage Example:

from mdutils import MdUtils
from mdutils.tools import TextUtils

md = MdUtils(file_name='advanced_formatting')

# Complex formatting combinations
bold_italic = TextUtils.text_format('Bold and Italic', bold_italics_code='bi')
bold_code = TextUtils.text_format('Bold Code', bold_italics_code='bc', color='blue')
centered_red_bold = TextUtils.text_format('Centered Red Bold', 
                                        bold_italics_code='b', 
                                        color='red', 
                                        align='center')

md.write(bold_italic)
md.new_line(bold_code)
md.new_paragraph(centered_red_bold)

# External links
github_link = TextUtils.text_external_link('Visit GitHub', 'https://github.com')
md.new_paragraph(f'Check out the project: {github_link}')

# Links with tooltips
tooltip_link = TextUtils.add_tooltip('https://example.com', 'Click to visit example site')
md.write(f'Link with tooltip: {tooltip_link}')

Code Block Formatting

Format code blocks with syntax highlighting and language specification.

class TextUtils:
    @staticmethod
    def insert_code(code: str, language: str = "") -> str:
        """
        Format code as a markdown code block with optional syntax highlighting.

        Parameters:
        - code (str): Code content to format
        - language (str, optional): Programming language for syntax highlighting

        Returns:
        str: Formatted code block with markdown syntax
        """

Usage Example:

from mdutils import MdUtils
from mdutils.tools import TextUtils

md = MdUtils(file_name='code_formatting')

# Python code block
python_code = '''
def calculate_fibonacci(n):
    if n <= 1:
        return n
    return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)
'''
formatted_python = TextUtils.insert_code(python_code, 'python')
md.write(formatted_python)

# JSON code block
json_code = '''
{
    "name": "example",
    "version": "1.0.0",
    "dependencies": {
        "mdutils": "^1.8.0"
    }
}
'''
formatted_json = TextUtils.insert_code(json_code, 'json')
md.write(formatted_json)

# Generic code block (no language)
bash_code = 'pip install mdutils'
formatted_bash = TextUtils.insert_code(bash_code)
md.write(formatted_bash)

Format Code Combinations

Understanding how to combine different formatting codes for complex text styling.

Format Code Reference:

  • 'b' - Bold text (text)
  • 'i' - Italic text (text)
  • 'c' - Inline code (text)
  • 'bi' or 'ib' - Bold italic (text)
  • 'bc' or 'cb' - Bold code (text)
  • 'ic' or 'ci' - Italic code (text)
  • 'bic' or combinations - Bold italic code (text)

Usage Example:

from mdutils import MdUtils

md = MdUtils(file_name='format_combinations')

# Using format codes in document methods
md.new_paragraph('This is bold text', bold_italics_code='b')
md.new_paragraph('This is italic text', bold_italics_code='i') 
md.new_paragraph('This is inline code', bold_italics_code='c')
md.new_paragraph('This is bold italic text', bold_italics_code='bi')
md.new_paragraph('This is bold code text', bold_italics_code='bc')

# Combining with colors and alignment
md.new_paragraph('Bold red centered text', 
               bold_italics_code='b', 
               color='red', 
               align='center')

md.new_paragraph('Italic blue right-aligned code', 
               bold_italics_code='ic', 
               color='blue', 
               align='right')

Install with Tessl CLI

npx tessl i tessl/pypi-mdutils

docs

advanced.md

core-document.md

headers-toc.md

index.md

links-images.md

lists.md

tables.md

text-formatting.md

tile.json