Skip to content

Instantly share code, notes, and snippets.

@polotek
Created August 17, 2010 02:53
Show Gist options
  • Save polotek/528215 to your computer and use it in GitHub Desktop.
Save polotek/528215 to your computer and use it in GitHub Desktop.
process.on('uncaughtException', function uncaughtException(e) {
if(outfile && outfile.writable) {
outfile.end();
outfile.destroy();
}
console.error(e.message);
console.error(e.stack);
})
var outfile;
var fs = require('fs')
, _ = require('underscore')._
, xml = require('libxmljs')
, dominiq = require('node-sizzle/dominiq')
, sizzFact = require('node-sizzle');
_ = require('underscorex').extend(_);
xml = require('xmlx').extend(xml);
Object.getAllPropertyNames = function getAllPropertyNames(obj) {
var keys = [];
while(obj && obj !== Object) {
keys = keys.concat(Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj);
}
return keys;
}
var pages = ['element'
, 'htmldocument'
, 'saxparser'
, 'syntaxerror'
, 'attribute'
, 'saxpushparser'
, 'xmldocument'
, 'namespace'
, 'parser'];
_.each(pages, function pageLoop(page, idx, pages) {
var page = page;
fs.readFile(page, 'utf8', function fileProcessor(err, htmlstr) {
if(err) throw err;
console.warn(page);
var doc = xml.parseHtmlString(htmlstr);
if(doc) {
outfile = fs.createWriteStream(page + '.md', {encoding:'utf8'
, mode: 0775});
outfile.writeln = function writeln(msg) { this.write(msg + '\n'); };
var content = doc.get('//body//*[@id="content"]');
var title = content.get('h1');
outfile.writeln('# ' + title.text() + '\n');
var desc = title.nextElement();
outfile.writeln(desc.text() + '\n');
var children = content.childElements();
_.each(children, function childLoop(child, idx) {
console.warn('child: ' + child.name());
var cur, cur2;
switch(child.name()) {
case 'h2':
if(child.text().indexOf('Instantiation') !== -1) {
outfile.writeln('### Constructor\n');
}
if(child.text().indexOf('Methods') !== -1) {
outfile.writeln('### Methods\n');
}
case 'p':
if(child.get('*[@class="CodeRay"]')) {
cur = '';
if(child.text().indexOf('new') === -1) cur = page + '.';
outfile.writeln('#### ' + cur + child.text() + '\n');
}
break;
case 'blockquote':
cur = child.get('p');
if(cur) {
cur = cur.text();
cur2 = cur.indexOf('returns');
if(cur2 !== -1) cur = cur.slice(0, cur2);
outfile.writeln('>' + cur + '\n');
}
cur = child.get('h3');
if(cur && (cur.text().indexOf('Arguments') !== -1)) {
outfile.writeln('>**args** ');
cur = child.find('ul/li');
if(cur) {
for(var i=0, len=cur.length; i<len; i++) {
cur2 = cur[i].get('strong');
outfile.write('*' + cur2.text() + '* ');
cur2 = cur[i].text();
cur2 = cur2.slice(cur2.indexOf('-'));
outfile.writeln(cur2 + ' ');
}
outfile.writeln('\n');
}
}
cur = child.get('p/strong');
// console.log(child.toString(), String(cur));
if(cur && (cur.text().indexOf('returns') !== -1)) {
outfile.write('>**returns** ');
while(cur = cur.nextSibling()) {
outfile.write(cur.text());
}
outfile.writeln('\n');
}
break;
}
});
outfile.end();
console.warn(page, ' done');
} else { console.log('errors in %s:\n%j', page, doc) }
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment