Commits

firefly committed 12e9a90

Added comments to the new `wyke.js`.

Comments (0)

Files changed (1)

   , wykedown = require('./wykedown') 
   , utils    = require('./utils')
 
+//-- Database helper functions --------------------------------------
 function createOrUpdate(coll, query, value, isUpdate, callback) {
 	coll.findOne(query, function(err, oldValue) {
 		if (err && callback) return callback(err)
 	return value
 }
 
+//-- Other utility functions ----------------------------------------
 exports.parseIdent = function(ident) {
 	var res = {}
 	var matches = ident.match(/(?:([A-Za-z_-]+):|)([A-Za-z][^\t\n\r#]*)/)
 	return res
 }
 
+//-- The actual wyke object -----------------------------------------
 var Wyke = exports.Wyke = function(opts) {
 	this.database = opts['database']
 }
 	}, obj, isUpdate, callback)
 }
 
+// Wraps a "raw" database page entry in a more OOP-y kind of object. This is the
+// preferred way to e.g. get the namespace of a page, since it caches the
+// result.
+//   Trying to wrap an already wrapped page object simply returns the same
+// object, so it is safe to use this function even if you're unsure whether the
+// page is wrapped or not.
 function wrapPage(wyke, page) {
 	if (page.getNamespace) return page
 
+	//-- page.getNamespace ------------------------------------------
 	page.getNamespace = function(callback) {
 		var self = this
 
 	return page
 }
 
+// Gets a specific page (by ident) from the database, or errors if not found.
+// This is the preferred way to fetch pages. Returns a wrapped page, so there is
+// no need to call `Wyke#wrapPage` on the page returned.
 Wyke.prototype.getPage = function(ident, callback) {
 	var self = this
 
 	})
 }
 
+// Includes a specific template through a specific includer on a specific source
+// tree.
+// TODO: This needs cleanup.
 function applyIncluder(source, sourceTree, template, includer, args) {
-	var ctx, res
-
-	ctx = {
+	var ctx = {
 		source       : source,
 		sourceTree   : sourceTree,
 		template     : template,
 		console      : console
 	}
 
+//  console.log("Applying " + includer.ident + " on " + template.ident)
+
 	return vm.runInNewContext(includer.body, ctx, "(includer) " + includer.ident)
 }
 
+// Renders a given page with a given namespace's renderer.
 function applyRendererByNS(page, str, ns, callback) {
 	var wyke = this
 
 	})
 }
 
+// Converts wykedown->HTML completely, including substitutions of templates.
 function formatString(str, args, callback) {
 	var wyke = this
-	var res  = wykedown.parse(str)
+
+	var res = wykedown.parse(str)
 	var templatesQuery = {}
 
+	// Process nodes that need post-processing. Also create a map of what
+	// templates are being included.
 	res = utils.tree.travel(res, function(nodeI) {
 		var ident, prop, value
 		var name  = this[0]
 		  , attrs = this[1]
 
+		// Process nodes here!
 		switch (name) {
 			case 'header':
 				attrs['id'] = utils.wykenc.encode(utils.tree.nodeToString(this))
 		}
 	})
 
-	return utils.async.map(templatesQuery, wyke.getPage.bind(wyke),
+	// Fetch all the needed templates asynchronously, then perform the
+	// inclusion.
+	utils.async.map(templatesQuery, wyke.getPage.bind(wyke),
 			function(err, templates) {
 		if (err) return callback(err)
 
 	})
 }
 
+// Renders a given page as HTML through its namespace's associated renderer.
+// First argument is either a Page or a string specifying the ident of the page.
 Wyke.prototype.renderPage = function(page, callback) {
 	var wyke = this
 
 	})
 }
 
+// Renders an internal page. First argument is an options object, with the
+// following properties:
+//   {ident: string, title: string, body: string[, internal: boolean]}
 Wyke.prototype.renderInternalPage = function(opts, callback) {
 	var page = {
 		ident: opts.indent || "--internal:unknown",
 	}
 
 	function getRenderer(ident, callback) {
-		if (typeof ident !== 'string'){
+		if (typeof ident != 'string') {    // Ident is already a renderer
 			callback(null, ident)
 		} else {
 			wyke.getPage(ident, callback)
 		}
 	})
 }
+
+//-- TEMPORARY: For debugging purposes ------------------------------
+//require('./app')