Monday, January 7, 2013

Sublime Text - Evil MS Word Char Replace Plugin

Tired of all those "smart quotes" and other proprietary special chars you get from office docs you have to publish? I for one don't like having to weed out all the black diamonds with question marks that sometimes show in WordPress and other CMS frameworks when you copy and paste content writers have given you from Microsoft Word documents so I wrote a plugin for Sublime Text to do a mass search and replace. Feel free to set it to your own macro and add to the list of evil chars, I've left the macro code absent for brevity's sake, read the docs or checkout a currently installed plugin with macros to get the syntax.


[sourcecode language="python"]
# -*- coding: utf-8 -*-
import sublime, sublime_plugin


class NormalizeUnicodePunctuationCommand(sublime_plugin.TextCommand):

def run(self, edit):
for region in self.view.sel():
content = self.view.substr(region)

content = content.replace(u'“', '"')
content = content.replace(u'”', '"')
content = content.replace(u'“', '"')
content = content.replace(u'‹', '"')
content = content.replace(u'›', '"')
content = content.replace(u'«', '"')
content = content.replace(u'»', '"')
content = content.replace(u'»', '"')
content = content.replace(u'‘', "'")
content = content.replace(u'’', "'")
content = content.replace(u'–', '-')
content = content.replace(u'…', '...')

self.view.replace(edit, region, content)
[/sourcecode]

No comments:

Post a Comment