Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

See also #81

Merged
merged 4 commits into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var last = require('lodash.last');
var unescape = require('lodash.unescape');
var marked = require('marked');
var index = require('./index');

var allElements = [
'blockquote', 'html', 'strong', 'em', 'br', 'del',
Expand All @@ -9,7 +10,7 @@ var allElements = [
];

function unhtml(text){
text = text.replace(/<code>/, '').replace(/<\/code>/, '');
// text = text.replace(/<code>/, '').replace(/<\/code>/, '');
return unescape(text);
}

Expand All @@ -18,7 +19,8 @@ exports.parse = function(markdown) {
var page = {
name: '',
description: '',
examples: []
examples: [],
seeAlso: []
};

var r = new marked.Renderer();
Expand All @@ -28,33 +30,59 @@ exports.parse = function(markdown) {
r[e] = function() { return ''; };
});

// paragraphs just pass through (automatically created by new lines)
r.paragraph = function(text) {
if (/^<code>.*<\/code>$/.test(text)) {
var example = last(page.examples);
if (example) {
example.code = unhtml(text);
r.codespan = function(text) {
if (index.hasPage(text) && text !== page.name) {
if (page.seeAlso.indexOf(text) < 0) {
page.seeAlso.push(text);
}
}
var example = last(page.examples);
if (example) {
example.code = unhtml(text);
}
return text;
};


// paragraphs just pass through (automatically created by new lines)
r.paragraph = function(text) {
// if (/^<code>.*<\/code>$/.test(text)) {
// var example = _.last(page.examples);
// if (example) {
// example.code = unhtml(text);
// }
// } else {
return text;
// }
};

// r.paragraph = function(text) {
// return text;
// };

r.heading = function(text, level) {
if (level === 1) {
page.name = text.trim();
}
return text;
};

r.blockquote = function(text) {
page.description += unhtml(text);
return text;
};

r.listitem = function(text) {
page.examples.push({});
last(page.examples).description = unhtml(text);
page.examples.push({
description: unhtml(text)
});
return text;
};

marked(markdown, {renderer: r, sanitize: true});
marked(markdown, {
renderer: r,
sanitize: true
});

page.examples = page.examples.filter(function(example) {
return example.description && example.code;
Expand Down
6 changes: 6 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ exports.toANSI = function(page) {
output.push('');
});

if (page.seeAlso && page.seeAlso.length > 0) {
output.push('');
output.push('See also: ' + page.seeAlso.join(', '));
output.push('');
}

return '\n' + output.join('\n') + '\n';
};

Expand Down
53 changes: 53 additions & 0 deletions test/parser.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var parser = require('../lib/parser');
var sinon = require('sinon');
var index = require('../lib/index');

describe('Parser', function() {

Expand Down Expand Up @@ -135,4 +137,55 @@ describe('Parser', function() {
page.examples[3].code.should.eql('bc <<< "1 + 1"');
});

describe('See also section', function() {

beforeEach(function() {
var hasPage = sinon.stub(index, 'hasPage');
hasPage.withArgs('lsb_release').returns(true);
hasPage.withArgs('ln').returns(true);
hasPage.withArgs('cp').returns(false);
hasPage.withArgs('mv').returns(false);
});

afterEach(function() {
index.hasPage.restore();
});

it('should parse seeAlso commands when mentioned in description', function() {
var page = parser.parse(
'\n# uname' +
'\n> See also `lsb_release`, `mv`'
);
page.seeAlso.should.eql(['lsb_release']);
});

it('should parse seeAlso commands when mentioned in examples', function() {
var page = parser.parse(
'\n# uname' +
'\n> Description for uname' +
'\n' +
'\n- example 1, see `ln` for details' +
'\n' +
'\n`cmd1 --foo`'
);
page.seeAlso.should.eql(['ln']);
});

it('should have only unique seeAlso commands when mentioned a few times', function() {
var page = parser.parse(
'\n# uname' +
'\n> Description for uname, see `lsb_release`, `ln`' +
'\n' +
'\n- example 1, see `ln`, `lsb_release` for details' +
'\n' +
'\n`cmd1 --foo`' +
'\n' +
'\n- example 2, see `ln` for details' +
'\n' +
'\n`cmd1 --foo`'
);
page.seeAlso.should.eql(['lsb_release', 'ln']);
});
});

});
17 changes: 17 additions & 0 deletions test/render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ describe('Render', function() {
text.should.containEql(' bye'.blackBG.red);
});

it('should correctly render see also section', function() {
var text = render.toANSI({
name: 'uname',
description: 'Description for `uname`.\n' +
'See also `lsb_release`.',
examples: [{
description: '1st example. You need `sudo` to run this',
code: 'uname {{token}}'
}],
seeAlso: [
'lsb_release',
'sudo'
]
});
text.should.containEql('See also: lsb_release, sudo');
});

});