Sublime Text 3 - 清洁粘贴

时间:2013-08-17 12:20:23

标签: python sublimetext3

Sublime Text 3看起来很棒,但是阻止我切换的一个项目是Clipboard Commands的兼容性。我唯一使用这个插件的是“clean_paste”函数,它基本上粘贴了从Microsoft Word(或任何其他文本编辑器)复制的内容,剥离了它通常带来的有趣角色。有没有人知道ST3提供的本机功能,我可以将键绑定映射到?这是ClipboardCommand所做的(在ST2版本中):

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

一般来说可能更多的是Python问题,但你也可以创建自己的键绑定,这个基本上只引用该命令:

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

所以,如果command是我可以把这个函数放进去的东西,那就太棒了,但不确定它在Python中是如何工作的。谢谢你的帮助!

1 个答案:

答案 0 :(得分:4)

没有太多工作要使这个python 3兼容:

# 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;

我已经分叉了链接插件并上传了更改here

在sublime3中测试它似乎可以工作,但没有测试用例我会把那个留给你。

相关问题