<commit_before>'use strict'

let path  = require('path'),
    fs    = require('fs'),
    async = require('async'),
    ejs   = require('./lib/ejs')

/*
 * Rename the extension of a file in a path.
 * @param {string} filePath - Path to a file.
 * @param {string} fileExt - New extension of the file.
 * @returns {string} filePath - Path ending with the new fileExt.
 */
const renameExtension = function(filePath, fileExt) {

	let parsedPath = path.parse(filePath)

	return parsedPath.dir + path.sep + parsedPath.name + '.' + fileExt

}

/*
 * Read and parse a JSON-file.
 * @param {string} filePath - Path to a valid JSON file.
 * @returns {object} JSON
 */
const readJson = function(filePath) {

	let data = fs.readFileSync(filePath)

	return JSON.parse(data)

}

/*
 * Load EJS and transform to HTML.
 * @public
 * @param {string} filePath - Absolute path to the requested file.
 * @param {string} srcPath - Absolute path to the source folder.
 * @param {string} distPath - Absolute path to the export folder.
 * @param {object} route - The route which matched the request URL.
 * @param {function} next - The callback that handles the response. Receives the following properties: err, result, savePath.
 */
module.exports = function(filePath, srcPath, distPath, route, next) {

	filePath = renameExtension(filePath, 'ejs')

	let folderPath   = path.dirname(filePath),
	    savePath     = renameExtension(filePath.replace(srcPath, distPath), 'html'),
	    relativePath = path.relative(srcPath, filePath),
	    globalsPath  = path.join(process.cwd(), 'globals.json'),
	    dataPath     = path.join(folderPath, '_data.json')

	let globals     = readJson(globalsPath),
	    current     = path.parse(relativePath),
	    locals      = readJson(dataPath)[current.name],
	    environment = process.env

	let data = {
		globals,
		locals,
		current,
		environment
	}

	async.waterfall([

		(next)      => fs.readFile(filePath, 'utf8', next),
		(str, next) => ejs(filePath, str, data, next)

	], (err, str) => {

		if (err!=null) {
			next(err, null, null)
			return false
		}

		next(null, str, savePath)

	})

}<commit_msg>Changed the data which will be passend to ejs<commit_after>'use strict'

let path  = require('path'),
    fs    = require('fs'),
    async = require('async'),
    ejs   = require('./lib/ejs')

/*
 * Rename the extension of a file in a path.
 * @param {string} filePath - Path to a file.
 * @param {string} fileExt - New extension of the file.
 * @returns {string} filePath - Path ending with the new fileExt.
 */
const renameExtension = function(filePath, fileExt) {

	let parsedPath = path.parse(filePath)

	return parsedPath.dir + path.sep + parsedPath.name + '.' + fileExt

}

/*
 * Read and parse a JSON-file.
 * @param {string} filePath - Path to a valid JSON file.
 * @returns {object} JSON
 */
const readJson = function(filePath) {

	let data = fs.readFileSync(filePath)

	return JSON.parse(data)

}

/*
 * Load EJS and transform to HTML.
 * @public
 * @param {string} filePath - Absolute path to the requested file.
 * @param {string} srcPath - Absolute path to the source folder.
 * @param {string} distPath - Absolute path to the export folder.
 * @param {object} route - The route which matched the request URL.
 * @param {function} next - The callback that handles the response. Receives the following properties: err, result, savePath.
 */
module.exports = function(filePath, srcPath, distPath, route, next) {

	filePath = renameExtension(filePath, 'ejs')

	let savePath     = renameExtension(filePath.replace(srcPath, distPath), 'html'),
	    dataPath     = path.join(process.cwd(), 'data.json'),
	    relativePath = path.relative(srcPath, filePath)

	let current     = path.parse(relativePath),
	    environment = process.env,
	    dataJSON    = readJson(dataPath),
	    globalData  = dataJSON['*'] || {},
	    pageData    = dataJSON[current.name] || {}

	let data = Object.assign({}, globalData, pageData, {
		current,
		environment
	})

	async.waterfall([

		(next)      => fs.readFile(filePath, 'utf8', next),
		(str, next) => ejs(filePath, str, data, next)

	], (err, str) => {

		if (err!=null) {
			next(err, null, null)
			return false
		}

		next(null, str, savePath)

	})

}
{'fraction_non_alphanumeric': 0.13262158956109135, 'fraction_numerical': 0.00047449584816132857, 'mean_word_length': 3.4008350730688934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4238289', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1325, 'n_words': 414}