Socket
Socket
Sign inDemoInstall

grunt-generator

Package Overview
Dependencies
41
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.0 to 0.4.0

spec/build_custom/custom_delim_frontmatter.html

14

CHANGELOG.md
# Changelog
## 0.4.0
- allows custom delimiters for frontmatter
- allows optional start delimiter for frontmatter, instead of just ending one (note: must be at start of file
- allows YAML frontmatter parsing (at the task level right now, can't be specified per file)
## 0.3.2
- added test for custom markdown engine (marked in the example)
## 0.3.1
- Dustjs partials support (master templates are still used like handlebars, although the functionality could be replaced by partials).
## 0.3.0

@@ -4,0 +18,0 @@

31

Gruntfile.js
var path = require('path');
var marked = require('marked');

@@ -80,2 +81,3 @@ var handlebarsHelpers = {

templateEngine: 'dust',
partialsGlob: 'spec/dust_pages/partials/*.{md,html}',
helpers: dustHelpers

@@ -94,6 +96,31 @@ }

}
},
customMarkdownEngine: {
files: [
{ cwd: 'spec/pages', src: ['**/*'], dest: 'spec/build_marked', ext: '.html' }
],
options: {
templates: 'spec/templates',
helpers: handlebarsHelpers,
partialsGlob: 'spec/pages/partials/*.html',
processors: {
'md' : marked
}
}
},
customFrontmatter: {
files: [
{ cwd: 'spec/custom_pages', src: ['**/*'], dest: 'spec/build_custom', ext: '.html' }
],
options: {
templates: 'spec/templates',
helpers: handlebarsHelpers,
partialsGlob: 'spec/pages/partials/*.html',
frontmatterDelimiter: '###',
frontmatterType: 'yaml'
}
}
},
clean: {
test: ['spec/build', 'spec/dust_build']
test: ['spec/build', 'spec/dust_build', 'spec/build_custom', 'spec/build_marked']
}

@@ -108,4 +135,4 @@ });

// Default task.
grunt.registerTask('default', ['jshint', 'clean:test', 'generator:test', 'generator:dustjs', 'exec:jasmine']);
grunt.registerTask('default', ['jshint', 'clean:test', 'generator:test', 'generator:dustjs', 'generator:customMarkdownEngine', 'generator:customFrontmatter', 'exec:jasmine']);
};

8

package.json
{
"name": "grunt-generator",
"description": "Static site generator with HTML and markdown authoring and multiple template engine support (handlebars, dustjs).",
"version": "0.3.0",
"version": "0.4.0",
"homepage": "https://github.com/clavery/grunt-generator",

@@ -37,3 +37,4 @@ "author": {

"grunt-exec": "*",
"grunt-contrib-clean": "~0.4.0"
"grunt-contrib-clean": "~0.4.0",
"marked": "~0.2.10"
},

@@ -53,4 +54,5 @@ "keywords": [

"dustjs-linkedin": "~2.0.1",
"dustjs-helpers": "~1.1.1"
"dustjs-helpers": "~1.1.1",
"js-yaml": "~2.1.3"
}
}

@@ -120,2 +120,4 @@ # grunt-generator

Note: Dust templates support the dust partials syntax but it is not recommended to mix HTML/Markdown partials. Since the markdown compilation for pages occurs after the partials are processed HTML partials mixed into Markdown pages may produce undesired results.
#### Custom

@@ -146,2 +148,24 @@

