Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: MarkupHeader #301

Open
WolfgangFahl opened this issue Nov 19, 2023 · 0 comments
Open

Feature Request: MarkupHeader #301

WolfgangFahl opened this issue Nov 19, 2023 · 0 comments

Comments

@WolfgangFahl
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant