5
votes

Sublime Text 3 is looking great, but one item that is keeping me from switching is the compatibility of Clipboard Commands. The only thing I use this plugin for is the "clean_paste" function which basically makes pasting copied content from Microsoft Word (or any other text editor) strip out the funny characters it normally comes with. Does anyone know of a native function that ST3 provides that I can map a keybinding to? Here is what ClipboardCommand does (in the ST2 version):

class ClipboardCommandsPastePlainText(sublime_plugin.TextCommand):
    def run(self, edit):
        copy(clean_paste(clipboard()))
        self.view.run_command('paste')

Possibly more of a Python question in general as well, but you can also create your own keybindings and this one basically just references that command:

"caption": "Clipboard: Paste Plain Text",
"command": "clipboard_commands_paste_plain_text"

So if the command is something I could just put that function into that'd be great, but not sure how that works in Python. Thanks for any help!

1
All you have to do is fix clean_copy and clean_paste. - Blender
I'm not sure I understand the question - Eric
To clarify, I'd love to port over this ST2 plugin to ST3, but not being a Python dev, I was wondering if someone knew the best way to map a new key binding in Sublime (which I know how to do), but reference that function that does the clean_paste into it instead of referencing a Class definition as it is does originally. Or, if this makes the question clearer, a how I could perform a clean paste from Miscrosoft Word into ST3 (so I can get rid of all the "special/encoded characters" - Zach
All commands must reference a class name. Sorry. That's a pretty small plugin - it should be pretty straightforward to port - Eric
Your understanding of SublimeText commands is lacking. To make this plugin work in SublimeText3, you'd need to port the clean_paste function to python3, which could probably be done with 2to3 as it doesn't seem complicated - the class itself is fine. But I also don't see a valid question here: if it's "can someone port this to ST3" this would be off topic, ask on the ST forums. A better question would be "can you help me port this to ST3", but this would require you to try something first. Right now it just reads like you just want someone to do work for you for a few internet points... - l4mpi

1 Answers

4
votes

Not too much work to make this python 3 compatible:

# coding=utf8
import sublime_plugin, sublime, re, html

def clipboard():
    return sublime.get_clipboard()

def copy(data):
    sublime.set_clipboard(data)

# to transfer data to sublime text
def clean_paste(data):
    # clean word
    data = str(data)
    data = data.replace(u'”', '"').replace(u'“', '"').replace(u'’', "'")
    data = data.replace('________________________________________', '\n')
    # clean htmlentities
    data = re.sub('&([^;]+);', lambda m: unichr(html.entities.name2codepoint[m.group(1)]), data)
    return data;

# to transfer data from sublime text
def clean_copy(data):
    # clean html
    data = str(data)
    data = re.sub(r'<br ?/?>', '\n', data, re.I);
    data = re.sub(r'<[^>]*>', '', data);
    # clean htmlentities
    data = re.sub('&([^;]+);', lambda m: unichr(html.entities.name2codepoint[m.group(1)]), data)
    return data;

I've forked the linked plugin and uploaded the changes here

Tested it in sublime3 and it appears to work, but without test cases I'll leave that one to you.