#!/usr/bin/env python
# encoding: utf-8

import re
import os
from cgi import escape


# EMOJIS dir: /hub/static/emoji/
EMOJIS = [
    ':airplane:', ':alien:', ':art:', ':bear:', ':beer:', ':bike:', ':bomb:',
    ':book:', ':bulb:', ':bus:', ':cake:', ':calling:', ':clap:', ':cocktail:',
    ':code:', ':computer:', ':cool:', ':cop:', ':email:', ':feet:', ':fire:',
    ':fish:', ':fist:', ':gift:', ':hammer:', ':heart:', ':iphone:', ':key:',
    ':leaves:', ':lgtm:', ':lipstick:', ':lock:', ':mag:', ':mega:', ':memo:',
    ':moneybag:', ':new:', ':octocat:', ':ok:', ':palm_tree:', ':pencil:',
    ':punch:', ':runner:', ':scissors:', ':ship:', ':shipit:', ':ski:', ':smile:',
    ':smoking:', ':sparkles:', ':star:', ':sunny:', ':taxi:', ':thumbsdown:',
    ':thumbsup:', ':tm:', ':tophat:', ':train:', ':trollface:', ':v:', ':vs:',
    ':warning:', ':wheelchair:', ':zap:', ':zzz:', ':see_no_evil:',':pig:',
    ':hear_no_evil:', ':speak_no_evil:', ':monkey:', ':monkey_face:', ':beers:',
    ':ruby:',
]


def parse_emoji(text, is_escape=True):
    if not text:
        return ''
    if is_escape:
        text = escape(text)
    if RE_EMOJI_ONLY.match(text.strip()):
        emoji_img = '<img src="/static/emoji/%s.png" align="absmiddle"/>'
    else:
        emoji_img = '<img src="/static/emoji/%s.png" height="20" width="20" align="absmiddle"/>'
    result = RE_EMOJI.sub(lambda x: emoji_img % x.group().strip(':'), text)
    return result


def all_emojis():
    curdir = os.path.abspath(os.path.curdir)
    emoji_dir = os.path.join(curdir, 'hub/static/emoji/')
    if os.path.isdir(emoji_dir):
        files = os.listdir(emoji_dir)
    else:
        realpath = os.path.dirname(os.path.realpath(__file__))
        curdir = os.path.join(realpath,os.path.pardir,'hub/static/emoji')
        curdir = os.path.abspath(curdir)
        if os.path.isdir(curdir):
            files = os.listdir(emoji_dir)
        else:
            return EMOJIS
    if files:
        return [':{}:'.format(fn[:-4]) for fn in files if fn.endswith('.png')]
    else:
        return EMOJIS


def url_for_emoji(emoji):
    return '/static/emoji/%s.png' % emoji[1:-1]


RE_EMOJI = re.compile(r'(' + '|'.join([re.escape(x) for x in all_emojis()]) + r')')
RE_EMOJI_ONLY = re.compile(r'^<p>\s*(' + '|'.join([re.escape(x) for x in all_emojis()]) + r')\s*</p>$')

@@ -121,894 +121,1093 @@
 MOJI
-S = %5B%0A    ':airplane:', ':alien:', ':art:', ':bear:', ':beer:', ':bike:', ':bomb:',%0A    ':book:', ':bulb:', ':bus:', ':cake:', ':calling:', ':clap:', ':cocktail:',%0A    ':code:', ':computer:', ':cool:', ':cop:', ':email:', ':feet:', ':fire:',%0A    ':fish:', ':fist:', ':gift:', ':hammer:', ':heart:', ':iphone:', ':key:',%0A    ':leaves:', ':lgtm:', ':lipstick:', ':lock:', ':mag:', ':mega:', ':memo:',%0A    ':moneybag:', ':new:', ':octocat:', ':ok:', ':palm_tree:', ':pencil:',%0A    ':punch:', ':runner:', ':scissors:', ':ship:', ':shipit:', ':ski:', ':smile:',%0A    ':smoking:', ':sparkles:', ':star:', ':sunny:', ':taxi:', ':thumbsdown:',%0A    ':thumbsup:', ':tm:', ':tophat:', ':train:', ':trollface:', ':v:', ':vs:',%0A    ':warning:', ':wheelchair:', ':zap:', ':zzz:', ':see_no_evil:',':pig:',%0A    ':hear_no_evil:', ':speak_no_evil:', ':monkey:', ':monkey_face:', ':beers:',%0A    ':ruby:',%0A%5D
+_GROUPS = %7B%0A        %22:mergetime:%22:%22%22%22%0A%0A:zap::zap::zap::zap::zap::zap::zap::zap::zap::zap:%0A:zap::metal: M E R G E T I M E :metal::zap:%0A:zap::zap::zap::zap::zap::zap::zap::zap::zap::zap:%0A%0A%22%22%22,%0A%0A        %22:sparklock:%22:%22%22%22%0A%0A:black_circle::point_down::black_circle:%0A:point_right::sparkler::point_left:%0A:black_circle::point_up_2::black_circle:%0A%0A%22%22%22,%0A%0A        %22:myballoon:%22:%22%22%22%0A%0A:cloud::partly_sunny::cloud::cloud::cloud::cloud::cloud:%0A %0A        :balloon:%0A %0A                    :runner::dash:%0A%0A%22%22%22,%0A%0A        %22:getit:%22:%22%22%22%0A%0A:balloon:%0A  :raised_hand:%0A%0A%22%22%22,%0A%0A        %22:apollo:%22:%22%22%22%0A%0A:octocat:      :star2:             :us:%0A:sparkles:                :sparkles:   :full_moon:%0A:star2:     :dizzy:         :rocket:%0A        :sparkles:     :collision:%0A:partly_sunny:        :collision:       :sparkles:%0A:zap:   :collision:%0A:earth_asia:          :sparkles:         :dizzy:   %0A%0A%22%22%22,%0A%7D%0A%0A%0Adef parse_emoji_groups(text):%0A    groups = set(RE_EMOJI_GROUPS.findall(text))%0A    for group in groups:%0A        text = text.replace(group, EMOJI_GROUPS%5Bgroup%5D)%0A        text = text.replace(' ', %22&nbsp;%22)%0A    return text
 %0A%0A%0Ad
@@ -1297,16 +1297,16 @@
 escape:%0A
-
         
@@ -1321,24 +1321,60 @@
 scape(text)%0A
+    text = parse_emoji_groups(text)%0A
     if RE_EM

add emoji group