"""Template tags for emoticons app"""
import re
import os

from django import template
from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape

from emoticons.settings import EMOTICONS_URL
from emoticons.settings import EMOTICONS_LIST
from emoticons.settings import EMOTICONS_CLASS

register = template.Library()

RE_EMOTICONS_LIST = [(re.compile(re.escape(emoticon[0])),
                      emoticon[0], emoticon[1])
                     for emoticon in EMOTICONS_LIST]


def replace_emoticons(content, autoescape=None):
    esc = autoescape and conditional_escape or (lambda x: x)
    content = esc(content)
    for emoticon, name, image in RE_EMOTICONS_LIST:
        if emoticon.search(content):
            emoticon_html = '<img class="%s" src="%s" alt="%s" />' % (
                EMOTICONS_CLASS, os.path.join(EMOTICONS_URL, image), name)
            content = emoticon.sub(emoticon_html, content)
    return mark_safe(content)
replace_emoticons.needs_autoescape = True


class SmileyNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        output = self.nodelist.render(context)
        return replace_emoticons(output)


def do_replace_emoticons(parser, token):
    nodelist = parser.parse(('endemoticons',))
    parser.delete_first_token()
    return SmileyNode(nodelist)


register.tag('emoticons', do_replace_emoticons)
register.filter('emoticons', replace_emoticons)

@@ -367,16 +367,21 @@
 LIST = %5B
+%0A    
 (re.comp
@@ -408,38 +408,16 @@
 on%5B0%5D)),
-%0A                     
  emotico
@@ -435,33 +435,16 @@
 con%5B1%5D)%0A
-                 
     for 
@@ -469,16 +469,17 @@
 ONS_LIST
+%0A
 %5D%0A%0A%0Adef 
@@ -507,115 +507,75 @@
 tent
-, autoescape=None):%0A    esc = autoescape and condi
+):%0A    %22%22%22%0A    Replace the emo
 ti
+c
 on
-al_escape or (lambda x: x)%0A    content = esc(content)
+s string by HTML images.%0A    %22%22%22
 %0A   
@@ -880,68 +880,15 @@
 urn 
-mark_safe(content)%0Areplace_emoticons.needs_autoescape = True
+content
 %0A%0A%0Ac
@@ -892,22 +892,24 @@
 %0A%0Aclass 
-Smiley
+Emoticon
 Node(tem
@@ -921,16 +921,88 @@
 .Node):%0A
+    %22%22%22%0A    Node for applying %60%60replace_emoticons%60%60 on content.%0A    %22%22%22%0A
     def 
@@ -1100,21 +1100,22 @@
         
-outpu
+conten
 t = self
@@ -1177,33 +1177,50 @@
 ons(
-output)%0A%0A%0Adef do_replace_
+content)%0A%0A%0A@register.tag('emoticons')%0Adef 
 emot
@@ -1224,16 +1224,20 @@
 moticons
+_tag
 (parser,
@@ -1245,16 +1245,65 @@
 token):%0A
+    %22%22%22%0A    Tag for rendering emoticons.%0A    %22%22%22%0A
     node
@@ -1322,17 +1322,17 @@
 r.parse(
-(
+%5B
 'endemot
@@ -1341,10 +1341,9 @@
 ons'
-,)
+%5D
 )%0A  
@@ -1387,14 +1387,16 @@
 urn 
-Smiley
+Emoticon
 Node
@@ -1408,16 +1408,17 @@
 list)%0A%0A%0A
+@
 register
@@ -1410,35 +1410,38 @@
 st)%0A%0A%0A@register.
-tag
+filter
 ('emoticons', do
@@ -1442,74 +1442,264 @@
 s', 
-do_replace_emoticons)%0Aregister.filter('emoticons', replace_emoticons)
+needs_autoescape=True)%0Adef emoticons_filter(content, autoescape=None):%0A    %22%22%22%0A    Filter for rendering emoticons.%0A    %22%22%22%0A    esc = autoescape and conditional_escape or (lambda x: x)%0A    content = mark_safe(replace_emoticons(esc(content)))%0A    return content
 %0A

Refactor template tag and filte