Skip to content

Commit

Permalink
Merge branch '1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Mar 3, 2013
2 parents a83aeca + 2515be5 commit 85f11d1
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 259 deletions.
118 changes: 64 additions & 54 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ module.exports = function(options, callback){
config: config,
theme: themeConfig,
__: themei18n.get,
layout: 'layout'/*,
cache: !watch*/
layout: 'layout',
cache: !watch
};

for (var i=0, len=layout.length; i<len; i++){
Expand All @@ -60,7 +60,10 @@ module.exports = function(options, callback){
}
}

renderFile(layoutPath, newLocals, callback);
renderFile(layoutPath, newLocals, function(err, result){
if (err) throw err;
callback(null, result);
});
}, next);
}, next);
}
Expand All @@ -83,73 +86,80 @@ module.exports = function(options, callback){
},
// Load theme layout
function(next){
file.dir(layoutDir, function(files){
for (var i=0, len=files.length; i<len; i++){
var item = files[i];
if (hiddenAdvFileRegex.test(item)){
var extname = pathFn.extname(item),
name = item.substring(0, item.length - extname.length);
themeLayout[name] = extname;
fs.exists(layoutDir, function(exist){
if (!exist) throw new Error('Layout not found.');
file.dir(layoutDir, function(files){
for (var i=0, len=files.length; i<len; i++){
var item = files[i];
if (hiddenAdvFileRegex.test(item)){
var extname = pathFn.extname(item),
name = item.substring(0, item.length - extname.length);
themeLayout[name] = extname;
}
}
}
next();
next();
});
});
},
// Load theme source
function(next){
var sourceDir = themeDir + 'source/',
list = Object.keys(renderer);

file.dir(sourceDir, function(files){
files = _.filter(files, function(item){
return hiddenAdvFileRegex.test(item);
});
fs.exists(sourceDir, function(exist){
if (!exist) return next();

file.dir(sourceDir, function(files){
files = _.filter(files, function(item){
return hiddenAdvFileRegex.test(item);
});

async.forEach(files, function(item, next){
var source = sourceDir + item,
extname = pathFn.extname(item).substring(1);
async.forEach(files, function(item, next){
var source = sourceDir + item,
extname = pathFn.extname(item).substring(1);

fs.stat(source, function(err, stats){
if (err) throw new Error('File status read error: ' + source);
fs.stat(source, function(err, stats){
if (err) throw new Error('File status read error: ' + source);

var data = dbAssets.findOne({source: source.substring(baseDir.length)}),
mtime = stats.mtime,
latest = false;
var data = dbAssets.findOne({source: source.substring(baseDir.length)}),
mtime = stats.mtime,
latest = false;

if (data){
if (data.mtime.getTime() === mtime.getTime()){
latest = true;
if (data){
if (data.mtime.getTime() === mtime.getTime()){
latest = true;
} else {
dbAssets.update(data._id, {mtime: mtime});
}
} else {
dbAssets.update(data._id, {mtime: mtime});
dbAssets.insert({source: source.substring(baseDir.length), mtime: mtime});
}
} else {
dbAssets.insert({source: source.substring(baseDir.length), mtime: mtime});
}

if (list.indexOf(extname) === -1){
var content = function(fn){
fn(null, fs.createReadStream(source));
};
content.latest = latest;
route.set(item, content);
} else {
var filename = item.substring(0, item.length - extname.length - 1),
fileext = pathFn.extname(filename),
dest = filename + '.' + (fileext ? fileext.substring(1) : renderer[extname].output);

var content = function(fn){
render({path: source}, function(err, result){
if (err) throw new Error('Compile error: ' + source);
fn(null, result);
});
};
content.latest = latest;
route.set(dest, content);
}
if (list.indexOf(extname) === -1){
var content = function(fn){
fn(null, fs.createReadStream(source));
};
content.latest = latest;
route.set(item, content);
} else {
var filename = item.substring(0, item.length - extname.length - 1),
fileext = pathFn.extname(filename),
dest = filename + '.' + (fileext ? fileext.substring(1) : renderer[extname].output);

var content = function(fn){
render({path: source}, function(err, result){
if (err) throw new Error('Compile error: ' + source);
fn(null, result);
});
};
content.latest = latest;
route.set(dest, content);
}

next();
});
}, next);
next();
});
}, next);
});
});
},
// Load theme i18n
Expand Down
6 changes: 2 additions & 4 deletions lib/plugins/helper/css.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var extend = require('../../extend');

extend.helper.register('css', function(){
return function(path){
return '<link rel="stylesheet" href="' + path + '" type="text/css">';
}
extend.helper.register('css', function(path){
return '<link rel="stylesheet" href="' + path + '" type="text/css">';
});
6 changes: 2 additions & 4 deletions lib/plugins/helper/gravatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ var md5 = function(str){
return crypto.createHash('md5').update(str).digest('hex');
};

extend.helper.register('gravatar', function(){
return function(email, size){
return 'http://www.gravatar.com/avatar/' + md5(email.toLowerCase()) + (size ? '?s=' + size : '');
}
extend.helper.register('gravatar', function(email, size){
return 'http://www.gravatar.com/avatar/' + md5(email.toLowerCase()) + (size ? '?s=' + size : '');
});
1 change: 0 additions & 1 deletion lib/plugins/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ require('./partial');
require('./titlecase');
require('./tagcloud');
require('./gravatar');
require('./render');
require('./paginator');
6 changes: 2 additions & 4 deletions lib/plugins/helper/js.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var extend = require('../../extend');

extend.helper.register('js', function(){
return function(path){
return '<script type="text/javascript" src="' + path + '"></script>';
}
extend.helper.register('js', function(path){
return '<script type="text/javascript" src="' + path + '"></script>';
});
114 changes: 56 additions & 58 deletions lib/plugins/helper/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,76 @@ var defaults = {
show_all: false
};

extend.helper.register('paginator', function(){
return function(options){
var options = _.extend(defaults, options),
current = options.current,
total = options.total,
end_size = options.end_size,
mid_size = options.mid_size,
space = options.space,
base = options.base,
format = options.format,
front = '',
back = '';
extend.helper.register('paginator', function(options){
var options = _.extend(defaults, options),
current = options.current,
total = options.total,
end_size = options.end_size,
mid_size = options.mid_size,
space = options.space,
base = options.base,
format = options.format,
front = '',
back = '';

var link = function(i){
return i == 1 ? base : base + format.replace('%d', i);
};
var link = function(i){
return i == 1 ? base : base + format.replace('%d', i);
};

var pageNum = function(i){
return '<a class="page-number" href="' + link(i) + '">' + i + '</a>';
};
var pageNum = function(i){
return '<a class="page-number" href="' + link(i) + '">' + i + '</a>';
};

if (options.prev_next){
if (current !== 1) front = '<a class="extend prev" href="' + link(current - 1) + '">' + options.prev_text + '</a>';
if (current !== total) back = '<a class="extend next" href="' + link(current + 1) + '">' + options.next_text + '</a>';
}
if (options.prev_next){
if (current !== 1) front = '<a class="extend prev" href="' + link(current - 1) + '">' + options.prev_text + '</a>';
if (current !== total) back = '<a class="extend next" href="' + link(current + 1) + '">' + options.next_text + '</a>';
}

if (options.show_all){
for (var i=1; i<=total; i++){
if (i == current){
front += '<span class="page-number current">' + i + '</span>';
} else {
front += pageNum(i);
}
if (options.show_all){
for (var i=1; i<=total; i++){
if (i == current){
front += '<span class="page-number current">' + i + '</span>';
} else {
front += pageNum(i);
}
}
} else {
if (end_size){
var endmax = current <= end_size ? current - 1 : end_size;
for (var i=1; i<=endmax; i++){
front += pageNum(i);
}
} else {
if (end_size){
var endmax = current <= end_size ? current - 1 : end_size;
for (var i=1; i<=endmax; i++){
front += pageNum(i);
}

var endmin = total - current <= end_size ? current + 1 : total - end_size + 1;
for (var i=total; i>=endmin; i--){
back = pageNum(i) + back;
}
var endmin = total - current <= end_size ? current + 1 : total - end_size + 1;
for (var i=total; i>=endmin; i--){
back = pageNum(i) + back;
}

if (space){
var space_html = '<span class="space">' + space + '</span>';
if (current - end_size - mid_size > 1) front += space_html;
if (total - end_size - mid_size > current) back = space_html + back;
}
if (space){
var space_html = '<span class="space">' + space + '</span>';
if (current - end_size - mid_size > 1) front += space_html;
if (total - end_size - mid_size > current) back = space_html + back;
}
}

if (mid_size){
var midmin = current - mid_size <= end_size ? current - mid_size + end_size : current - mid_size;
if (midmin > 1){
for (var i=midmin; i<=current-1; i++){
front += pageNum(i);
}
if (mid_size){
var midmin = current - mid_size <= end_size ? current - mid_size + end_size : current - mid_size;
if (midmin > 1){
for (var i=midmin; i<=current-1; i++){
front += pageNum(i);
}
}

var midmax = current + mid_size + end_size > total ? current + mid_size - end_size : current + mid_size;
if (midmax < total){
for (var i=midmax; i>=current+1; i--){
back = pageNum(i) + back;
}
var midmax = current + mid_size + end_size > total ? current + mid_size - end_size : current + mid_size;
if (midmax < total){
for (var i=midmax; i>=current+1; i--){
back = pageNum(i) + back;
}
}

front += '<span class="page-number current">' + current + '</span>';
}

return front + back;
front += '<span class="page-number current">' + current + '</span>';
}

return front + back;
});
26 changes: 9 additions & 17 deletions lib/plugins/helper/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,17 @@ var resolve = function(base, part){
return path.resolve(path.dirname(base), path.extname(part) ? part : part + path.extname(base));
};

var render = function(source, template, locals){
return function(part, options){
var partial = resolve(source, part),
extname = path.extname(partial).substring(1);
var render = function(view, options){
var source = resolve(this.filename, view);

if (locals.cache && cache.hasOwnProperty(partial)){
var content = cache[partial];
} else {
var content = file.readSync(partial);
if (locals.cache){
cache[partial] = content;
}
}

var newLocals = _.clone(locals);
newLocals.partial = render(partial, content, _.extend(locals, options));

return renderSync({text: content, path: partial}, _.extend(newLocals, options));
if (this.cache && cache.hasOwnProperty(source)){
var content = cache[source];
} else {
var content = file.readSync(source);
if (this.cache) cache[source] = content;
}

return renderSync({text: content, path: source}, _.extend(_.clone(this), options, {filename: source}));
};

extend.helper.register('partial', render);
8 changes: 0 additions & 8 deletions lib/plugins/helper/render.js

This file was deleted.

6 changes: 2 additions & 4 deletions lib/plugins/helper/strip.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var extend = require('../../extend');

extend.helper.register('strip_html', function(){
return function(content){
return content.replace(/<[^>]*>/g, '');
}
extend.helper.register('strip_html', function(content){
return content.replace(/<[^>]*>/g, '');
});
Loading

0 comments on commit 85f11d1

Please sign in to comment.