Added Pygments-based source file previewer

This commit is contained in:
Pedro Ferreira 2015-08-28 16:25:11 +02:00
parent 557bd540d9
commit 6fd1ec7e30
5 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1 @@
graft indico_previewer_code/templates

View File

@ -0,0 +1,21 @@
# This file is part of Indico.
# Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.
"""Syntax highlighting for common programming languages and related formats."""
from .plugin import CodePreviewerPlugin
__all__ = ('CodePreviewerPlugin',)

View File

@ -0,0 +1,84 @@
# This file is part of Indico.
# Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import mimetypes
from flask import render_template
from pygments import highlight
from pygments.lexers import get_lexer_for_mimetype, CppLexer, PhpLexer, JavaLexer, RubyLexer
from pygments.formatters import HtmlFormatter
from indico.core import signals
from indico.core.plugins import IndicoPlugin
from indico.modules.attachments.preview import Previewer
def register_custom_mimetypes():
mimetypes.add_type(b'text/x-csharp', b'.cs')
register_custom_mimetypes()
class PygmentsPreviewer(Previewer):
# All supported MIME types
MIMETYPES = ('text/css', 'text/x-python', 'text/x-ruby-script', 'text/x-java-source', 'text/x-c',
'application/javascript', 'text/x-c', 'text/x-fortran', 'text/x-csharp', 'text/php',
'text/x-php')
# Python's mimetypes lib and Pygments do not quite agree on some MIME types
CUSTOM_LEXERS = {
'text/x-c': CppLexer(),
'text/x-java-source': JavaLexer(),
'text/x-ruby-script': RubyLexer(),
'text/php': PhpLexer()
}
@classmethod
def can_preview(cls, attachment_file):
return attachment_file.content_type in cls.MIMETYPES
@classmethod
def generate_content(cls, attachment):
mime_type = attachment.file.content_type
lexer = cls.CUSTOM_LEXERS.get(mime_type)
if lexer is None:
lexer = get_lexer_for_mimetype(mime_type)
with attachment.file.open() as f:
html_formatter = HtmlFormatter(style='tango', linenos='inline', prestyles='mono')
html_code = highlight(f.read(), lexer, html_formatter)
css_code = html_formatter.get_style_defs('.highlight')
return render_template('previewer_code:pygments_preview.html', attachment=attachment,
html_code=html_code, css_code=css_code)
class CodePreviewerPlugin(IndicoPlugin):
"""Syntax highlighter (Pygments)"""
configurable = False
def init(self):
super(CodePreviewerPlugin, self).init()
self.connect(signals.attachments.get_file_previewers, self._get_file_previewers)
def _get_file_previewers(self, sender, **kwargs):
yield PygmentsPreviewer

View File

@ -0,0 +1,18 @@
<style>
{{ css_code }}
.highlight pre {
font-size: 0.9em;
}
.lineno {
color: #aaa;
padding-right: 0.5em;
}
</style>
<pre class="text-preview-content">
{{ html_code | safe }}
</pre>

38
previewer_code/setup.py Normal file
View File

@ -0,0 +1,38 @@
# This file is part of Indico.
# Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
from setuptools import setup, find_packages
setup(
name='indico_previewer_code',
version='0.1',
url='https://github.com/indico/indico-plugins',
license='https://www.gnu.org/licenses/gpl-3.0.txt',
author='Indico Team',
author_email='indico-team@cern.ch',
packages=find_packages(),
zip_safe=False,
include_package_data=True,
platforms='any',
install_requires=[
'indico>=1.9.4',
'pygments'
],
entry_points={'indico.plugins': {'previewer_code = indico_previewer_code:CodePreviewerPlugin'}}
)