text:
"""
Copyright (c) 2014 High-Performance Computing and GIS (HPCGIS) Laboratory. All rights reserved.
Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Authors and contributors: Eric Shook (eshook@kent.edu); Zhengliang Feng (odayfans@gmail.com, zfeng2@kent.edu)
"""
from ..util.SharedMemory import *
from ..util.Messaging import *
from .PCMLPrims import *
import PCMLConfig as PCMLConfig
import copy
import multiprocessing as mp
try:
   PCMLConfig.scipyenabled = 1
   from scipy.spatial import cKDTree
except ImportError as e:
   PCMLConfig.scipyenabled = 0

class BoundingBox(object):
    """
    BoundingBox defines a rectangular location (y,x) + (h,w) and may contain data describing something within its boundaries.
    This class is also used as the parent class of Layer and Subdomain.
    """

    def __init__(self, y, x, h, w):
        """Create a new BoundingBox.
            :param y (double): The y location (lower left corner typically) of the :class:`BoundingBox`.
            :param x (double): The x location (lower left corner typically) of the :class:`BoundingBox`.
            :param h (double): The height of the :class:`BoundingBox`.
            :param w (double): The width of the :class:`BoundingBox`.
        """
        self.y = y
        self.x = x
        self.h = h
        self.w = w
        # Adding buffer data for points
        self.buffx = None
        self.buffy = None
        self.buffh = None
        self.buffw = None

        # Sanity check
        if(h <= 0):
            raise PCMLInvalidInput("BoundingBox does not support a negative or zero height", h)
        if(w <= 0):
            raise PCMLInvalidInput("BoundingBox does not support a negative or zero width", w)

        # Data is held within the internal structure _data
        # and the data structure (e.g., array, list) and type (e.g., location, float, int) must also be described
        self._data = None
        self.data_structure = Datastructure.array  # FIXME: For now we assume the data_structure is an array
        self.data_type = None
        self.tree = None
        # TODO: This should be looked at with dependency on data_structure/type. Perhaps a new class should be created to encapsulate data.
        # TODO: If data_structure is an array, then must describe the cellsize and set a nodata_value
        # By default set to none
        self.nodata_value = None
        self.cellsize = None
        self.nrows = None
        self.ncols = None

    def __repr__(self):
        return "<BoundingBox: (%f,%f) [%f,%f]>" % (self.y, self.x, self.h, self.w)

    def set_nparray(self, nparr, cellsize, nodata_value):
        if nparr is None:
            raise PCMLInvalidInput("BoundingBox.set_nparray does not support a nparr of None", nparr)
        if cellsize is None:
            raise PCMLInvalidInput("BoundingBox.set_nparray does not support a cellsize of None", cellsize)
        if cellsize <= 0:
            raise PCMLInvalidInput("BoundingBox.set_nparray does not support cellsize<=0", cellsize)

        self.data_structure = Datastructure.array
        PCMLNotImplemented("self.data_type is not set")
        # Unfortunately you cannot hide shared memory in another module as rawarray will return the same reference
        # It may have to do with saving the __shmem_data variable, could explore this later
        self.__shmem_data = mp.RawArray(ctypes.c_double, nparr.size)
        self._data = shmem_as_ndarray(self.__shmem_data).reshape(nparr.shape)
        self._data[:, :] = nparr
        self.cellsize = cellsize
        self.nodata_value = nodata_value
        self._reset_dim()

    def get_nparray(self):
        return self._data

    def set_pointlist(self, pointlist, ref=False):
        self.data_structure = Datastructure.pointlist
        # FIXME: Should check if pointlist is a list datastructure
        if not ref:
            self._data = pointlist
        else:
            self._data = mp.Manager().list(pointlist)

    def get_pointlist(self):
        assert(self.data_structure == Datastructure.pointlist), "Cannot get point list if datastructure is not a point list"
        return self._data

    # Bounding box check
    def isinsidebounds(self, point, usehalo=False):
        x, y, w, h = self.x, self.y, self.w, self.h
        if usehalo:
            x, y, w, h = self.buffx, self.buffy, self.buffw, self.buffh
        a = y
        b = y + h
        c = point['y']
        if point['x'] < x+w and point['x'] >= x and point['y'] < y+h and point['y'] >= y:
            return True
        else:
            return False

    # Get points with out including halozone
    def get_pointlistwithouthalozone(self):
        pointswithouthalozone = []
        for point in self._data:
            if self.isinsidebounds(point):
                pointswithouthalozone.append(point)
        return pointswithouthalozone

    def _reset_dim(self):
        nparr = self.get_nparray()
        # Set nrows and ncols based on dimensions of array
        self.nrows = len(nparr)
        self.ncols = len(nparr[0])

        h = self.nrows * self.cellsize
        w = self.ncols * self.cellsize
        if h != self.h:
            PCMLUserInformation("Updating height from " + str(self.h) + " to " + str(h))
            self.h = h
        if w != self.w:
            PCMLUserInformation("Updating width from " + str(self.w) + " to " + str(w))
            self.w = w

    def set_data_ref(self, ref):
        """
        Set the _data variable to a reference (used for shared memory accesses - particularly subdomains)
        """
        self._data = ref
        self._reset_dim()

    def get_locval(self, loc):
        newloc = copy.copy(loc)
        newloc['v'] = self._data[loc['r'] - self.r][loc['c'] - self.c]
        return newloc

    def get_ind_from_loc(self, loc):
        return {
                        'r': loc['r'] - self.r,
                        'c': loc['c'] - self.c}

    def get_yxloc(self, locind):
        return {
                'y': self.y + self.cellsize * locind['r'],
                'x': self.x + self.cellsize * locind['c'],
                'z': self._data[locind['r']][locind['c']]}

    def bufferedlocgetarr(self, loc, buffersize):
        ind = self.get_ind_from_loc(loc)
        # FIXME: NEED TO DOUBLE CHECK THIS LOGIC, NEED TO RECORD RDIFF,CDIFF FOR H/W
        r = max(0, ind['r'] - buffersize)
        c = max(0, ind['c'] - buffersize)
        h = buffersize + (ind['r'] - r) + 1
        if (r + h > self.nrows):
            h = self.nrows - r
        w = buffersize + (ind['c'] - c) + 1
        if (c + w > self.ncols):
            w = self.ncols - c
        return self.slice_nparray(r, c, h, w)

    def slice_nparray(self, r, c, h, w):
        # IMPORTANT: Notice slice uses r,c NOT x,y!
        return self._data[r:r + h, c:c + w]

    def print_data(self):
        """Print out all data."""
        print(self._data)

    # Get neighbors for points using cKDTree
    def getneighbors(self, location, count=1, radius=np.inf, excludesearchlocation=False, distreq=False):
        if PCMLConfig.scipyenabled == 0:
            PCMLNotSupported("SciPy module required for getneighbors()")
        if excludesearchlocation:
            count += 1
        if self.tree is None:
            pointlist = self.get_pointlist()
            pointdata = []
            if len(pointlist) == 0:
                if not distreq:
                    return []
                else:
                    return [[], []]
            for point in pointlist:
                pointdata.append([point['x'], point['y']])
            self.tree = cKDTree(pointdata)
        points = [location['x'], location['y']]
        dist, neighbors = self.tree.query(points, k=count, distance_upper_bound=radius)
        if count == 1:
            if neighbors == self.tree.n:
                if not distreq:
                    return []
                else:
                    return [[], []]
            else:
                if not distreq:
                    return [neighbors]
                else:
                    return [[neighbors], [dist]]
        if not distreq:
            return neighbors[neighbors != self.tree.n]
        else:
            return [neighbors[neighbors != self.tree.n], dist[dist != np.inf]]


