Skip to content

Commit ced4678

Browse files
committed
making publish work
0 parents  commit ced4678

File tree

7 files changed

+291
-0
lines changed

7 files changed

+291
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
temp/

.jshintrc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"globals": {
3+
"define": true,
4+
"steal": true,
5+
"can": true,
6+
"QUnit": true,
7+
"System": true,
8+
"SimpleDOM": true,
9+
"test": true,
10+
"asyncTest": true,
11+
"expect": true,
12+
"module": true,
13+
"ok": true,
14+
"equal": true,
15+
"notEqual": true,
16+
"deepEqual": true,
17+
"propEqual": true,
18+
"notDeepEqual": true,
19+
"strictEqual": true,
20+
"notStrictEqual": true,
21+
"raises": true,
22+
"start": true,
23+
"stop": true,
24+
"global": true,
25+
"describe": true,
26+
"it": true
27+
},
28+
"strict": false,
29+
"curly": true,
30+
"eqeqeq": true,
31+
"freeze": true,
32+
"indent": false,
33+
"latedef": 0,
34+
"noarg": true,
35+
"undef": true,
36+
"unused": "vars",
37+
"trailing": true,
38+
"maxdepth": 4,
39+
"boss" : true,
40+
"jquery": true,
41+
"eqnull": true,
42+
"evil": true,
43+
"loopfunc": true,
44+
"smarttabs": true,
45+
"maxerr" : 200,
46+
"browser": true,
47+
"phantom": true,
48+
"node": true,
49+
"esversion": 6
50+
}

bit-docs.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var tags = require("./tags");
2+
3+
module.exports = function(bitDocs){
4+
var pkg = require("./package.json");
5+
var deps = {};
6+
7+
deps[pkg.name] = pkg.version;
8+
9+
bitDocs.register("html", {
10+
dependencies: deps
11+
});
12+
13+
bitDocs.register("tags", tags);
14+
};

highlight-line.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var $ = require("jquery");
2+
3+
var getLines = function(lineString) {
4+
var lineArray = lineString.split(',');
5+
var result = {};
6+
7+
for (var i = 0; i < lineArray.length; i++) {
8+
var val = lineArray[i];
9+
10+
// Matches any string with 1+ digits dash 1+ digits
11+
// will ignore non matching strings
12+
if (/^([\d]+-[\d]+)$/.test(val)) {
13+
var values = val.split('-'),
14+
start = (values[0] - 1),
15+
finish = (values[1] - 1);
16+
17+
for (var j = start; finish >= j; j++) {
18+
result[j] = true;
19+
}
20+
//matches one or more digits
21+
} else if (/^[\d]+$/.test(val)) {
22+
result[val - 1] = true;
23+
}
24+
25+
}
26+
return result;
27+
};
28+
29+
function addHighlights() {
30+
31+
$('span[line-highlight]').each(function(i, el) {
32+
var $el = $(el);
33+
var lines = getLines($el.attr('line-highlight'));
34+
var codeBlock = $el.parent().prev('pre').children('code');
35+
36+
var lineMap = [[]];
37+
var k = 0;
38+
codeBlock.children().each(function(i, el) {
39+
var nodeText = $(el).text();
40+
if (/\n/.test(nodeText)) {
41+
42+
var cNames = $(el).attr('class');
43+
var str = nodeText.split('\n');
44+
var l = str.length;
45+
46+
for (var j = 0; j < l; j++) {
47+
var text = j === (l - 1) ? str[j] : str[j] + '\n';
48+
var newNode = document.createElement('span');
49+
newNode.className = cNames;
50+
$(newNode).text(text);
51+
lineMap[k].push(newNode);
52+
53+
if (j !== (l - 1)) {
54+
k++;
55+
lineMap[k] = [];
56+
}
57+
}
58+
} else {
59+
lineMap[k].push(el);
60+
}
61+
});
62+
63+
codeBlock.empty();
64+
65+
for (var key in lineMap) {
66+
if (lineMap.hasOwnProperty(key)) {
67+
var newNode = document.createElement('span');
68+
newNode.className = lines[key] ? 'line highlight': 'line' ;
69+
$(newNode).append(lineMap[key]);
70+
codeBlock.append(newNode);
71+
}
72+
}
73+
});
74+
}
75+
76+
addHighlights();

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "bit-docs-html-highlight-line",
3+
"version": "0.0.1",
4+
"description": "Highlight a line in source code",
5+
"main": "highlight-line.js",
6+
"scripts": {
7+
"preversion": "npm test",
8+
"test": "mocha test.js --reporter spec",
9+
"release:patch": "npm version patch && npm publish",
10+
"release:minor": "npm version minor && npm publish",
11+
"release:major": "npm version major && npm publish"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/bit-docs/bit-docs-html-highlight-line.git"
16+
},
17+
"keywords": [
18+
"bit-docs",
19+
"donejs"
20+
],
21+
"author": "Bitovi",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/bit-docs/bit-docs-html-highlight-line/issues"
25+
},
26+
"homepage": "https://github.com/bit-docs/bit-docs-html-highlight-line#readme",
27+
"dependencies": {
28+
"jquery": "^2.2.4"
29+
},
30+
"devDependencies": {
31+
"bit-docs-generate-html": "^0.0.3",
32+
"connect": "^2.14.4",
33+
"mocha": "^2.5.3",
34+
"zombie": "^4.2.1"
35+
}
36+
}

