Exemplo n.º 1
0
    def run(self, edit, target='external', output_type='html'):
        output_type = output_type if output_type in FORMATS else 'html'
        pygmented = self.highlight(output_type)

        if target == 'external':
            filename = '%s.%s' % (self.view.id(), output_type,)
            tmp_file = self.write_file(filename, pygmented)
            sublime.status_message(u'Written %s preview file: %s'
                                   % (output_type.upper(), tmp_file))
            if desktop.get_desktop() == 'Mac OS X':
                # for some reason desktop.open is broken under OSX Lion
                subprocess.call("open %s" % tmp_file, shell=True)
            else:
                desktop.open(tmp_file)
        elif target == 'clipboard':
            if desktop.get_desktop() == 'Mac OS X':
                # on mac osx we have `pbcopy` :)
                filename = '%s.%s' % (self.view.id(), output_type,)
                tmp_file = self.write_file(filename, pygmented)
                subprocess.call("cat %s | pbcopy -Prefer %s"
                                % (tmp_file, output_type,), shell=True)
                os.remove(tmp_file)
            else:
                sublime.set_clipboard(pygmented)
        elif target == 'sublime':
            new_view = self.view.window().new_file()
            if output_type == 'html':
                new_view.set_syntax_file('Packages/HTML/HTML.tmLanguage')
            new_edit = new_view.begin_edit()
            new_view.insert(new_edit, 0, pygmented)
            new_view.end_edit(new_edit)
        else:
            sublime.error_message(u'Unsupported target "%s"' % target)
    def run(self, edit, target='external', output_type='html'):
        output_type = output_type if output_type in FORMATS else 'html'
        platform = desktop.get_desktop()

        # html clipboard output on windows should not be self-contained
        win = all([
            platform == 'Windows', output_type == 'html', target == 'clipboard'
        ])
        full = False if win else settings.get('full', True)

        pygmented = self.highlight(output_type, full)

        if target == 'external':
            filename = '%s.%s' % (
                self.view.id(),
                output_type,
            )
            tmp_file = self.write_file(filename, pygmented)
            sublime.status_message(u'Written %s preview file: %s' %
                                   (output_type.upper(), tmp_file))
            if platform == 'Mac OS X':
                # for some reason desktop.open is broken under OSX Lion
                subprocess.call("open %s" % tmp_file, shell=True)
            else:
                desktop.open(tmp_file)
        elif target == 'clipboard':
            if platform == 'Mac OS X':
                # on mac osx we have `pbcopy` :)
                filename = '%s.%s' % (
                    self.view.id(),
                    output_type,
                )
                tmp_file = self.write_file(filename, pygmented)
                subprocess.call("cat %s | pbcopy -Prefer %s" % (
                    tmp_file,
                    output_type,
                ),
                                shell=True)
                os.remove(tmp_file)
            elif platform == 'Windows':
                if self.view.line_endings != 'Windows':
                    pygmented = WIN_CR_RE.sub("\r\n", pygmented)
                    plaintext = WIN_CR_RE.sub("\r\n", self.code)
                else:
                    plaintext = self.code
                winclip.Paste(pygmented, output_type, plaintext)
            else:
                sublime.set_clipboard(pygmented)
        elif target == 'sublime':
            new_view = self.view.window().new_file()
            if output_type == 'html':
                new_view.set_syntax_file('Packages/HTML/HTML.tmLanguage')
            new_edit = new_view.begin_edit()
            new_view.insert(new_edit, 0, pygmented)
            new_view.end_edit(new_edit)
        else:
            sublime.error_message(u'Unsupported target "%s"' % target)
