Created
May 3, 2014 14:30
-
-
Save unknownuser88/d174820e8547536f2287 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sublime, sublime_plugin, webbrowser | |
global SETTINGS | |
global FIDDLE_API | |
def initializeGlobals(): | |
global SETTINGS | |
global FIDDLE_API | |
SETTINGS = sublime.load_settings('fiddlepusher.sublime-settings') | |
FIDDLE_API = "http://jsfiddle.net/api/post/" + SETTINGS.get('defaultFramework') + "/" + SETTINGS.get('defaultFrameworkVersion') + "/dependencies/more/" | |
def submitForm(args): | |
tmpFileHtml = \ | |
""" | |
<h2 style=\"text-align: center;\">Sending to JsFiddle...</h2> | |
<form id='form' style=\"display: none;\" action='{0}' method='POST' />{1}</form> | |
<script>document.getElementById("form").submit();</script> | |
""" | |
inputField = "<input type='hidden' name='{0}' value='{1}' />" | |
textareaField = "<textarea name='{0}'>{1}</textarea>" | |
formContent = "" | |
for key, value in args.items(): | |
if key == 'html' or key == 'css' or key == 'js' : | |
formContent += textareaField.format(key, value) | |
else: | |
formContent += inputField.format(key, value) | |
with open('temp_file.html', 'w') as file: | |
file.write(tmpFileHtml.format( FIDDLE_API, formContent)) | |
file.close() | |
webbrowser.open(os.path.abspath(file.name)) | |
class fiddlePushCommand(sublime_plugin.TextCommand): | |
def run(self, edit, to): | |
initializeGlobals() | |
pasteTitle = self.view.file_name() | |
if pasteTitle is not None: | |
pasteTitle = os.path.basename(pasteTitle) | |
else: | |
pasteTitle = 'Untitled' | |
for region in self.getSelection(): | |
content = self.view.substr(region) | |
args = { | |
to: content, | |
'title': pasteTitle, | |
'description': 'Created by SendToJsfiddle Sublime Text Plugin', | |
'dtd': 'html 5', | |
'wrap': 'l' | |
} | |
submitForm(args); | |
def getSelection(self): | |
selections = self.view.sel() | |
has_selections = False | |
for sel in selections: | |
if sel.empty() == False: | |
has_selections = True | |
if not has_selections: | |
full_region = sublime.Region(0, self.view.size()) | |
selections.add(full_region) | |
return selections |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment