This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
SublimeHighlight.py
190 lines (165 loc) · 7.16 KB
/
SublimeHighlight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
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):
"""Code highlighter command."""
@property
def code(self):
return self.view.substr(self.region)
@property
def encoding(self):
encoding = self.view.encoding()
if encoding == 'Undefined':
encoding = 'utf-8'
elif encoding == 'Western (Windows 1252)':
encoding = 'windows-1252'
return encoding
@property
def region(self):
regions = self.view.sel()
if len(regions) > 0 and regions[0].size() > 0:
return regions[0]
else:
return sublime.Region(0, self.view.size())
def guess_lexer_from_syntax(self):
syntax = self.view.settings().get('syntax')
if not syntax:
return
match = re.match(r"Packages/.*/(.*?)\.(?:tmLanguage|sublime-syntax)$", syntax)
if not match:
return
try:
return pygments.lexers.get_lexer_by_name(match.group(1).lower())
except pygments.util.ClassNotFound:
return
def get_formatter(self, output_type, full=True):
return pygments.formatters.get_formatter_by_name(output_type,
linenos=settings_get('linenos', False),
noclasses=settings_get('noclasses', False),
style=settings_get('theme', DEFAULT_STYLE),
full=settings_get('full', full),
fontface=settings_get('fontface', ''))
def get_lexer(self, code=None):
code = code if code is not None else self.code
lexer = None
if self.view.file_name():
try:
lexer = pygments.lexers.get_lexer_for_filename(
self.view.file_name(), code)
except pygments.util.ClassNotFound:
pass
if not lexer:
lexer = self.guess_lexer_from_syntax()
if not lexer:
lexer = pygments.lexers.guess_lexer(code)
try:
options = settings_get('lexer_options', {}).get(lexer.name)
except AttributeError:
return lexer
if not options:
return lexer
# handle lexer options
for option, value in options.items():
try:
setattr(lexer, option, value)
except AttributeError:
sublime.error_message('Highlight: unsupported %s lexer option: "%s"'
% (lexer.name, option))
return lexer
def highlight(self, output_type, full=True):
return pygments.highlight(self.code, self.get_lexer(),
self.get_formatter(output_type, full))
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)
def write_file(self, filename, contents, encoding=None):
"""Writes highlighted contents onto the filesystem."""
encoding = encoding if encoding is not None else self.encoding
tmp_fullpath = os.path.join(tempfile.gettempdir(), filename)
tmp_file = open(tmp_fullpath, 'wb')
tmp_file.write(contents.encode(encoding))
tmp_file.close()
return tmp_fullpath