Exemplo n.º 3
0
    def __init__(self, config):
        gtk.VBox.__init__(self)
        
        self.set_spacing(8)
        self.set_border_width(8)
        
        self.config = config
        
        detected = desktop.get_desktop(True)
        if detected:
            commandline = ' '.join(desktop.get_command(detected, '')).strip()
            tmp = {
                'detected': detected,
                'commandline': commandline,
            }
            markup = _('The detected desktop environment is '
                '<b>"%(detected)s"</b>.\n'
                '<span face="Monospace">%(commandline)s</span> '
                'will be used to open links and files') % tmp
        else:
            markup = _('<b>No desktop environment detected.</b> '
                'The first browser found will be used to open links')
        
        self.infolabel = gtk.Label()
        self.infolabel.set_markup(markup)
        self.infolabel.set_line_wrap(True)
        self.infolabel.set_alignment(0.0, 0.0)
        
        self.hboxentry = gtk.HBox()
        self.entry = gtk.Entry()
        self.entry.connect('activate', self.save)
        self.hboxentry.pack_start(gtk.Label(_("Command line:")), False)
        self.hboxentry.pack_start(self.entry)
        
        self.override = gtk.CheckButton(_('Override detected settings'))
        self.override.set_active(self.config.glob['overrideDesktop'] != '')
        self.override.connect('toggled', self.toggleOverride)

        self.helplabel = gtk.Label()
        self.helplabel.set_markup(_("<i>Note:</i> %s is replaced by "
            "the actual url to be opened") % "%url%")
        self.helplabel.set_alignment(0.5, 1.0)
        
        self.hboxtest = gtk.HBox()
        self.testbutton = gtk.Button(_('Click to test'))
        self.testbutton.connect('clicked', self.testDesktop)
        self.hboxtest.pack_start(gtk.Label())
        self.hboxtest.pack_start(self.testbutton, False, True, 6)
        
        self.pack_start(self.infolabel, False)
        self.pack_start(self.override, False)
        self.pack_start(self.hboxentry, False)
        self.pack_start(self.hboxtest, False)
        self.pack_start(self.helplabel, False)
        
        self.toggleOverride()
