Commit 3c43f201 authored by Daniel Shumway's avatar Daniel Shumway

dep: trying to add Raise as a dependency

Everything is a bit broken right now
parent a974e35e

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.

var _ = require('lodash');
var path = require('path');
var fs = require('fs-extra');
var globby = require('globby');
var matcher = require('matcher');
var asciidoctor = (function () {
require('asciidoctor.js-pug');
return require('asciidoctor.js')();
}());
/*
* File extension patterns that I care about.
*/
var EXTENSIONS = (function () {
var templates = {
PAGE : ['*.page.*'],
TEMPLATE : ['*.template.*'],
STYLE : ['*.style.*'],
DRAFT : ['*.draft.*']
//TODO: add explicit COPY in case you want a file to be otherwise ignored.
};
return templates;
}());
async function loaddir(directory) {
var sources = await globby('**/*', {
cwd : directory
});
return _.map(sources, function (path) {
return {
//TODO: separate path and name
path : path,
content : null,
attributes : {}, // Only set for pages right now
source : directory,
};
});
}
async function compile_pages(pages, options) {
/* Set page attributes - these will get passed into the final template system */
var docs = await (async function setattributes(pages) {
var docs = _.filter(pages, function (page) {
var name = path.parse(page.path).base;
return matcher([name], EXTENSIONS.PAGE).length;
});
_.each(docs, function (doc) {
doc.attributes = asciidoctor.load(doc.content).getAttributes();
});
return docs;
}(pages));
/*
* Rewrite paths and attributes to align with each other:
* - set ``path`` to ``attribute.url`` (if it exists)
* - resolve attribute paths (url, stylesheet, template_dir)
*
* If the path is absolute, it's treated as starting in the home directory,
* otherwise it's treated as starting from the current page path.
*
* If a path doesn't end with an extension, assume it points to a directory.
*
* - NOTE: for a first pass implementation, _only_ directories are supported for most paths.
* - NOTE: for a first pass implementation, ``stylesheet`` is assumed to _always_ be a file.
* - NOTE: for a first pass implementation, assume that absolute paths are always '/'.
*/
(function (docs) {
//You can pass in plural urls, but they'll be combined back into a string.
//So you'll need to re-split them via space later if you want to access.
function normalize (base, urls) {
urls = urls ? urls.split(' ') : [''];
base = path.parse(base).dir;
return _.map(urls, function (url) {
if (!path.isAbsolute(url)) {
url = '/' + path.join(base, url);
}
return url.slice(1); //Strip leading '/', see the NOTE above.
}).join(' ');
}
//TODO: check if there's a cleaner way to do this.
_.each(docs, function (doc) {
var attrs = {
url : function (url) {
return path.extname(url) ? url : path.join(url, './index.html');
},
stylesheet : function () {},
template_dir : function () {},
template_dirs : function () {}
};
_.each(attrs, function (modify, attr) {
if (doc.attributes[attr] == null) { return; }
doc.attributes[attr] = modify(
normalize(doc.path, doc.attributes[attr]));
});
//Set path last so it doesn't interfere with the above logic.
doc.path = doc.attributes.url;
});
}(docs));
/*
* Load templates - write onto map in correct place
* Load less templates - write onto map in correct place
* Load docs again with new attributes/templates and then rewrite them in place (change PAGE to standard html).
*/
return pages;
}
/*
* Actual build process:
* - for the most part, this is the only thing you should be calling
*/
async function build(sources, options) {
sources = _.isArray(sources) ? sources : [sources];
/*
* Loop over all sources and compile them into a single set of files.
* For a description of what a "file" looks like, see ``loaddir``.
*/
var files = await (async function () {
var file_array = _.map(sources, function (source) {
//TODO: users shouldn't be forced to manually pass in files.
//They should be able to use some kind of sitemap
return _.isString(source) ? loaddir(source) : source;
});
return Promise.all(file_array)
.then(function (file_array) {
/*
* To maintain compatability with AsciiDoctor logic
* later directories in an array of sources should override earlier directories
*
* By default, lodash ``unionBy`` will defer to the first encountered instance of an element.
*/
file_array = _.reverse(file_array);
return _.unionBy.apply(_, file_array.concat('path')); //NOTE: unionBy expects arguments to be split. Annoying, I know.
});
}());
/*
* Set file content:
*
* Will ignore any files that already have their content set.
* This allows you to preload content or add virtual directories.
*/
files = await (async function loadcontent(files) {
var loads = _.map(files, function (file) {
if (file.content != null) { return file.content; }
return fs.readFile(
path.join(file.source, file.path), 'utf8');
});
return await Promise.all(loads)
.then(function (loads) {
_.each(loads, function (content, index) {
files[index].content = content;
});
return files;
});
}(files));
files = await compile_pages(files, options);
return files;
}