Пример #1
0
def resolve_code(document: Document, call: FunctionCall) -> Component:
    if call.argument is None:
        raise call.error('Code function requires a captured component.')

    options = utils.make_options_dict(call, key_for_str='lang')

    lang = options.pop('lang', None)

    utils.check_unknown_options(options, call)

    contents = None

    if lang is not None:
        grammar = registry.get_grammar(lang)

        if grammar is not None:
            text = utils.make_plain_text(call.argument)

            try:
                contents = []
                nodes = grammar.tokenize(text)
                generate_component(call.location, nodes, contents)
            except GramatError:
                contents = None

    if contents is None:
        contents = utils.make_component_list(call.argument)

    return CodeBlock(call.location, contents, lang=lang)
Пример #2
0
def resolve_admonition(document: Document, call: FunctionCall) -> Component:
    options = utils.make_options_dict(call, key_for_str='type')

    data_type = options.pop('type', None)

    utils.check_unknown_options(options, call)

    return make_admonition(document, call, data_type)
Пример #3
0
def resolve_toc(document: Document, call: FunctionCall) -> Component:
    options = utils.make_options_dict(call, key_for_str='title')

    title = options.pop('title', None)

    utils.check_unknown_options(options, call)

    toc = TableOfContents(call.location, title)

    generate_toc(document, toc)

    return toc
Пример #4
0
def resolve_custom_style(document: Document, call: FunctionCall) -> Component:
    if call.argument is None:
        raise call.error(
            'Custom style function requires a captured component.')

    options = utils.make_options_dict(call, key_for_str='style')
    contents = utils.make_component_list(call.argument)

    custom_style = options.pop('style')

    utils.check_unknown_options(options, call)

    return CustomText(call.location, contents, custom_style)
Пример #5
0
def layout_function(document: Document, call: FunctionCall) -> Component:
    if call.argument is None:
        raise call.error('Layouts require a captured component.')

    components = utils.make_component_list(call.argument)

    options = utils.make_options_dict(call, key_for_str='dir')

    direction = options.pop('dir', None)

    utils.check_unknown_options(options, call)

    return Layout(call.location, components, direction)
Пример #6
0
def resolve_image(document: Document, call: FunctionCall) -> Component:
    options = utils.make_options_dict(call, 'src')

    src = options.pop('src', None)
    alt = options.pop('alt', None)

    utils.check_unknown_options(options, call)

    if src is None:
        raise StxError(f'Missing `src` parameter in image.', call.location)
    elif not alt:
        logger.warning('Image without `alt`', call.location)

    return Image(call.location, src, alt)
Пример #7
0
def resolve_embed(document: Document, call: FunctionCall) -> Component:
    options = utils.make_options_dict(call, 'src')

    src = options.pop('src', None)

    utils.check_unknown_options(options, call)

    if src is None:
        raise StxError(f'Missing `src` parameter in embed.', call.location)

    src = resolve_sibling(document.source_file, src)

    logger.info(f'Embedding: {src}')

    with open(src, 'r', encoding='UTF-8') as f:
        text = f.read()

    # TODO add support for more type of files

    return Literal(call.location, text, source=src)
Пример #8
0
def resolve_information(document: Document, call: FunctionCall) -> Component:
    utils.check_unknown_options(call.options, call)

    return make_admonition(document, call, 'information')
Пример #9
0
def resolve_line_feed(document: Document, call: FunctionCall) -> Component:
    utils.check_unknown_options(call.options, call)

    # TODO create component to represent line feed
    return PlainText(call.location, '\n')