Esempio n. 1
0
def jinja(path: str, **kwargs) -> str:
    """

    :param path:
    :param kwargs:
    :return:
    """
    environ.abort_thread()
    return templating.render_file(path, **kwargs)
Esempio n. 2
0
def jinja(path: str, **kwargs) -> str:
    """

    :param path:
    :param kwargs:
    :return:
    """
    environ.abort_thread()
    return templating.render_file(path, **kwargs)
Esempio n. 3
0
    def test_render_file(self):
        """

        :return:
        """

        result = templating.render_file(
            environ.paths.package('resources', 'templates', 'unit_test.html'),
            value='hello'
        )
        self.assertEqual(result, 'hello')
Esempio n. 4
0
def markdown(source: str = None,
             source_path: str = None,
             preserve_lines: bool = False,
             font_size: float = None,
             **kwargs) -> dict:
    """
    Renders a markdown file with support for Jinja2 templating. Any keyword
    arguments will be passed to Jinja2 for templating prior to rendering the
    markdown to HTML for display within the notebook.

    :param source:
        A string of markdown text that should be rendered to HTML for 
        notebook display.
    :param source_path:
        The path to a markdown file that should be rendered to HTML for
        notebook display.
    :param preserve_lines:
        If True, all line breaks will be treated as hard breaks. Use this
        for pre-formatted markdown text where newlines should be retained
        during rendering.
    :param font_size:
        Specifies a relative font size adjustment. The default value is 1.0,
        which preserves the inherited font size values. Set it to a value
        below 1.0 for smaller font-size rendering and greater than 1.0 for
        larger font size rendering.
    :return:
        The HTML results of rendering the specified markdown string or file.
    """

    environ.abort_thread()

    library_includes = []

    rendered = textwrap.dedent(
        templating.render_file(source_path, **kwargs)
        if source_path else templating.render(source or '', **kwargs))

    if md is None:
        raise ImportError('Unable to import the markdown package')

    offset = 0
    while offset < len(rendered):
        bound_chars = '$$'
        start_index = rendered.find(bound_chars, offset)

        if start_index < 0:
            break

        inline = rendered[start_index + 2] != '$'
        bound_chars = '$$' if inline else '$$$'
        end_index = rendered.find(bound_chars, start_index + len(bound_chars))

        if end_index < 0:
            break
        end_index += len(bound_chars)

        chunk = rendered[start_index: end_index] \
            .strip('$') \
            .strip() \
            .replace('@', '\\')

        if inline:
            chunk = chunk.replace('\\', '\\\\')

        chunk = latex(chunk, inline)
        rendered = '{pre}{gap}{latex}{gap}{post}'.format(
            pre=rendered[:start_index],
            latex=chunk,
            post=rendered[end_index:],
            gap='' if inline else '\n\n')

        if 'katex' not in library_includes:
            library_includes.append('katex')

        offset = end_index

    extensions = [
        'markdown.extensions.extra', 'markdown.extensions.admonition',
        'markdown.extensions.sane_lists',
        'markdown.extensions.nl2br' if preserve_lines else None
    ]

    body = templating.render_template(
        'markdown-block.html',
        text=md.markdown(rendered,
                         extensions=[e for e in extensions if e is not None]),
        font_size=font_size)

    pattern = re.compile('src="(?P<url>[^"]+)"')
    body = pattern.sub('data-src="\g<url>"', body)
    return dict(body=body,
                library_includes=library_includes,
                rendered=rendered)
