Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def markdown_to_html(text, record=None):
ctx = get_ctx()
if ctx is None:
raise RuntimeError('Context is required for markdown rendering')
# These markdown parsers are all terrible. Not one of them does not
# modify internal state. So since we only do one of those per thread
# we can at least cache them on a thread local.
md = getattr(_markdown_cache, 'md', None)
if md is None:
md = make_markdown(ctx.env)
_markdown_cache.md = md
meta = {}
ctx.env.plugin_controller.emit('markdown-meta-init', meta=meta,
record=record)
md.renderer.meta = meta
md.renderer.record = record
def markdown_to_html(text, record=None):
ctx = get_ctx()
if ctx is None:
raise RuntimeError('Context is required for markdown rendering')
# These markdown parsers are all terrible. Not one of them does not
# modify internal state. So since we only do one of those per thread
# we can at least cache them on a thread local.
md = getattr(_markdown_cache, 'md', None)
if md is None:
md = make_markdown(ctx.env)
_markdown_cache.md = md
meta = {}
ctx.env.plugin_controller.emit('markdown-meta-init', meta=meta,
record=record)
md.renderer.meta = meta
md.renderer.record = record
def highlight_code(self, text, lang):
get_ctx().record_dependency(self.config_filename)
lexer = get_lexer_by_name(lang)
return highlight(text, lexer, self.get_formatter())
sources = self._known_bags.get(name)
if not sources:
return None
rv = self._bags.get(name)
if rv is None:
filenames = []
rv = OrderedDict()
for filename in sources:
filename = os.path.join(self.root_path, filename)
rv = merge(rv, load_databag(filename))
filenames.append(filename)
self._bags[name] = (rv, filenames)
else:
rv, filenames = rv
ctx = get_ctx()
if ctx is not None:
for filename in filenames:
ctx.record_dependency(filename)
return rv
def get_pygments_stylesheet(artifact_name='/static/pygments.css'):
ctx = get_ctx()
@ctx.sub_artifact(artifact_name=artifact_name, sources=[
self.config_filename])
def build_stylesheet(artifact):
with artifact.open('w') as f:
f.write(self.get_formatter().get_style_defs())
return artifact_name
def __render(self):
# When the markdown instance is attached to a cached object we can
# end up in the situation where the context changed from the time
# we were put into the cache to the time where we got referenced
# by something elsewhere. In that case we need to re-process our
# markdown. For instance this affects relative links.
if self.__html is None or \
self.__cached_for_ctx != get_ctx():
self.__html, self.__meta = notebook_to_html(
self.source, self.__record())
self.__cached_for_ctx = get_ctx()
def _require_ctx(record):
ctx = get_ctx()
if ctx is None:
raise RuntimeError('This operation requires a context but none was '
'on the stack.')
if ctx.pad is not record.pad:
raise RuntimeError('The context on the stack does not match the '
'pad of the record.')
return ctx
def __render(self):
# When the markdown instance is attached to a cached object we can
# end up in the situation where the context changed from the time
# we were put into the cache to the time where we got referenced
# by something elsewhere. In that case we need to re-process our
# markdown. For instance this affects relative links.
if self.__html is None or \
self.__cached_for_ctx != get_ctx():
self.__html, self.__meta = markdown_to_html(
self.source, self.__record())
self.__cached_for_ctx = get_ctx()
def link(self, link, title, text):
if self.record is not None:
url = url_parse(link)
if not url.scheme:
link = self.record.url_to('!' + link,
base_url=get_ctx().base_url)
link = escape(link)
if not title:
return '<a href="%s">%s</a>' % (link, text)
title = escape(title)
return '<a title="%s" href="%s">%s</a>' % (link, title, text)
def __render(self):
# When the markdown instance is attached to a cached object we can
# end up in the situation where the context changed from the time
# we were put into the cache to the time where we got referenced
# by something elsewhere. In that case we need to re-process our
# markdown. For instance this affects relative links.
if self.__html is None or \
self.__cached_for_ctx != get_ctx():
self.__html, self.__meta = markdown_to_html(
self.source, self.__record())
self.__cached_for_ctx = get_ctx()