Created
March 19, 2013 12:27
-
-
Save 2no/5195687 to your computer and use it in GitHub Desktop.
grunt-templater にパス取得用のヘルパー。現在処理中のテンプレートのパスやファイル名の取得などが行える…はず。$ npm install grunt grunt-templater ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) | |
{ | |
'use strict'; | |
var ROOT_PATH = '.', | |
TEMPLATE_PATH = ROOT_PATH + '/templates'; | |
grunt.config.init({ | |
template: { | |
hoge: { | |
src: TEMPLATE_PATH + '/sample.ejs', | |
dest: ROOT_PATH + '/sample.html', | |
variables: {}, | |
}, | |
fuga: { | |
src: TEMPLATE_PATH + '/sample.ejs', | |
dest: ROOT_PATH + '/htdocs/sample.html', | |
variables: {}, | |
}, | |
foo: { | |
src: TEMPLATE_PATH + '/sample.ejs', | |
dest: ROOT_PATH + '/htdocs/dir/sample.html', | |
variables: {}, | |
}, | |
news: { | |
src: TEMPLATE_PATH + '/sample.ejs', | |
dest: ROOT_PATH + '/news/sample.html', | |
variables: {}, | |
}, | |
}, | |
}); | |
grunt.task.loadNpmTasks('grunt-templater'); | |
grunt.task.renameTask('template', 'grunt-templater_template'); | |
grunt.task.registerMultiTask('template', | |
'generates an html file from a specified template', | |
(function() { | |
var p = require('path'), | |
rootPath = null, | |
currentPath = null, | |
Path = { | |
getRelative: function(to, from) | |
{ | |
var path, dirTo, dirFrom, basename, re; | |
to = this.normalize(to); | |
from = this.normalize(from) || currentPath; | |
dirTo = trimRootPath(p.dirname(to )) || '.'; | |
dirFrom = trimRootPath(p.dirname(from)) || '.'; | |
path = p.relative(dirFrom, dirTo); | |
basename = trimRootPath(p.basename(to)); | |
path = path + (path ? p.sep : '') + basename; | |
return p.normalize(path); | |
}, | |
getAbsolute: function(to, from) | |
{ | |
return this.toAbsolute(this.getRelative(to, from)); | |
}, | |
toAbsolute: function(path) | |
{ | |
return p.normalize(p.sep + path); | |
}, | |
getRoot: function() | |
{ | |
return this.getRelative(rootPath, currentPath); | |
}, | |
getCurrent: function() | |
{ | |
return trimRootPath(currentPath); | |
}, | |
setCurrent: function(path) | |
{ | |
currentPath = p.normalize(path); | |
rootPath = getRootPath(path); | |
return this; | |
}, | |
getDirname: function(path) | |
{ | |
return p.dirname(path); | |
}, | |
getBasename: function(path) | |
{ | |
return p.basename(path); | |
}, | |
normalize: function(path) | |
{ | |
if (path && path.indexOf(p.sep) === 0) { | |
path = p.normalize(rootPath + path); | |
} | |
return path; | |
}, | |
}; | |
function trimRootPath(path) | |
{ | |
var re = new RegExp('^' + quote(rootPath + p.sep) + '?'); | |
return path.replace(re, ''); | |
} | |
function getRootPath(path) | |
{ | |
var root = path.split(p.sep).slice(0, 2).join(p.sep); | |
return p.dirname(root); | |
} | |
function quote(str) | |
{ | |
return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); | |
} | |
return function() { | |
var config = {}; | |
this.data.variables.Path = Path.setCurrent(this.data.dest); | |
config[this.target] = this.data; | |
grunt.config.set('grunt-templater_template', config); | |
grunt.task.run(['grunt-templater_template']); | |
}; | |
}()) | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Path.getCurrent() -> <%= Path.getCurrent() %> | |
Path.getRoot() -> <%= Path.getRoot() %> | |
Path.toAbsolute(Path.getCurrent()) -> <%= Path.toAbsolute(Path.getCurrent()) %> | |
Path.toAbsolute(Path.getRoot()) -> <%= Path.toAbsolute(Path.getRoot()) %> | |
Path.getRelative() -> <%= Path.getRelative('/img/hoge.jpg') %> | |
Path.getAbsolute() -> <%= Path.getAbsolute('/img/hoge.jpg') %> | |
Path.getRelative() -> <%= Path.getRelative('fuga/hoge.html') %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment