Skip to content

util

Markdown utils.

Attributes

PY3 = sys.version_info[0] == 3 module-attribute

string_type = str module-attribute

text_type = str module-attribute

int2str = chr module-attribute

BLOCK_LEVEL_ELEMENTS = re.compile('^(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|hr|hr/|style|li|dt|dd|thead|tbody|tr|th|td|section|footer|header|group|figure|figcaption|aside|article|canvas|output|progress|video|nav|main)$', re.IGNORECASE) module-attribute

STX = '\x02' module-attribute

ETX = '\x03' module-attribute

INLINE_PLACEHOLDER_PREFIX = STX + 'klzzwxh:' module-attribute

INLINE_PLACEHOLDER = INLINE_PLACEHOLDER_PREFIX + '%s' + ETX module-attribute

INLINE_PLACEHOLDER_RE = re.compile(INLINE_PLACEHOLDER % '([0-9]+)') module-attribute

AMP_SUBSTITUTE = STX + 'amp' + ETX module-attribute

HTML_PLACEHOLDER = STX + 'wzxhzdk:%s' + ETX module-attribute

HTML_PLACEHOLDER_RE = re.compile(HTML_PLACEHOLDER % '([0-9]+)') module-attribute

TAG_PLACEHOLDER = STX + 'hzzhzkh:%s' + ETX module-attribute

Constants you probably do not need to change

RTL_BIDI_RANGES = (('\u0590', '߿'), ('ⴰ', '⵿')) module-attribute

Classes

AtomicString

Bases: unicode

A string which should not be further processed.

Processor(markdown_instance=None)

Bases: object

Base class for Markdown processors.

Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def __init__(self, markdown_instance=None):
    if markdown_instance:
        self.markdown = markdown_instance

Attributes

markdown = markdown_instance instance-attribute

HtmlStash()

Bases: object

HTML objects stash.

Create a HtmlStash.

Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def __init__(self):
    """Create a HtmlStash."""
    self.html_counter = 0  # for counting inline html segments
    self.rawHtmlBlocks = []
    self.tag_counter = 0
    self.tag_data = []  # list of dictionaries in the order tags appear

Attributes

html_counter = 0 instance-attribute
rawHtmlBlocks = [] instance-attribute
tag_counter = 0 instance-attribute
tag_data = [] instance-attribute

Functions

store(html, safe=False)

Saves an HTML segment for later reinsertion.

Returns a placeholder string that needs to be inserted into the document.

Parameters:

Name Type Description Default
html str

an html segment

required
safe bool

label an html segment as safe for safemode

False

Returns:

Type Description
str

a placeholder string

Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def store(self, html, safe=False):
    """Saves an HTML segment for later reinsertion.

    Returns a placeholder string that needs to be inserted into the
    document.

    Args:
        html (str): an html segment
        safe (bool): label an html segment as safe for safemode

    Returns:
        (str): a placeholder string
    """
    self.rawHtmlBlocks.append((html, safe))
    placeholder = self.get_placeholder(self.html_counter)
    self.html_counter += 1
    return placeholder
reset()
Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def reset(self):
    self.html_counter = 0
    self.rawHtmlBlocks = []
get_placeholder(key)
Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def get_placeholder(self, key):
    return HTML_PLACEHOLDER % key
store_tag(tag, attrs, left_index, right_index)

Store tag data and return a placeholder.

Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def store_tag(self, tag, attrs, left_index, right_index):
    """Store tag data and return a placeholder."""
    self.tag_data.append({'tag': tag, 'attrs': attrs,
                          'left_index': left_index,
                          'right_index': right_index})
    placeholder = TAG_PLACEHOLDER % unicode(self.tag_counter)
    self.tag_counter += 1  # equal to the tag's index in self.tag_data
    return placeholder

Functions

isBlockLevel(tag)

Check if the tag is a block level HTML tag.

Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def isBlockLevel(tag):
    """Check if the tag is a block level HTML tag."""
    if isinstance(tag, string_type):
        return BLOCK_LEVEL_ELEMENTS.match(tag)
    # Some ElementTree tags are not strings, so return False.
    return False

parseBoolValue(value, fail_on_errors=True, preserve_none=False)

Parses a string representing bool value.

If parsing was successful, returns True or False. If preserve_none=True, returns True, False, or None. If parsing was not successful, raises ValueError, or, if fail_on_errors=False, returns None.

Parameters:

Name Type Description Default
value str

String to parse.

required
fail_on_errors bool

If True, raises ValueError.

True
preserve_none bool

If True and value is None, returns None.

False

Returns:

Type Description
bool

boolean value

Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def parseBoolValue(value, fail_on_errors=True, preserve_none=False):
    """Parses a string representing bool value.

    If parsing was successful, returns True or False.
    If preserve_none=True, returns True, False, or None.
    If parsing was not successful, raises ValueError, or, if
    fail_on_errors=False, returns None.


    Args:
        value (str): String to parse.
        fail_on_errors (bool): If True, raises ValueError.
        preserve_none (bool): If True and value is None, returns None.


    Returns:
        (bool): boolean value
    """
    if not isinstance(value, string_type):
        if preserve_none and value is None:
            return value
        return bool(value)
    elif preserve_none and value.lower() == 'none':
        return None
    elif value.lower() in ('true', 'yes', 'y', 'on', '1'):
        return True
    elif value.lower() in ('false', 'no', 'n', 'off', '0', 'none'):
        return False
    elif fail_on_errors:
        raise ValueError('Cannot parse bool value: %r' % value)