Posted By

birdspider on 05/21/10


Tagged

javascript beanshell jedit


Versions (?)

jEdit macro for .js compression with YUI compressor


 / Published in: Java
 

language: beanshell

be sure to adapt your YUICompressor path

  1. /*
  2.  * compress.bsh - a BeanShell macro script for the
  3.  * jEdit text editor - compresses js files and saves as <name>-compressed.js
  4.  *
  5.  * Copyright (C) 2010 Patrik Plihal
  6.  * patrik.plihal gmail com
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version 2
  11.  * of the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  */
  19.  
  20. /* Be sure to adapt 'myPathToYUICompressor' */
  21.  
  22. void compress(){
  23. if(buffer.isNewFile())
  24. buffer.saveAs(view, true);
  25. else
  26. buffer.save(view, buffer.getPath());
  27.  
  28. mode = buffer.getMode().getName();
  29. path = buffer.getPath();
  30. os = System.getProperty("os.name");
  31.  
  32. if(os.indexOf("Windows") != -1)
  33. path = "\"" + path + "\"";
  34.  
  35. if(mode.equals("javascript")) {
  36. ls = System.getProperty("line.separator");
  37.  
  38. newpath = buffer.getPath();
  39. newpath = newpath.replace(".js", "-compressed.js");
  40. myPathToYUICompressor = "\"E:\\lib\\yuicompressor-2.4.2.jar\"";
  41.  
  42. runInSystemShell(view,"java -jar " + myPathToYUICompressor + " " + path + " -o " + newpath);
  43.  
  44. /* I want my .js head comments also in my -compress.js */
  45. compressed_comment = "";
  46. for(int i=0;i<8;i++){ //ajust this for amount of comment lines
  47. compressed_comment = compressed_comment + buffer.getLineText(i) + ls;
  48. }
  49.  
  50. //wait for YUI Compressor
  51. waitForConsole(view);
  52.  
  53. StringBuilder contents = new StringBuilder();
  54. try {
  55. //read
  56. BufferedReader input = new BufferedReader(new FileReader(newpath));
  57. try {
  58. String line = null; //not declared within while loop
  59. while (( line = input.readLine()) != null){
  60. contents.append(line);
  61. }
  62. }
  63. finally {
  64. input.close();
  65. }
  66.  
  67. //modify (prepend)
  68. compressed = compressed_comment + ls + contents.toString();
  69.  
  70. //write
  71. BufferedWriter out = new BufferedWriter(new FileWriter(newpath));
  72. try {
  73. out.write(compressed);
  74. }
  75. finally {
  76. out.close();
  77. }
  78. }
  79. catch (IOException ex){
  80. ex.printStackTrace();
  81. }
  82. }
  83. else {
  84. Macros.error(view,
  85. "The current file does not appear to be a javascript.");
  86. }
  87. }
  88.  
  89. compress();

Report this snippet  

You need to login to post a comment.