Pablo Caro Revuelta avatar Pablo Caro Revuelta committed c5b21ad

Yui compressor support. More docs. New verbose option

Comments (0)

Files changed (4)

 0.3 (unreleased)
 ----------------
 
-- Nothing changed yet.
+- Support for several tools (yui compressor) using inout in minimizer
+- Verbose mode
+- Updated documentation: new link to github project
 
 
 0.2 (2011-06-02)
   * Create initial configuration files from html files
   * Reemplace in html script tags by minimized ones.
 
-The code is open source.
-Similar tool (more powerfull) is `jstools`_
+The project is open source.
+
+A similar tool is `jstools`_ which do recursive search for the javascript files and also support dependencies.
 
 Requirements
 -------------
 Python 2.6
 
-For javascript minimization you can use  `jsmin`_ or `slimit`_
+For javascript minimization you can use:
+
+  * `jsmin`_ or `slimit`_  (you must install the egg first)
+  * Any program which reads the code from stdin and writes to stdout like `yui compressor`_
+    or `UglifyJS`_
+
 
 
 
   * If you use buildout, add it to the ``eggs`` list in your ``buildout.cfg``,
     or as a dependency of one of your own packages in ``setup.py``.
 
+The code is available in the `jspack github repo`_
 
 Usage
 -----
 If you have a config file (test.cfg) like::
 
     [DEFAULT]
-    compress = true
+    minimizer = jsmin
 
     [concatenated.js]
-    root = /one/path
-    compress = true
+    # yui compressor
+    minimizer = inout: java -jar compressor.jar --type js
+    # Can be used to replace the js tags
+    html = test.html
     files = file/relative/to/root.js
             /absolute/file.js
 
     [compressed.js]
-    root = /another/path
-    compress = false
+    minimizer = slimit
     files = file/relative/to/root.js
             js/two.js
 
+    [css/all.css]
+    root = css
+    minimizer = none
+    files = one.css
+            two.css
 
-It generates /one/path/concatenated.js and /another/path/compressed.js
+
+And generates concatenated.js, compressed.js and css/all.css
 
 If root option is not specified, the relative files are considered relative to the config parent directory (test.cfg).
 
 You can use the help::
 
     $ bin/jspack  --help
-    Usage: jspack [options] file_or_directory
+    Usage:
+    jspack [options] file_or_directory
+
+    Typical Usage:
+
+    1. First create cfg files:
+       $ jspack -w htmlfile_or_directory
+    2. Now we can edit the jspack.cfg files and customize
+    3. Then generate minimized:
+       $ jspack cfgfile_or_directory
+    4. Last, replace script tags:
+       $ jspack -rn cfgfile_or_directory
+
+    Note: 3 and 4 can be summarized which: $ jspack -r cfgfile_or_directory
+
 
     Options:
       -h, --help            show this help message and exit
-      -w, --write-config    For each FILE.html file write a FILE-jspack.cfg file
+      -w, --write-config    For each FILE.html file write a FILE.jspack.cfg file
                             which later can be used to produce FILE-minimized.js
       -o, --ovewrite-config
-                            Overwrite a .cfg file if exists
+                            Overwrite a jspack.cfg file if exists. Use which -w
+                            option
       -i INCLUDE, --include-only=INCLUDE
-                            When parsing html, Include only javascript files which
-                            match an expresion. Default (?!http), Use  for no
-                            restriction
+                            When parsing html (to generate a .jspack.cfg file) ,
+                            include only javascript files which match this regular
+                            expression. Default (?!http), Use  for no restriction
+      -s, --allow-strict-mode
+                            When concatenate js files, dont't drop 'use strict'
+                            lines. Default is remove strip mode
+      -n, --no-pack         no packing when using the jspack.cfg. Only usefull
+                            with -r option for only replace
       -r, --replace-script-tags
                             Replace the script tags in html files
       -q, --quiet           Supress non error output
+      -v, --verbose         Vebose output (debug mode)
+
+
 
 
 Roadmap
 -------
-- More documentation
-- Support of yahoo miminizer
+- CSS links replacement in html files
+- Better documentation
+- Tests
 
 Credits
 -------
-
+- `Yaco Sistemas`_
 - `Distribute`_
 - `Buildout`_
 - `modern-package-template`_
+- `zest.releaser`_
 
+.. _jspack github repo: http://github.com/pcaro/jspack
 .. _jstools: http://pypi.python.org/pypi/JSTools/
 .. _jsmin: http://pypi.python.org/pypi/jsmin
 .. _slimit: http://pypi.python.org/pypi/slimit
+.. _yui compressor: http://developer.yahoo.com/yui/compressor/
+.. _UglifyJS: http://github.com/mishoo/UglifyJS
+.. _Yaco Sistemas: http://www.yaco.es
 .. _Buildout: http://www.buildout.org/
 .. _Distribute: http://pypi.python.org/pypi/distribute
 .. _`modern-package-template`: http://pypi.python.org/pypi/modern-package-template