Exemplo n.º 4
0
    def run(self, edit, target='external', output_type='html'):
        output_type = output_type if output_type in FORMATS else 'html'
        platform = desktop.get_desktop()

        # html clipboard output on windows should not be self-contained
        win = all([platform == 'Windows', output_type == 'html',
            target == 'clipboard'])
        full = False if win else settings.get('full', True)

        pygmented = self.highlight(output_type, full)

        if target == 'external':
            filename = '%s.%s' % (self.view.id(), output_type,)
            tmp_file = self.write_file(filename, pygmented)
            sublime.status_message(u'Written %s preview file: %s'
                                   % (output_type.upper(), tmp_file))
            if platform == 'Mac OS X':
                # for some reason desktop.open is broken under OSX Lion
                subprocess.call("open %s" % tmp_file, shell=True)
            else:
                desktop.open(tmp_file)
        elif target == 'clipboard':
            if platform == 'Mac OS X':
                # on mac osx we have `pbcopy` :)
                filename = '%s.%s' % (self.view.id(), output_type,)
                tmp_file = self.write_file(filename, pygmented)
                subprocess.call("cat %s | pbcopy -Prefer %s"
                                % (tmp_file, output_type,), shell=True)
                os.remove(tmp_file)
            elif platform == 'Windows':
                if self.view.line_endings != 'Windows':
                    pygmented = WIN_CR_RE.sub("\r\n", pygmented)
                    plaintext = WIN_CR_RE.sub("\r\n", self.code)
                else:
                    plaintext = self.code
                winclip.Paste(pygmented, output_type, plaintext)
            else:
                sublime.set_clipboard(pygmented)
        elif target == 'sublime':
            new_view = self.view.window().new_file()
            if output_type == 'html':
                new_view.set_syntax_file('Packages/HTML/HTML.tmLanguage')
            new_edit = new_view.begin_edit()
            new_view.insert(new_edit, 0, pygmented)
            new_view.end_edit(new_edit)
        else:
            sublime.error_message(u'Unsupported target "%s"' % target)
    def run(self, edit, target='external', output_type='html'):
        output_type = output_type if output_type in FORMATS else 'html'
        platform = desktop.get_desktop()

        # html clipboard output on windows should not be self-contained
        win = all([platform == 'Windows', output_type == 'html',
            target == 'clipboard'])
        full = False if win else settings_get('full', True)

        pygmented = self.highlight(output_type, full)

        if target == 'external':
            filename = '%s.%s' % (self.view.id(), output_type,)
            tmp_file = self.write_file(filename, pygmented)
            sublime.status_message('Written %s preview file: %s'
                                   % (output_type.upper(), tmp_file))
            if platform == 'Mac OS X':
                # for some reason desktop.open is broken under OSX Lion
                subprocess.call("open %s" % tmp_file, shell=True)
            else:
                desktop.open(tmp_file)
        elif target == 'clipboard':
            if platform == 'Mac OS X':
                filename = '%s.%s' % (self.view.id(), output_type,)
                tmp_file = self.write_file(filename, pygmented)

                # If copying RTF, we also need to copy as HTML for browser rich
                # text editors like Google Docs, email and other WYSIWYG editors
                if output_type == 'rtf':
                    # Create HTML & txt files
                    pyg_html = self.highlight('html', False)
                    html_filename = '%s.html' % self.view.id()
                    html_file = self.write_file(html_filename, pyg_html)

                    txt_filename = '%s.txt' % self.view.id()
                    txt_file = self.write_file(txt_filename, self.code)

                    # Add RTF, HTML and TXT to the clipboard
                    cwd = os.path.dirname(os.path.realpath(__file__))
                    script = os.path.join(cwd, 'html_rft_clipboard.scpt')
                    out = subprocess.call("osascript '%s' '%s' '%s' '%s'" % (
                        script,
                        txt_file,
                        html_file,
                        tmp_file), shell=True)

                    # Cleanup
                    os.remove(txt_file)
                    os.remove(html_file)
                    os.remove(tmp_file)

                # For html, use `pbcopy` :)
                else:
                    filename = '%s.%s' % (self.view.id(), output_type,)
                    tmp_file = self.write_file(filename, pygmented)
                    subprocess.call("cat %s | pbcopy -Prefer %s"
                                    % (tmp_file, output_type,), shell=True)
                    os.remove(tmp_file)
            elif platform == 'Windows':
                if self.view.line_endings != 'Windows':
                    pygmented = WIN_CR_RE.sub("\r\n", pygmented)
                    plaintext = WIN_CR_RE.sub("\r\n", self.code)
                else:
                    plaintext = self.code
                winclip.Paste(pygmented, output_type, plaintext)
            else:
                sublime.set_clipboard(pygmented)
        elif target == 'sublime':
            new_view = self.view.window().new_file()
            if output_type == 'html':
                new_view.set_syntax_file('Packages/HTML/HTML.tmLanguage')
            new_view.run_command("open_highlight", {'content': pygmented})
        else:
            sublime.error_message('Unsupported target "%s"' % target)
import os
import re
import sublime
import sublime_plugin
import subprocess
import tempfile

import sys
sys.path.append(os.path.dirname(__file__)+"/HighlightLib")

import desktop
import pygments.lexers
import pygments.formatters

if desktop.get_desktop() == 'Windows':
    from .HighlightLib import winclip

DEFAULT_STYLE = "default"
FORMATS = ('html', 'rtf',)
WIN_CR_RE = re.compile(r"\r(?!\n)|(?<!\r)\n")

def settings_get(name, default=None):
    plugin_settings = sublime.load_settings('SublimeHighlight.sublime-settings')
    return plugin_settings.get(name, default)

class OpenHighlightCommand(sublime_plugin.TextCommand):
    def run(self, edit, content):
        self.view.insert(edit, 0, content)

class SublimeHighlightCommand(sublime_plugin.TextCommand):
import desktop
import os
import pygments
import re
import sublime
import sublime_plugin
import subprocess
import tempfile

