Source

Pypaste / contrib / Friendpaste.tmbundle / Commands / Insert Paste from clipoard.tmCommand

The default branch has multiple heads

Full commit
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>beforeRunningCommand</key>
	<string>nop</string>
	<key>command</key>
	<string>#!/usr/bin/env python
# -*- coding: utf-8 -
# Copyright 2008 by Benoît Chesneau &lt;benoit@e-engura.com&gt;
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from httplib import HTTPConnection
import os
import re
import urllib

# if we have python2.6 installed use its json extension
try:
    import json 
except ImportError:
    import simplejson as json

def utf8(text):
    if isinstance(text, unicode):
        return text.encode('utf-8')
    elif isinstance(text, str):
        return text
    else:
        return str(text)

def get_paste(server, uri):
    if uri.endswith("\n"):
        uri = uri[:-1]
    http = HTTPConnection(server)
    http.request("GET", str(uri), headers={ "Accept": "application/json" })
    resp = http.getresponse()
    data = resp.read()
    ret = json.loads(data)
    http.close()
    return ret.get('snippet', None)
    
def insert():
    if "FRIENDPASTE_SERVER" in os.environ:
        server = os.environ["FRIENDPASTE_SERVER"]
    else:
        server = "friendpaste.com"
        
    re_url = re.compile(r"http:\/\/([^.]*\.|)%s\/([^\/].*)" % server)
    #re_changeset = re.compile(r"http:\/\/%s\/([^\/].*)\/changeset\?rev=(.*)" % server)
        
    url = os.popen("/usr/bin/pbpaste").read()
    if url and not url.startswith("http://"):
        print "No valid friendpaste url in your clipboard."
        return 1
        
    paste = ""
    if re_url.match(url):
        paste = get_paste(server, url)
        if paste is None:
            print "Paste don't exist or an error happend."
            return 1
    
    print utf8(paste)
    return 0
            
insert()</string>
	<key>input</key>
	<string>none</string>
	<key>keyEquivalent</key>
	<string>~@S</string>
	<key>name</key>
	<string>Insert Paste from clipoard</string>
	<key>output</key>
	<string>insertAsSnippet</string>
	<key>uuid</key>
	<string>CE88B5CA-C0B9-48EE-870B-E6F94C40AF03</string>
</dict>
</plist>