Return to Snippet

Revision: 27083
at May 21, 2010 11:01 by birdspider


Initial Code
/*
 * compress.bsh - a BeanShell macro script for the
 * jEdit text editor - compresses js files and saves as <name>-compressed.js
 *
 * Copyright (C) 2010 Patrik Plihal
 * patrik.plihal gmail com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

 /* Be sure to adapt 'myPathToYUICompressor' */

void compress(){
	if(buffer.isNewFile())
		buffer.saveAs(view, true);
	else
		buffer.save(view, buffer.getPath());
	
	mode = buffer.getMode().getName();
	path = buffer.getPath();
	os = System.getProperty("os.name");
	
	if(os.indexOf("Windows") != -1)
		path = "\"" + path + "\"";
	
	if(mode.equals("javascript"))  {
		ls = System.getProperty("line.separator");
		
		newpath = buffer.getPath();
		newpath = newpath.replace(".js", "-compressed.js");
		myPathToYUICompressor = "\"E:\\lib\\yuicompressor-2.4.2.jar\"";
		
		runInSystemShell(view,"java -jar " + myPathToYUICompressor + " " + path + " -o " + newpath);
		
		/* I want my .js head comments also in my -compress.js */	
		compressed_comment = "";
		for(int i=0;i<8;i++){ //ajust this for amount of comment lines
			compressed_comment = compressed_comment + buffer.getLineText(i) + ls;
		}
		
		//wait for YUI Compressor
		waitForConsole(view); 	
		
		StringBuilder contents = new StringBuilder();
		try {
			//read
			BufferedReader input =  new BufferedReader(new FileReader(newpath));
			try {
				String line = null; //not declared within while loop
				while (( line = input.readLine()) != null){
					contents.append(line);
				}
			}
			finally {
				input.close();
			}
			
			//modify (prepend)
			compressed = compressed_comment + ls  + contents.toString();
			
			//write
			BufferedWriter out = new BufferedWriter(new FileWriter(newpath));
			try {
				out.write(compressed);
			}
			finally {
				out.close();
			}			
		}
		catch (IOException ex){
			ex.printStackTrace();
		}
	}
	else {
		Macros.error(view,
			"The current file does not appear to be a javascript.");
	}
}

compress();

Initial URL

                                

Initial Description
*language: beanshell*

be sure to adapt your YUICompressor path

Initial Title
jEdit macro for .js compression with YUI compressor

Initial Tags
javascript

Initial Language
Java