from pygments import highlight
from pygments.lexers import *
from pygments.formatters import *
from pygments.styles import STYLE_MAP

if desktop.get_desktop() == 'Windows':
    import winclip

# Don't judge me. Just don't. If you knew, you wouldn't.
__lexers = [
    '_asybuiltins', '_clbuiltins', '_lassobuiltins', '_luabuiltins',
    '_mapping', '_openedgebuiltins', '_phpbuiltins', '_postgres_builtins',
    '_scilab_builtins', '_sourcemodbuiltins', '_stan_builtins', '_vimbuiltins'
]
for l in __lexers:
    __import__('pygments.lexers.%s' % l)
for s in STYLE_MAP:
    __import__('pygments.styles.%s' % s)

DEFAULT_STYLE = "default"
FORMATS = (
Exemplo n.º 8
0
    def run(self, edit, target='external', output_type='html'):
        output_type = output_type if output_type in FORMATS else 'html'
        platform = desktop.get_desktop()

        # html clipboard output on windows should not be self-contained
        win = all([
            platform == 'Windows', output_type == 'html', target == 'clipboard'
        ])
        full = False if win else settings_get('full', True)

        pygmented = self.highlight(output_type, full)

        if target == 'external':
            filename = '%s.%s' % (
                self.view.id(),
                output_type,
            )
            tmp_file = self.write_file(filename, pygmented)
            sublime.status_message('Written %s preview file: %s' %
                                   (output_type.upper(), tmp_file))
            if platform == 'Mac OS X':
                # for some reason desktop.open is broken under OSX Lion
                subprocess.call("open %s" % tmp_file, shell=True)
            else:
                desktop.open(tmp_file)
        elif target == 'clipboard':
            if platform == 'Mac OS X':
                filename = '%s.%s' % (
                    self.view.id(),
                    output_type,
                )
                tmp_file = self.write_file(filename, pygmented)

                # If copying RTF, we also need to copy as HTML for browser rich
                # text editors like Google Docs, email and other WYSIWYG editors
                if output_type == 'rtf':
                    # Create HTML & txt files
                    pyg_html = self.highlight('html', False)
                    html_filename = '%s.html' % self.view.id()
                    html_file = self.write_file(html_filename, pyg_html)

                    txt_filename = '%s.txt' % self.view.id()
                    txt_file = self.write_file(txt_filename, self.code)

                    # Add RTF, HTML and TXT to the clipboard
                    cwd = os.path.dirname(os.path.realpath(__file__))
                    script = os.path.join(cwd, 'html_rft_clipboard.scpt')
                    out = subprocess.call(
                        "osascript '%s' '%s' '%s' '%s'" %
                        (script, txt_file, html_file, tmp_file),
                        shell=True)

                    # Cleanup
                    os.remove(txt_file)
                    os.remove(html_file)
                    os.remove(tmp_file)

                # For html, use `pbcopy` :)
                else:
                    filename = '%s.%s' % (
                        self.view.id(),
                        output_type,
                    )
                    tmp_file = self.write_file(filename, pygmented)
                    subprocess.call("cat %s | pbcopy -Prefer %s" % (
                        tmp_file,
                        output_type,
                    ),
                                    shell=True)
                    os.remove(tmp_file)
            elif platform == 'Windows':
                if self.view.line_endings != 'Windows':
                    pygmented = WIN_CR_RE.sub("\r\n", pygmented)
                    plaintext = WIN_CR_RE.sub("\r\n", self.code)
                else:
                    plaintext = self.code
                winclip.Paste(pygmented, output_type, plaintext)
            else:
                sublime.set_clipboard(pygmented)
        elif target == 'sublime':
            new_view = self.view.window().new_file()
            if output_type == 'html':
                new_view.set_syntax_file('Packages/HTML/HTML.tmLanguage')
            new_view.run_command("open_highlight", {'content': pygmented})
        else:
            sublime.error_message('Unsupported target "%s"' % target)