UNPKG

3.01 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * module dependencies
5 */
6
7var path = require('path');
8var Core = require('assemble-core');
9var plugins = require('./lib/plugins');
10var utils = require('./lib/utils');
11
12/**
13 * Create an `assemble` app. This is the main function exported
14 * by the assemble module.
15 *
16 * ```js
17 * var assemble = require('assemble');
18 * var app = assemble();
19 * ```
20 * @param {Object} `options` Optionally pass default options to use.
21 * @api public
22 */
23
24function Assemble(options) {
25 if (!(this instanceof Assemble)) {
26 return new Assemble(options);
27 }
28 Core.call(this, options);
29 this.is('assemble');
30 this.initAssemble();
31}
32
33/**
34 * Inherit `Core`
35 */
36
37Core.extend(Assemble);
38Core.bubble(Assemble);
39
40/**
41 * Initialize assemble and emit pre and post init events
42 */
43
44Assemble.prototype.initAssemble = function() {
45 Assemble.emit('assemble.preInit', this);
46 Assemble.initAssemble(this);
47 Assemble.emit('assemble.postInit', this);
48};
49
50/**
51 * Initialize Assemble defaults, plugins and views
52 */
53
54Assemble.initAssemble = function(app) {
55 Assemble.initDefaults(app);
56 Assemble.initPlugins(app);
57 Assemble.initViews(app);
58};
59
60/**
61 * Initialize defaults
62 */
63
64Assemble.initDefaults = function(app) {
65 var exts = app.options.exts || ['md', 'hbs', 'html'];
66
67 /**
68 * Default engine
69 */
70
71 app.engine(exts, require('engine-handlebars'));
72
73 /**
74 * Middleware for parsing front matter
75 */
76
77 app.onLoad(utils.extRegex(exts), function(view, next) {
78 // check options inside the middleware to account for options defined after init
79 if (view.options.frontMatter === false) {
80 next();
81 return;
82 }
83 if (app.options.frontMatter === false) {
84 next();
85 return;
86 }
87 utils.matter.parse(view, next);
88 });
89};
90
91/**
92 * Load default plugins. Built-in plugins can be disabled
93 * on the `assemble` options.
94 *
95 * ```js
96 * var app = assemble({
97 * plugins: {
98 * loader: false,
99 * store: false
100 * }
101 * });
102 * ```
103 */
104
105Assemble.initPlugins = function(app) {
106 enable('logger', plugins.logger);
107 enable('loader', plugins.loader);
108 enable('config', plugins.config);
109 enable('argv', plugins.argv);
110 enable('cli', plugins.cli);
111
112 function enable(name, fn) {
113 if (app.option('plugins') === false) return;
114 if (app.option('plugins.' + name) !== false) {
115 app.use(fn());
116 }
117 }
118};
119
120/**
121 * Built-in view collections
122 * | partials
123 * | layouts
124 * | pages
125 */
126
127Assemble.initViews = function(app) {
128 if (app.isFalse('collections')) return;
129
130 app.create('partials', {
131 engine: app.options.engine || 'hbs',
132 viewType: 'partial',
133 renameKey: function(fp) {
134 return path.basename(fp, path.extname(fp));
135 }
136 });
137
138 app.create('layouts', {
139 engine: app.options.engine || 'hbs',
140 viewType: 'layout',
141 renameKey: function(fp) {
142 return path.basename(fp, path.extname(fp));
143 }
144 });
145
146 app.create('pages', {
147 engine: app.options.engine || 'hbs'
148 });
149};
150
151/**
152 * Expose the `Assemble` constructor
153 */
154
155module.exports = Assemble;