Skip to content

Commit

Permalink
Merge pull request #113 from shapov/feature/embedable-scripts
Browse files Browse the repository at this point in the history
Insert gist as embeddable script
  • Loading branch information
condemil committed Mar 29, 2014
2 parents c15fd51 + 9506fed commit c16f861
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Gist.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"caption": "Gist: Insert Gist",
"command": "insert_gist_list"
},
{
"caption": "Gist: Embed as script",
"command": "insert_gist_embed_list"
},
{
"caption": "Gist: Add File to Gist",
"command": "gist_add_file"
Expand Down
1 change: 1 addition & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
{ "command": "gist_private", "caption": "Create Private Gist…" },
{ "command": "gist_list", "caption": "Open Gist…" },
{ "command": "insert_gist_list", "caption": "Insert Gist…" },
{ "command": "insert_gist_embed_list", "caption": "Embed Gist as script" },
{ "command": "gist_add_file", "caption": "Add File to Gist…" },
{ "caption": "-" },
{ "command": "gist_copy_url", "caption": "Copy Gist URL" },
Expand Down
27 changes: 27 additions & 0 deletions gist.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,25 @@ def insert_gist(gist_url):

view.end_edit(edit)

def insert_gist_embed(gist_url):
gist = api_request(gist_url)
files = sorted(gist['files'].keys())

for gist_filename in files:
view = sublime.active_window().active_view()

template = '<script src="{0}"></script>'.format(gist['files'][gist_filename]['raw_url'])
if PY3:
view.run_command('insert', {
'characters': template,
})
else:
edit = view.begin_edit()

for region in view.sel():
view.replace(edit, region, template)

view.end_edit(edit)

def get_gists(url):
return api_request(url)
Expand Down Expand Up @@ -655,6 +674,14 @@ def handle_gist(self, gist):
def get_window(self):
return self.window

class InsertGistEmbedListCommand(GistListCommandBase, sublime_plugin.WindowCommand):
@catch_errors
def handle_gist(self, gist):
insert_gist_embed(gist['url'])

def get_window(self):
return self.window


class GistAddFileCommand(GistListCommandBase, sublime_plugin.TextCommand):
def is_enabled(self):
Expand Down

0 comments on commit c16f861

Please sign in to comment.