# Path: cms_content/settings.py
# ROOT_URL = getattr(settings, 'CMS_CONTENT_ROOT_URL', '/cms/')
# CODE_HIGHLIGHT = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT', True)
# CODE_HIGHLIGHT_CSS = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT_CSS', 'code_highlight')
# CODE_HIGHLIGHT_LINENOS = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT_LINENOS', False)
# CODE_HIGHLIGHT_DEFAULT = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT_DEFAULT', 'text')
# EDITOR = getattr(settings, 'CMS_CONTENT_EDITOR', 'WYMEditor')
# ARTICLE_PERPAGE = getattr(settings, 'CMS_CONTENT_ARTICLE_PERPAGE', 10)
# AKISMET_API_KEY = '773ea92115d8'
# UPLOAD_TO = getattr(settings, 'CMS_CONTENT_UPLOAD_TO', 'upload')
# TAGCLOUD_MIN = getattr(settings, 'TAGGIT_TAGCLOUD_MIN', 1.0)
# TAGCLOUD_MAX = getattr(settings, 'TAGGIT_TAGCLOUD_MAX', 3.0) 


from django.db import models
from django.db.models import Count
from django.template import Library
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
from django.template.defaultfilters import stringfilter
from templatetag_sugar.register import tag
from templatetag_sugar.parser import Name, Variable, Constant, Optional, Model
from taggit.managers import TaggableManager
from taggit.models import TaggedItem, Tag
from pygments import highlight
from pygments.lexers import LEXERS
from pygments.lexers import PythonLexer
from pygments.lexers import guess_lexer
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from BeautifulSoup import BeautifulSoup
from cms_content.settings import *
from cms_content import settings as app_settings
@register.filter
@stringfilter
def code_highlight(content):
    css = ''
    if CODE_HIGHLIGHT:
        soup = BeautifulSoup(content)
        code_blocks = soup.findAll(u'pre')
        if code_blocks:
            if CODE_HIGHLIGHT_LINENOS:
                css = u'<link href="%scms_content/css/code_highlight_table.css" rel="stylesheet" type="text/css" />' % settings.MEDIA_URL
            else:
                css = u'<link href="%scms_content/css/code_highlight_div.css" rel="stylesheet" type="text/css" />' % settings.MEDIA_URL
        for code in code_blocks: 
            if code.has_key(u'class'):
                lang = code[u'class']
                if lang not in reduce(lambda a,b: a + b[2], LEXERS.itervalues(), ()):
                    lang = CODE_HIGHLIGHT_DEFAULT
                lexer = get_lexer_by_name(lang, stripall=True, encoding=u'utf-8')
            else:
                try:
                    lexer = guess_lexer(code.string)
                except ValueError:
                    lexer = PythonLexer
            format = HtmlFormatter(cssclass=CODE_HIGHLIGHT_CSS, linenos=CODE_HIGHLIGHT_LINENOS)
            code.replaceWith(highlight(code.string, lexer, format))
        return mark_safe(css + force_unicode(soup).replace('&amp;', '&'))
    else:
        return content



# Path: cms_content/settings.py
# ROOT_URL = getattr(settings, 'CMS_CONTENT_ROOT_URL', '/cms/')
# CODE_HIGHLIGHT = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT', True)
# CODE_HIGHLIGHT_CSS = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT_CSS', 'code_highlight')
# CODE_HIGHLIGHT_LINENOS = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT_LINENOS', False)
# CODE_HIGHLIGHT_DEFAULT = getattr(settings, 'CMS_CONTENT_CODE_HIGHLIGHT_DEFAULT', 'text')
# EDITOR = getattr(settings, 'CMS_CONTENT_EDITOR', 'WYMEditor')
# ARTICLE_PERPAGE = getattr(settings, 'CMS_CONTENT_ARTICLE_PERPAGE', 10)
# AKISMET_API_KEY = '773ea92115d8'
# UPLOAD_TO = getattr(settings, 'CMS_CONTENT_UPLOAD_TO', 'upload')
# TAGCLOUD_MIN = getattr(settings, 'TAGGIT_TAGCLOUD_MIN', 1.0)
# TAGCLOUD_MAX = getattr(settings, 'TAGGIT_TAGCLOUD_MAX', 3.0) 


# Path: cms_content_patch/templatetags/content_tags.py
from django.db import models
from django.db.models import Count
from django.template import Library
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
from django.template.defaultfilters import stringfilter
from templatetag_sugar.register import tag
from templatetag_sugar.parser import Name, Variable, Constant, Optional, Model
from taggit.managers import TaggableManager
from taggit.models import TaggedItem, Tag
from pygments import highlight
from pygments.lexers import LEXERS
from pygments.lexers import PythonLexer
from pygments.lexers import guess_lexer
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from BeautifulSoup import BeautifulSoup
from cms_content.settings import *
from cms_content import settings as app_settings

@register.filter
@stringfilter
def code_highlight(content):
    css = ''
    if CODE_HIGHLIGHT:
        soup = BeautifulSoup(content)
        code_blocks = soup.findAll(u'pre')
        if code_blocks:
            if CODE_HIGHLIGHT_LINENOS:
                css = u'<link href="%scms_content/css/code_highlight_table.css" rel="stylesheet" type="text/css" />' % settings.MEDIA_URL
            else:
                css = u'<link href="%scms_content/css/code_highlight_div.css" rel="stylesheet" type="text/css" />' % settings.MEDIA_URL
        for code in code_blocks: 
            if code.has_key(u'class'):
                lang = code[u'class']
                if lang not in reduce(lambda a,b: a + b[2], LEXERS.itervalues(), ()):
                    lang = CODE_HIGHLIGHT_DEFAULT
                lexer = get_lexer_by_name(lang, stripall=True, encoding=u'utf-8')
            else:
                try:
                    lexer = guess_lexer(code.string)
                except ValueError:
                    lexer = PythonLexer
            format = HtmlFormatter(cssclass=CODE_HIGHLIGHT_CSS, linenos=CODE_HIGHLIGHT_LINENOS)
            code.replaceWith(highlight(code.string, lexer, format))
        return mark_safe(css + force_unicode(soup).replace('&amp;', '&'))
    else:
        return content



T_MAX = getattr(app_settings, 'TAGCLOUD_MAX', 6.0)