mirror of https://github.com/lukechilds/node.git
Ryan Dahl
15 years ago
66 changed files with 916 additions and 698 deletions
@ -0,0 +1,167 @@ |
|||
var asciidoc = { // Namespace.
|
|||
|
|||
/////////////////////////////////////////////////////////////////////
|
|||
// Table Of Contents generator
|
|||
/////////////////////////////////////////////////////////////////////
|
|||
|
|||
/* Author: Mihai Bazon, September 2002 |
|||
* http://students.infoiasi.ro/~mishoo
|
|||
* |
|||
* Table Of Content generator |
|||
* Version: 0.4 |
|||
* |
|||
* Feel free to use this script under the terms of the GNU General Public |
|||
* License, as long as you do not remove or alter this notice. |
|||
*/ |
|||
|
|||
/* modified by Troy D. Hanson, September 2006. License: GPL */ |
|||
/* modified by Stuart Rackham, 2006, 2009. License: GPL */ |
|||
|
|||
// toclevels = 1..4.
|
|||
toc: function (toclevels) { |
|||
|
|||
function getText(el) { |
|||
var text = ""; |
|||
for (var i = el.firstChild; i != null; i = i.nextSibling) { |
|||
if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
|
|||
text += i.data; |
|||
else if (i.firstChild != null) |
|||
text += getText(i); |
|||
} |
|||
return text; |
|||
} |
|||
|
|||
function TocEntry(el, text, toclevel) { |
|||
this.element = el; |
|||
this.text = text; |
|||
this.toclevel = toclevel; |
|||
} |
|||
|
|||
function tocEntries(el, toclevels) { |
|||
var result = new Array; |
|||
var re = new RegExp('[hH]([2-'+(toclevels+1)+'])'); |
|||
// Function that scans the DOM tree for header elements (the DOM2
|
|||
// nodeIterator API would be a better technique but not supported by all
|
|||
// browsers).
|
|||
var iterate = function (el) { |
|||
for (var i = el.firstChild; i != null; i = i.nextSibling) { |
|||
if (i.nodeType == 1 /* Node.ELEMENT_NODE */) { |
|||
var mo = re.exec(i.tagName); |
|||
if (mo) |
|||
result[result.length] = new TocEntry(i, getText(i), mo[1]-1); |
|||
iterate(i); |
|||
} |
|||
} |
|||
} |
|||
iterate(el); |
|||
return result; |
|||
} |
|||
|
|||
var toc = document.getElementById("toc"); |
|||
var entries = tocEntries(document.getElementById("content"), toclevels); |
|||
for (var i = 0; i < entries.length; ++i) { |
|||
var entry = entries[i]; |
|||
if (entry.element.id == "") |
|||
entry.element.id = "_toc_" + i; |
|||
var a = document.createElement("a"); |
|||
a.href = "#" + entry.element.id; |
|||
a.appendChild(document.createTextNode(entry.text)); |
|||
var div = document.createElement("div"); |
|||
div.appendChild(a); |
|||
div.className = "toclevel" + entry.toclevel; |
|||
toc.appendChild(div); |
|||
} |
|||
if (entries.length == 0) |
|||
toc.parentNode.removeChild(toc); |
|||
}, |
|||
|
|||
|
|||
/////////////////////////////////////////////////////////////////////
|
|||
// Footnotes generator
|
|||
/////////////////////////////////////////////////////////////////////
|
|||
|
|||
/* Based on footnote generation code from: |
|||
* http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
|
|||
*/ |
|||
|
|||
footnotes: function () { |
|||
var cont = document.getElementById("content"); |
|||
var noteholder = document.getElementById("footnotes"); |
|||
var spans = cont.getElementsByTagName("span"); |
|||
var refs = {}; |
|||
var n = 0; |
|||
for (i=0; i<spans.length; i++) { |
|||
if (spans[i].className == "footnote") { |
|||
n++; |
|||
// Use [\s\S] in place of . so multi-line matches work.
|
|||
// Because JavaScript has no s (dotall) regex flag.
|
|||
note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1]; |
|||
noteholder.innerHTML += |
|||
"<div class='footnote' id='_footnote_" + n + "'>" + |
|||
"<a href='#_footnoteref_" + n + "' title='Return to text'>" + |
|||
n + "</a>. " + note + "</div>"; |
|||
spans[i].innerHTML = |
|||
"[<a id='_footnoteref_" + n + "' href='#_footnote_" + n + |
|||
"' title='View footnote' class='footnote'>" + n + "</a>]"; |
|||
var id =spans[i].getAttribute("id"); |
|||
if (id != null) refs["#"+id] = n; |
|||
} |
|||
} |
|||
if (n == 0) |
|||
noteholder.parentNode.removeChild(noteholder); |
|||
else { |
|||
// Process footnoterefs.
|
|||
for (i=0; i<spans.length; i++) { |
|||
if (spans[i].className == "footnoteref") { |
|||
var href = spans[i].getElementsByTagName("a")[0].getAttribute("href"); |
|||
href = href.match(/#.*/)[0]; // Because IE return full URL.
|
|||
n = refs[href]; |
|||
spans[i].innerHTML = |
|||
"[<a href='#_footnote_" + n + |
|||
"' title='View footnote' class='footnote'>" + n + "</a>]"; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
}; |
|||
|
|||
(function() { |
|||
var includes = ['sh_main.js', 'sh_javascript.min.js', 'sh_vim-dark.css']; |
|||
var head = document.getElementsByTagName("head")[0]; |
|||
|
|||
for (var i = 0; i < includes.length; i ++) { |
|||
var ext = includes[i].match(/\.([^.]+)$/); |
|||
switch (ext[1]) { |
|||
case 'js': |
|||
var element = document.createElement('script'); |
|||
element.type = 'text/javascript'; |
|||
element.src = includes[i]; |
|||
break; |
|||
case 'css': |
|||
var element = document.createElement('link'); |
|||
element.type = 'text/css'; |
|||
element.rel = 'stylesheet'; |
|||
element.media = 'screen'; |
|||
element.href = includes[i]; |
|||
break; |
|||
} |
|||
|
|||
head.appendChild(element); |
|||
} |
|||
var i = setInterval(function () { |
|||
if (window["sh_highlightDocument"]) { |
|||
sh_highlightDocument(); |
|||
|
|||
try { |
|||
var pageTracker = _gat._getTracker("UA-10874194-2"); |
|||
pageTracker._trackPageview(); |
|||
} catch(err) {} |
|||
|
|||
clearInterval(i); |
|||
} |
|||
}, 100); |
|||
|
|||
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
|||
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
|||
})(); |
@ -1,156 +1 @@ |
|||
var posix = require("./posix"); |
|||
var events = require('events'); |
|||
/*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */ |
|||
/*globals exports, node, __filename */ |
|||
|
|||
exports.debugLevel = 0; // Increase to get more verbose debug output
|
|||
|
|||
function debugMessage (msg) { |
|||
if (exports.debugLevel > 0) { |
|||
process.error(__filename + ": " + msg.toString()); |
|||
} |
|||
} |
|||
|
|||
function debugObject (obj) { |
|||
if (exports.debugLevel > 0) { |
|||
process.error(__filename + ": " + JSON.stringify(obj)); |
|||
} |
|||
} |
|||
|
|||
exports.read = posix.cat; |
|||
|
|||
exports.write = function (filename, data, encoding) { |
|||
var promise = new events.Promise(); |
|||
|
|||
encoding = encoding || "utf8"; // default to utf8
|
|||
|
|||
posix.open(filename, process.O_WRONLY | process.O_TRUNC | process.O_CREAT, 0666) |
|||
.addCallback(function (fd) { |
|||
function doWrite (_data) { |
|||
posix.write(fd, _data, 0, encoding) |
|||
.addErrback(function () { |
|||
posix.close(fd); |
|||
promise.emitError(); |
|||
}) |
|||
.addCallback(function (written) { |
|||
if (written === _data.length) { |
|||
posix.close(fd); |
|||
promise.emitSuccess(); |
|||
} else { |
|||
doWrite(_data.slice(written)); |
|||
} |
|||
}); |
|||
} |
|||
doWrite(data); |
|||
}) |
|||
.addErrback(function () { |
|||
promise.emitError(); |
|||
}); |
|||
|
|||
return promise; |
|||
}; |
|||
|
|||
exports.File = function (filename, mode, options) { |
|||
var self = this; |
|||
|
|||
options = options || {}; |
|||
self.encoding = options.encoding || "utf8"; |
|||
|
|||
self.filename = filename; |
|||
|
|||
self.actionQueue = []; |
|||
self.currentAction = null; |
|||
|
|||
switch (mode) { |
|||
case "r": |
|||
self.flags = process.O_RDONLY; |
|||
break; |
|||
|
|||
case "r+": |
|||
self.flags = process.O_RDWR; |
|||
break; |
|||
|
|||
case "w": |
|||
self.flags = process.O_CREAT | process.O_TRUNC | process.O_WRONLY; |
|||
break; |
|||
|
|||
case "w+": |
|||
self.flags = process.O_CREAT | process.O_TRUNC | process.O_RDWR; |
|||
break; |
|||
|
|||
case "a": |
|||
self.flags = process.O_APPEND | process.O_CREAT | process.O_WRONLY; |
|||
break; |
|||
|
|||
case "a+": |
|||
self.flags = process.O_APPEND | process.O_CREAT | process.O_RDWR; |
|||
break; |
|||
|
|||
default: |
|||
throw new Error("Unknown mode"); |
|||
} |
|||
|
|||
self.open(self.filename, self.flags, 0666).addCallback(function (fd) { |
|||
debugMessage(self.filename + " opened. fd = " + fd); |
|||
self.fd = fd; |
|||
}).addErrback(function () { |
|||
self.emit("error", ["open"]); |
|||
}); |
|||
}; |
|||
|
|||
var proto = exports.File.prototype; |
|||
|
|||
proto._maybeDispatch = function () { |
|||
var self, args, method, promise, userPromise; |
|||
|
|||
self = this; |
|||
|
|||
if (self.currentAction) { return; } |
|||
self.currentAction = self.actionQueue.shift(); |
|||
if (!self.currentAction) { return; } |
|||
|
|||
debugObject(self.currentAction); |
|||
|
|||
args = self.currentAction.args || []; |
|||
method = self.currentAction.method; |
|||
|
|||
|
|||
if (method !== "open") { |
|||
args.unshift(self.fd); |
|||
} |
|||
|
|||
if (!args[3] && (method === "read" || method === "write")) { |
|||
args[3] = self.encoding; |
|||
} |
|||
promise = posix[method].apply(self, args); |
|||
|
|||
userPromise = self.currentAction.promise; |
|||
|
|||
promise.addCallback(function () { |
|||
process.assert(self.currentAction.promise === userPromise); |
|||
userPromise.emitSuccess.apply(userPromise, arguments); |
|||
self.currentAction = null; |
|||
self._maybeDispatch(); |
|||
}).addErrback(function () { |
|||
debugMessage("Error in method " + method); |
|||
process.assert(self.currentAction.promise === userPromise); |
|||
userPromise.emitError.apply(userPromise, arguments); |
|||
self.currentAction = null; |
|||
self._maybeDispatch(); |
|||
}); |
|||
}; |
|||
|
|||
proto._queueAction = function (method, args) { |
|||
var userPromise = new events.Promise(); |
|||
this.actionQueue.push({ method: method, args: args, promise: userPromise }); |
|||
this._maybeDispatch(); |
|||
return userPromise; |
|||
}; |
|||
|
|||
|
|||
(["open", "write", "read", "close"]).forEach(function (name) { |
|||
proto[name] = function () { |
|||
return this._queueAction(name, Array.prototype.slice.call(arguments, 0)); |
|||
}; |
|||
}); |
|||
|
|||
throw new Error("The 'file' module has been removed. 'file.read' is now 'fs.readFile', and 'file.write' is now 'fs.writeFile'."); |
|||
|
@ -0,0 +1 @@ |
|||
throw new Error("The 'posix' module has been renamed to 'fs'"); |
@ -1,38 +0,0 @@ |
|||
process.mixin(require("./common")); |
|||
|
|||
var testTxt = path.join(fixturesDir, "test.txt"); |
|||
|
|||
var libDir = path.join(testDir, "../../lib"); |
|||
require.paths.unshift(libDir); |
|||
process.mixin(require("file")); |
|||
|
|||
var fileUnlinked = false; |
|||
|
|||
var file = new File(testTxt, "w+"); |
|||
file.write("hello\n"); |
|||
file.write("world\n"); |
|||
setTimeout(function () { |
|||
file.write("hello\n"); |
|||
file.write("world\n"); |
|||
file.close().addCallback(function () { |
|||
error("file closed..."); |
|||
var out = posix.cat(testTxt).wait(); |
|||
print("the file contains: "); |
|||
p(out); |
|||
assert.equal("hello\nworld\nhello\nworld\n", out); |
|||
var file2 = new File(testTxt, "r"); |
|||
file2.read(5).addCallback(function (data) { |
|||
puts("read(5): " + JSON.stringify(data)); |
|||
assert.equal("hello", data); |
|||
posix.unlink(testTxt).addCallback(function () { |
|||
fileUnlinked = true; |
|||
}); |
|||
}); |
|||
file2.close(); |
|||
}); |
|||
}, 10); |
|||
|
|||
process.addListener("exit", function () { |
|||
assert.equal(true, fileUnlinked); |
|||
puts("done"); |
|||
}); |
@ -0,0 +1,5 @@ |
|||
process.mixin(require('./common')); |
|||
|
|||
var fixture = path.join(__dirname, "fixtures/x.txt"); |
|||
|
|||
assert.equal("xyz\n", fs.readFileSync(fixture)); |
Loading…
Reference in new issue