diff --git a/mikoto/__init__.py b/mikoto/__init__.py
index 4391033..53bfce7 100644
--- a/mikoto/__init__.py
+++ b/mikoto/__init__.py
@@ -1,29 +1,35 @@
# -*- coding: utf-8 -*-
-from mikoto.markdown import render_markdown
+from mikoto.markdown import render_markdown, init_markdown
from mikoto.rst import render_rst
from mikoto.code import render_code, render_highlight_code
from mikoto.text import translate_to_unicode
__all__ = ['Mikoto']
+
class Mikoto(object):
- def __init__(self, text):
- self.text = text
- self.unicode = translate_to_unicode(text)
+ def __init__(self):
+ self.markdown = None
+
+ def init_markdown(self, **kw):
+ self.markdown = init_markdown(**kw)
- @property
- def markdown(self):
- return render_markdown(self.unicode)
+ def render_markdown(self, text, path):
+ text = translate_to_unicode(text)
+ if self.markdown:
+ return render_markdown(text)
+ return self.markdown.render(text)
- @property
- def restructuredtext(self):
- return render_rst(self.unicode)
+ def render_restructuredtext(self, text, path):
+ text = translate_to_unicode(text)
+ return render_rst(text)
- @property
- def code(self):
- return render_code(self.unicode)
+ def render_code(self, text, path):
+ text = translate_to_unicode(text)
+ return render_code(text)
- def highlight_code(self, path):
- return render_highlight_code(self.unicode, path)
+ def render_highlight_code(self, text, path):
+ text = translate_to_unicode(text)
+ return render_highlight_code(text, path)
diff --git a/mikoto/code.py b/mikoto/code.py
index dfb94ef..d9a04dd 100644
--- a/mikoto/code.py
+++ b/mikoto/code.py
@@ -59,11 +59,12 @@ def render_highlight_code(text, path, **kwargs):
formatter = CodeHtmlFormatter
- return highlight(text, lexer, formatter(linenos='inline',
- lineanchors='L',
- anchorlinenos=True,
- encoding='utf-8',
- **kwargs))
+ output = highlight(text, lexer, formatter(linenos='inline',
+ lineanchors='L',
+ anchorlinenos=True,
+ encoding='utf-8',
+ **kwargs))
+ return output.decode('utf-8')
class CodeHtmlFormatter(HtmlFormatter):
diff --git a/mikoto/libs/emoji.py b/mikoto/emoji.py
similarity index 66%
rename from mikoto/libs/emoji.py
rename to mikoto/emoji.py
index 8189657..1962b6a 100644
--- a/mikoto/libs/emoji.py
+++ b/mikoto/emoji.py
@@ -1,9 +1,7 @@
-#!/usr/bin/env python
-# encoding: utf-8
+# -*- coding: utf-8 -*-
-import re
import os
-from cgi import escape
+import re
EMOJIS = [
':airplane:', ':alien:', ':art:', ':bear:', ':beer:', ':bike:', ':bomb:',
@@ -21,33 +19,28 @@
]
-# EMOJIS dir: /hub/static/emoji/
-EMOJI_GROUPS = {
+GROUPED_EMOJIS = {
":mergetime:": """
:zap::zap::zap::zap::zap::zap::zap::zap::zap::zap:
:zap::metal: M E R G E T I M E :metal::zap:
:zap::zap::zap::zap::zap::zap::zap::zap::zap::zap:
""",
-
":sparklock:": """
:black_circle::point_down::black_circle:
:point_right::sparkler::point_left:
:black_circle::point_up_2::black_circle:
""",
-
":myballoon:": """
:cloud::partly_sunny::cloud::cloud::cloud::cloud::cloud:
-
+
:balloon:
-
+
:runner::dash:
""",
-
":getit:": """
:balloon:
:raised_hand:
""",
-
":apollo:": """
:octocat: :star2: :us:
:sparkles: :sparkles: :full_moon:
@@ -55,41 +48,55 @@
:sparkles: :collision:
:partly_sunny: :collision: :sparkles:
:zap: :collision:
-:earth_asia: :sparkles: :dizzy:
+:earth_asia: :sparkles: :dizzy:
""",
}
-def parse_emoji_groups(text):
- groups = set(RE_EMOJI_GROUPS.findall(text))
- for group in groups:
- group_text = EMOJI_GROUPS[group]
- group_text = group_text.replace(' ', ' ')
- group_text = group_text.replace('\n', "
")
- text = text.replace(group, group_text)
- return text
+class Emoji(object):
+
+ def __init__(self, emojis=None, grouped_emojis=None):
+ self.emojis = emojis or []
+ self.grouped_emojis = grouped_emojis or {}
+
+ def init(self):
+ self.emoji_pattern = re.compile(r'(' + '|'.join([re.escape(x) for x in self.emojis]) + r')')
+ self.emoji_only_pattern = re.compile(r'^
\s*(' + '|'.join([re.escape(x) for x in self.emojis]) + r')\s*
$') + self.grouped_emoji_pattern = re.compile('|'.join([re.escape(x) for x in self.grouped_emojis.keys()])) -def parse_emoji(text, is_escape=True): +emoji = Emoji(emojis=EMOJIS, grouped_emojis=GROUPED_EMOJIS) +emoji.init() + + +def render_emoji(emoji, text): if not text: return '' - if is_escape: - text = escape(text) - text = parse_emoji_groups(text) - if RE_EMOJI_ONLY.match(text.strip()): + text = render_grouped_emoji(emoji, text) + if emoji.emoji_only_pattern.match(text.strip()): emoji_img = '
'
else:
emoji_img = '\s*(' + '|'.join([re.escape(x) for x in all_emojis()]) + r')\s*
$') -RE_EMOJI_GROUPS = re.compile('|'.join([re.escape(x) for x in EMOJI_GROUPS.keys()])) diff --git a/mikoto/htmlrenderer.py b/mikoto/htmlrenderer.py index 49ee1b2..25700cb 100644 --- a/mikoto/htmlrenderer.py +++ b/mikoto/htmlrenderer.py @@ -6,7 +6,7 @@ from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter -from mikoto.libs.emoji import parse_emoji +from mikoto.emoji import render_emoji from mikoto.checklist import render_checklist RE_USER_MENTION = re.compile(r'(^|\W)@([a-zA-Z0-9_]+)') @@ -15,11 +15,17 @@ class HtmlRenderer(misaka.HtmlRenderer): + def __init__(self, *k, **kw): + self.emoji = kw.pop('emoji', None) + self.link_prefix = kw.pop('link_prefix', None) + super(HtmlRenderer, self).__init__(*k, **kw) + def postprocess(self, text): if not text: return text text = render_checklist(text) - text = parse_emoji(text, is_escape=False) + if self.emoji: + text = render_emoji(self.emoji, text) return RE_USER_MENTION.sub(USER_LINK_TEXT, text) def block_code(self, text, lang): @@ -44,18 +50,20 @@ def __text_to_unichr(self, text): text = text.replace("@", "@") return text - def __link_to_local_project(self, link): + def __link_to_local(self, link): if not (link.startswith("http://") or link.startswith("https://")): - link = "[PROJECT]%s" % link + link = "[MIKOTO_PREFIX]%s" % link return link def image(self, link, title, alt_text): alt_text = alt_text or "" - link = self.__link_to_local_project(link) + if self.link_prefix: + link = self.__link_to_local(link) return '