+.. _zest.releaser: http://pypi.python.org/pypi/zest.releaser
 parts = python scripts releaser
 develop = .
 eggs = jspack
+       jsmin
+       slimit
 
 [python]
 recipe = zc.recipe.egg

src/jspack/__init__.py

 from ConfigParser import ConfigParser
 from sys import stdout, stderr, exit
 import logging
+from subprocess import PIPE, Popen
 
+INOUTPREFIX = 'inout:'
 comments = re.compile(r'<!--.*?-->', re.DOTALL)
 scripttag = re.compile(r'(<script[^>]*src="([^"]*)"(?! class="jspack).*?(?:</script>|/>))')
 usestrict = re.compile(r'("use strict";)')
 
 
 ## Header inserted at the start of each file in the output
-HEADER = "/* " + "#"*30 + "%s */\n"
+HEADER = "/* %s */\n"
 
 
 def compress(jcode, minimizer):
         return slimit(jcode)
     elif minimizer == 'jsmin':
         return jsmin(jcode)
+    elif isinstance(minimizer, basestring) and minimizer.startswith(INOUTPREFIX):
+        return inout(jcode, minimizer[len(INOUTPREFIX):])
     else:
         return jcode
 
 
+def inout(jcode, command):
+    logging.debug('running: "%s"' % command)
+    proc = Popen(command, stdin=PIPE, stdout=PIPE, shell=True)
+    out, err = proc.communicate(jcode)
+    if err:
+        logging.error('Error calling %s' % command)
+        return jcode
+    return out
+
+
 def slimit(jcode):
     from slimit import minify
     return minify(jcode)
                     html = join(root, html)
                 # TODO: Improve. Now we are wrinting the file len(sections) times
                 replace(html, section, cfiles)
-        else:
+        if options.pack:
             minimizer = False
             if config.has_option(section, 'minimizer'):
                 minimizer = config.get(section, 'minimizer')
         # TODO: Fails with js/abc.js followed by js/abc.js.more.js
         tosearch = scripttemplate % f
         if not re.search(r'<!-- \S* includes: ' + tosearch, data):
-            data = re.sub(tosearch, r'<!-- %s includes: \1 -->' % jsminimized ,data)
+            data = re.sub(tosearch, r'<!-- %s includes: \1 -->' % jsminimized, data)
     newtag = '<script type="text/javascript"  src="%s" class="jspack"/>' % jsminimized
     if not newtag in data:
         data = data.replace('</head>', '%s\n</head>' % newtag)
 %prog [options] file_or_directory
 
 Typical Usage:
-First create cfg files:
-  %prog -w htmlfile_or_directory
-Then generate minimized:
-  %prog cfgfile_or_directory
-Last, replace script tags:
-  %prog -r cfgfile_or_directory
+
+1. First create cfg files:
+   $ %prog -w htmlfile_or_directory
+2. Now we can edit the jspack.cfg files and customize
+3. Then generate minimized:
+   $ %prog cfgfile_or_directory
+4. Last, replace script tags:
+   $ %prog -rn cfgfile_or_directory
+
+Note: 3 and 4 can be summarized which: $ %prog -r cfgfile_or_directory
     """
     parser = OptionParser(usage=usage)
     parser.add_option("--write-config", "-w",
                   default=False)
     parser.add_option("--include-only", "-i",
                   dest="include",
-                  help="When parsing html, Include only javascript files which match an expresion. Default (?!http), Use "" for no restriction",
+                  help="When parsing html (to generate a .jspack.cfg file) , include only javascript files which match this regular expression. Default (?!http), Use "" for no restriction",
                   default="(?!http)")
     parser.add_option("--allow-strict-mode", "-s",
                   dest="replacestrict",
                   help="When concatenate js files, dont't drop 'use strict' lines. Default is remove strip mode",
                   action="store_false",
                   default=True)
+    parser.add_option("--no-pack", "-n",
+                  dest="pack",
+                  help="no packing when using the jspack.cfg. Only usefull with -r option for only replace",
+                  action="store_false",
+                  default=True)
     parser.add_option("--replace-script-tags", "-r",
                   dest="replace",
                   help="Replace the script tags in html files",
                   help='Supress non error output',
                   default=False,
                   action='store_true')
+    parser.add_option('-v', '--verbose',
+                  dest='verbose',
+                  help='Vebose output (debug mode)',
+                  default=False,
+                  action='store_true')
     (options, args) = parser.parse_args()
 
     if len(args) != 1:
     logger.setLevel(logging.INFO)
     if options.quiet:
         logger.setLevel(logging.WARN)
+    elif options.verbose:
+        logger.setLevel(logging.DEBUG)
 
     global customscripttag
     customscripttag = re.compile(scripttemplate % options.include)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.