Visitar URL original
Feature Request: MarkupHeader · Issue #301 · astanin/python-tabulate · GitHub
Skip to content

Feature Request: MarkupHeader #301

Description

@WolfgangFahl

To output multiple tables in different formats i often need a MarkupHeader - i created the following helper class today to streamline the process:

"""
Created on 19.11.2023

@author: wf
"""
class MarkupHeader:
    """
    Helper to generate tabulate compatible markup header lines.
    """
    @classmethod
    def get_markup(cls, title: str, markup_format: str, level: int = 1) -> str:
        """
        Generates a formatted header string based on the specified markup format.

        Args:
            title (str): The title to be formatted as the header.
            markup_format (str): The markup format for the header.
            level (int): The section level to generate a header for.

        Returns:
            str: The formatted header string.
        """
        if markup_format == "github":
            return f"{'#' * level} {title}\n"
        elif markup_format == "mediawiki":
            return f"{'=' * level} {title} {'=' * level}\n"
        elif markup_format == "html" or markup_format=="unsafehtml":
            return f"<h{level}>{title}</h{level}>"
        elif markup_format == "latex":
            if level == 1:
                return f"\\section{{{title}}}"
            elif level == 2:
                return f"\\subsection{{{title}}}"
            elif level == 3:
                return f"\\subsubsection{{{title}}}"
        elif markup_format == "textile":
            return f"h{level}. {title}"
        elif markup_format == "plain":
            return title
        else:
            # Default case for other formats
            return title

Here is the unit test for it:

"""
Created on 2023-11-19

@author: wf
"""
from ngwidgets.markup_header import MarkupHeader
from ngwidgets.basetest import Basetest

class TestsMarkupHeader(Basetest):
    """
    Test markup header handling.
    """

    def test_markup_header(self):
        """
        Test all available tabulate markup formats to create
        valid headers for different levels.
        """
        test_title = "Test Title"
        formats = [
            ("plain", 1, f"{test_title}"),
            ("simple", 1, f"{test_title}"),
            ("github", 1, f"# {test_title}"),
            ("github", 2, f"## {test_title}"),
            ("github", 3, f"### {test_title}"),
            ("grid", 1, f"{test_title}"),
            ("simple_grid", 1, f"{test_title}"),
            ("rounded_grid", 1, f"{test_title}"),
            ("heavy_grid", 1, f"{test_title}"),
            ("mixed_grid", 1, f"{test_title}"),
            ("double_grid", 1, f"{test_title}"),
            ("fancy_grid", 1, f"{test_title}"),
            ("outline", 1, f"{test_title}"),
            ("simple_outline", 1, f"{test_title}"),
            ("rounded_outline", 1, f"{test_title}"),
            ("heavy_outline", 1, f"{test_title}"),
            ("mixed_outline", 1, f"{test_title}"),
            ("double_outline", 1, f"{test_title}"),
            ("fancy_outline", 1, f"{test_title}"),
            ("pipe", 1, f"{test_title}"),
            ("orgtbl", 1, f"{test_title}"),
            ("asciidoc", 1, f"{test_title}"),
            ("jira", 1, f"{test_title}"),
            ("presto", 1, f"{test_title}"),
            ("pretty", 1, f"{test_title}"),
            ("psql", 1, f"{test_title}"),
            ("rst", 1, f"{test_title}"),
            ("mediawiki", 1, f"= {test_title} ="),
            ("mediawiki", 2, f"== {test_title} =="),
            ("mediawiki", 3, f"=== {test_title} ==="),
            ("moinmoin", 1, f"{test_title}"),
            ("youtrack", 1, f"{test_title}"),
            ("html", 1, f"<h1>{test_title}</h1>"),
            ("html", 2, f"<h2>{test_title}</h2>"),
            ("html", 3, f"<h3>{test_title}</h3>"),
            ("unsafehtml", 1, f"<h1>{test_title}</h1>"),
            ("unsafehtml", 2, f"<h2>{test_title}</h2>"),
            ("unsafehtml", 3, f"<h3>{test_title}</h3>"),
            ("latex", 1, f"\\section{{{test_title}}}"),
            ("latex", 2, f"\\subsection{{{test_title}}}"),
            ("latex", 3, f"\\subsubsection{{{test_title}}}"),
            ("latex_raw", 1, f"{test_title}"),
            ("latex_booktabs", 1, f"{test_title}"),
            ("latex_longtable", 1, f"{test_title}"),
            ("textile", 1, f"h1. {test_title}"),
            ("textile", 2, f"h2. {test_title}"),
            ("textile", 3, f"h3. {test_title}"),
            ("tsv", 1, f"{test_title}")
        ]

        for markup_format, level, expected_content in formats:
            with self.subTest(f"Testing format: {markup_format}, level: {level}"):
                header = MarkupHeader.get_markup(test_title, markup_format, level)
                self.assertEqual(header.strip(), expected_content)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions