Esempio n. 1
0
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)
Esempio n. 2
0
 def highlightString(src):
     try:
         if self.currentExt == 'php':
             from pygments.lexers import PhpLexer
             return highlight(src, PhpLexer(), HtmlFormatter())
         elif self.currentExt == 'py':
             from pygments.lexers import PythonLexer
             return highlight(src, PythonLexer(), HtmlFormatter())
         elif self.currentExt == 'rb':
             from pygments.lexers import RubyLexer
             return highlight(src, RubyLexer(), HtmlFormatter())
         elif self.currentExt == 'pl':
             from pygments.lexers import PerlLexer
             return highlight(src, PerlLexer(), HtmlFormatter())
         elif self.currentExt == 'java':
             from pygments.lexers import JavaLexer
             return highlight(src, JavaLexer(), HtmlFormatter())
         elif self.currentExt == 'cs':
             from pygments.lexers import CSharpLexer
             return highlight(src, CSharpLexer(), HtmlFormatter())
         else:
             from pygments.lexers import JavascriptLexer
             return highlight(src, JavascriptLexer(), HtmlFormatter())
     except: 
         return "File could not be highlighted"
Esempio n. 3
0
def ruby2html(ruby_file):
    with open(ruby_file, encoding='utf-8') as rbf:
        code = rbf.readlines()
    html = highlight(''.join(code), RubyLexer(), HtmlFormatter())
    html_name = ''.join(ruby_file.split('.')[:-1]) + '.html'
    with open(html_name, 'w', encoding='utf-8') as hf:
        hf.writelines(html)
Esempio n. 4
0
 def recognize(cls, location):
     """
     Yield one or more Package manifest objects given a file ``location`` pointing to a
     package archive, manifest or similar.
     """
     with io.open(location, encoding='utf-8') as loc:
         file_contents = loc.read()
     formatted_file_contents = highlight(
         file_contents, RubyLexer(), ChefMetadataFormatter())
     package_data = json.loads(formatted_file_contents)
     return build_package(cls, package_data)
Esempio n. 5
0
def parse(location):
    """
    Return a Package object from a metadata.json file or a metadata.rb file or None.
    """
    if is_metadata_json(location):
        with io.open(location, encoding='utf-8') as loc:
            package_data = json.load(loc, object_pairs_hook=OrderedDict)
        return build_package(package_data)

    if is_metadata_rb(location):
        with io.open(location, encoding='utf-8') as loc:
            file_contents = loc.read()
        formatted_file_contents = highlight(
            file_contents, RubyLexer(), ChefMetadataFormatter())
        package_data = json.loads(formatted_file_contents, object_pairs_hook=OrderedDict)
        return build_package(package_data)
Esempio n. 6
0
def lexer():
    yield RubyLexer()
Esempio n. 7
0
 def setUp(self):
     self.lexer = RubyLexer()
     self.maxDiff = None