This article mainly introduces the python sublime3 less compilation plug-in example of the relevant information using the interface provided by the http://tool.oschina.net/less, send requests for remote compilation.
Save the compiled less file as a file with the same name and suffix css.
When python is used for the first time, the code is also pooled. the thread needs to be added for asynchronous requests, but not...
The code is as follows:
Import sublime, sublime_plugin
Import urllib
Import json
Class exampleCommand (sublime_plugin.TextCommand ):
Def run (self, edit ):
File_name = self. view. file_name ();
If file_name.find ('. less') =-1:
Print ('only. less file can compile to css !! ');
Return;
File_object_from = open (file_name );
All_the_text = file_object_from.read ();
Url = "http://tool.oschina.net/action/less/less_compile ";
Data = all_the_text.encode (encoding = 'utf8 ');
Headers = {'user-Agent': 'Sublime _ plugin '};
Req = urllib. request. Request (url, data, headers );
Response = urllib. request. urlopen (req );
The_page = response. read ();
Css = json. loads (the_page.decode ("utf8") ['css '];
File_object_to = open (self. view. file_name (). replace ('. less', '.css '), 'w ')
File_object_to.write (css );
File_object_from.close ();
File_object_to.close ();
Print (css );