Esempio n. 5
0
def markdown(
        source: str = None,
        source_path: str = None,
        preserve_lines: bool = False,
        font_size: float = None,
        **kwargs
) -> dict:
    """
    Renders a markdown file with support for Jinja2 templating. Any keyword
    arguments will be passed to Jinja2 for templating prior to rendering the
    markdown to HTML for display within the notebook.

    :param source:
        A string of markdown text that should be rendered to HTML for 
        notebook display.
    :param source_path:
        The path to a markdown file that should be rendered to HTML for
        notebook display.
    :param preserve_lines:
        If True, all line breaks will be treated as hard breaks. Use this
        for pre-formatted markdown text where newlines should be retained
        during rendering.
    :param font_size:
        Specifies a relative font size adjustment. The default value is 1.0,
        which preserves the inherited font size values. Set it to a value
        below 1.0 for smaller font-size rendering and greater than 1.0 for
        larger font size rendering.
    :return:
        The HTML results of rendering the specified markdown string or file.
    """

    environ.abort_thread()

    library_includes = []

    rendered = textwrap.dedent(
        templating.render_file(source_path, **kwargs)
        if source_path else
        templating.render(source or '', **kwargs)
    )

    if md is None:
        raise ImportError('Unable to import the markdown package')

    offset = 0
    while offset < len(rendered):
        bound_chars = '$$'
        start_index = rendered.find(bound_chars, offset)

        if start_index < 0:
            break

        inline = rendered[start_index + 2] != '$'
        bound_chars = '$$' if inline else '$$$'
        end_index = rendered.find(
            bound_chars,
            start_index + len(bound_chars)
        )

        if end_index < 0:
            break
        end_index += len(bound_chars)

        chunk = rendered[start_index: end_index] \
            .strip('$') \
            .strip() \
            .replace('@', '\\')

        if inline:
            chunk = chunk.replace('\\', '\\\\')

        chunk = latex(chunk, inline)
        rendered = '{pre}{gap}{latex}{gap}{post}'.format(
            pre=rendered[:start_index],
            latex=chunk,
            post=rendered[end_index:],
            gap='' if inline else '\n\n'
        )

        if 'katex' not in library_includes:
            library_includes.append('katex')

        offset = end_index

    extensions = [
        'markdown.extensions.extra',
        'markdown.extensions.admonition',
        'markdown.extensions.sane_lists',
        'markdown.extensions.nl2br' if preserve_lines else None
    ]

    body = templating.render_template(
        'markdown-block.html',
        text=md.markdown(rendered, extensions=[
            e for e in extensions if e is not None
        ]),
        font_size=font_size
    )

    pattern = re.compile('src="(?P<url>[^"]+)"')
    body = pattern.sub(r'data-src="\g<url>"', body)
    return dict(
        body=body,
        library_includes=library_includes,
        rendered=rendered
    )
Esempio n. 6
0
def markdown(source: str = None, source_path: str = None, **kwargs) -> dict:
    """
    Renders a markdown file with support for Jinja2 templating. Any keyword
    arguments will be passed to Jinja2 for templating prior to rendering the
    markdown to HTML for display within the notebook.

    :param source:
        A string of markdown text that should be rendered to HTML for 
        notebook display.
    :param source_path:
        The path to a markdown file that should be rendered to HTML for
        notebook display.

    :return:
        The HTML results of rendering the specified markdown string or file.
    """

    environ.abort_thread()

    library_includes = []

    rendered = textwrap.dedent(
        templating.render_file(source_path, **kwargs)
        if source_path else templating.render(source or '', **kwargs))

    if md is None:
        raise ImportError('Unable to import the markdown package')

    offset = 0
    while offset < len(rendered):
        bound_chars = '$$'
        start_index = rendered.find(bound_chars, offset)

        if start_index < 0:
            break

        inline = rendered[start_index + 2] != '$'
        bound_chars = '$$' if inline else '$$$'
        end_index = rendered.find(bound_chars, start_index + len(bound_chars))

        if end_index < 0:
            break
        end_index += len(bound_chars)

        chunk = rendered[start_index: end_index] \
            .strip('$') \
            .strip() \
            .replace('@', '\\')

        if inline:
            chunk = chunk.replace('\\', '\\\\')

        chunk = latex(chunk, inline)
        rendered = '{pre}{gap}{latex}{gap}{post}'.format(
            pre=rendered[:start_index],
            latex=chunk,
            post=rendered[end_index:],
            gap='' if inline else '\n\n')

        if 'katex' not in library_includes:
            library_includes.append('katex')

        offset = end_index

    body = templating.render("""
        <div class="textbox markdown">{{ text }}</div>
        """,
                             text=md.markdown(rendered))

    pattern = re.compile('src="(?P<url>[^"]+)"')
    body = pattern.sub('data-src="\g<url>"', body)
    return dict(body=body, library_includes=library_includes)