Python implementation Sublime3 's less compile plug-in example

Source: Internet
Author: User
Use the interface provided by Http://tool.oschina.net/less to send requests for remote compilation.
The compiled less is then saved in a file with the same name as the suffix CSS.
The first time you use Python, the code is pieced together. You need to add a thread to make an asynchronous request, but not ...

Copy the Code code 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);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.