Skip to content

Commit 5371f9a

Browse files
committed
Merge branch 'docs'
2 parents fe926e1 + 64c126e commit 5371f9a

18 files changed

+2784
-7
lines changed

Makefile

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ BASE_FILES = ${SRC_DIR}/core.js\
1616
JS = ${DIST_DIR}/jstat.js
1717
JS_MIN = ${DIST_DIR}/jstat.min.js
1818

19-
all: update_submodules core
19+
DOC_DIR = doc
20+
DOC_LIST = `ls ${DOC_DIR}/md/`
21+
22+
all: update_submodules core doc
2023

2124
core: jstat min lint
2225
@@echo "jStat build complete."
@@ -28,19 +31,27 @@ jstat: ${JS}
2831

2932
${JS}: ${DIST_DIR}
3033
@@echo "Building" ${JS}
31-
3234
@@cat ${BASE_FILES} > ${JS}
3335

3436
lint: jstat
3537
@@if test ! -z ${JS_ENGINE}; then \
3638
echo "Checking jStat against JSHint..."; \
37-
${JS_ENGINE} build/jshint-check.js; \
39+
${JS_ENGINE} ${BUILD_DIR}/jshint-check.js; \
3840
else \
3941
echo "You must have NodeJS installed in order to test jStat against JSHint."; \
4042
fi
4143

4244
min: jstat ${JS_MIN}
4345

46+
doc:
47+
@@echo 'Generating documentation...'
48+
@@mkdir -p ${DIST_DIR}/docs/assets
49+
@@cp ${DOC_DIR}/assets/*.css ${DIST_DIR}/docs/assets/
50+
@@cp ${DOC_DIR}/assets/*.js ${DIST_DIR}/docs/assets/
51+
@@for i in ${DOC_LIST}; do \
52+
${JS_ENGINE} ${BUILD_DIR}/doctool.js ${DOC_DIR}/assets/template.html ${DOC_DIR}/md/$${i} ${DIST_DIR}/docs/$${i%.*}.html; \
53+
done
54+
4455
${JS_MIN}: ${JS}
4556
@@if test ! -z ${JS_ENGINE}; then \
4657
echo "Minifying jStat" ${JS_MIN}; \
@@ -56,4 +67,4 @@ clean:
5667
pull: pull_submodules
5768
@@git pull ${REMOTE} ${BRANCH}
5869

59-
.PHONY: all jstat lint min clean update_submodules pull_submodules pull core
70+
.PHONY: all jstat lint min doc clean update_submodules pull_submodules pull core

build/doctool.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
Process a single doc file
3+
4+
argv[2] = template file
5+
argv[3] = input file
6+
argv[4] = output file
7+
*/
8+
9+
var fs = require("fs"),
10+
path = require("path"),
11+
markdown = require("./lib/markdown"),
12+
argv = process.argv,
13+
argc = argv.length,
14+
template = fs.readFileSync(argv[2], "utf8");
15+
16+
17+
function formatIdString( str ) {
18+
str = str
19+
.replace(/\([^)}]*\)/gmi, "")
20+
.replace(/[^A-Za-z0-9_.]+/gmi, "_");
21+
22+
return str.substr(0, 1).toLowerCase() + str.substr(1);
23+
}
24+
25+
26+
function generateToc(data) {
27+
var last_level = 0,
28+
first_level = 0,
29+
toc = [
30+
'<div id="toc">',
31+
'<h2>Table Of Contents</h2>'
32+
];
33+
34+
data.replace(/(^#+)\W+([^$\n]+)/gmi, function(src, level, text) {
35+
level = level.length;
36+
37+
if (first_level == 0) first_level = level;
38+
39+
if (level <= last_level) {
40+
toc.push("</li>");
41+
}
42+
43+
if (level > last_level) {
44+
toc.push("<ul>");
45+
} else if (level < last_level) {
46+
for(var c=last_level-level; 0 < c ; c-- ) {
47+
toc.push("</ul>");
48+
toc.push("</li>");
49+
}
50+
}
51+
52+
toc.push("<li>");
53+
toc.push('<a href="#'+formatIdString(text)+'">'+text+'</a>');
54+
55+
last_level = level;
56+
});
57+
58+
for(var c=last_level-first_level; 0 <= c ; c-- ) {
59+
toc.push("</li>");
60+
toc.push("</ul>");
61+
}
62+
63+
toc.push("<hr />")
64+
toc.push("</div>");
65+
66+
return toc.join("");
67+
}
68+
69+
70+
var includeExpr = /^@include\s+([A-Za-z0-9-_]+)(?:\.)?([a-zA-Z]*)$/gmi;
71+
// Allow including other pages in the data.
72+
function loadIncludes(data, current_file) {
73+
return data.replace(includeExpr, function(src, name, ext) {
74+
try {
75+
var include_path = path.join(current_file, "../", name+"."+(ext || "markdown"))
76+
return loadIncludes(fs.readFileSync(include_path, "utf8"), current_file);
77+
} catch(e) {
78+
return "";
79+
}
80+
});
81+
}
82+
83+
84+
function convertData(data) {
85+
// Convert it to HTML from Markdown
86+
var html = markdown.toHTML(markdown.parse(data), {xhtml:true})
87+
.replace(/<hr><\/hr>/g, "<hr />")
88+
.replace(/(\<h[2-6])\>([^<]+)(\<\/h[1-6]\>)/gmi, function(o, ts, c, te) {
89+
return ts+' id="'+formatIdString(c)+'">'+c+te;
90+
});
91+
92+
return html;
93+
}
94+
95+
96+
if (argc > 3) {
97+
var filename = argv[3],
98+
output = template,
99+
html;
100+
101+
fs.readFile(filename, "utf8", function(err, data) {
102+
if (err) throw err;
103+
104+
// go recursion.
105+
data = loadIncludes(data, filename);
106+
// go markdown.
107+
html = convertData(data);
108+
filename = path.basename(filename, '.md');
109+
110+
if (filename != "_toc" && filename != "index") {
111+
if (data) {
112+
html = generateToc(data) + "\n" + html;
113+
}
114+
115+
output = output.replace("{{section}}", filename+" - ")
116+
} else {
117+
output = output.replace("{{section}}", "");
118+
output = output.replace(/<body([^>]*)>/, '<body class="'+filename+'" $1>');
119+
}
120+
if (html.length == 0) {
121+
html = "Sorry, this section is currently undocumented, \
122+
but we'll be working on it.";
123+
}
124+
output = output.replace("{{content}}", html);
125+
126+
if (argc > 4) {
127+
fs.writeFile(argv[4], output);
128+
} else {
129+
process.stdout.write(output);
130+
}
131+
});
132+
}

0 commit comments

Comments
 (0)