var blocks = {},
pages = {},
files = [],
dir, fileRepo, key, comment, page, details, ext, input, foundsection;
function anchorize(match, p1, p2, offset, string) {
return '</div><div id="' + key + '-s' + (foundsection++) + '" class="section md"><h' + p1 + '>' + p2 + '</h' + p1 + '>';
}
module.exports = function(config) {
var i, j;
var opts = {},
owd = process.cwd();
if (!fs.existsSync(config.outputAbsolute)) {
mkdirp.sync(config.outputAbsolute);
}
ncp(path.join(__dirname, 'public'), path.join(config.outputAbsolute, '__assets'), function(err) {
if (err) {
console.log(err);
}
});
var matches = [];
process.chdir(config.configDir);
for (var section in config.files) {
expand({
filter: 'isFile'
}, config.files[section]).forEach(function(f) {
matches.push(path.join(config.configDir, f));
});
}
process.chdir(owd);
config.assets = config.assets || [];
config.assets.forEach(function(asset) {
ncp(path.join(config.configDir, asset), path.join(config.outputAbsolute, '__assets', path.basename(asset)), function(err) {
if (err) {
console.log(err);
}
});
});
matches.forEach(function(file) {
ext = path.extname(file).slice(1);
input = fs.readFileSync(file, 'utf8');
fileRepo = path.relative(process.cwd(), file);
key = fileRepo.replace(/\//g, '_').replace(/\./g, '_').toLowerCase();
dir = path.dirname(path.relative(process.cwd(), file)).toLowerCase();
if (dir === '.') {
dir = '';
}
dir = dir + '/';
files.push({
file: path.basename(path.relative(process.cwd(), file)),
dir: dir,
key: key
});
if (ext === 'md') {
foundsection = 1;
i = 1;
input = marked(input, {
renderer: renderer
});
page = input.match(/<h([1-3])>(.*)<\/h[1-3]>/gi);
for (j in page) {
details = {
page: page[j].match(/<h[1-3]>(.*)<\/h[1-3]>/)[1],
section: i++,
h: page[j].match(/<h([1-3])>/)[1],
key: key
};
details.page = strip(details.page);
pages[key] = pages[key] || [];
pages[key].push(details);
}
var content = '<div>' + input.replace(/<h([1-3])>(.*)<\/h[1-3]>/gi, anchorize) + '</div>';
blocks[key] = {
md: true,
file: file,
fileRepo: fileRepo,
key: key,
block: content
};
} else {
opts.ext = ext;
opts.ctypes = config.ctypes;
opts.encode = false;
opts.sanitize = true;
opts.ignores = config.ignores || {};
noddocco.process(input, opts, function(err, noddoccoData) {
if (err) {
console.log(err);
} else {
blocks[key] = {
file: file,
fileRepo: fileRepo,
key: key,
blocks: noddoccoData
};
for (i in noddoccoData) {
comment = noddoccoData[i].comments;
page = comment.match(/<h([1-3])>(.*)<\/h[1-3]>/i);
if (page) {
details = {
page: page[2],
section: (+i + 1),
h: page[1],
key: key
};
pages[key] = pages[key] || [];
pages[key].push(details);
}
}
}
});
}
});