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)
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)
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
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)
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)
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)
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)
def resolve_information(document: Document, call: FunctionCall) -> Component: utils.check_unknown_options(call.options, call) return make_admonition(document, call, 'information')
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')