# -*- coding: utf-8 -*-

from pygments import highlight
from pygments.lexers import (TextLexer,
                             RstLexer,
                             MakoHtmlLexer,
                             PythonLexer,
                             guess_lexer_for_filename)
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound

IGNORE_FILE_EXTS = (
    # compress
    '.tar', '.zip', '.rar', '.gz', '.bz', '.bz2', '.dmg', '.jar',
    # image
    '.bmp', '.jpeg', '.jpg', '.png', '.gif', '.svg', '.ico',
    # font
    '.ttf',
    # media
    '.mp3', '.swf', '.fla', '.flash', '.wmv', '.asf', '.asx', '.rm',
    '.rmvb', '.mpg', '.mpeg', '.mpe', '.3gp', '.mov', '.mp4', '.m4v',
    '.avi', '.dat', '.mkv', '.flv', '.vob',
    # min
    '.min.js', '.min.css',
    # binary
    '.pdf', '.svn-base', '.pcap',
)


def render_code(text):
    lexer = TextLexer(encoding='utf-8')
    # the output of highlight is string
    output = highlight(text,
                       lexer,
                       HtmlFormatter(linenos=True,
                                     lineanchors='L',
                                     anchorlinenos=True,
                                     encoding='utf-8'))
    return output.decode('utf-8')


def render_highlight_code(text, path, div=False, **kwargs):
    try:
        if path.endswith(('.html', '.mako')):
            lexer = MakoHtmlLexer(encoding='utf-8')
        elif path.endswith('.ptl'):
            lexer = PythonLexer(encoding='utf-8')
        elif path.endswith('.md'):
            lexer = RstLexer(encoding='utf-8')
        else:
            if path.endswith(IGNORE_FILE_EXTS):
                text = 'Hmm.., this is binary file.'
            lexer = guess_lexer_for_filename(path, text)
        lexer.encoding = 'utf-8'
        lexer.stripnl = False
    except ClassNotFound:
        # no code highlight
        lexer = TextLexer(encoding='utf-8')

    if div:
        formatter = CodeHtmlFormatter
    else:
        formatter = HtmlFormatter

    return highlight(text, lexer, formatter(linenos=True,
                                            lineanchors='L',
                                            anchorlinenos=True,
                                            encoding='utf-8',
                                            **kwargs))


class CodeHtmlFormatter(HtmlFormatter):

    def wrap(self, source, outfile):
        return self._wrap_div(self._wrap_pre(self._wrap_a_line(source)))

    def _wrap_a_line(self, source):
        for i, t in source:
            if i == 1:
                # it's a line of formatted code
                t = '<div>' + t + '</div>'
                yield i, t

@@ -1294,19 +1294,8 @@
 ath,
- div=False,
  **k
@@ -1915,24 +1915,8 @@
 ')%0A%0A
-    if div:%0A    
     
@@ -1948,52 +1948,8 @@
 tter
-%0A    else:%0A        formatter = HtmlFormatter
 %0A%0A  
@@ -1990,36 +1990,40 @@
 rmatter(linenos=
-True
+'inline'
 ,%0A              
@@ -2360,43 +2360,11 @@
 rap_
-div(self._wrap_pre(self._wrap_a_lin
+cod
 e(so
@@ -2368,18 +2368,16 @@
 (source)
-))
 %0A%0A    de
@@ -2388,13 +2388,11 @@
 rap_
-a_lin
+cod
 e(se
@@ -2400,24 +2400,51 @@
 f, source):%0A
+        yield 0, '%3Ctable%3E'%0A
         for 
@@ -2556,52 +2556,393 @@
 = '%3C
-div%3E' + t + '%3C/div
+tr%3E%3Ctd class=%22blob-num%22%3E%25s%3C/td%3E%3C/tr%3E' %25 t%0A            yield i, t%0A        yield 0, '%3C/table
 %3E'%0A
+%0A
     
-            yield i, t
+def _wrap_num(self, source):%0A        for i, t in source:%0A            if i == 1:%0A                t = '%3C/td%3E%3Ctd class=%22blob-code%22%3E%25s' %25 t%0A            yield i, t%0A%0A    def _format_lines(self, tokensource):%0A        return self._wrap_num(super(CodeHtmlFormatter, self)._format_lines(tokensource))
 %0A

Fix highlight code.