-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparseDocco.js
More file actions
67 lines (66 loc) · 2.13 KB
/
parseDocco.js
File metadata and controls
67 lines (66 loc) · 2.13 KB
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
/**
* Parse a docco text.
*
* @param {String} text the text to parse
*/
function parseDocco(text, separators){
separators = separators || [{
// for js
head : '//',
tail : ''
},{ // for html
head : '<!--',
tail : '-->'
}];
var sections = [];
// go thru each lines, to build the sections
var lines = text.split('\n');
lines.forEach(function(line){
// determine the type of last line and current line
var lastType = sections.length === 0 ? "none" : sections[sections.length-1].type;
var curType = isComment(line) ? "comment" : "code"
// create a section if needed
if( lastType !== curType ) sections.push({ type : curType});
// if this is a comment, remove the header immediatly
if( curType === "comment" ) line = removeCommentMarkup(line);
// get current sections
var section = sections[sections.length-1];
// create text if needed
section.text = section.text || '';
// append a line
section.text += line+'\n';
})
// go thru each sections to htmlize it
sections.forEach(function(section){
if( section.type === "code" && prettyPrintOne ){
section.text = section.text.replace(/</g, '<');
section.text = section.text.replace(/>/g, '>');
section.text = section.text.replace(/\t/g, ' ');
section.html = prettyPrintOne(section.text, undefined, true);
}else if( section.type === "comment" && Showdown ){
section.html = new Showdown.converter().makeHtml(section.text);
section.html = section.html.replace(/<br ?\/>/g, ' ');
}
});
return sections;
function isComment(line){
for(var i = 0; i < separators.length; i++){
var separator = separators[i];
var re = new RegExp('^\\s*' + separator.head + '\\s?')
var matches = line.match(re);
if( matches ) return true;
}
return false;
}
function removeCommentMarkup(line){
for(var i = 0; i < separators.length; i++){
var separator = separators[i];
var re = new RegExp('^\\s*' + separator.head + '\\s?(.*)'
+ (separator.tail ? '('+separator.tail+')' :''));
var matches = line.match(re);
if( matches ) return matches[1] ? matches[1] : "";
}
console.assert(false,"this point should NEVER be reached");
return undefined;
}
}