text:
<?php

// TODO: get rid of this using proper composer packages
require_once 'mcnetic/phpzipstreamer/ZipStreamer.php';

class GET_TYPE {
	const FILE = 1;
	const ZIP_FILES = 2;
	const ZIP_DIR = 3;
}

/**
 * Class for file server access
 *
 */
class OC_Files {

	/**
	 * @param string $filename
	 * @param string $name
	 * @param bool $zip
	 */
	private static function sendHeaders($filename, $name, $zip = false) {
		OC_Response::setContentDispositionHeader($name, 'attachment');
		header('Content-Transfer-Encoding: binary');
		OC_Response::disableCaching();
		if ($zip) {
			header('Content-Type: application/zip');
		} else {
			$filesize = \OC\Files\Filesystem::filesize($filename);
			header('Content-Type: '.\OC\Files\Filesystem::getMimeType($filename));
			if ($filesize > -1) {
				header("Content-Length: ".$filesize);
			}
		}
	}

	/**
	 * return the content of a file or return a zip file containing multiple files
	 *
	 * @param string $dir
	 * @param string $files ; separated list of files to download
	 * @param boolean $only_header ; boolean to only send header of the request
	 */
	public static function get($dir, $files, $only_header = false) {
		$xsendfile = false;
		if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) ||
			isset($_SERVER['MOD_X_SENDFILE2_ENABLED']) ||
			isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
			$xsendfile = true;
		}

		if (is_array($files) && count($files) === 1) {
			$files = $files[0];
		}

		if (is_array($files)) {
			$get_type = GET_TYPE::ZIP_FILES;
			$basename = basename($dir);
			if ($basename) {
				$name = $basename . '.zip';
			} else {
				$name = 'download.zip';
			}

			$filename = $dir . '/' . $name;
		} else {
			$filename = $dir . '/' . $files;
			if (\OC\Files\Filesystem::is_dir($dir . '/' . $files)) {
				$get_type = GET_TYPE::ZIP_DIR;
				// downloading root ?
				if ($files === '') {
					$name = 'download.zip';
				} else {
					$name = $files . '.zip';
				}

			} else {
				$get_type = GET_TYPE::FILE;
				$name = $files;
			}
		}

		if ($get_type === GET_TYPE::FILE) {
			$zip = false;
			if ($xsendfile && OC_App::isEnabled('files_encryption')) {
				$xsendfile = false;
			}
		} else {
			$zip = new ZipStreamer(false);
		}
		OC_Util::obEnd();
		if ($zip or \OC\Files\Filesystem::isReadable($filename)) {
			self::sendHeaders($filename, $name, $zip);
		} elseif (!\OC\Files\Filesystem::file_exists($filename)) {
			header("HTTP/1.0 404 Not Found");
			$tmpl = new OC_Template('', '404', 'guest');
			$tmpl->assign('file', $name);
			$tmpl->printPage();
		} else {
			header("HTTP/1.0 403 Forbidden");
			die('403 Forbidden');
		}
		if($only_header) {
			return ;
		}
		if ($zip) {
			$executionTime = intval(ini_get('max_execution_time'));
			set_time_limit(0);
			if ($get_type === GET_TYPE::ZIP_FILES) {
				foreach ($files as $file) {
					$file = $dir . '/' . $file;
					if (\OC\Files\Filesystem::is_file($file)) {
						$fh = \OC\Files\Filesystem::fopen($file, 'r');
						$zip->addFileFromStream($fh, basename($file));
						fclose($fh);
					} elseif (\OC\Files\Filesystem::is_dir($file)) {
						self::zipAddDir($file, $zip);
					}
				}
			} elseif ($get_type === GET_TYPE::ZIP_DIR) {
				$file = $dir . '/' . $files;
				self::zipAddDir($file, $zip);
			}
			$zip->finalize();
			set_time_limit($executionTime);
		} else {
			if ($xsendfile) {
				$view = \OC\Files\Filesystem::getView();
				/** @var $storage \OC\Files\Storage\Storage */
				list($storage) = $view->resolvePath($filename);
				if ($storage->isLocal()) {
					self::addSendfileHeader($filename);
				} else {
					\OC\Files\Filesystem::readfile($filename);
				}
			} else {
				\OC\Files\Filesystem::readfile($filename);
			}
		}
	}

	/**
	 * @param false|string $filename
	 */
	private static function addSendfileHeader($filename) {
		if (isset($_SERVER['MOD_X_SENDFILE_ENABLED'])) {
			$filename = \OC\Files\Filesystem::getLocalFile($filename);
			header("X-Sendfile: " . $filename);
 		}
 		if (isset($_SERVER['MOD_X_SENDFILE2_ENABLED'])) {
			$filename = \OC\Files\Filesystem::getLocalFile($filename);
			if (isset($_SERVER['HTTP_RANGE']) &&
				preg_match("/^bytes=([0-9]+)-([0-9]*)$/", $_SERVER['HTTP_RANGE'], $range)) {
				$filelength = filesize($filename);
 				if ($range[2] === "") {
 					$range[2] = $filelength - 1;
 				}
 				header("Content-Range: bytes $range[1]-$range[2]/" . $filelength);
 				header("HTTP/1.1 206 Partial content");
 				header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " $range[1]-$range[2]");
 			} else {
 				header("X-Sendfile: " . $filename);
 			}
		}

		if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
			$filename = \OC::$WEBROOT . '/data' . \OC\Files\Filesystem::getRoot() . $filename;
			header("X-Accel-Redirect: " . $filename);
		}
	}

	/**
	 * @param string $dir
	 * @param ZipStreamer $zip
	 * @param string $internalDir
	 */
	public static function zipAddDir($dir, $zip, $internalDir='') {
		$dirname=basename($dir);
		$rootDir = $internalDir.$dirname;
		if (!empty($rootDir)) {
			$zip->addEmptyDir($rootDir);
		}
		$internalDir.=$dirname.='/';
		// prevent absolute dirs
		$internalDir = ltrim($internalDir, '/');

		$files=\OC\Files\Filesystem::getDirectoryContent($dir);
		foreach($files as $file) {
			$filename=$file['name'];
			$file=$dir.'/'.$filename;
			if(\OC\Files\Filesystem::is_file($file)) {
				$fh = \OC\Files\Filesystem::fopen($file, 'r');
				$zip->addFileFromStream($fh, $internalDir.$filename);
				fclose($fh);
			}elseif(\OC\Files\Filesystem::is_dir($file)) {
				self::zipAddDir($file, $zip, $internalDir);
			}
		}
	}

	/**
	 * set the maximum upload size limit for apache hosts using .htaccess
	 *
	 * @param int $size file size in bytes
	 * @return bool false on failure, size on success
	 */
	static function setUploadLimit($size) {
		//don't allow user to break his config -- upper boundary
		if ($size > PHP_INT_MAX) {
			//max size is always 1 byte lower than computerFileSize returns
			if ($size > PHP_INT_MAX + 1)
				return false;
			$size -= 1;
		} else {
			$size = OC_Helper::phpFileSize($size);
		}

		//don't allow user to break his config -- broken or malicious size input
		if (intval($size) === 0) {
			return false;
		}

		//suppress errors in case we don't have permissions for
		$htaccess = @file_get_contents(OC::$SERVERROOT . '/.htaccess');
		if (!$htaccess) {
			return false;
		}

		$phpValueKeys = array(
			'upload_max_filesize',
			'post_max_size'
		);

		foreach ($phpValueKeys as $key) {
			$pattern = '/php_value ' . $key . ' (\S)*/';
			$setting = 'php_value ' . $key . ' ' . $size;
			$hasReplaced = 0;
			$content = preg_replace($pattern, $setting, $htaccess, 1, $hasReplaced);
			if ($content !== null) {
				$htaccess = $content;
			}
			if ($hasReplaced === 0) {
				$htaccess .= "\n" . $setting;
			}
		}

		//check for write permissions
		if (is_writable(OC::$SERVERROOT . '/.htaccess')) {
			file_put_contents(OC::$SERVERROOT . '/.htaccess', $htaccess);
			return OC_Helper::computerFileSize($size);
		} else {
			OC_Log::write('files',
				'Can\'t write upload limit to ' . OC::$SERVERROOT . '/.htaccess. Please check the file permissions',
				OC_Log::WARN);
		}
		return false;
	}
}

redpajama_set_name: RedPajamaGithub


text:
#include "remoteSubscription.h"
#include "remoteSubscriptionHandle.h"

namespace one {
namespace client {
namespace events {

SubscriptionHandlePtr RemoteSubscription::createHandle(
    std::int64_t subscriptionId, Streams &streams,
    SequencerStream &stream) const
{
    return std::make_unique<RemoteSubscriptionHandle>(
        streamKey(), streams, subscriptionId, serialize(), stream);
}

} // namespace events
} // namespace client
} // namespace one


text:
module Saucer
  class Client
    extend Memoist
    attr_accessor :user, :token, :api
    def initialize
      @user = Env.sauce_username
      @token = Env.sauce_access_key
      @api = Faraday.new "https://saucelabs.com/rest/v1/#{@user}" do |conn|
        conn.request :json
        conn.request :basic_auth, @user, @token

        conn.response :json, :content_type => /\bjson$/
        conn.adapter Faraday.default_adapter
      end
    end

    def jobs(count = 200)
      ids = job_ids(count)
      responses = ids.pmap(100) do |id|
        slug = ['jobs', id].join('/')
        api.get(slug)
      end.flatten

      responses.map do |response|
        hsh = response.body
        normalize_hash_keys(hsh)
      end

    end
    memoize :jobs

    def normalize_hash_keys(hsh)
      Hash[hsh.map {|k, v| [k.underscore('-'), v] }]
    end

    def job_ids(count = 200)
      args = get_iterations(count)
      responses = args.pmap(400) do |args|
        response = api.get('jobs', args)
      end.flatten
      ids = extract_key(responses, 'id')
    end

    def extract_key(responses, key = 'id')
      job_ids = responses.map do |item|
        item.body.map { |i| i[key] }
      end.flatten.uniq
    end

    def get_iterations(count)
      count -= 100
      increment = 100
      steps = 0.step(count, increment).map { |a| a }
      limits = Array.new(steps.count, increment)
      args = steps.zip(limits)
      args.each_with_object([]) do |item, obj|
        skip, limit = item
        obj << {skip: skip, limit: limit}
      end
    end
  end
