Skip to content

Commit

Permalink
Merge pull request #70 from kss-node/pull-30-rebased
Browse files Browse the repository at this point in the history
Update Handlebars.js to resolve HTML entities problem
  • Loading branch information
JohnAlbin committed Jul 18, 2014
2 parents 1e3b6a0 + f6b01a3 commit 0000e2b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
74 changes: 34 additions & 40 deletions bin/kss-node
Expand Up @@ -15,7 +15,7 @@ var kss = require(__dirname + '/../lib/kss.js'),
fs = require('fs'),
template, styleguide,
generatePage, generateStylesheet,
options = {
config = {
templateDirectory: __dirname + '/../lib/template',
sourceDirectory: __dirname + '/../demo',
destinationDirectory: process.cwd() + '/styleguide'
Expand Down Expand Up @@ -87,42 +87,42 @@ if (argv.init) {
return;
}

options.sourceDirectory = path.relative(process.cwd(), argv['_'][0]);
config.sourceDirectory = path.relative(process.cwd(), argv['_'][0]);
if (argv['_'].length > 1) {
options.destinationDirectory = path.relative(process.cwd(), argv['_'][1]);
config.destinationDirectory = path.relative(process.cwd(), argv['_'][1]);
}
if (argv.template) {
options.templateDirectory = path.relative(process.cwd(), argv.template);
config.templateDirectory = path.relative(process.cwd(), argv.template);
}

console.log('');
console.log('Generating your KSS Styleguide!');
console.log('');
console.log(' * Source: ' + path.resolve(options.sourceDirectory));
console.log(' * Destination: ' + path.resolve(options.destinationDirectory));
console.log(' * Template: ' + path.resolve(options.templateDirectory));
console.log(' * Source: ' + path.resolve(config.sourceDirectory));
console.log(' * Destination: ' + path.resolve(config.destinationDirectory));
console.log(' * Template: ' + path.resolve(config.templateDirectory));
console.log('');

// Compile the Handlebars template
template = fs.readFileSync(options.templateDirectory + '/index.html', 'utf8');
template = fs.readFileSync(config.templateDirectory + '/index.html', 'utf8');
template = handlebars.compile(template);

// Create a new "styleguide" directory and copy the contents
// of "public" over.
try {
fs.mkdirSync(options.destinationDirectory);
fs.mkdirSync(config.destinationDirectory);
} catch (e) {}

wrench.copyDirSyncRecursive(
options.templateDirectory + '/public',
options.destinationDirectory + '/public'
config.templateDirectory + '/public',
config.destinationDirectory + '/public'
);

// Generate the static HTML pages in the next tick, i.e. after the other functions have
// been defined and handlebars helpers set up.
process.nextTick(function() {
console.log('...compiling KSS styles');
less.render('@import "' + path.relative(process.cwd(), options.destinationDirectory) + '/public/kss.less";', function(err, css) {
less.render('@import "' + path.relative(process.cwd(), config.destinationDirectory) + '/public/kss.less";', function(err, css) {
if (err) {
console.error(err);
throw err;
Expand All @@ -131,11 +131,11 @@ process.nextTick(function() {
css = cleanCss.process(css);

// Write the compiled LESS styles from the template.
fs.writeFileSync(options.destinationDirectory + '/public/kss.css', css, 'utf8');
fs.writeFileSync(config.destinationDirectory + '/public/kss.css', css, 'utf8');

console.log('...parsing your styleguide');

kss.traverse(options.sourceDirectory, {
kss.traverse(config.sourceDirectory, {
multiline : true,
markdown : true,
markup : true,
Expand Down Expand Up @@ -243,7 +243,7 @@ generateStylesheet = function(argv) {
saveStylesheet = function(buffer) {
buffer = cleanCss.process(buffer.toString());
fs.writeFileSync(
options.destinationDirectory + '/public/style.css',
config.destinationDirectory + '/public/style.css',
buffer, 'utf8'
);
};
Expand All @@ -256,7 +256,7 @@ generatePage = function(styleguide, sections, root, pages, sectionRoots) {
styleguide.section(root) ? styleguide.section(root).header() : 'Unnamed',
']'
);
fs.writeFileSync(options.destinationDirectory + '/section-'+root+'.html',
fs.writeFileSync(config.destinationDirectory + '/section-'+root+'.html',
template({
styleguide: styleguide,
sections: jsonSections(sections),
Expand All @@ -273,14 +273,14 @@ generatePage = function(styleguide, sections, root, pages, sectionRoots) {
generateIndex = function(styleguide, sections, pages, sectionRoots) {
try {
console.log('...generating styleguide overview');
fs.writeFileSync(options.destinationDirectory + '/index.html',
fs.writeFileSync(config.destinationDirectory + '/index.html',
template({
styleguide: styleguide,
sectionRoots: sectionRoots,
sections: jsonSections(sections),
rootNumber: 0,
argv: argv || {},
overview: marked(fs.readFileSync(options.sourceDirectory + '/styleguide.md', 'utf8'))
overview: marked(fs.readFileSync(config.sourceDirectory + '/styleguide.md', 'utf8'))
})
);
} catch(e) {
Expand Down Expand Up @@ -318,37 +318,36 @@ jsonModifiers = function(modifiers) {
* Equivalent to the {#if} block helper with multiple arguments.
*/
handlebars.registerHelper('ifAny', function() {
var argLength = arguments.length - 2,
content = arguments[argLength + 1],
var numItems = arguments.length - 1,
options = arguments[numItems],
success = true;

for (var i = 0; i < argLength; i += 1) {
for (var i = 0; i < numItems; i += 1) {
if (!arguments[i]) {
success = false;
break;
}
}

return success ? content(this) : content.inverse(this);
return success ? options.fn(this) : options.inverse(this);
});

/**
* Returns a single section, found by its reference number
* @param {String|Number} reference The reference number to search for.
*/
handlebars.registerHelper('section', function(reference) {
handlebars.registerHelper('section', function(reference, options) {
var section = styleguide.section(reference);
if (!section) return false;

return arguments[arguments.length-1](section.data);
return section ? options.fn(section.data) : false;
});

/**
* Loop over a section query. If a number is supplied, will convert into
* a query for all children and descendants of that reference.
* @param {Mixed} query The section query
*/
handlebars.registerHelper('eachSection', function(query) {
handlebars.registerHelper('eachSection', function(query, options) {
var sections,
i, l, buffer = "";

Expand All @@ -360,7 +359,7 @@ handlebars.registerHelper('eachSection', function(query) {

l = sections.length;
for (i = 0; i < l; i += 1) {
buffer += arguments[arguments.length-1](sections[i].data);
buffer += options.fn(sections[i].data);
}

return buffer;
Expand All @@ -369,7 +368,7 @@ handlebars.registerHelper('eachSection', function(query) {
/**
* Loop over each section root, i.e. each section only one level deep.
*/
handlebars.registerHelper('eachRoot', function() {
handlebars.registerHelper('eachRoot', function(options) {
var sections,
i, l, buffer = "";

Expand All @@ -378,7 +377,7 @@ handlebars.registerHelper('eachRoot', function() {

l = sections.length;
for (i = 0; i < l; i += 1) {
buffer += arguments[arguments.length-1](sections[i].data);
buffer += options.fn(sections[i].data);
}

return buffer;
Expand All @@ -393,34 +392,29 @@ handlebars.registerHelper('eachRoot', function() {
* ANYTHING ELSE
* {{/refDepth}}
*/
handlebars.registerHelper('whenDepth', function(depth, context) {
if (!(context && this.refDepth)) {
handlebars.registerHelper('whenDepth', function(depth, options) {
if (!this.refDepth) {
return '';
}
if (depth == this.refDepth) {
return context(this);
}
if (context.inverse) {
return context.inverse(this);
}
return (depth == this.refDepth) ? options.fn(this) : options.inverse(this);
});

/**
* Similar to the {#eachSection} helper, however will loop over each modifier
* @param {Object} section Supply a section object to loop over it's modifiers. Defaults to the current section.
*/
handlebars.registerHelper('eachModifier', function(section) {
handlebars.registerHelper('eachModifier', function(options) {
var modifiers, i, l, buffer = '';

// Default to current modifiers, but allow supplying a custom section
if (section.data) modifiers = section.data.modifiers;
if (options.data) modifiers = options.data.modifiers;
modifiers = modifiers || this.modifiers || false;

if (!modifiers) return {};

l = modifiers.length;
for (i = 0; i < l; i++) {
buffer += arguments[arguments.length-1](modifiers[i].data || '');
buffer += options.fn(modifiers[i].data || '');
}
return buffer;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"marked": "0.1.x",
"natural": "0.0.65",
"handlebars": "1.3.0",
"handlebars": "1.3.x",
"wrench": "1.3.x",
"less": "1.x.x",
"stylus": "0.46.x",
Expand Down

0 comments on commit 0000e2b

Please sign in to comment.