写一个Sublime Text 2插件(CSS文件里px单位替换成rem单位)
import sublime, sublime_plugin class ExampleCommand(sublime_plugin.TextCommand): def run(self, edit): self.view.insert(edit, 0, "Hello, World!")
view.run_command('example')
这个时候注意观察当前的文件开头插入了“Hello,World”,当前文件也变成待保存的状态,好了,测试成功,我们按ctrl+z,
让它回到开始的状态的吧。
等等,为什么是“example”?
它是从前面插件例子里
class ExampleCommand 来的,你需要换一个属于你自己插件的名字,把
ExampleCommand里的‘Example’改一下。这一改,然后有的人发现运行自己的插件名称运行不了了,原来这里的设置有个规则,你用驼峰命名,例如PxToRemCommand 你运行的时候需要是view.run_command(
‘
px_to_rem‘
),即用下划线隔开,否则像例子那样好了,开头大写后面全是小写。
第三步设置快捷键
[ { "command": "px_to_rem", "keys": [ "ctrl+alt+k" ] } ]
view.run_command(
‘
px_to_rem‘
),你可以改成不会冲突的别的按键,试试看看生效了没。
第四步就是编写程序
1、从设置文件里读取信息,之前同事写的是json格式的,很熟悉呀
{ "files": ["./test/style.css"], "root_value": 20, "unit_precision": 5, "prop_white_list": ["width", "height", "padding", "padding-top", "padding-right", "padding-bottom", "padding-left", "margin", "margin-top", "margin-right", "margin-bottom", "margin-left"], "replace": false, "media_query": false }保存。然后我们到之前我们建的插件文件text.py里去试试看能不能获取到我们的设置信息。
import sublime, sublime_plugin SETTINGS_FILE = "px_to_rem.sublime-settings" class PxToRemCommand(sublime_plugin.TextCommand): def run(self, edit): #load config settings = sublime.load_settings(SETTINGS_FILE) #file files = settings.get("files"); print files
我们先用
sublime.load_settings()把设置文件引入,然后用settings.get()去取我们想要的属性。最后用print
打印到控制台。然后我们用crtl+`打开控制台,然后我们运行我们的插件ctrl+alt+k,看看打印出我们想要的东西没。<pre name="code" class="python">prop_white_list = settings.get("prop_white_list",[])
for text in prop_white_list: matches[text] = self.view.find_all(text+r":(\s*(auto)?([+-]?\d*\.?\d+(px)?)?)+")
for text in prop_white_list: for i in matches[text]: reStr = self.view.substr(i) replaced = re.sub("(?P<number>([+-]?)\d*\.?\d+)",replaceToRem,reStr) replaced = str(replaced).replace("px","rem") #self.view.replace(edit, i, replaced) print replaced self.view.insert(edit, i.end(), ";"+replaced)
其中的函数replaceToRem 如下:
def replaceToRem(replaceString): # intStr = replaceString.group("number") intValue = float(intStr) # newValue = intValue / root_value # newValue = round(newValue,unit_precision) newValueStr = str(newValue) # newValueStr = re.sub("\.[0]+$","",newValueStr) return newValueStr
import sublime, sublime_plugin, re import math SETTINGS_FILE = "px_to_rem.sublime-settings" class PxToRemCommand(sublime_plugin.TextCommand): def run(self, edit): #load config settings = sublime.load_settings(SETTINGS_FILE) #file files = settings.get("files"); #get rootValue (int) root_value = int(settings.get("root_value",20)) # toFixed the value (int) unit_precision = int(settings.get("unit_precision",5)) #replace prop list (list) prop_white_list = settings.get("prop_white_list",[]) # relpace or append to end replace = settings.get("replace",True) # media_query = settings.get("media_query",True) #self.view.insert(edit, 0, root_value) #content = self.view.substr(sublime.Region(0,self.view.size()-1)) # matches ={} def replaceToRem(replaceString): # intStr = replaceString.group("number") intValue = float(intStr) # newValue = intValue / root_value # newValue = round(newValue,unit_precision) newValueStr = str(newValue) # newValueStr = re.sub("\.[0]+$","",newValueStr) return newValueStr for text in prop_white_list: matches[text] = self.view.find_all(text+r":(\s*(auto)?([+-]?\d*\.?\d+(px)?)?)+") for text in prop_white_list: for i in matches[text]: reStr = self.view.substr(i) replaced = re.sub("(?P<number>([+-]?)\d*\.?\d+)",replaceToRem,reStr) replaced = str(replaced).replace("px","rem") #self.view.replace(edit, i, replaced) print replaced self.view.insert(edit, i.end(), ";"+replaced) print matches #self.view.insert(edit, 0, matches)
px_to_rem.sublime-settings文件
{ "files": ["./test/style.css"], "root_value": 20, "unit_precision": 5, "prop_white_list": ["width", "height", "padding", "padding-top", "padding-right", "padding-bottom", "padding-left", "margin", "margin-top", "margin-right", "margin-bottom", "margin-left"], "replace": false, "media_query": false }
Default (Windows).sublime-keymap 文件
[ { "command": "px_to_rem", "keys": [ "ctrl+alt+k" ] } ]Main.sublime-menu 文件
[ { "id": "view", "children": [ { "caption": "pxToRem", "id": "px-to-rem", "command": "px_to_rem" } ] } ]
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。