def html(content) -> str: """ :param content: :return: """ environ.abort_thread() return templating.render('<div class="box">{{content}}</div>', content=content)
def svg(svg_data: str) -> str: """ :param svg_data: :return: """ environ.abort_thread() return templating.render('<div class="svg-box">{{ svg }}</div>', svg=svg_data)
def svg(svg_data: str) -> str: """ :param svg_data: :return: """ environ.abort_thread() return templating.render( '<div class="svg-box">{{ svg }}</div>', svg=svg_data )
def html(content) -> str: """ :param content: :return: """ environ.abort_thread() return templating.render( '<div class="box">{{content}}</div>', content=content )
def test_id_filter(self): """ """ result = templating.render('{{ "test" | id }}') parts = result.split('-', 2) self.assertEqual( parts[0], 'cdi', msg='"{}" should start with "cdi"'.format(result) ) self.assertEqual( parts[1], 'test', msg='"{}" should match the prefix'.format(result) )
def render_to_console(self, message: str, **kwargs): """ Renders the specified message to the console using Jinja2 template rendering with the kwargs as render variables. The message will also be dedented prior to rendering in the same fashion as other Cauldron template rendering actions. :param message: Template string to be rendered. :param kwargs: Variables to be used in rendering the template. """ rendered = templating.render(message, **kwargs) return self.write_to_console(rendered)
def run( project: 'projects.Project', step: 'projects.ProjectStep' ) -> dict: """ :param project: :param step: :return: """ with open(step.source_path, 'r') as f: code = f.read() step.report.append_body(render.html(templating.render( template=code, **project.shared.fetch(None) ))) return {'success': True}
def header(contents: str, level: int = 1, expand_full: bool = False) -> str: """ :param level: :param contents: :param expand_full: :return: """ environ.abort_thread() classes = [ 'cd-Header', 'cd-Header--{}'.format('full' if expand_full else 'limited') ] return templating.render(""" <h{{ level }} class="{{ classes }}">{{ contents }}</h{{ level }}> """, level=level, contents=contents, classes=' '.join(classes))
def header(contents: str, level: int = 1, expand_full: bool = False) -> str: """ :param level: :param contents: :param expand_full: :return: """ environ.abort_thread() classes = [ 'cd-Header', 'cd-Header--{}'.format('full' if expand_full else 'limited') ] return templating.render( """ <h{{ level }} class="{{ classes }}">{{ contents }}</h{{ level }}> """, level=level, contents=contents, classes=' '.join(classes) )
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)
def test_latex_filter(self): """ """ result = templating.render('{{ "e = mc^2" | latex }}') self.assertNotEqual(result.find('katex'), -1, 'where is katex?')
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 )
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)