Created
December 12, 2010 22:39
-
-
Save CarterA/738413 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The issue I am having has to do with something about the way I am requiring classes, and maybe a bit to do with inheritance issues. The basic concept is: | |
| Site: Creates a bunch of File and Page instances, calls the process() method on each one, then returns. | |
| File: Manages the metadata and converter settings for a given file. | |
| Page: Inherits from File. Eventually it will have a different process() implementation, so that pages are processed separately from other files. | |
| I want to create File instances for every file in a given directory, except for files that are .html. Those should be created as Pages, not files. Get it? | |
| Yet currently, when I call process() (line 57, site.js), it says that the method doesn't exist. What gives? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Find the metadata for the file. Metadata is stored in the native | |
| format for comments in whatever language is used. Currently supported | |
| languages include HTML, CSS, JavaScript, and anything else that uses | |
| the comment format of those languages. | |
| */ | |
| var getMetadata = function(content, callback) { | |
| var metadata; | |
| if (content) { | |
| commentRegexes = [ | |
| /<!--Omen\s+(\{[^}]*\})\s*-->/, | |
| /\*!Omen\s+(\{[^}]*\})\s*\*/ | |
| ]; | |
| var match; | |
| commentRegexes.forEach(function(regex) { | |
| matches = content.match(regex) | |
| if (matches) match = matches[1]; | |
| }); | |
| if (match) metadata = JSON.parse(match); | |
| } | |
| callback(metadata); | |
| }; | |
| var File = exports = module.exports = function(content, options) { | |
| //console.log("Omen.File init with options: "); | |
| //console.log(options); | |
| getMetadata(content, function(metadata) { | |
| //console.log(metadata); | |
| }); | |
| }; | |
| File.prototype.process = function(callback) { | |
| console.log("Omen.File instance process()") | |
| callback(); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fileClass = require("./file"); | |
| var Page = exports = module.exports = fileClass; | |
| Page.prototype = fileClass; | |
| Page.prototype.constructor = fileClass; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs = require("fs"), | |
| path = require("path"), | |
| walker = require("walker"); | |
| var pageClass = require("./page"), | |
| fileClass = require("./file"); | |
| var defaults = { | |
| "inputDirectory" : "./", | |
| "outputDirectory" : "./build", | |
| "blogRoot" : "", | |
| "articlesDirectory" : "./articles", | |
| "templatesDirectory" : "./templates", | |
| "templateEngine" : "ejs", | |
| "defaultPageTemplate" : "default", | |
| "defaultArticleTemplate" : "article" | |
| }; | |
| var Site = exports = module.exports = function(options) { | |
| if (!options) options = defaults; | |
| // Merge the passed options hash with the default values | |
| Object.keys(defaults).forEach(function(key) { | |
| if (!options[key]) options[key] = defaults[key]; | |
| }); | |
| //console.log("Omen.Site init with options: "); | |
| //console.log(options); | |
| this.options = options; | |
| }; | |
| Site.prototype.process = function(callback) { | |
| var options = this.options | |
| console.log(fileClass); | |
| // Create a list of files to be processed using Walker | |
| var files = []; | |
| walker(options["inputDirectory"]).on("file", function(file) { | |
| if (!(path.basename(file).match(/^./) == ".") || options["processHiddenFiles"]) files.push(file); | |
| }).on("end", function() { | |
| //console.log("The input directory has been walked, and the following files were found:"); | |
| //console.log(files); | |
| files.forEach(function(file) { | |
| fs.readFile(file, function(error, data) { | |
| var fileInstance; | |
| if (/.html$/.test(path.basename(file))) fileInstance = new pageClass(data.toString(), null); | |
| else fileInstance = new fileClass(data.toString(), null); | |
| fileInstance.process(function() { | |
| console.log("File: " + file + "has been processed."); | |
| }); | |
| }); | |
| }); | |
| callback(); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment