Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Give Fabricator Assemble the ability to output materials/partials in … #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,20 @@ Error handler function. Receives an `error` object param.
Type: `String`
Default: `dist`

Destination of compiled views (where files are saved to)
### options.materialPreviews

Type: `String`
Default: `null`

Destination of independently compiled materials for preview and name of wrapper template

E.g.:

```javascript
materialPreviews : "preview"
```

will output all independently rendered materials files into the directory "preview" and use the "preview.html" layout template to wrap them.

## Usage

Expand Down
37 changes: 37 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ var defaults = {
*/
dest: 'dist',

/**
* Location to write material preview files
* @type {String}
*/
materialPreviews: null,

/**
* beautifier options
* @type {Object}
Expand Down Expand Up @@ -137,6 +143,12 @@ var assembly = {
*/
materialData: {},

/**
* Each material's independently rendered state
* @type {Object}
*/
materialRender: {},

/**
* Meta data for user-created views (views in views/{subdir})
* @type {Object}
Expand Down Expand Up @@ -332,12 +344,14 @@ var parseMaterials = function () {
// capture meta data for the material
if (!isSubCollection) {
assembly.materials[collection].items[key] = {
filename: id,
name: toTitleCase(id),
notes: (fileMatter.data.notes) ? md.render(fileMatter.data.notes) : '',
data: localData
};
} else {
assembly.materials[parent].items[collection].items[key] = {
filename: id,
name: toTitleCase(id.split('.')[1]),
notes: (fileMatter.data.notes) ? md.render(fileMatter.data.notes) : '',
data: localData
Expand All @@ -364,6 +378,7 @@ var parseMaterials = function () {

// register the partial
Handlebars.registerPartial(id, content);
assembly.materialRender[id] = content;

});

Expand Down Expand Up @@ -646,9 +661,31 @@ var assemble = function () {
fs.writeFileSync(copyPath, template(context));
}
});
if(options.materialPreviews) {
createIndependentMaterialPreviews();
}

};

/**
* Assemble the unique materials into independent preview files
*/
var createIndependentMaterialPreviews = function() {

var materials = assembly.materialRender;
for(var material in materials) {
var materialTemplate = "{{> "+material+"}}";

var source = wrapPage(materialTemplate, assembly.layouts[options.materialPreviews || options.layout]),
context = assembly.materialData[material],
template = Handlebars.compile(source);

var copyPath = path.normalize(path.join(options.dest, options.materialPreviews, path.basename(material)));
mkdirp.sync(path.dirname(copyPath));
fs.writeFileSync(copyPath+".html", template(context));
}
};


/**
* Module exports
Expand Down