##### Custom Frontmatter
The frontmatter can be changed to use custom delimiters and also to use YAML syntax instead of JSON:
```javascript
generator: {
dev: {
files: [
{ cwd: 'pages', src: ['**/*'], dest: 'build', ext: '.html' }
],
options: {
templates: 'templates',
helpers: helpers,
environment: 'dev',
frontmatterDelimiter: '###',
frontmatterType: 'yaml'
}
}
}
...
```
[q]: [https://github.com/kriskowal/q/]

@@ -148,0 +172,0 @@

@@ -7,2 +7,3 @@ /*global xit*/

var _ = require('lodash');
var marked = require('marked');

@@ -76,2 +77,39 @@ describe('generator', function() {

it('should allow dustjs partials syntax', function() {
var test = grunt.file.read(__dirname + '/dust_build/dust_partials.html');
expect(test).toMatch(/This is a header partial: dust_partials/m);
});
it('should allow custom markdown engines', function() {
var test1 = grunt.file.read(__dirname + '/build_marked/test1.html'),
test2 = grunt.file.read(__dirname + '/build_marked/testdir/test2.html'),
test3 = grunt.file.read(__dirname + '/build_marked/test3.html');
expect(test1).toMatch(/<html>/m);
expect(test1).toMatch(/<title>test1 title<\/title>/m);
expect(test1).toMatch(/<p>this is a test<\/p>/m);
expect(test1).toMatch(/<\/html>/m);
expect(test3).toMatch(/<title>default title<\/title>/m);
});
it('should allow a start delimeter for frontmatter', function() {
var test1 = grunt.file.read(__dirname + '/build/start_delim.html');
expect(test1).toMatch(/<html>/m);
expect(test1).toMatch(/<title>test1 title<\/title>/m);
expect(test1).toMatch(/<p>this is a test<\/p>/m);
expect(test1).toMatch(/<\/html>/m);
});
it('should allow a custom frontmatter delimeter and type', function() {
var test1 = grunt.file.read(__dirname + '/build_custom/custom_delim_frontmatter.html');
expect(test1).toMatch(/<html>/m);
expect(test1).toMatch(/<title>custom delim and frontmatter title<\/title>/m);
expect(test1).toMatch(/<p>this is a test<\/p>/m);
expect(test1).toMatch(/<p>test first name<\/p>/m);
expect(test1).toMatch(/<\/html>/m);
});
//TODO

@@ -78,0 +116,0 @@ xit('should handle empty/no templates', function() {

@@ -33,3 +33,5 @@ /*

'helpers': {},
'compressWhitespace' : false
'compressWhitespace' : false,
'frontmatterDelimiter' : '---',
'frontmatterType' : 'json'
});

@@ -36,0 +38,0 @@ var generator = new Generator(grunt, options, this);

@@ -21,2 +21,3 @@ /*

var dustjsHelpers = require('dustjs-helpers');
var yaml = require('js-yaml');

@@ -73,2 +74,11 @@ /*

var frontmatterParsers = {
'json' : function(jsonString) {
return JSON.parse(jsonString);
},
'yaml' : function(yamlString) {
return yaml.safeLoad(yamlString);
}
};
/**

@@ -109,2 +119,10 @@ * @class Generator

this.frontmatterRegex = new RegExp('^' + this.options.frontmatterDelimiter + '$', 'm');
try {
this.frontmatterParser = frontmatterParsers[this.options.frontmatterType.toLowerCase()];
} catch(e) {
grunt.log.error('bad frontmatter type: ' + this.options.frontmatterType);
}
this.options.grunt = grunt;

@@ -117,4 +135,3 @@ this.options.gruntConfig = grunt.config.data;

if(this.options.partialsGlob &&
this.options.partialsGlob !== '' &&
this.options.templateEngine === 'handlebars') {
this.options.partialsGlob !== '') {

@@ -125,6 +142,12 @@ var partials = grunt.file.expand(this.options.partialsGlob);

partials.forEach(function(v, i) {
var contents = grunt.file.read(v);
var name = path.basename(v, path.extname(v));
var contents = grunt.file.read(v);
var name = path.basename(v, path.extname(v));
Handlebars.registerPartial(name, contents);
if(me.options.templateEngine === 'handlebars') {
Handlebars.registerPartial(name, contents);
} else if(me.options.templateEngine === 'dust') {
var templateName = name;
var source = dust.compile(contents, templateName);
dust.loadSource(source);
}
});

@@ -158,3 +181,3 @@ }

var fileContents = grunt.file.read(fullpath);
var parsed = fileContents.split(/^---$/m, 2);
var parsed = fileContents.split(me.frontmatterRegex, 3);
var frontMatter = null;

@@ -166,4 +189,10 @@ var extension = path.extname(filepath);

try {
if(parsed.length === 2) {
frontMatter = JSON.parse(parsed.shift());
if(parsed.length > 1) {
frontMatter = parsed.shift();
if(frontMatter.match(/\A[\s\r\n]*\Z/mg) !== null || frontMatter.length === 0) {
frontMatter = parsed.shift();
}
frontMatter = me.frontmatterParser(frontMatter);
}

@@ -170,0 +199,0 @@ } catch(e) {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc