-
Notifications
You must be signed in to change notification settings - Fork 7
/
Carbon.py
executable file
·198 lines (173 loc) · 5.29 KB
/
Carbon.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
191
192
193
194
195
196
197
198
import os
from urllib.parse import urlencode
import webbrowser
import sublime
import sublime_plugin
SETTINGS_FILE = 'Carbon.sublime-settings'
CODE_MAX_LENGTH = 3400
# Carbon language mapping
LANGUAGE_MAPPING = {
'Auto': 'auto',
'Apache': 'text/apache',
'Shell-Unix-Generic': 'application/x-sh',
'Plain text': 'text',
'C': 'text/x-csrc',
'C++': 'text/x-c++src',
'C#': 'text/x-csharp',
'Clojure': 'clojure',
'Cobol': 'cobol',
'CoffeeScript': 'coffeescript',
'Crystal': 'crystal',
'CSS': 'css',
'D': 'd',
'Dart': 'dart',
'Diff': 'text/x-diff',
'Django': 'django',
'Docker': 'dockerfile',
'Elixir': 'elixir',
'Elm': 'elm',
'Erlang': 'erlang',
'Fortran': 'fortran',
'F#': 'mllike',
'OCaml': 'mllike',
'GraphQL': 'graphql',
'Go': 'go',
'Groovy': 'groovy',
'Handlebars': 'handlebars',
'Haskell': 'haskell',
'Haxe': 'haxe',
'HTML': 'htmlmixed',
'Java': 'text/x-java',
'JavaScript': 'javascript',
'JavaScript (Babel)': 'javascript',
'JavaScriptNext': 'javascript',
'JSON': 'application/json',
'JSON (Sublime)': 'application/json',
'JSX': 'jsx',
'Julia': 'julia',
'Kotlin': 'text/x-kotlin',
'Lisp': 'commonlisp',
'Lua': 'lua',
'Markdown': 'markdown',
'Mathematica': 'mathematica',
'MySQL': 'text/x-mysql',
'NGINX': 'nginx',
'Nim': 'nimrod',
'Objective-C': 'text/x-objectivec',
'Pascal': 'pascal',
'Perl': 'perl',
'PHP': 'text/x-php',
'PowerShell': 'powershell',
'Python': 'python',
'R': 'r',
'Ruby': 'ruby',
'Rust': 'rust',
'Sass': 'sass',
'Scala': 'text/x-scala',
'Smalltalk': 'smalltalk',
'SQL': 'sql',
'Swift': 'swift',
'TCL': 'tcl',
'TypeScript': 'application/typescript',
'VB.NET': 'vb',
'Verilog': 'verilog',
'VHDL': 'vhdl',
'Vue': 'vue',
'XML': 'xml',
'YAML': 'yaml',
}
# Url parameters
PARAMS_SHORTHAND = {
'backgroundColor': 'bg',
'theme': 't',
'windowTheme': 'wt',
'language': 'l',
'dropShadow': 'ds',
'dropShadowOffsetY': 'dsyoff',
'dropShadowBlurRadius': 'dsblur',
'windowControls': 'wc',
'widthAdjustment': 'wa',
'paddingVertical': 'pv',
'paddingHorizontal': 'ph',
'lineNumbers': 'ln',
'firstLineNumber': 'fl',
'fontFamily': 'fm',
'fontSize': 'fs',
'lineHeight': 'lh',
'squaredImage': 'si',
'exportSize': 'es',
'watermark': 'wm',
}
def convert_settings_to_params(settings):
default = settings.get('default')
query = {}
for key in default:
try:
paramName = PARAMS_SHORTHAND[key]
except KeyError:
continue
value = str(default.get(key))
if value == 'False' or value == 'True':
query[paramName] = value.lower()
else:
query[paramName] = value
return query
def convert_tabs_using_tab_size(view, string):
tab_size = view.settings().get("tab_size")
if tab_size:
return string.replace("\t", " " * tab_size)
return string.replace("\t", " ")
def get_whitespace_from_line_beginning(view, region):
n_space = len(view.substr(view.line(region.begin()))) - len(
view.substr(view.line(region.begin())).lstrip()
)
return " " * n_space
def show_status_message(settings, window, emoji, message):
if settings.get('show_status_messages'):
if settings.get('use_emojis_in_status_messages'):
message = emoji + ' ' + message
window.status_message(message)
class CarbonCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
settings = sublime.load_settings(SETTINGS_FILE)
code = self.normalize_code(settings)
if not code:
return
self.generate_carbon_link(code, settings)
def normalize_code(self, settings):
view = self.view
indent_size = 0
if len(view.sel()) and not view.sel()[0].empty():
region = view.sel()[0]
if settings.get("trim_indent"):
indent_size = len(get_whitespace_from_line_beginning(view, region))
else:
# no text selected, so consider the whole view
region = sublime.Region(0, view.size())
body = view.substr(region)
if len(body) > CODE_MAX_LENGTH:
show_status_message(
settings,
self.view.window(),
'❌',
'Carbon: The selected text is too big.'
)
return None
body = '\n'.join(x[indent_size:].rstrip() for x in body.splitlines())
body = convert_tabs_using_tab_size(view, body)
return body
def generate_carbon_link(self, code, settings):
view = self.view
query = convert_settings_to_params(settings)
query['code'] = code
# get current view syntax
syntax = os.path.splitext(os.path.basename(view.settings().get("syntax")))[0]
# set language from the mapping
if syntax in LANGUAGE_MAPPING:
language = LANGUAGE_MAPPING[syntax]
else:
language = 'auto'
query['l'] = language
base_url = 'https://carbon.now.sh/?'
webbrowser.open(base_url + urlencode(query))
show_status_message(settings, self.view.window(), '✔️', 'Carbon opened in your default browser.')