end


text:
#ifndef _Z_CTK_THREADEXCEPTION_H_
#define _Z_CTK_THREADEXCEPTION_H_

#include "ctk/ctk_config.h"

#include "ctk/base/exception.h"

namespace ctk
{

    class CTK_API NullHandleException : public Exception
    {
        typedef Exception _Super;

    public:
        NullHandleException(CTK_CURRENT_ARGS);

    };

    class CTK_API ThreadSyscallException : public Exception
    {
        typedef Exception _Super;

    public:
        ThreadSyscallException(CTK_CURRENT_ARGS, int _err);

        virtual void dump(std::ostream&) const;

        int error() const;

    private:
        const int error_;

    };

    class CTK_API ThreadLockedException : public Exception
    {
        typedef Exception _Super;

    public:
        ThreadLockedException(CTK_CURRENT_ARGS);

    };

    class CTK_API ThreadStartedException : public Exception
    {
        typedef Exception _Super;

    public:
        ThreadStartedException(CTK_CURRENT_ARGS);

    };

    class CTK_API ThreadNotStartedException : public Exception
    {
        typedef Exception _Super;

    public:
        ThreadNotStartedException(CTK_CURRENT_ARGS);

    };

    class CTK_API DeadlockException : public Exception
    {
        typedef Exception _Super;

