stephenjbell

Basic Minify JS or CSS files - dotCMS

Sep 26th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. ## This code has a few issues and probably shouldn't be used in production - Feel free to modify into something useful.
  2.  
  3.  
  4. ## VERY basic minify functionality for dotCMS
  5. ## If you're looking for something more efficient and fancier
  6. ## (that requires plugin installation), try http://geekyplugins.com/dot-cms/minifier.dot
  7. ##
  8. ## Copyright 2011 Oklahoma Christian University (Stephen Bell)
  9. ##
  10. ## -----------------------------------
  11. ## USAGE:
  12. ## -----------------------------------
  13. ##
  14. ## Put this code in a widget in a blank template (no other HTML) in an
  15. ## index.dot page in a folder (ie. /minify/index.dot)
  16. ##
  17. ##
  18. ## -----------------------------------
  19. ## JAVASCRIPT:
  20. ## -----------------------------------
  21. ##
  22. ## If your existing javascript is
  23. ## <script type="text/javascript" src="/project/js/stuff.js"></script>
  24. ##
  25. ## Then make you would reference this as
  26. ## <script type="text/javascript" src="/minify?m=/project/js/stuff.js"></script>
  27. ##
  28. ##
  29. ## -----------------------------------
  30. ## CSS:
  31. ## -----------------------------------
  32. ##
  33. ## CSS works the same way as JS:
  34. ## <link rel="stylesheet" href="/mystuff/whatever.css"/>
  35. ##
  36. ## Becomes:
  37. ## <link rel="stylesheet" href="/minify?m=/mystuff/whatever.css"/>
  38. ##
  39. ##
  40. ## -----------------------------------
  41. ## MULTIPLE FILES
  42. ## -----------------------------------
  43. ##
  44. ## Separate them with a comma:
  45. ## <script type="text/javascript" src="/minify?m=/myfolder/js/file1.js,/myfolder/js/file2.js"></script>
  46. ##
  47. ##
  48. ## -----------------------------------
  49. ## NOTES (read before using)
  50. ## -----------------------------------
  51. ##
  52. ## 1. You'll want to remove the tabs and extra line breaks from this code,
  53. ## and it will still leave a bit of extra white space, because velocity is "special"
  54. ## 2. Single line breaks are not taken out of JS files (though comments and other whitespace are)
  55. ## because of this http://bit.ly/p4MAYR, and because I can't write anything complicated
  56. ## enough to handle that
  57. ## 3. Use at your own risk
  58. ##
  59.  
  60. #set($myDomain = "http://www.yoursite.com")
  61.  
  62. #set($query = $request.getParameter("m"))
  63. #set($files = $query.split(","))
  64.  
  65. #set($jscount=0)
  66. #set($csscount=0)
  67. #set($warning=0)
  68.  
  69. #foreach($file in $files)
  70. #if(!$file.matches("^\/.*"))
  71. #set($warning = $warning + 1)
  72. #end
  73. #if($file.matches(".*\.js$"))
  74. #set($jscount=$jscount+1)
  75. #end
  76. #if($file.matches(".*\.css$"))
  77. #set($csscount=$csscount+1)
  78. #end
  79. #end
  80.  
  81. #if($warning > 0)
  82. <p>Sorry chief, local files only.</p>
  83. #else
  84. #if($csscount== $files.size() )
  85. #set($contenttype="css")
  86. $response.setContentType('text/css')
  87. #end
  88.  
  89. #if($jscount== $files.size())
  90. #set($contenttype="js")
  91. $response.setContentType('application/javascript')
  92. #end
  93.  
  94. #dotcache($request.getParameter("m"),60)
  95. #if($contenttype=="css")
  96. #foreach ($file in $files)
  97. #set($filecontents = $import.read("${myDomain}${file}"))
  98. #set($filecontents = $filecontents.replaceAll("\s*\/\*+([^\*]|[/s])*\*+\/\s*","")) ## Multi line comments
  99. #set($filecontents = $filecontents.replaceAll("\/\/.*[\n|\r]","")) ## Single line comments
  100. #set($filecontents = $filecontents.replaceAll("[\s]{1,9999}"," ")) ## White space
  101. #set($filecontents = $filecontents.replaceAll("(\{\s*)","{")) ## Spaces just inside opening curly quotes
  102. #set($filecontents = $filecontents.replaceAll("(\s*\})","}")) ## Spaces just inside closing curly quotes
  103. $filecontents
  104. #end
  105. #elseif($contenttype=="js")
  106. #foreach ($file in $files)
  107. #set($filecontents = $import.read("${myDomain}${file}"))
  108. #set($filecontents = $filecontents.replaceAll("\s*\/\*+([^\*]|[/s])*\*+\/\s*","")) ## Multi line comments
  109. #set($filecontents = $filecontents.replaceAll("\/\/.*[\n|\r]","")) ## Single line comments
  110. #set($filecontents = $filecontents.replaceAll("\t","")) ## All tabs
  111. #set($filecontents = $filecontents.replaceAll("[ ]{2,9999}","")) ## Groups of more than one space
  112. #set($filecontents = $filecontents.replaceAll("(\n)\n*","$1")) ## Groups of more than one space
  113. $filecontents
  114. #end
  115. #else
  116. Error: Please list files that are either all JS or all CSS
  117. #end
  118. #end
  119. #end
Advertisement
Add Comment
Please, Sign In to add comment