/*global env: true */
/**
 * @overview Copy static files to output. Useful for tutorials
 */
'use strict';

var fs = require('jsdoc/fs'),
    pathUtil = require('jsdoc/path'),
    conf = env.conf.staticFiles || {},
    filter = new (require('jsdoc/src/filter')).Filter(conf),
    scanner = new (require('jsdoc/src/scanner')).Scanner(),
    outDir = env.opts.destination,
    SLASH = pathUtil.sep,
    RECURSIVE = conf.recursive || false,
    RECURSIVE_LEVEL = conf.recursiveLevel || (!RECURSIVE ? 1 : 3);

var splitInclude = function (paths) {
    var output = [];
    paths.forEach( function(path) {
        if ((/:/).test(path)) {
            var split = path.split(":");
            output.push({ dest: split[0], source: split[1] });
        } else {
            output.push({ dest: path, source: path });
        }
    });
    return output;
}

exports.handlers = {

    // We would copy the source files only after all processing is done
    processingComplete: function (d) {
        var includes,
            filteredList,
            destPath,
            srcPath,
            toDir;

        if (conf.include) {
            includes = splitInclude(conf.include);
            includes.forEach( function(elem) {
                destPath = pathUtil.join(outDir, elem.dest) + SLASH;
                filteredList = scanner.scan([elem.source], RECURSIVE_LEVEL, filter);
                filteredList.forEach( function(currentPath) {
                    srcPath = fs.statSync(elem.source).isDirectory() ? elem.source :
                        pathUtil.dirname(elem.source);
                    toDir = fs.toDir(currentPath.replace(srcPath, destPath));
                    fs.mkPath(toDir);
                    fs.copyFileSync(currentPath, toDir);
                });
            });
        }
    }
}

@@ -503,255 +503,933 @@
 );%0A%0A
-var splitInclude = function (paths) %7B%0A    var output = %5B%5D;%0A    paths.forEach( function(path) %7B%0A        if ((/:/).test(path)) %7B%0A            var split = path.split(%22:%22);%0A            output.push(%7B dest: split%5B0%5D, source: split%5B1%5D %7D);%0A        %7D else %7B
+/**%0A * Splits include paths into an array of source and destination paths%0A *%0A * @example%0A * %5B%22images:tutorials/images%22, %22js%22%5D%0A * @param %7Barray%7D paths - Array of items in the format %60%22destination:source%22%60 or, simply, %60%22source%22%60%0A * @returns %7Barray%7D An array containing relative source and destination paths in the format %60%7Bdest: %22%22, source: %22%22%7D%60%0A */%0Avar splitInclude = function (paths) %7B%0A    var output = %5B%5D;%0A    paths.forEach( function(path) %7B%0A        // Test if path contains a colon%0A        if ((/:/).test(path)) %7B%0A            // If it contains a colon, split it into two parts based on the colon%0A            var split = path.split(%22:%22);%0A            // Add an object with the first part of the split path as destination%0A            // and the second as source%0A            output.push(%7B dest: split%5B0%5D, source: split%5B1%5D %7D);%0A        %7D else %7B%0A            // If path does not contain a colon, destination and source will be same
 %0A   
@@ -1551,34 +1551,32 @@
    /
-/ We would copy the source
+**%0A     * Process static
  fil
@@ -1593,11 +1593,19 @@
 ter 
-all
+rest of the
  pro
@@ -1619,12 +1619,24 @@
  is 
-done
+complete%0A     */
 %0A   
@@ -1666,17 +1666,16 @@
 nction (
-d
 ) %7B%0A    
@@ -1793,78 +1793,260 @@
     
-if (conf.include) %7B%0A            includes = splitInclude(conf.include);
+// Proceed if %60include%60 paths are provided, else silently exit%0A        if (conf.include) %7B%0A            // Split the %60conf.include%60%0A            includes = splitInclude(conf.include);%0A            // Iterate over each %60include%60 paths and perform operation
 %0A   
@@ -2085,24 +2085,71 @@
 ion(elem) %7B%0A
+                // Prepare path to destination%0A
             
@@ -2225,76 +2225,251 @@
     
-filteredList = scanner.scan(%5Belem.source%5D, RECURSIVE_LEVEL, filter);
+// Scan the source path and filter inclusions and exclusions%0A                filteredList = scanner.scan(%5Belem.source%5D, RECURSIVE_LEVEL, filter);%0A                // Iterate over each of the filtered paths available and copy them to destination
 %0A   
@@ -2523,24 +2523,68 @@
 rentPath) %7B%0A
+                    // Generate source path%0A
             
@@ -2707,24 +2707,86 @@
 em.source);%0A
+                    // Prepare directory path for destination%0A
             
@@ -2875,25 +2875,152 @@
     
-fs.mkPath(toDir);
+// Create destination directory%0A                    fs.mkPath(toDir);%0A                    // Finally, copy the file synchronously to destination
 %0A   

Add inline comments to staticFiles.js