    public:
        DeadlockException(CTK_CURRENT_ARGS);

    };

} // end of namespace ctk

#endif //_Z_CTK_THREADEXCEPTION_H_


text:
/* ex: set tabstop=4 expandtab: */

// global imports
var fs = require('fs');
var path = require('path');
var sys = require('sys');
var md = require('markdown').markdown;
var mustache = require('mustache');
var exec  = require('child_process').exec;
var kompressor = require('htmlKompressor');
var less = require('less');

// app imports
var blocks = require('./blocks/block');
var appUtil   = require('./appUtil');
var appConfig   = require('./appConfig');

// Define a couple of templates that are used multiple times or
// are text strings that should be maintainable and not spread all over the code.
var templates = {
    ERROR_MARKDOWN_SYNTAX:{
        TITLE: 'ERROR: Syntax error in "{{markdownFileName}}", stopped parsing block "{{blockTitle}}".',
        HINT: 'In the following find explaination about the syntax of this markdown block.'
    },
    WARNING_USING_DEFAULT_RENDERER:{
        TITLE: 'WARNING: No render method given for block "{{blockTitle}}" in "{{tplFileName}}.html" using the default method "simpleBox".',
        HINT: 'If you want to set a render method use mustache comments for it, like so: { {!simpleBox} } or { {!listBox} }.'
    }
};

var errorMessages = []; // Store all the error messages and concat them later.

// Simple mixin, note: it does not check for hasOwnProperty!
function mixin(target, obj){
    for (var prop in obj){
        target[prop] = obj[prop];
    }
}

//////////////////////////////////////////////////////

// Description:
//      This build tool renders HTML pages based on markdown pages and HTML templates
//      The default build requires mapping of markdown files to html templates (home.md -> home.html)
//      Optionally you can define one-to-many HTML to Markdown mapping.
//
//      One-to-many mapping must map to a directory e.g.
//
//          { 'team': 'team/' }
//
//      maps all Markdown files in the /team directory to the team.html template.
//
//      App flow:
//
//      1. Cleanup previous build: clean()
//      2. Reading contents of the source directory based on processFiles call: processFiles()
//      3. Reading Markdown content (optionally iterative if mapping is present): readMarkdown()
//      4. Write out result: writeView()
//      5. Compiling less to one CSS-file: prepareLess()

console.log('Welcome to the epic');
try {
    fs.mkdirSync(appConfig.releaseDir, 0755);
}catch(e){}
appConfig.printValues();
clean(function(){

    processFiles('', true);
    processFiles('/error', false); // Static, no markdown

    fs.mkdirSync(path.join(appConfig.releaseDir, "static", "css"), 0755);
    processLess(path.join(appConfig.releaseDir, "static/less/style.less"), path.join(appConfig.releaseDir, "static/css/style.css"));

    if (appConfig.postProcessor){
        console.log('Firing up post processor');
        try{
            require(appConfig.postProcessor);
        }catch(e){
            appUtil.statusLog('ERROR in post processor\t Can\'t include ' + appConfig.postProcessor + '. Make sure the path is correct');
        }
    }
});

//////////////////////////////////////////////////////

function clean(callb){
    // Summary:
    //      1. Cleaning up release dir
    //      2. Copying media, static files and favicon.ico to release dir
    //      3. Call provided callback

    appUtil.statusLog('Deleting\t' + appConfig.releaseDir + '/*');
    exec('rm -rf ' + appConfig.releaseDir + '/*', function(err, stdout, stderr){
        appUtil.statusLog('Copying\t' + appConfig.srcDir + '/static to ' + appConfig.releaseDir + '/');
        var cmd = 'cp -r ' + appConfig.srcDir + '/static ' + appConfig.releaseDir + '/;';

        appUtil.statusLog('Copying\t' + appConfig.contentDir + '/media to ' + appConfig.releaseDir + '/');
        cmd += 'cp -r ' + appConfig.contentDir + '/media  ' + appConfig.releaseDir + '/;';

        appUtil.statusLog('Copying\t' + appConfig.contentDir + '/favicon.ico to ' + appConfig.releaseDir + '/');
        cmd += 'cp ' + appConfig.contentDir + '/favicon.ico  ' + appConfig.releaseDir + '/';

        exec(cmd,
            function (err, stdout, stderr) {
                if (err !== null) {
                    console.log('Error copying (' + cmd + ') static files: ' + err);
                }else{
                    console.log('Copied static files');
                    callb && callb();
                }
            }
        );
    });
}

function processFiles(dir, hasMarkdown){
    // Summary:
    //      Reads out files from src directory and then kicks of the (optional) markdown parsing and rendering-to-file
    //      process.
    //
    //      1. Reading of src dir
    //      2. Checking whether we want to map several md files onto one template
    //      3. Kick of the (optional) markdown renderer and rendering the result to file.

    // 1.
    appUtil.statusLog('Reading HTML files from\t' + appConfig.srcDir + dir);
    var fileNames = fs.readdirSync(appConfig.srcDir + dir);
    fileNames && fileNames.forEach(function(file){
        if (file.indexOf('.html') > -1){

            var resourceName = file.split('.html')[0];
            var tmplLocation = appConfig.srcDir + dir + '/' + file;
            var tmplContent = fs.readFileSync(tmplLocation, 'utf-8');

            // 2.
            // Check whether we have a specific html to markdown mapping
            // one to many relationship
            if (appConfig.mapping[resourceName]){
                var map = appConfig.mapping[resourceName];

                function render(tmplContent, dir, resourceName){
                    var parseProps = {};
                    if (appConfig.isDebug){
                        parseProps.useLess = true;
                    }
                    // 3.
                    hasMarkdown && readMarkdown(appConfig.contentDir + dir + '/' + resourceName + '.md', resourceName, parseProps);
                    writeView(dir, resourceName + '.html', tmplContent, parseProps);
                }

                function processMapping(item, resourceName, tmplContent){
                    if (item.indexOf('.md') > -1){
                        var resourceName = item.split('.md')[0];
                        render(tmplContent, dir, resourceName);
                    }else{
                        var filenNames = fs.readdirSync(appConfig.contentDir + item);
                        filenNames && filenNames.forEach(function(file){
                            if (file.indexOf('.md') > -1){
                                var resourceName = file.split('.md')[0];
                                render(tmplContent, dir + item, resourceName);
                            }
                        });
                    }
                }

                if (Object.prototype.toString.apply(map) === '[object Array]'){
                    map.forEach(function(item){
                        processMapping(item, resourceName, tmplContent);
                    });
                }else if (Object.prototype.toString.apply(map) === '[object String]'){
                    processMapping(map, resourceName, tmplContent);
                }
            }else{
                render(tmplContent, dir, resourceName);
            }
        }
    });
}

