Exemple #1
0
    def _generate_preview_html(self, data):
        """Returns the first few truncated lines of the text file."""
        from reviewboard.diffviewer.chunk_generator import \
            NoWrapperHtmlFormatter

        charset = self.mimetype[2].get('charset', 'ascii')
        try:
            text = data.decode(charset)
        except UnicodeDecodeError:
            logging.error(
                'Could not decode text file attachment %s using '
                'charset "%s"', self.attachment.pk, charset)
            text = data.decode('utf-8', 'replace')

        try:
            lexer = guess_lexer_for_filename(self.attachment.filename, text)
        except ClassNotFound:
            lexer = TextLexer()

        lines = highlight(text, lexer, NoWrapperHtmlFormatter()).splitlines()

        return ''.join([
            '<pre>%s</pre>' % line
            for line in lines[:self.TEXT_CROP_NUM_HEIGHT]
        ])
Exemple #2
0
    def _generate_preview_html(self, data):
        """Return the first few truncated lines of the text file.

        Args:
            data (bytes):
                The contents of the attachment.

        Returns:
            django.utils.safestring.SafeText:
            The resulting HTML-safe thumbnail content.
        """
        from reviewboard.diffviewer.chunk_generator import \
            NoWrapperHtmlFormatter

        charset = self.mimetype[2].get('charset', 'ascii')

        try:
            text = data.decode(charset)
        except UnicodeDecodeError:
            logging.error(
                'Could not decode text file attachment %s using '
                'charset "%s"', self.attachment.pk, charset)
            text = data.decode('utf-8', 'replace')

        try:
            lexer = guess_lexer_for_filename(self.attachment.filename, text)
        except ClassNotFound:
            lexer = TextLexer()

        lines = highlight(text, lexer, NoWrapperHtmlFormatter()).splitlines()

        return format_html_join(
            '', '<pre>{0}</pre>',
            ((mark_safe(line), )
             for line in lines[:self.TEXT_CROP_NUM_HEIGHT]))
Exemple #3
0
    def generate_highlighted_text(self):
        """Generates syntax-highlighted text for the file.

        This will render the text file to HTML, applying any syntax
        highlighting that's appropriate. The contents will be split into
        reviewable lines and will be cached for future renders.
        """
        data = self.get_text()

        lexer = self.get_source_lexer(self.obj.filename, data)
        lines = highlight(data, lexer, NoWrapperHtmlFormatter()).splitlines()

        return ['<pre>%s</pre>' % line for line in lines]
Exemple #4
0
    def _generate_preview_html(self, data):
        """Returns the first few truncated lines of the text file."""
        from reviewboard.diffviewer.chunk_generator import \
            NoWrapperHtmlFormatter

        try:
            lexer = guess_lexer_for_filename(self.attachment.filename, data)
        except ClassNotFound:
            lexer = TextLexer()

        lines = highlight(data, lexer, NoWrapperHtmlFormatter()).splitlines()

        return ''.join([
            '<pre>%s</pre>' % line
            for line in lines[:self.TEXT_CROP_NUM_HEIGHT]
        ])
Exemple #5
0
    def _apply_pygments(self, data, filename):
        """Applies Pygments syntax-highlighting to a file's contents.

        Syntax highlight obeys a explicitly provided list of preferences by
        extension or it is derived from the contents of the file.

        The resulting HTML will be returned as a list of lines.
        """
        lexer = self._get_preferred_lexer(filename,
                                          stripln=False,
                                          encoding='utf-8')
        logger.debug('preferred lexer for %s: %s' % (filename, lexer))
        if not lexer:
            lexer = guess_lexer_for_filename(filename,
                                             data,
                                             stripnl=False,
                                             encoding='utf-8')

        lexer.add_filter('codetagify')

        return split_line_endings(
            highlight(data, lexer, NoWrapperHtmlFormatter()))