Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Add options to change the generated file extension and prevent the expansion of subpaths for dest #32

Open
wants to merge 10 commits 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
117 changes: 63 additions & 54 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,72 @@
'use strict';

module.exports = function (grunt) {
grunt.loadTasks('tasks');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
module.exports = function(grunt) {
grunt.loadTasks('tasks');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-jsbeautifier');

grunt.initConfig({
swig: {
development: {
init: {
allowErrors: false,
autoescape: true
grunt.initConfig({
swig: {
development: {
init: {
allowErrors: false,
autoescape: true
},
dest: 'test/dest',
src: ['**/*.swig', '!templates/*.swig'],
siteUrl: 'http://mydomain.net/',
generateSitemap: true,
generateRobotstxt: true,
test: {
var1: 'long path file',
var2: 'short path file'
},
sitemap_priorities: {
'_DEFAULT_': '0.7',
'fixtures/index.html': 0.8
}
}
},
dest: 'test/dest',
src: ['**/*.swig', '!templates/*.swig'],
siteUrl: 'http://mydomain.net/',
generateSitemap: true,
generateRobotstxt: true,
test: {
var1: 'long path file',
var2: 'short path file'
jshint: {
options: {
'jshintrc': '.jshintrc',
'reporter': 'jslint',
'reporterOutput': 'jslint.xml',
'force': true
},
all: [
'Gruntfile.js',
'tasks/*.js'
]
},
sitemap_priorities: {
'_DEFAULT_': '0.7',
'fixtures/index.html': 0.8
mochaTest: {
options: {
reporter: 'xunit',
captureFile: 'tests.xml'
},
files: ['test/*_test.js']
},
clean: {
files: 'test/dest'
},
jsbeautifier: {
files: [
'tasks/**/*.js',
'Gruntfile.js'
],
options: {}
}
}
},
jshint: {
options: {
'jshintrc': '.jshintrc',
'reporter': 'jslint',
'reporterOutput': 'jslint.xml',
'force': true
},
all: [
'Gruntfile.js',
'tasks/*.js'
]
},
mochaTest: {
options: {
reporter: 'xunit',
captureFile: 'tests.xml'
},
files: ['test/*_test.js']
},
clean: {
files: 'test/dest'
}
});
});

grunt.registerTask('test', [
'clean',
'swig',
'jshint',
'mochaTest',
]);
grunt.registerTask('test', [
'clean',
'swig',
'jshint',
'mochaTest',
]);

grunt.registerTask('default', 'test');
grunt.registerTask('default', 'test');
grunt.registerTask('beautify', ['jsbeautifier']);
};
94 changes: 59 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

> A static site compiler for grunt based on [swig templates](http://paularmstrong.github.com/swig/)

## Version 1.0.0

This branch will serve as the integration point for the next release: v1.0.0.

[Check out the discussion of v1.0.0 features](https://github.com/rtgibbons/grunt-swig/issues/33)

The latest stable release is [v0.2.1](https://github.com/rtgibbons/grunt-swig/tree/master) which can be found in the master branch

## Call for Help

As time has passed, I haven't had a need to use this beyond the initial creation of the task. I've tried to keep up with it, but I no longer have the time to bug fix and verify the pull requests. If you are interested in helping out, get some pull requests coming in and let me know you are interested in maintaining this package.
Expand Down Expand Up @@ -32,51 +40,67 @@ In your project's Gruntfile, add a section named `swig` to the data object passe

```js
swig: {
development: {
init: {
autoescape: true
// The default options. If the task does not specify an 'options' object, this will be used instead. Or it can be omitted entirely.
options: {
generateSitemap: false,
templateData: {
greeting: "Hello Default Greeting!"
},
},
dev: {
options: {
generateSitemap: true, // Whether or not to generate a sitemap
siteUrl: 'http://mydomaindev.net/', // Used when sitemap is generated
templateData: {
greeting: "Hello overridden Greeting!",
myVar: "some other variable"
},
// The sitemap weightings. If omitted, the default will be used
sitemap: {
'default': { // Override the default sitemap options. This will be used if the specific file is not specified.
changefreq: 'never',
lastmod: '2013-01-01',
priority: '0.5'
},
'index.html': {
changefreq: 'daily',
lastmod: '2009-01-01',
priority: '0.8'
},
'test/index.html': {
changefreq: 'monthly',
priority: '0.4'
}
},
swigOptions: { // Options to pass in to swig
cache: false
}
},
src: ['**/*.swig'], // The pattern to search for files
dest: 'www/' // The destination directory path where the generated files will be placed
},
dest: "www/",
src: ['**/*.swig'],
generateSitemap: true,
generateRobotstxt: true,
siteUrl: 'http://mydomain.net/',
production: false,
fb_appid: '1349v',
ga_account_id: 'UA-xxxxxxxx-1',
robots_directive: 'Disallow /',
sitemap_priorities: {
'_DEFAULT_': '0.5',
'index.html': '0.8',
'subpage.html': '0.7'
prod: {
src: ['src-prod/**/*.swig'],
dest: 'www-prod/'
}
}
}
```

Grunt Swig will loop through files listed in `src`
Grunt Swig will loop through files listed in ```src```

__Note: Only files ending with ```.swig``` will be processed__

Your file extension is specified within your filename. For example:

Ex. `source/index.swig`. It will look for a `source/index.json` and add it to
the rest of the variables provided in `swig:development` or in `global.js`, and then run swig
against `source/index.swig` saving the output to `www/index.html`
```index.html.swig``` will generate the file ```index.html```

You can also provide context, for example `swig:development:blue` which will
perform the same actions above, but after process the JSON it will also expand
the variable list with `source/index.blue.json` and provide the variable
`context` to the rest of the swig template.
```myFile.txt.swig``` will generate the file ```myFile.txt```

The siteUrl is used to build a sitemap. Right now all the other elements are
hard coded, eventually this could be set in the config object.
The siteUrl is used to build a sitemap. You can adjust the defaults for each file/page in the options.

The 'sitemap_priorities' will set custom priorities based on the page name when
building the sitemap. The first item '_DEFAULT_' will be the default priority
used if a page name is not explicitly set. In the above example the page
'index.html' would be given priority of '0.8', 'subpage.html' would be given
a priority of '0.7', and all other pages would get a priority of '0.5',
You need to give the relative path to the output html file for this to work.
To override the default extension of `.html` by specifying the ```generatedExtension``` (without the period) in your config.

Path and base name of the source template file are available in `tplFile` variable, `tplFile.path` for
the path and `tplFile.basename` for the basename.
To write your files directly to the specified `dest` directory and not have it expand the subdirectory path, set ```expandDirectories``` to ```false```.

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
Expand Down
13 changes: 13 additions & 0 deletions grunt-swig/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict"
var swig = require('swig');
var fs = require('fs');

var GruntSwig = {
init: function init(options) {
console.log(options.config.data);


}
};

module.exports = GruntSwig;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grunt-swig",
"version": "0.2.1",
"version": "0.9.0",
"description": "Static site compiler built around swig",
"repository": {
"type": "git",
Expand All @@ -26,8 +26,11 @@
},
"devDependencies": {
"grunt": "~0.4.1",
"chai": "~1.9.2",
"mocha": "~1.21.4",
"grunt-contrib-clean": "~0.5.0",
"grunt-mocha-test": "~0.7.0",
"grunt-jsbeautifier": "^0.2.7",
"grunt-contrib-jshint": "~0.7.2"
},
"peerDependencies": {
Expand Down
16 changes: 16 additions & 0 deletions scratch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
swig: {
options {
generateSitemap: true
},
dev: {
options{
generateSitemap: false
},
src: [**/*.swig],
dest: 'www/'
},
prod: {
src: [**/*.swig],
dest: 'www/'
}
}
Loading