/tags/jsdoc_toolkit-1.3.3/plugins/min.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 316 lines · 230 code · 25 blank · 61 comment · 36 complexity · f016290606c0b5274fd3c5ddf0246328 MD5 · raw file

  1. /**
  2. @overview Remove unnecessary characters from source code and save file.
  3. Uses Franck Marcia's adaptation of Douglas Crockford's JsMin tool.
  4. @author Michael Mathews micmath@gmail.com
  5. */
  6. function deploy_begin(context) {
  7. context.src = context.d+"/src";
  8. MakeDir(context.src);
  9. }
  10. function deploy_each(sourceFile, context) {
  11. var name = sourceFile.fileName.replace(/(\.\.?)?[\/\\]/g, "_");
  12. inform("Saving minimized source file to "+name);
  13. var mini = jsmin(sourceFile.content);
  14. SaveFile(context.src, name, mini);
  15. }
  16. function deploy_finish(context) {
  17. }
  18. /*
  19. jsmin.js - 2006-08-31
  20. Author: Franck Marcia
  21. This work is an adaptation of jsminc.c published by Douglas Crockford.
  22. and copyright (c) 2002 Douglas Crockford (www.crockford.com)
  23. Permission is hereby granted to use the Javascript version under the same
  24. conditions as the jsmin.c on which it is based.
  25. Permission is hereby granted, free of charge, to any person obtaining a copy of
  26. this software and associated documentation files (the "Software"), to deal in
  27. the Software without restriction, including without limitation the rights to
  28. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  29. of the Software, and to permit persons to whom the Software is furnished to do
  30. so, subject to the following conditions:
  31. The above copyright notice and this permission notice shall be included in all
  32. copies or substantial portions of the Software.
  33. The Software shall be used for Good, not Evil.
  34. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  40. SOFTWARE.
  41. */
  42. String.prototype.has = function(c) {
  43. return this.indexOf(c) > -1;
  44. };
  45. /*
  46. level:
  47. 1: minimal, keep linefeeds if single
  48. 2: normal, the standard algorithm
  49. 3: agressive, remove any linefeed and doesn't take care of potential
  50. missing semicolons (can be regressive)
  51. */
  52. function jsmin(input, level) {
  53. if (level === undefined || level < 1 || level > 3) {
  54. level = 2;
  55. }
  56. var a = '',
  57. b = '',
  58. EOF = -1,
  59. LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
  60. DIGITS = '0123456789',
  61. ALNUM = LETTERS + DIGITS + '_$\\',
  62. theLookahead = EOF;
  63. /* isAlphanum -- return true if the character is a letter, digit, underscore,
  64. dollar sign, or non-ASCII character.
  65. */
  66. function isAlphanum(c) {
  67. return c != EOF && (ALNUM.has(c) || c.charCodeAt(0) > 126);
  68. }
  69. /* get -- return the next character. Watch out for lookahead. If the
  70. character is a control character, translate it to a space or
  71. linefeed.
  72. */
  73. function get() {
  74. var c = theLookahead;
  75. if (get.i == get.l) {
  76. return EOF;
  77. }
  78. theLookahead = EOF;
  79. if (c == EOF) {
  80. c = input.charAt(get.i);
  81. ++get.i;
  82. }
  83. if (c >= ' ' || c == '\n') {
  84. return c;
  85. }
  86. if (c == '\r') {
  87. return '\n';
  88. }
  89. return ' ';
  90. }
  91. get.i = 0;
  92. get.l = input.length;
  93. /* peek -- get the next character without getting it.
  94. */
  95. function peek() {
  96. theLookahead = get();
  97. return theLookahead;
  98. }
  99. /* next -- get the next character, excluding comments. peek() is used to see
  100. if a '/' is followed by a '/' or '*'.
  101. */
  102. function next() {
  103. var c = get();
  104. if (c == '/') {
  105. switch (peek()) {
  106. case '/':
  107. for (;;) {
  108. c = get();
  109. if (c <= '\n') {
  110. return c;
  111. }
  112. }
  113. break;
  114. case '*':
  115. get();
  116. for (;;) {
  117. switch (get()) {
  118. case '*':
  119. if (peek() == '/') {
  120. get();
  121. return ' ';
  122. }
  123. break;
  124. case EOF:
  125. throw 'Error: Unterminated comment.';
  126. }
  127. }
  128. break;
  129. default:
  130. return c;
  131. }
  132. }
  133. return c;
  134. }
  135. /* action -- do something! What you do is determined by the argument:
  136. 1 Output A. Copy B to A. Get the next B.
  137. 2 Copy B to A. Get the next B. (Delete A).
  138. 3 Get the next B. (Delete B).
  139. action treats a string as a single character. Wow!
  140. action recognizes a regular expression if it is preceded by ( or , or =.
  141. */
  142. function action(d) {
  143. var r = [];
  144. if (d == 1) {
  145. r.push(a);
  146. }
  147. if (d < 3) {
  148. a = b;
  149. if (a == '\'' || a == '"') {
  150. for (;;) {
  151. r.push(a);
  152. a = get();
  153. if (a == b) {
  154. break;
  155. }
  156. if (a <= '\n') {
  157. throw 'Error: unterminated string literal: ' + a;
  158. }
  159. if (a == '\\') {
  160. r.push(a);
  161. a = get();
  162. }
  163. }
  164. }
  165. }
  166. b = next();
  167. if (b == '/' && '(,=:[!&|'.has(a)) {
  168. r.push(a);
  169. r.push(b);
  170. for (;;) {
  171. a = get();
  172. if (a == '/') {
  173. break;
  174. } else if (a =='\\') {
  175. r.push(a);
  176. a = get();
  177. } else if (a <= '\n') {
  178. throw 'Error: unterminated Regular Expression literal';
  179. }
  180. r.push(a);
  181. }
  182. b = next();
  183. }
  184. return r.join('');
  185. }
  186. /* m -- Copy the input to the output, deleting the characters which are
  187. insignificant to JavaScript. Comments will be removed. Tabs will be
  188. replaced with spaces. Carriage returns will be replaced with
  189. linefeeds.
  190. Most spaces and linefeeds will be removed.
  191. */
  192. function m() {
  193. var r = [];
  194. // ignore UTF BOM characters
  195. do {
  196. a = next();
  197. } while (a.charCodeAt(0) > 255);
  198. r.push(action(3));
  199. while (a != EOF) {
  200. switch (a) {
  201. case ' ':
  202. if (isAlphanum(b)) {
  203. r.push(action(1));
  204. } else {
  205. r.push(action(2));
  206. }
  207. break;
  208. case '\n':
  209. switch (b) {
  210. case '{':
  211. case '[':
  212. case '(':
  213. case '+':
  214. case '-':
  215. r.push(action(1));
  216. break;
  217. case ' ':
  218. r.push(action(3));
  219. break;
  220. default:
  221. if (isAlphanum(b)) {
  222. r.push(action(1));
  223. } else {
  224. if (level == 1 && b != '\n') {
  225. r.push(action(1));
  226. } else {
  227. r.push(action(2));
  228. }
  229. }
  230. }
  231. break;
  232. default:
  233. switch (b) {
  234. case ' ':
  235. if (isAlphanum(a)) {
  236. r.push(action(1));
  237. break;
  238. }
  239. r.push(action(3));
  240. break;
  241. case '\n':
  242. if (level == 1 && a != '\n') {
  243. r.push(action(1));
  244. } else {
  245. switch (a) {
  246. case '}':
  247. case ']':
  248. case ')':
  249. case '+':
  250. case '-':
  251. case '"':
  252. case '\'':
  253. if (level == 3) {
  254. r.push(action(3));
  255. } else {
  256. r.push(action(1));
  257. }
  258. break;
  259. default:
  260. if (isAlphanum(a)) {
  261. r.push(action(1));
  262. } else {
  263. r.push(action(3));
  264. }
  265. }
  266. }
  267. break;
  268. default:
  269. r.push(action(1));
  270. break;
  271. }
  272. }
  273. }
  274. return r.join('');
  275. }
  276. return m(input);
  277. }