forked from aralejs/nico-arale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nico.js
132 lines (122 loc) · 3.51 KB
/
nico.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var path = require('path');
var util = require('util');
var fs = require('fs');
var nico = require('nico');
var urilab = require('nico/lib/utils/uri');
var pathlib = require('nico/lib/utils/path');
var Post = nico.Post;
// {{ settings for nico
exports.theme = __dirname
exports.source = process.cwd()
exports.output = path.join(process.cwd(), '_site')
exports.permalink = '{{directory}}/{{filename}}.html'
exports.ignore = ['_site', 'node_modules']
exports.writers = [
nico.PageWriter,
nico.StaticWriter,
nico.FileWriter,
path.join(__dirname, 'theme') + '.MochaWriter'
]
exports.PostRender = Post
exports.filters = {
debug: function(args) {
return args.indexOf('debug') != -1;
},
find: function(pages, cat) {
var ret;
pages.some(function(item) {
if (item.category == cat) {
ret = item;
return true;
}
});
if (!ret) return null;
ret = nico.underscore.sortBy(ret, function(i) { return i.meta.order || 0});
return ret;
},
find_all: function(pages, cat) {
var ret = [];
pages.forEach(function(item) {
if (item.category == cat) {
ret.push(item);
}
});
ret = nico.underscore.sortBy(ret, function(i) { return i.meta.order || 0});
return ret;
},
replace_code: function(content) {
var key, value, regex;
var src = findSrc();
var p = exports.package;
for (key in src) {
value = util.format('%s/%s/%s/%s', p.root, p.name, p.version, key);
var regex = new RegExp(
'<span class="string">(\'|\")' + key + '(\'|\")</span>', 'g'
);
content = content.replace(regex, '<span class="string">$1' + value + '$2</span>');
}
return content;
}
}
exports.functions = {
render_src: function(writer) {
var base = urilab.relative(writer.filepath, '');
return JSON.stringify(findSrc(base.slice(0, -1)));
}
}
// end settings }}
// extends for theme usage, that can be accessable by {{config.xxx}}
exports.package = require(path.join(process.cwd(), 'package.json'))
if (fs.existsSync(path.join(process.cwd(), 'HISTORY.md'))) {
exports.hasHistory = true
} else {
exports.hasHistory = false
}
if (fs.existsSync(path.join(process.cwd(), 'tests'))) {
exports.hasTest = true
} else {
exports.hasTest = false
}
function findSrc(base) {
base = base || '..';
var src = path.join(process.cwd(), 'src');
if (!fs.existsSync(src)) return {};
var walker = pathlib.walkdir(src);
var files = walker.files;
var key, relative, ret = {};
files.forEach(function(item) {
relative = pathlib.relative(src, item);
key = path.basename(relative);
if (path.extname(key) === '.js') {
key = key.slice(0, -3);
relative = relative.replace(/\\/g, '/');
ret[key] = base + '/src/' + relative;
}
});
return ret;
}
Post.metadata = ['category']
Object.defineProperty(Post.prototype, 'template', {
configurable: true,
get: function() {
if (this.relative_filepath == 'HISTORY.md') return 'history.html';
return 'post.html';
}
});
Object.defineProperty(Post.prototype, 'filename', {
configurable: true,
get: function() {
if (this.relative_filepath == 'README.md') return 'index';
if (this.meta.filename) return this.meta.filename;
var basename = path.basename(this.relative_filepath);
return basename.split('.')[0];
}
});
Object.defineProperty(Post.prototype, 'category', {
configurable: true,
get: function() {
if (this.relative_filepath == 'README.md') return 'docs';
if (this.meta.category) return this.meta.category;
return this.directory;
}
});