Skip to content

Commit

Permalink
Copying content from old SVN repository
Browse files Browse the repository at this point in the history
  • Loading branch information
TehShrike committed May 6, 2012
1 parent afdb09a commit c91ee84
Show file tree
Hide file tree
Showing 16 changed files with 1,888 additions and 0 deletions.
1,332 changes: 1,332 additions & 0 deletions Markdown.Converter.js

Large diffs are not rendered by default.

142 changes: 142 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
var path = require('path');
var fs = require("fs");

exports.getCache = function(directory, encoding, expiration)
{
var encoding = encoding || "binary";
var expiration = expiration || false;
var storage = {}

var updateFileExistence = function(file_name)
{
if (!file_name)
return;

var full_path = path.join(process.cwd(), directory, file_name);

path.exists(full_path, function(exists)
{
if (exists)
{
storage[file_name] = {};
storage[file_name].loading = false;

if (expiration === false)
loadFile(file_name);
}
else
{
//console.log(full_path + " does not exist");
delete storage[file_name];
}
});
}

var resetExpirationCounter = function(file_name)
{
if (storage[file_name])
{
if (storage[file_name].timeout_id)
{
clearTimeout(storage[file_name].timeout_id);
delete storage[file_name].timeout_id;
}

if (expiration > 0)
{
//console.log("Expiring " + file_name + " in " + (expiration * 1000));
storage[file_name].timeout_id = setTimeout(function()
{
//console.log(file_name + " expired");
if (storage[file_name] && storage[file_name].data)
{
delete storage[file_name].data;
delete storage[file_name].timeout_id;
}
}, expiration * 1000);
}
}
}

var loadFile = function(file_name, callback)
{
if (!file_name)
return;

if (storage[file_name] && storage[file_name].loading)
{
storage[file_name].callbacks.push(callback);
return;
}

var full_path = path.join(process.cwd(), directory, file_name);
storage[file_name] = {};
storage[file_name].loading = true;
storage[file_name].callbacks = [callback];

//console.log("Attempting to read file " + file_name);

fs.readFile(full_path, encoding, function(err, data)
{
if (!err)
{
if (expiration === false || expiration > 0)
{
//console.log("Caching " + file_name);
storage[file_name].data = data;
if (expiration > 0)
resetExpirationCounter(file_name);
}
}
else
{
data = false;
//console.log("Unable to read " + full_path + " I guess");
}

storage[file_name].loading = false;
var callback;
while (callback = storage[file_name].callbacks.shift())
{
//console.log("Calling a callback on load of " + file_name);
callback(data);
}
});

}

fs.readdir(path.join(process.cwd(), directory), function(err, files)
{
if (!err)
{
for (i in files)
{
updateFileExistence(files[i]);
}
}
});

fs.watch(directory, { persistant: false }, function(event, file_name)
{
updateFileExistence(file_name);
});

return {
getFile: function(file_name, callback)
{
if (!storage[file_name])
callback(false);
else if (storage[file_name].data)
{
callback(storage[file_name].data);
resetExpirationCounter(file_name);
}
else
loadFile(file_name, callback);
},
getCurrentCache: function()
{
return storage;
}
}
}
5 changes: 5 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.extensions = {file: './file.js', post: './post.js', html: './html.js'}

exports.post = { file_suffix: ".md" }

exports.html = { file_suffix: ".html", cache_expiration: false }
15 changes: 15 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.loaded_extensions = {};

exports.loadExtensions = function(extensions_to_load)
{
for (var extension_name in extensions_to_load)
{
exports.loaded_extensions[extension_name] = require(extensions_to_load[extension_name]);

for (var extension in exports.loaded_extensions[extension_name])
{
if (extension.load)
extension.load();
}
}
}
41 changes: 41 additions & 0 deletions file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var path = require('path');

exports.handleRequest = function(noddity_url, response, send404)
{
if (noddity_url.path)
{
var local_path = path.join(process.cwd(), "file", path.normalize(noddity_url.path));

path.exists(local_path, function(exists)
{
if (exists)
{
require("fs").readFile(local_path, "binary", function(err, file)
{
if(err)
{
response.writeHeader(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
}
else
{
response.writeHeader(200);
response.write(file, "binary");
response.end();
}
});
}
else
{
console.log(local_path + " does not exist, sending 404");
send404(response);
}
});
}
else
{
console.log("No noddity_url.path, sending 404");
send404(response);
}
}
Binary file added file/Awesome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/conan.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var cache = require('./cache.js').getCache('html', 'utf8', false);
var config = require('./config.js');

// Tries to find a filename that matches: html_file specified in content metadata, extension name, slug name, index
exports.provideBaseOutput = function(page, callback)
{
var suffix = config.html.file_suffix;

cache.getFile(page.metadata.html_file + suffix, function(output)
{
if (output !== false)
callback(output);
else
cache.getFile(page.url.extension + suffix, function(output)
{
if (output !== false)
callback(output);
else
cache.getFile(page.url.slug + suffix, function(output)
{
if (output !== false)
callback(output)
else
cache.getFile('index' + suffix, function(output)
{
callback(output || "No HTML file found");
});
});
});
});
}

exports.provideReplaceable = function(page, parameter, callback)
{
cache.getFile(parameter + config.html.file_suffix, function(output)
{
callback(output || "");
});
}
2 changes: 2 additions & 0 deletions html/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<img src="../file/awesome.png" height=100px/>
<h1>Welcome to my awesome site! This is the header, and it goes everywhere!</h1>
18 changes: 18 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<title>
{?post:metadata:title}
</title>
</head>
<body>
{?html:header}
<div id="content">
{?content_html}
</div>
<div id="menu">
<div id="posts">
{?post:last5}
</div>
</div>
</body>
</html>
13 changes: 13 additions & 0 deletions html/no-menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<title>
{?content:title}
</title>
</head>
<body>
{?html:header}
<div id="content">
{?content}
</div>
</body>
</html>
Loading

0 comments on commit c91ee84

Please sign in to comment.