Cosmetic changes on mdrender service.

remotes/origin/enhancement/email-actions
Andrey Antukh 2014-05-11 10:22:04 +02:00
parent 77c9a4757d
commit f9d3a1d987
1 changed files with 32 additions and 16 deletions

View File

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