Permalink
Browse files

Rebuild Rocco.coffee

  • Loading branch information...
1 parent f256417 commit fb513eb4dfbb342c0df4411f0957afef2c9405b2 @daffl daffl committed Dec 27, 2011
Showing with 27 additions and 12 deletions.
  1. +27 −12 lib/rocco.js
View
@@ -1,18 +1,18 @@
(function() {
var configuration, destination, ensure_directory, exec, ext, fs, generate_documentation, generate_html, get_language, highlight, highlight_end, highlight_start, l, languages, parse, path, rocco, rocco_styles, rocco_template, showdown, sources, spawn, template, _ref;
+
generate_documentation = function(source, callback) {
return fs.readFile(source, "utf-8", function(error, code) {
var sections;
- if (error) {
- throw error;
- }
+ if (error) throw error;
sections = parse(source, code);
return highlight(source, sections, function() {
generate_html(source, sections);
return callback();
});
});
};
+
parse = function(source, code) {
var code_text, docs_text, has_code, language, line, lines, save, sections, _i, _len;
lines = code.split('\n');
@@ -41,24 +41,21 @@
save(docs_text, code_text);
return sections;
};
+
highlight = function(source, sections, callback) {
var language, output, pygments, section;
language = get_language(source);
pygments = spawn('pygmentize', ['-l', language.name, '-f', 'html', '-O', 'encoding=utf-8,tabsize=2']);
output = '';
pygments.stderr.addListener('data', function(error) {
- if (error) {
- return console.error(error.toString());
- }
+ if (error) return console.error(error.toString());
});
pygments.stdin.addListener('error', function(error) {
console.error("Could not use Pygments to highlight the source.");
return process.exit(1);
});
pygments.stdout.addListener('data', function(result) {
- if (result) {
- return output += result;
- }
+ if (result) return output += result;
});
pygments.addListener('exit', function() {
var fragments, i, section, _len;
@@ -84,6 +81,7 @@
return pygments.stdin.end();
}
};
+
generate_html = function(source, sections) {
var dest, html, title;
title = path.basename(source);
@@ -100,10 +98,15 @@
console.log("rocco: " + source + " -> " + dest);
return fs.writeFile(dest, html);
};
+
fs = require('fs');
+
path = require('path');
+
showdown = require('./../vendor/showdown').Showdown;
+
_ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
+
languages = {
'.coffee': {
name: 'coffee-script',
@@ -122,45 +125,57 @@
symbol: '#'
}
};
+
for (ext in languages) {
l = languages[ext];
l.comment_matcher = new RegExp('^\\s*' + l.symbol + '\\s?');
l.comment_filter = new RegExp('(^#![/]|^\\s*#\\{)');
l.divider_text = '\n' + l.symbol + 'DIVIDER\n';
l.divider_html = new RegExp('\\n*<span class="c1?">' + l.symbol + 'DIVIDER<\\/span>\\n*');
}
+
get_language = function(source) {
return languages[path.extname(source)];
};
+
destination = function(filepath) {
return configuration.output + '/' + path.basename(filepath, path.extname(filepath)) + '.html';
};
+
ensure_directory = function(dir, callback) {
return exec("mkdir -p " + dir, function() {
return callback();
});
};
+
template = function(str) {
return new Function('obj', 'var p=[],print=function(){p.push.apply(p,arguments);};' + 'with(obj){p.push(\'' + str.replace(/[\r\t\n]/g, " ").replace(/'(?=[^<]*%>)/g, "\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g, "',$1,'").split('<%').join("');").split('%>').join("p.push('") + "');}return p.join('');");
};
+
rocco_template = template(fs.readFileSync(__dirname + '/../resources/rocco.jst').toString());
+
rocco_styles = fs.readFileSync(__dirname + '/../resources/rocco.css').toString();
+
highlight_start = '<div class="highlight"><pre>';
+
highlight_end = '</pre></div>';
+
sources = [];
+
configuration = {};
+
rocco = function(srcs, config) {
var files, next_file;
sources = srcs;
configuration = config;
fs.writeFile("" + configuration.output + "/rocco.css", rocco_styles);
files = sources.slice(0);
next_file = function() {
- if (files.length) {
- return generate_documentation(files.shift(), next_file);
- }
+ if (files.length) return generate_documentation(files.shift(), next_file);
};
return next_file();
};
+
module.exports = rocco;
+
}).call(this);

0 comments on commit fb513eb

Please sign in to comment.