@@ -3,6 +3,7 @@ var through = require('through2');
33var Handlebars = require ( 'handlebars' ) ;
44var fs = require ( 'fs' ) ;
55var extend = require ( 'util' ) . _extend ;
6+ var path = require ( 'path' ) ;
67
78function handlebars ( data , opts ) {
89
@@ -36,23 +37,35 @@ function handlebars(data, opts) {
3637 } ;
3738
3839 var partialName = function ( filename , base ) {
39- var name = filename . substr ( 0 , filename . lastIndexOf ( '.' ) ) ;
40- name = name . replace ( new RegExp ( '^' + base + '\\/' ) , '' ) ;
41- return name . substring ( name . charAt ( 0 ) === '_' ? 1 : 0 ) ;
40+ var name = path . join ( path . dirname ( filename ) , path . basename ( filename , path . extname ( filename ) ) ) ;
41+ if ( name . indexOf ( base ) === 0 ) {
42+ name = name . slice ( base . length ) ;
43+ }
44+ // Change the name of the partial to use / in the partial name, not \
45+ name = name . replace ( / \\ / g, '/' ) ;
46+
47+ // Remove leading _ and / character
48+ var firstChar = name . charAt ( 0 ) ;
49+ if ( firstChar === '_' || firstChar === '/' ) {
50+ name = name . substring ( 1 ) ;
51+ }
52+
53+ return name ;
4254 } ;
4355
4456 var registerPartial = function ( filename , base ) {
4557 if ( ! isHandlebars ( filename ) ) { return ; }
4658 var name = partialName ( filename , base ) ;
4759 var template = fs . readFileSync ( filename , 'utf8' ) ;
60+
4861 Handlebars . registerPartial ( name , template ) ;
4962 } ;
5063
5164 var registerPartials = function ( dir , base , depth ) {
5265 if ( depth > maxDepth ) { return ; }
5366 base = base || dir ;
5467 fs . readdirSync ( dir ) . forEach ( function ( basename ) {
55- var filename = dir + '/' + basename ;
68+ var filename = path . join ( dir , basename ) ;
5669 if ( isDir ( filename ) ) {
5770 registerPartials ( filename , base ) ;
5871 } else {
@@ -67,6 +80,7 @@ function handlebars(data, opts) {
6780 if ( typeof options . batch === 'string' ) options . batch = [ options . batch ] ;
6881
6982 options . batch . forEach ( function ( dir ) {
83+ dir = path . normalize ( dir ) ;
7084 registerPartials ( dir , dir , 0 ) ;
7185 } ) ;
7286 }
0 commit comments