function parse_code(code, lang, outputFormat, template, callback) {
var parsed_code = [],
this_line,
in_comment,
in_code,
spaces,
commentblock = [],
codeblock = [],
tempblock = [];
if (code && typeof code !== 'string') code = code.toString().split("\n");
if (typeof lang === 'undefined' || !code) return;
function pushblock() {
parsed_code.push({ code: codeblock.join('\n'), comment: commentblock.join('\n') });
codeblock = [];
commentblock = [];
in_code = false;
}
for (var i = 0, l = code.length; i < l; i++) {
this_line = code[i];
if (this_line.match(lang.comment) && !in_comment && !this_line.match(/^#\!/)) {
if (in_code) pushblock();
commentblock.push(this_line.replace(lang.comment, ''))
} else if (this_line.match(lang.start) && !in_comment) {
if (lang.name === 'python' && in_code) {
while (codeblock[codeblock.length - 1].trim() !== '') {
tempblock.push(codeblock.pop());
}
}
if (in_code) pushblock();
if (lang.name === 'python') {
for (var ti = 0, tl = tempblock.length; ti < tl; ti++) {
codeblock.push(tempblock.pop());
}
}
in_comment = true;
spaces = this_line.match(/^\s+/);
if (spaces) spaces = spaces[0].length;
this_line = this_line.replace(lang.start, '');
if (this_line.match(lang.end)) {
this_line = this_line.replace(lang.end, '');
in_comment = false;
}
if (this_line.trim() !== '') commentblock.push(this_line);
} else if (this_line.match(lang.end) && in_comment) {
this_line = this_line.replace(lang.end, '');
if (this_line.trim() !== '') commentblock.push(this_line);
in_comment = false;
} else if (this_line.trim() === '' && !in_comment && !in_code) {
pushblock();
} else {
if (in_comment) {
if (lang.name === 'python') this_line = this_line.substring(spaces);
commentblock.push(this_line);
} else {
if (!in_code && this_line.trim() !== '') in_code = true;
codeblock.push(this_line);
}
}
}
pushblock();
if (outputFormat === 'md') {
generate_md(parsed_code, lang, callback);
} else if (outputFormat === 'html') {
generate_html(parsed_code, lang, template, callback);
}
}
|
function generate_html(parsed_code, language, template, callback) {
var outfile,
templatePath,
template;
if (typeof template === 'undefined') {
templatePath = path.join(__dirname, 'template.jade');
} else {
templatePath = template;
}
template = fs.readFileSync(__dirname + '/template.jade', 'utf-8');
var fn = jade.compile(template);
callback(null, fn({ gfm: gfm, data: parsed_code, hljs: hljs, lang: language.name }));
}
|
var C_LINE_COMMENT = /^\s*\/\/\s?/,
C_BLOCK_COMMENT_START = /^\s*\/\*\s?/,
C_BLOCK_COMMENT_END = /\*\/\s*$/,
HASH_LINE_COMMENT = /^\s*#\s?/,
NEVER_MATCH = /a\bc/;
var languages = {
'.js': { name: 'javascript', comment: C_LINE_COMMENT, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.py': { name: 'python', comment: HASH_LINE_COMMENT, start: /^\s*\"\"\"\s?/, end: /\"\"\"\s*$/ },
'.rb': { name: 'ruby', comment: HASH_LINE_COMMENT, start: /^\s*\=begin\s?/, end: /\=end\s*$/ },
'.lua': { name: 'lua', comment: /^\s*--\s?/, start: /^\s*--\[\[\s?/, end: /--\]\]\s*$/ },
'.coffee': { name: 'coffeescript', comment: /^\s*#(?!##)\s?/, start: /^\s*###\s?/, end: /###\s*$/ },
'.php': { name: 'php', comment: /^\s*(?:#|\/\/\s?)/, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.c': { name: null, comment: C_LINE_COMMENT, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.h': { name: null, comment: C_LINE_COMMENT, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.pl': { name: 'perl', comment: HASH_LINE_COMMENT, start: NEVER_MATCH, end: NEVER_MATCH },
'.cpp': { name: 'cpp', comment: C_LINE_COMMENT, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.cs': { name: 'cs', comment: C_LINE_COMMENT, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.m': { name: 'objectivec', comment: C_LINE_COMMENT, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.sql': { name: 'sql', comment: /^\s*--\s?/, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.sh': { name: 'bash', comment: HASH_LINE_COMMENT, start: NEVER_MATCH, end: NEVER_MATCH },
'.css': { name: 'css', comment: NEVER_MATCH, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END },
'.as': { name: 'actionscript', comment: C_LINE_COMMENT, start: C_BLOCK_COMMENT_START, end: C_BLOCK_COMMENT_END }
};
|