Cosmetic changes on mdrender service.
parent
77c9a4757d
commit
f9d3a1d987
|
@ -1,18 +1,38 @@
|
|||
import hashlib
|
||||
import functools
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.utils.encoding import force_bytes
|
||||
|
||||
from markdown import markdown
|
||||
from markdown.extensions.wikilinks import WikiLinkExtension
|
||||
from .gfm import (AutolinkExtension, AutomailExtension, HiddenHiliteExtension,
|
||||
SemiSaneListExtension, SpacedLinkExtension,
|
||||
StrikethroughExtension)
|
||||
from fn import F
|
||||
|
||||
from .gfm import AutolinkExtension
|
||||
from .gfm import AutomailExtension
|
||||
from .gfm import HiddenHiliteExtension
|
||||
from .gfm import SemiSaneListExtension
|
||||
from .gfm import SpacedLinkExtension
|
||||
from .gfm import StrikethroughExtension
|
||||
|
||||
from .processors.emoji import emoji
|
||||
from .processors.mentions import mentions
|
||||
from .processors.references import references
|
||||
|
||||
from fn import F
|
||||
|
||||
def _make_extensions_list(wikilinks_config=None):
|
||||
return [AutolinkExtension(),
|
||||
AutomailExtension(),
|
||||
SemiSaneListExtension(),
|
||||
SpacedLinkExtension(),
|
||||
StrikethroughExtension(),
|
||||
WikiLinkExtension(wikilinks_config),
|
||||
"extra",
|
||||
"codehilite"])
|
||||
|
||||
|
||||
def cache_by_sha(func):
|
||||
@functools.wraps(func)
|
||||
def _decorator(project, text):
|
||||
sha1_hash = hashlib.sha1(force_bytes(text)).hexdigest()
|
||||
key = "{}-{}".format(sha1_hash, project.id)
|
||||
|
@ -23,23 +43,18 @@ def cache_by_sha(func):
|
|||
return cached
|
||||
|
||||
returned_value = func(text)
|
||||
|
||||
cache.set(key, returned_value, timeout=None)
|
||||
return returned_value
|
||||
|
||||
return _decorator
|
||||
|
||||
|
||||
def _render_markdown(project, text):
|
||||
wikilinks_config = {
|
||||
"base_url": "#/project/{}/wiki/".format(project.slug),
|
||||
"end_url": ""
|
||||
}
|
||||
return markdown(text, extensions=[
|
||||
AutolinkExtension(), AutomailExtension(),
|
||||
SemiSaneListExtension(), SpacedLinkExtension(),
|
||||
StrikethroughExtension(), WikiLinkExtension(wikilinks_config), "extra",
|
||||
"codehilite"
|
||||
])
|
||||
wikilinks_config = {"base_url": "#/project/{}/wiki/".format(project.slug),
|
||||
"end_url": ""}
|
||||
extenstions = _make_extensions_list(wikilinks_config=wikilinks_config)
|
||||
return markdown(text, extensions=extensions)
|
||||
|
||||
|
||||
def _preprocessors(project, text):
|
||||
pre = F() >> mentions >> F(references, project)
|
||||
|
@ -49,10 +64,11 @@ def _postprocessors(project, html):
|
|||
post = F() >> emoji
|
||||
return post(html)
|
||||
|
||||
|
||||
#@cache_by_sha
|
||||
def render(project, text):
|
||||
renderer = F() >> F(_preprocessors, project) >> F(_render_markdown, project) >> F(_postprocessors, project)
|
||||
|
||||
return renderer(text)
|
||||
|
||||
|
||||
__all__ = ['render']
|
||||
|
|
Loading…
Reference in New Issue