Commits

firefly committed 10eb1ba

Renamed wikidown->wykedown.

  • Participants
  • Parent commits 8ce2fde

Comments (0)

Files changed (2)

 ###########################################################
 
 express   = require 'express'
-<<<<<<< local
 Mongolian = require 'mongolian'
 wykeUtils = require './wyke'
-=======
-#Mongolian = require 'mongolian'
-{Wyke}    = require './wyke'
->>>>>>> other
 
 basename = __dirname.replace /\/[^\/]+$/, ''
 
 # Init the Express web server
 app = express.createServer()
 app.use express.bodyParser()
+app.use express.cookieParser()
+app.use express.session secret: "there is no_secret!"
 app.use express.static basename + '/res'
 
 # Setup the database...
 wyke = new wykeUtils.Wyke {database: database}
 
 # Catch all /wyke/anything/ident URLs, even when ident contains '/' characters.
-app.all /\/wyke\/[^\/]+(?:\/(.*))?/, (req, res, next) ->
+app.all /^\/wyke\/[^\/]+(?:\/(.*))?/, (req, res, next) ->
+	console.log "Test", req.session.toString()
+
 	req.ident = req.params[0] or "Index"
 	req.addr  = req.connection.remoteAddress or "???"
 	do next
 
-app.get '/', (req, res) -> res.redirect '/wyke/view/Index'
+app.all /^\/(?!wyke)(.*)/, (req, res) ->
+	req.ident = "Page:#{req.params[0] or 'Index'}"
+	getRaw req, res
+
+# app.get '/?',      (req, res) -> res.redirect '/wyke/view/Index'
 app.get '/wyke/?', (req, res) -> res.redirect '/wyke/view/Index'
 
 #### /wyke/view/:ident ####################################
 app.get '/wyke/view/*?', (req, res) ->
 	console.log "[#{req.addr}] [  view] #{req.ident}"
 	
-	# FIXME
 	wyke.getPage req.ident, (err, page) ->
 		if err and err.errno == 404
+			# TODO: Add proper 404 page.
 			res.send "Page not found: #{req.ident}", 404
 			return
 		
 				if err
 					res.send "<pre>Error:\n" + err.message + "</pre>"
 				else
-				#	res.contentType(ns.mimetype)
 					res.send result
 
 #### /wyke/raw/:ident #####################################
-# FIXME: Abstract together with the above function.
-app.get '/wyke/raw/*', (req, res) ->
+# FIXME: Join with the above function.
+app.get '/wyke/raw/*', getRaw = (req, res) ->
 	console.log "[#{req.addr}] [  raw ] #{req.ident}"
 	
 	wyke.getPage req.ident, (err, page) ->
 
 app.post '/wyke/edit/*', (req, res) ->
 	throw new Error "Something went wrong!" if not req.body.page
-	console.log "[#{req.addr}] [# save] #{req.ident}"
-	
-<<<<<<< local
-=======
 	console.log "[#{req.addr}] [# SAVE] #{req.ident}"
->>>>>>> other
+
 	content = req.body.page.body.replace /\r\n/g, '\n'
 	
 	wyke.storePage req.ident, content, (err, page) ->
 vm         = require 'vm'
 #Mongolian  = require 'mongolian'
-wikidown   = require './wikidown'
+wykedown   = require './wykedown'
 utils      = require './utils'
 
 #### Database helper functions ############################
 			coll.insert value
 			console.log "Adding collection #{JSON.stringify query}..."
 		
-		callback(null, value) if callback
+		callback null, value if callback
 	
 	value
 
 	obj["name"] = nsName
 	createOrUpdate @database.collection('namespaces'), {name: nsName}, 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 value,
+# so it is safe to use this function even if you're unsure whether the page is
+# wrapped.
 wrapPage = (wyke, page) ->
 	#### page.getNamespace ################################
 	return page if page.getNamespace
 	
 	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::getPage = (ident, callback) ->
 	@database.collection('pages').findOne {ident: ident}, (err, page) =>
 		return callback err if err
 		
 		callback null, wrapPage this, page
 
+# Includes a specific tepmlate through a specific includer on a specific
+# source tree.
+# TODO: This needs cleanup.
 applyIncluder = (source, sourceTree, template, includer, args) ->	# , callback
 	ctx =
-		source     : source
-		sourceTree : sourceTree
-		template   : template
-		args       : args
+		source       : source
+		sourceTree   : sourceTree
+		template     : template
+		args         : args
 		
-		formatString : formatString.bind(this)
-		utils      : utils
-		vm         : vm
-		console    : console
+		formatString : formatString.bind this
+		utils        : utils
+		vm           : vm
+		console      : console
 	
 #	console.log "Applying #{includer.ident} on #{template.ident}"
 	
 	res = vm.runInNewContext includer.body, ctx, "(includer) #{includer.ident}"
 
+# Renders a given page with a given namespace's renderer.
 applyRendererByNS = (page, str, ns, callback) ->
 	wyke = this
-#	ns.getRenderer (err, renderer) ->
 	wyke.getPage ns.renderer, (err, renderer) ->
 		renderer.getNamespace (err, rNS) ->
 			wyke.getPage rNS.includer, (err, includer) ->
 				res = applyIncluder.call wyke, page, null, renderer, includer, ["FAKE-NULL", str]
 				callback null, res
 
+# Converts wykedown->HTML completely, including substitution of templates.
 formatString = (str, args, callback) ->
 	wyke = this
 	
-	res = wikidown.parse str
+	res = wykedown.parse str
 	templatesQuery = {}
 	
 	# Process nodes that need post-processing. Also create a map of what
 				delete attrs['target']
 			
 			when 'template'
-				if attrs['name'][0] == ':'
-					prop = attrs['name'][1..]
-					utils.tree.replaceNode nodeI, (args[prop] or ["{{#{attrs['name']}}}"]).map (x) -> {original: x}
+				if attrs['name'][0] == ':'	# we're dealing with an argument (e.g. {{:1}})
+					prop  = attrs['name'][1..]
+					value = args[prop] or ["{{#{attrs['name']}}}"]
+					utils.tree.replaceNode nodeI, value.map (x) -> {original: x}
 				
 				else
 					ident = utils.wykenc.titleEncode attrs['name']
 			
 			callback null, res
 
+# 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::renderPage = (page, callback) ->
 	wyke = this
 	
 			applyRendererByNS.call wyke, page, res, globalRendererNS, (err, res) ->
 				return callback err if err
 				formatString.call wyke, res, {}, (err, res) ->
-					callback null, wikidown.toHTML res
+					callback null, wykedown.toHTML res
 
+# Renders an internal page. First argument is an options object, with the
+# following properties: {ident: string, title: string, body: string[, internal: boolean]}
 Wyke::renderInternalPage = (opts, callback) ->
 	page = {
 		ident    : opts.ident ? "--internal:unknown"
 		ctx =
 			page     : page
 			content  : page.body
-			wikidown : wikidown
+			wykedown : wykedown
 		
 		try
 			res = vm.runInNewContext renderer.body, ctx, "(renderer) #{renderer.ident}"