/* * compress.bsh - a BeanShell macro script for the * jEdit text editor - compresses js files and saves as -compressed.js * * Author: CNSKnight * Contributor: 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. * * You should have received a copy of the GNU General Public License * along with the jEdit program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ /* 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();