tags.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
exports.demo = {
2+
add: function(line, curData) {
3+
var lines = line.replace("@highlight","").trim();
4+
var html = "<span line-highlight='"+lines+"'></span>";
5+
var validCurData = (curData && curData.length !== 2);
6+
var useCurData = validCurData && (typeof curData.description === "string") && !curData.body;
7+
8+
if(useCurData) {
9+
curData.description += html;
10+
} else {
11+
this.body += html;
12+
}
13+
}
14+
};

test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
var assert = require("assert");
2+
var generate = require("bit-docs-generate-html/generate");
3+
var path = require("path");
4+
5+
var Browser = require("zombie"),
6+
connect = require("connect");
7+
8+
var find = function(browser, property, callback, done){
9+
var start = new Date();
10+
var check = function(){
11+
if(browser.window && browser.window[property]) {
12+
callback(browser.window[property]);
13+
} else if(new Date() - start < 2000){
14+
setTimeout(check, 20);
15+
} else {
16+
done("failed to find "+property);
17+
}
18+
};
19+
check();
20+
};
21+
22+
var waitFor = function(browser, checker, callback, done){
23+
var start = new Date();
24+
var check = function(){
25+
if(checker(browser.window)) {
26+
callback(browser.window);
27+
} else if(new Date() - start < 2000){
28+
setTimeout(check, 20);
29+
} else {
30+
done(new Error("checker was never true"));
31+
}
32+
};
33+
check();
34+
};
35+
36+
37+
var open = function(url, callback, done){
38+
var server = connect().use(connect.static(path.join(__dirname))).listen(8081);
39+
var browser = new Browser();
40+
browser.visit("http://localhost:8081/"+url)
41+
.then(function(){
42+
callback(browser, function(){
43+
server.close();
44+
});
45+
}).catch(function(e){
46+
server.close();
47+
done(e);
48+
});
49+
};
50+
51+
describe("bit-docs-tag-demo", function(){
52+
it("basics works", function(done){
53+
this.timeout(60000);
54+
55+
var docMap = Promise.resolve({
56+
index: {
57+
name: "index",
58+
demo: "path/to/demo.html",
59+
body: ("```"+`
60+
var a = "bar";
61+
var b = "bar";
62+
var c = "bar";
63+
var d = "bar";\n`+
64+
"```\n"+
65+
"<span line-highlight='1-2'></span>").replace(/\n\s+/g,"\n")
66+
}
67+
});
68+
69+
generate(docMap, {
70+
html: {
71+
dependencies: {
72+
"bit-docs-prettify": "^0.0.3",
73+
"bit-docs-html-highlight-line": __dirname
74+
}
75+
},
76+
dest: path.join(__dirname, "temp"),
77+
parent: "index",
78+
forceBuild: true,
79+
debug: true,
80+
minifyBuild: false
81+
}).then(function(){
82+
83+
open("temp/index.html",function(browser, close){
84+
waitFor(browser, function(window){
85+
return window.document.getElementsByClassName("highlight").length;
86+
}, function(){
87+
var doc = browser.window.document;
88+
var highlights = doc.getElementsByClassName("highlight");
89+
// NOTE: there should be 2 lines. But it seems
90+
// like prettify doesn't work in zombie right.
91+
assert.ok(highlights.length, "there are 2 tabs");
92+
93+
close();
94+
done();
95+
}, done);
96+
},done);
97+
}, done);
98+
});
99+
});

0 commit comments

Comments
 (0)