var css;
function processLess(input, output){
    fs.readFile(input, 'utf-8', function (e, data){
        if (e){
            sys.puts("less: " + e.message);
            process.exit(1);
        }

        new(less.Parser)({
            paths: [path.dirname(input)],
            optimization: 1,
            filename: input
        }).parse(data, function (err, tree){
            if (err){
                less.writeError(err);
                process.exit(1);
            }else{
                try{
                    css = tree.toCSS({ compress: true });
                    if (output){
                        fd = fs.openSync(output, "w");
                        appUtil.statusLog('Compiling less');
                        appUtil.statusLog('Less input \t' + input);
                        appUtil.statusLog('Less output \t' + output);
                        fs.writeSync(fd, css, 0, "utf8");
                    }else{
                        sys.print(css);
                    }
                }catch (e){
                    less.writeError(e);
                    process.exit(2);
                }
            }
        });
    });
}

function readMarkdown(input, name, parseProps){
    // Summary
    //      1. Reads markdown file
    //      2. Preparing rendering callback

    appUtil.statusLog('Reading and parsing \t' + input);
    var markdown = fs.readFileSync(input, 'utf-8');
    var tree = appUtil.parseMarkdown(markdown);
    parseProps.errorMessage = "";
    parseProps[name] = true;
    tree.forEach(function(block, index){
        // Based on the tree array we create mustache blocks with custom callbacks
        // handling the mapping of Markdown elements to Mustache tags.
        parseProps['block' + index] = function(){
            return function(text, parse){
                // Looking for mustache comment '{{!boxName}}' which specifies the box's render method
                // we want to use, if none found we default to "simpleBox".
                var boxMatch = text.match(/[\s\S]*{{!(\w+)}}/);
                var func;
                if (boxMatch){
                    func = boxMatch[1];
                } else {
                    func = "simpleBox";
                    appUtil.consoleLog([
                        {tpl: templates.WARNING_USING_DEFAULT_RENDERER.TITLE, vars:{tplFileName:input, blockTitle:block[0][1]}},
                        {tpl: templates.WARNING_USING_DEFAULT_RENDERER.HINT, verbose:true}
                    ]);
                }
                try {
                    var parsedBlock = blocks[func]([].concat(block)); // Make a copy of the block, since the method modifies it inside.
                    return mustache.to_html(text, parsedBlock);
                }catch(e){
                    var errorMsg = {
                        title: mustache.to_html(templates.ERROR_MARKDOWN_SYNTAX.TITLE, {markdownFileName:input, blockTitle:block[0][1]}),
                        text: blocks[func].__docs__
                    };
                    errorMessages.push(errorMsg);
                    appUtil.consoleLog([
                        {msg: errorMsg.title},
                        {msg: '\n' + mustache.to_html(templates.ERROR_MARKDOWN_SYNTAX.HINT, {}), verbose:true},
                        {msg: '\n' + errorMsg.text, verbose:true}
                    ]);
                    return "";
                }
            }
        }
    });
}

function writeView(dir, fileName, tmplContent, parseProps){
    // Summary:
    //      Renders the parse properties into the template and
    //      write the output to the release directory.

    try {
        fs.mkdirSync(path.join(appConfig.releaseDir, dir), 0755);
    }catch(e){}

    if (errorMessages.length){
        var titles = [];
        var texts = [];
        errorMessages.forEach(function(m){ titles.push(m.title); texts.push(m.text); });
        parseProps.errorMessage = titles.join("\n") + "\n\n" + texts.join("\n************************\n");
    }

    var out = mustache.to_html(tmplContent, parseProps);

    // The markdown parser returns elements which don't require a closing tag with a closing tag
    // Converting

 to

    var voidTags = /^area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr$/i;
    out = out.replace(/<(\w+)([^<>]*)><\/\1>/g, function(_, tagName, attribs){
        var markup = "<" + tagName + attribs;
        markup += voidTags.test(tagName) ? " />" : "></" + tagName + ">";
        return markup;
    });

    // If debug is true we compress the HTML and write it to the filesystem
    var file = path.join(appConfig.releaseDir, dir, fileName);
    appUtil.statusLog('Writing\t' + file);
    fs.writeFile(file, appConfig.isDebug ? out : kompressor(out, true), encoding='utf8');
}