Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 53a3091

Browse files
committed
working version of pseudo patterns without DRYness
1 parent 453497f commit 53a3091

File tree

10 files changed

+79
-8
lines changed

10 files changed

+79
-8
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PL-node-v0.8.0
66
- FIX: support for displaying the HTML and Mustache in the code viewer
77
- ADD: pattern link support
88
- CHG: updated included mustache templates to reflect pattern links in navigation and compiling pages direct from templates
9+
- THX: @getsetbro for finding and fixing a typo
910

1011
PL-node-v0.1.7
1112
- ADD: pattern export

builder/object_factory.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"use strict";
1313

1414
var oPattern = function(subdir, filename, data){
15+
//console.log(filename);
1516
this.fileName = filename.substring(0, filename.indexOf('.'));
1617
this.subdir = subdir;
1718
this.name = (subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName).replace(/\\/g, '-'); //this is the unique name with the subDir

builder/pattern_assembler.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
(function () {
2-
"use strict";
3-
2+
"use strict";
3+
4+
var fs = require('fs-extra'),
5+
path = require('path');
6+
7+
var pattern_assembler = function(){
8+
9+
function exportPatterns(patternlab){
10+
11+
}
12+
13+
return {
14+
export_patterns: function(patternlab){
15+
exportPatterns(patternlab);
16+
}
17+
};
18+
19+
};
20+
21+
module.exports = pattern_assembler;
422

523
}());

builder/patternlab.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ var patternlab_engine = function(){
1414
extend = require('util')._extend,
1515
diveSync = require('diveSync'),
1616
mustache = require('mustache'),
17+
glob = require('glob'),
1718
of = require('./object_factory'),
1819
pa = require('./pattern_assembler'),
1920
mh = require('./media_hunter'),
2021
lh = require('./lineage_hunter'),
2122
pe = require('./pattern_exporter'),
23+
pa = require('./pattern_assembler'),
2224
he = require('html-entities').AllHtmlEntities,
2325
patternlab = {};
2426

@@ -53,6 +55,8 @@ var patternlab_engine = function(){
5355
}
5456

5557
function buildPatterns(callback){
58+
var assembler = new pa();
59+
5660
patternlab.data = fs.readJSONSync('./source/_data/data.json');
5761
patternlab.listitems = fs.readJSONSync('./source/_data/listitems.json');
5862
patternlab.header = fs.readFileSync('./source/_patternlab-files/pattern-header-footer/header.html', 'utf8');
@@ -101,8 +105,6 @@ var patternlab_engine = function(){
101105
}
102106
currentPattern.template = fs.readFileSync(abspath, 'utf8');
103107

104-
//look for a pseudo pattern by checking if there is a file containing same name, with ~ in it, ending in .json
105-
106108
//find pattern lineage
107109
var lineage_hunter = new lh();
108110
lineage_hunter.find_lineage(currentPattern, patternlab);
@@ -122,6 +124,44 @@ var patternlab_engine = function(){
122124
}
123125
patternlab.partials[partialname] = currentPattern.template;
124126

127+
//look for a pseudo pattern by checking if there is a file containing same name, with ~ in it, ending in .json
128+
var needle = currentPattern.subdir + '/' + currentPattern.fileName+ '~*.json';
129+
var pseudoPatterns = glob.sync(needle, {
130+
cwd: 'source/_patterns/', //relative to gruntfile
131+
debug: false,
132+
nodir: true,
133+
});
134+
135+
if(pseudoPatterns.length > 0){
136+
for(var i = 0; i < pseudoPatterns.length; i++){
137+
//we want to do everything we normally would here, except instead head the pseudoPattern data
138+
var variantFileData = fs.readJSONSync('source/_patterns/' + pseudoPatterns[i]);
139+
140+
//extend any existing data with variant data
141+
variantFileData = extend(variantFileData, currentPattern.data);
142+
143+
var variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0];
144+
var patternVariant = new of.oPattern(subdir, currentPattern.fileName + '-' + variantName + '.mustache', variantFileData);
145+
146+
//see if this file has a state
147+
if(patternlab.config.patternStates[patternVariant.patternName]){
148+
patternVariant.patternState = patternlab.config.patternStates[patternVariant.patternName];
149+
} else{
150+
patternVariant.patternState = "";
151+
}
152+
153+
//use the same template as the non-variant
154+
patternVariant.template = currentPattern.template;
155+
156+
//find pattern lineage
157+
lineage_hunter.find_lineage(patternVariant, patternlab);
158+
159+
//add to patternlab object so we can look these up later.
160+
patternlab.data.link[patternVariant.patternGroup + '-' + patternVariant.patternName] = '/patterns/' + patternVariant.patternLink;
161+
patternlab.patterns.push(patternVariant);
162+
}
163+
}
164+
125165
//add to patternlab object so we can look these up later.
126166
patternlab.data.link[currentPattern.patternGroup + '-' + currentPattern.patternName] = '/patterns/' + currentPattern.patternLink;
127167
patternlab.patterns.push(currentPattern);

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"ignored-directories" : ["scss"],
88
"contentSyncPort" : 8002,
99
"navSyncPort" : 8003,
10-
"debug": false,
10+
"debug": true,
1111
"ishControlsVisible": {
1212
"s": true,
1313
"m": true,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"matchdep": "^0.3.0",
1717
"fs-extra": "^0.14.0",
1818
"diveSync": "^0.2.1",
19-
"html-entities": "^1.1.1"
19+
"html-entities": "^1.1.1",
20+
"glob": "^4.3.5"
2021
},
2122
"keywords": [
2223
"Pattern Lab",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="alert alert-{{ alertClass }}">
2+
{{ excerpt.short }}
3+
</div>

source/_patterns/03-templates/00-homepage.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{> organisms-header }}
33
<div role="main">
44
{{# emergency }}
5-
{{> molecules-alert:error }}
5+
{{> molecules-alert }}
66
{{/ emergency }}
77
{{# hero }}
88
{{> molecules-block-hero }}

source/_patterns/04-pages/00-homepage.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"title" : "Home Page",
33
"bodyClass": "home",
4-
"emergency" : false,
54
"hero" : [
65
{
76
"img": {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"emergency": {
3+
"alertClass" : "error",
4+
"excerpt" : {
5+
"short" : "Emergency! This is a variation of the core homepage template."
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)