Beispiel #1
0
def parse_inline_link(ctx: CTX, mark: str, location: Location,
                      content: Content, indentation: int) -> int:
    if mark != link_text_begin_mark:
        return PASS

    with ctx.using_stop_mark(link_text_end_mark):
        # It uses the original indentation
        #   so the paragraph can be continued.
        contents = parse_inline(ctx, content.get_location(), content,
                                indentation)

    if not content.pull(link_text_end_mark):
        raise StxError(f'Expected mark: {link_text_end_mark}')

    if content.pull(link_ref_begin_mark):
        out = StringIO()

        while not content.pull(link_ref_end_mark):
            c = content.peek()

            if c is None:
                raise StxError(f'Expected {link_ref_end_mark}',
                               content.get_location())

            out.write(c)
            content.move_next()

        reference = out.getvalue()
    else:
        reference = None

    ctx.composer.add(LinkText(location, contents, reference))

    return CONSUMED
Beispiel #2
0
def parse_container(ctx: CTX, mark: str, location: Location, content: Content,
                    indentation_before_mark: int) -> int:
    if mark != container_area_begin_mark:
        return PASS

    content.read_spaces()

    function = try_parse_entry(content)

    content.expect_end_of_line()

    with ctx.using_stop_mark(container_area_end_mark):
        component = capture_component(ctx, indentation_before_mark)

    content.pull(container_area_end_mark)
    content.expect_end_of_line()

    if function is not None:
        component = FunctionCall(
            location,
            inline=False,
            key=function.name,
            options=function.value,
            argument=component,
        )

    ctx.composer.add(component)

    return CONSUMED
Beispiel #3
0
def parse_inline_style(ctx: CTX, mark: str, location: Location,
                       content: Content, indentation: int) -> int:
    if mark == strong_begin_mark:
        end_mark = strong_end_mark
        style = 'strong'
    elif mark == emphasized_begin_mark:
        end_mark = emphasized_end_mark
        style = 'emphasized'
    elif mark == code_begin_mark:
        end_mark = code_end_mark
        style = 'code'
    elif mark == deleted_begin_mark:
        end_mark = deleted_end_mark
        style = 'deleted'
    elif mark == d_quote_begin_mark:
        end_mark = d_quote_end_mark
        style = 'double-quote'
    elif mark == s_quote_begin_mark:
        end_mark = s_quote_end_mark
        style = 'single-quote'
    else:
        return PASS

    with ctx.using_stop_mark(end_mark):
        # It uses the original indentation
        #   so the paragraph can be continued.
        contents = parse_inline(ctx, content.get_location(), content,
                                indentation)

    if not content.pull(end_mark):
        raise StxError(f'Expected mark: {end_mark}')

    ctx.composer.add(StyledText(location, contents, style))

    return CONSUMED
Beispiel #4
0
def parse_inline_container(ctx: CTX, mark: str, location: Location,
                           content: Content, indentation: int) -> int:
    if mark != container_begin_mark:
        return PASS

    with ctx.using_stop_mark(container_end_mark):
        # It uses the original indentation
        #   so the paragraph can be continued.
        contents = parse_inline(ctx, content.get_location(), content,
                                indentation)

    if not content.pull(container_end_mark):
        raise StxError(f'Expected mark: {container_end_mark}')

    if not content.pull(function_begin_mark):
        raise StxError(f'Expected mark: {function_begin_mark}')

    skip_void(content)

    function_location = content.get_location()

    function = parse_entry(content)

    skip_void(content)

    if not content.pull(function_end_mark):
        raise StxError(f'Expected mark: {function_end_mark}')

    ctx.composer.add(
        FunctionCall(function_location,
                     inline=True,
                     key=function.name,
                     options=function.value,
                     argument=Composite(location, contents)))

    return CONSUMED
Beispiel #5
0
def parse_inline_function(ctx: CTX, mark: str, location: Location,
                          content: Content) -> int:
    if mark != function_begin_mark:
        return PASS

    skip_void(content)

    function_location = content.get_location()

    function = parse_entry(content)

    skip_void(content)

    if not content.pull(function_end_mark):
        raise StxError(f'Expected mark: {function_end_mark}')

    ctx.composer.add(
        FunctionCall(function_location,
                     inline=True,
                     key=function.name,
                     options=function.value))

    return CONSUMED
Beispiel #6
0
def parse_inline_text(ctx: CTX, mark: str, location: Location,
                      content: Content, indentation: int) -> int:
    if mark is not None:
        return PASS

    out = StringIO()

    completed = False

    while content.peek() is not None:
        # Check if the text is broken by an inline or stop mark
        if content.test_any(inline_marks):
            break
        elif content.test(ctx.stop_mark):
            break

        c = content.peek()

        if c == '\n':
            out.write(c)
            content.move_next()

            # Check if the text is completed by an empty line
            if content.consume_empty_line():
                completed = True
                break

            loc0 = content.get_location()

            spaces = content.read_spaces(indentation)

            # Check if the text is completed by indentation change
            if spaces < indentation:
                content.go_back(loc0)
                completed = True
                break

            # Check if the text is completed by a non-inline mark
            if content.test_any(not_inline_marks):
                content.go_back(loc0)
                completed = True
                break
        elif c == escape_char:
            content.move_next()

            escaped_mark = content.pull_any(all_marks)
            if escaped_mark is not None:
                out.write(escaped_mark)
            elif content.pull(ctx.stop_mark):
                out.write(ctx.stop_mark)
            elif content.pull(escape_char):
                out.write(escape_char)
            else:
                raise StxError('invalid escaped char')
        else:
            out.write(c)
            content.move_next()

    text = out.getvalue()

    if text == '':
        return EXIT

    ctx.composer.add(PlainText(location, text))

    if completed:
        return EXIT
    return CONSUMED