forked from nodejitsu/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
215 lines (172 loc) · 5.47 KB
/
content.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var findit = require('findit'),
path = require('path'),
hl = require('../../vendor/highlight/lib/highlight').Highlight,
markdown = require('github-flavored-markdown'),
mkdirp = require('mkdirp'),
fs = require('fs'),
fs2 = require('../fs2'),
docs = require('../docs'),
weld = require('weld').weld,
helpers = require('../helpers');
var content = exports;
content.weld = function(dom, pages) {
//
// Remark: Create a short-cut reference to the jQuery object
//
var $ = docs.window.$;
// Here, we build up the table of contents.
var toc = helpers.buildToc(docs.src);
/*console.log((function () {
var sorts = {};
Object.keys(pages).forEach( function (i) {
if (pages[i].metadata && pages[i].metadata.order) {
Object.keys(pages[i].metadata.order).forEach(function (j) {
sorts[i+"/"+j] = pages[i].metadata.order[j];
});
}
});
return fs2.filesToTree(sorts);
})());*/
Object.keys(pages).forEach( function (i) {
// parse content if it exists
if (pages[i].content) {
var md;
try {
md = markdown.parse(pages[i].content.toString());
} catch (err) {
md = err.message;
}
}
var metadata = pages[i].metadata;
//join the author metadata
if (metadata && metadata.author) {
metadata.author = docs.content.authors[metadata.author]
|| { name: metadata.author};
}
// Use article view if there's content
if (md) {
dom.innerHTML = docs.content.theme['./theme/article.html'].toString();
var data = {
metadata: metadata,
content: md,
toc: toc
};
// use directory view
} else {
dom.innerHTML = docs.content.theme['./theme/directory.html'];
//set up the data
var data = {
pwd: helpers.unresolve(docs.src, i),
ls: pages[i].ls,
toc: toc,
metadata: metadata
};
}
weld(dom, data, {
map: function(parent, element, key, val) {
//
// Create breadcrumb
//
if ($(element).hasClass('breadcrumb')) {
var crumb = '';
$('.breadcrumb', parent).each(function(i,v){
crumb += ('/' + $(v).html());
});
crumb += ('/' + val);
$(element).attr('href', crumb);
$(element).html(val);
return false
}
if ($(element).hasClass("ls")) {
var title = val.split("/");
title = title[title.length - 1].replace(/-/g, " ");
var listing = $("<tr>").attr("class", "ls").append(
$("<td>").append(
$("<a>").attr("href", val.replace("pages/", "")).text(title)
)
);
$("tr", $(element)).replaceWith(listing);
return false;
}
//Handle dates
if ($(element).hasClass("date")) {
var date = val ? new Date(val).toISOString() : undefined;
if (val) {
$(element).attr("datetime", new Date(val).toISOString());
$(element).text(val);
} else {
//TODO: If there isn't any metadata, we shouldn't have the
//"by x on y" div.
}
return false;
}
//Handle author metadata
if ($(element).hasClass("github")) {
if (val) {
$(element).append(
$("<a>")
.attr("href", "https://github.com/"+val)
.text("[github]")
);
}
return false;
}
element.innerHTML = val;
return false;
}
});
//Give the page a title
$('title', dom).html('node docs - ' + (metadata && metadata.title) || "");
//Add some meta tags
$('meta[name=keywords]', dom).attr('content',
(metadata && metadata.tags || [])
.concat(docs.config.get("tags") || []).join(',')
);
//
// Remark: perform code highlighting, convert only inside <code/>
//
// TODO: the current code highlighting is slow, very slow.
//
//
// Remark: The hilighter tries to hilight ">" as
//
// `&<span class="identifier">gt</span>;`
//
// There are probably a few other html identities that also get incorrectly
// highlighted (such as <).
// The easiest way to fix this, besides using a different highlighter,
// turns out to be running a greedy search/replace for the
// bungled highlight.
dom.innerHTML = hl(dom.innerHTML, false, true)
.replace( new RegExp("&<span class=\"identifier\">gt</span>;",
"g"), ">");
pages[i].content = dom.innerHTML
|| docs.content.theme['./theme/article.html'].toString();
});
return dom;
};
content.generate = function(output, pages) {
//
// Generate articles and directory pages
//
Object.keys(pages).forEach(function(file){
var newPath = file.replace(path.resolve(docs.src), path.resolve(docs.dst));
//newPath = path.dirname(newPath);
newPath = path.normalize(newPath + '/index.html');
fs2.writeFile(newPath, pages[file].content, function(){});
});
return pages;
};
// Load all content with an fs2.readDirSync. It should be relatively
// indiscriminate.
content.load = function () {
if (!docs.src) {
docs.src = "../../pages";
}
// load all the contents
// { "path": "content" }
var pages = fs2.readDirSync(docs.src, true);
// do some conversion here
pages = helpers.dirToContent(docs.src, pages, true);
return pages;
};