ClammingPy 1.8

https://sourceforge.net/projects/clamming/

Module clamming

Class ClamUtils

Description

Some utilities for ClammingPy.

Constructor

Create a ClamUtils instance.

View Source
def __init__(self):
    """Create a ClamUtils instance."""
    self.markdowner = None
    self.lexer = None
    self.formatter = None
    if len(HTML) == 0:
        self.markdowner = markdown2.Markdown()
        self.formatter = pygments_formatter.HtmlFormatter(**ClamUtils.HTML_FORMATTER_ARGS)
        self.lexer = pygments_lexers.PythonLexer()

Public functions

get_class

Return the class matching the given class name or None if invalid.

Example
 >>> c = ClamUtils.get_class("ClamsPack", "clamming")
 >>> print(c)
 > <class 'clamming.clamspack.ClamsPack'>
 >>> c = ClamUtils.get_class("something", "clamming")
 >>> print(c)
 > None
Parameters
  • class_name: (str) Name of a class
  • module_name: (str) Name of the module to search for the class
Returns
  • (class|None) Class if valid given parameters, or None.
View Source
@staticmethod
def get_class(class_name: str, module_name: str | None=None) -> Any:
    """Return the class matching the given class name or None if invalid.

        :example:
        >>> c = ClamUtils.get_class("ClamsPack", "clamming")
        >>> print(c)
        <class 'clamming.clamspack.ClamsPack'>
        >>> c = ClamUtils.get_class("something", "clamming")
        >>> print(c)
        None

        :param class_name: (str) Name of a class
        :param module_name: (str) Name of the module to search for the class
        :return: (class|None) Class if valid given parameters, or None.

        """
    if module_name is None:
        module_name = __name__
    try:
        module = importlib.import_module(module_name)
    except ModuleNotFoundError:
        return None
    class_inst = getattr(module, class_name, None)
    if class_inst is not None and inspect.isclass(class_inst) is False:
        return None
    return class_inst

markdown_convert

Return HTML of the given markdown content.

Parameters
  • md: (str) A standard-limited markdown content
Returns
  • (str) The HTML content
View Source
def markdown_convert(self, md):
    """Return HTML of the given markdown content.

        :param md: (str) A standard-limited markdown content
        :return: (str) The HTML content

        """
    if len(HTML) > 0:
        logging.warning('Markdown to HTML conversion is disabled: {:s}'.format(HTML))
        return ''
    return self.markdowner.convert(md)

markdown_to_html

Turn a markdown content into HTML.

Parameters
  • content: (str) A complex markdown content
Returns
  • (str) The HTML content
View Source
def markdown_to_html(self, content):
    """Turn a markdown content into HTML.

        :param content: (str) A complex markdown content
        :return: (str) The HTML content

        """
    if len(HTML) > 0:
        logging.warning('Markdown to HTML conversion is disabled: {:s}'.format(HTML))
        return ''
    h = list()
    code = list()
    md = list()
    i = 0
    all_lines = content.split('\n')
    while i < len(all_lines):
        line = all_lines[i]
        if line.strip().startswith('```') is True:
            has_language = len(line.strip()) == 3
            if len(md) > 0:
                h.append(self.markdowner.convert('\n'.join(md)))
                md = list()
            line = ''
            while line.strip().startswith('```') is False:
                code.append(line)
                i = i + 1
                if i > len(all_lines):
                    break
                line = all_lines[i]
            if len(code) > 0:
                if has_language is True:
                    h.append('<pre>')
                    h.append('\n'.join(code))
                    h.append('</pre>')
                else:
                    h.append(pygments_highlight('\n'.join(code), self.lexer, self.formatter))
                code = list()
        else:
            idx = self.__is_code(line)
            if idx != -1:
                if len(md) > 0:
                    h.append(self.markdowner.convert('\n'.join(md)))
                    md = list()
                if idx > 0:
                    code.append(line[idx:])
                else:
                    code.append(line)
            else:
                if len(code) > 0:
                    h.append(pygments_highlight('\n'.join(code), self.lexer, self.formatter))
                    code = list()
                md.append(line)
        i = i + 1
    if len(code) > 0:
        h.append(pygments_highlight('\n'.join(code), self.lexer, self.formatter))
    if len(md) > 0:
        h.append(self.markdowner.convert('\n'.join(md)))
    html_result = '\n'.join(h)
    return html_result.replace('<p></p>', '')

source_to_html

Turn a source code content into HTML.

Parameters
  • source: (str) The source code content
Returns
  • (str) The HTML content
View Source
def source_to_html(self, source):
    """Turn a source code content into HTML.

        :param source: (str) The source code content
        :return: (str) The HTML content

        """
    if len(HTML) > 0:
        logging.warning('Source code to HTML conversion is disabled: {:s}'.format(HTML))
        return ''
    return pygments_highlight(source, self.lexer, self.formatter)

Protected functions

__is_code

View Source
@staticmethod
def __is_code(line):
    entry = line.strip()
    if entry.startswith('>>>') is True:
        return line.index('>') - 1
    if entry.startswith('> ') is True:
        return line.index('>') - 1
    return -1