Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## This code has a few issues and probably shouldn't be used in production - Feel free to modify into something useful.
- ## VERY basic minify functionality for dotCMS
- ## If you're looking for something more efficient and fancier
- ## (that requires plugin installation), try http://geekyplugins.com/dot-cms/minifier.dot
- ##
- ## Copyright 2011 Oklahoma Christian University (Stephen Bell)
- ##
- ## -----------------------------------
- ## USAGE:
- ## -----------------------------------
- ##
- ## Put this code in a widget in a blank template (no other HTML) in an
- ## index.dot page in a folder (ie. /minify/index.dot)
- ##
- ##
- ## -----------------------------------
- ## JAVASCRIPT:
- ## -----------------------------------
- ##
- ## If your existing javascript is
- ## <script type="text/javascript" src="/project/js/stuff.js"></script>
- ##
- ## Then make you would reference this as
- ## <script type="text/javascript" src="/minify?m=/project/js/stuff.js"></script>
- ##
- ##
- ## -----------------------------------
- ## CSS:
- ## -----------------------------------
- ##
- ## CSS works the same way as JS:
- ## <link rel="stylesheet" href="/mystuff/whatever.css"/>
- ##
- ## Becomes:
- ## <link rel="stylesheet" href="/minify?m=/mystuff/whatever.css"/>
- ##
- ##
- ## -----------------------------------
- ## MULTIPLE FILES
- ## -----------------------------------
- ##
- ## Separate them with a comma:
- ## <script type="text/javascript" src="/minify?m=/myfolder/js/file1.js,/myfolder/js/file2.js"></script>
- ##
- ##
- ## -----------------------------------
- ## NOTES (read before using)
- ## -----------------------------------
- ##
- ## 1. You'll want to remove the tabs and extra line breaks from this code,
- ## and it will still leave a bit of extra white space, because velocity is "special"
- ## 2. Single line breaks are not taken out of JS files (though comments and other whitespace are)
- ## because of this http://bit.ly/p4MAYR, and because I can't write anything complicated
- ## enough to handle that
- ## 3. Use at your own risk
- ##
- #set($myDomain = "http://www.yoursite.com")
- #set($query = $request.getParameter("m"))
- #set($files = $query.split(","))
- #set($jscount=0)
- #set($csscount=0)
- #set($warning=0)
- #foreach($file in $files)
- #if(!$file.matches("^\/.*"))
- #set($warning = $warning + 1)
- #end
- #if($file.matches(".*\.js$"))
- #set($jscount=$jscount+1)
- #end
- #if($file.matches(".*\.css$"))
- #set($csscount=$csscount+1)
- #end
- #end
- #if($warning > 0)
- <p>Sorry chief, local files only.</p>
- #else
- #if($csscount== $files.size() )
- #set($contenttype="css")
- $response.setContentType('text/css')
- #end
- #if($jscount== $files.size())
- #set($contenttype="js")
- $response.setContentType('application/javascript')
- #end
- #dotcache($request.getParameter("m"),60)
- #if($contenttype=="css")
- #foreach ($file in $files)
- #set($filecontents = $import.read("${myDomain}${file}"))
- #set($filecontents = $filecontents.replaceAll("\s*\/\*+([^\*]|[/s])*\*+\/\s*","")) ## Multi line comments
- #set($filecontents = $filecontents.replaceAll("\/\/.*[\n|\r]","")) ## Single line comments
- #set($filecontents = $filecontents.replaceAll("[\s]{1,9999}"," ")) ## White space
- #set($filecontents = $filecontents.replaceAll("(\{\s*)","{")) ## Spaces just inside opening curly quotes
- #set($filecontents = $filecontents.replaceAll("(\s*\})","}")) ## Spaces just inside closing curly quotes
- $filecontents
- #end
- #elseif($contenttype=="js")
- #foreach ($file in $files)
- #set($filecontents = $import.read("${myDomain}${file}"))
- #set($filecontents = $filecontents.replaceAll("\s*\/\*+([^\*]|[/s])*\*+\/\s*","")) ## Multi line comments
- #set($filecontents = $filecontents.replaceAll("\/\/.*[\n|\r]","")) ## Single line comments
- #set($filecontents = $filecontents.replaceAll("\t","")) ## All tabs
- #set($filecontents = $filecontents.replaceAll("[ ]{2,9999}","")) ## Groups of more than one space
- #set($filecontents = $filecontents.replaceAll("(\n)\n*","$1")) ## Groups of more than one space
- $filecontents
- #end
- #else
- Error: Please list files that are either all JS or all CSS
- #end
- #end
- #end
Advertisement
Add Comment
Please, Sign In to add comment