Ejemplo n.º 1
0
    def save(self):
        super(TextFile, self).save()

        lexer = None

        content = self.f.value.decode('utf-8')

        plain_ext = '.txt', '.log',

        if self.ext in plain_ext:
            lexer = TextLexer

        else:

            try:
                lexer = get_lexer_for_filename(self.filename)
            except ClassNotFound:
                try:
                    lexer = get_lexer_for_mimetype(self.mimetype)
                except ClassNotFound:
                    try:
                        lexer = guess_lexer(content)
                    except ClassNotFound:
                        lexer = TextLexer

        html = highlight(
            content, lexer,
            HtmlFormatter(linenos=True, lineanchors='line',
                          anchorlinenos=True))

        txt = Document()

        txt.file_id = self.id
        txt.html = html
        txt.content = content

        txt.save()

        return self.base
Ejemplo n.º 2
0
    def save(self):
        super(TextFile, self).save()

        lexer = None

        content = self.f.value.decode('utf-8')

        plain_ext = '.txt', '.log',

        if self.ext in plain_ext:
            lexer = TextLexer

        else:

            try:
                lexer = get_lexer_for_filename(self.filename)
            except ClassNotFound:
                try:
                    lexer = get_lexer_for_mimetype(self.mimetype)
                except ClassNotFound:
                    try:
                        lexer = guess_lexer(content)
                    except ClassNotFound:
                        lexer = TextLexer

        html = highlight(content, lexer, HtmlFormatter(linenos=True, lineanchors='line', anchorlinenos = True))

        txt = Document()

        txt.file_id = self.id
        txt.html    = html
        txt.content = content

        txt.save()

        return self.base