Exemplo n.º 1
0
def _check_link(source, target, length, start):
    """ check inline link, link reference or link definition
    link :: <!>?<lb><whitespace>*<link_name>whitespace*<rb>[link_definition|link_url]
    link_name :: span_element
    """
    index, is_img = start, False
    if source[index] == '!':
        is_img = True
        index += 1
    if index >= length or source[index] != '[':
        return start
    index += 1
    (index, content) = helper.forward_until(source, index, ']')
    brackets = helper.elimate_whitespace_around(content)
    index += 1
    jumps = helper.elimate_leading_whitespace(source[index:], '[')
    index += jumps
    if index >= length or source[index] not in ':[(':
        return start
    if source[index] == ':':
        new_index = _split_link_definition(source, target, length, index + 1,
                                           brackets)
    else:
        new_index = _split_link(source, target, length, index,
                                (brackets, is_img))
    return new_index if new_index > index else start
Exemplo n.º 2
0
def _check_emphasis(source, target, length, start):
    """ emphasis :: [bold_emphasis|italic_emphasis]
    bold_emphasis :: [*|_][2]<whitespace>*<span_element><whitespace>*[*|_][2]
    italic_emphasis :: [*|_]<whitespace>*<span_element><whitespace>*[*|_]
    """
    if source[start] not in '_*':
        return start
    symbol = source[start]
    index, emp_type, content = start, 'italic', []
    if index + 1 < length and source[index + 1] == symbol:
        index += 1
        emp_type = 'bold'
    index += 1
    (index, content) = helper.forward_until(source, index, symbol)
    if index >= length:
        return start
    content = helper.elimate_whitespace_around(content)
    if emp_type == 'bold' and (index + 1 >= length
                               or source[index + 1] != symbol):
        target.append({'span_type': 'text', 'content': [symbol]})
        emp_type = 'italic'
    target.append({'span_type': emp_type, 'content': parse_span(content, [])})
    if emp_type == 'italic':
        index += 1
    else:
        index += 2
    return index
Exemplo n.º 3
0
def _split_link(source, target, length, start, prev_info):
    """
    link_url :: [regular_link_url|reference_link_url]
    regular_link_url :: <lp><whitespace>*<url_and_title><whitespace>*<rp>
    reference_link_url :: <whitespace>*<lb><whitespace>*<url_and_title><whitespace>*<rb>
    url_and_title :: <text_element><whitespace>+<text_element>*
    """
    index = start
    inner, is_img = prev_info
    link_type = 'inline' if source[index] == '(' else 'refer'
    end = ')' if link_type == 'inline' else ']'
    index += 1
    (index, content) = helper.forward_until(source, index, end)
    if index >= length:
        return start
    content = helper.elimate_whitespace_around(content)
    if link_type == 'inline':
        _check_inline_link(target, content, inner, is_img)
    else:
        target.append({
            'span_type': 'ref_link',
            'inner': parse_span(inner, []),
            'content': content,
            'is_img': is_img
        })
    return index + 1
Exemplo n.º 4
0
def _check_strike_out(source, target, length, start):
    """ strike_out :: <~>[2]<span_element><~>[2]
    """
    if source[:2] != '~~':
        return start
    index = start + 2
    (index, content) = helper.forward_until(source, index, '~~')
    if index + 1 >= length:
        return start
    content = helper.elimate_whitespace_around(content)
    target.append({
        'span_type': 'strike_out',
        'content': parse_span(content, [])
    })
    return index + 2
Exemplo n.º 5
0
def _check_script(source, target, length, start):
    """
    script :: [~|^]<whitespace>*<span_element><whitespace>*[~|^]
    """
    if source[start] not in '~^':
        return start
    symbol = source[start]
    index, content = start, []
    script_type = 'super_script' if source[start] == '^' else 'sub_script'
    index += 1
    (index, content) = helper.forward_until(source, index, symbol)
    if index >= length:
        return start
    content = helper.elimate_whitespace_around(content)
    target.append({
        'span_type': script_type,
        'content': parse_span(content, [])
    })
    return index + 1