Permalink
Browse files

Extract renderer into own module.

This way, it can be used by both the CLI and the web viewer.
  • Loading branch information...
alecperkins committed Oct 17, 2012
1 parent 31a31cf commit 25c6f259e648479bd9e3db3edb232b10f5c1106c
Showing with 139 additions and 97 deletions.
  1. +0 −1 .slugignore
  2. +9 −4 Cakefile
  3. +2 −1 package.json
  4. +1 −0 src/VERSION.coffee
  5. +8 −0 src/http_utils.coffee
  6. +11 −79 src/proto.coffee
  7. +71 −0 src/renderer.coffee
  8. +37 −12 viewer/app.coffee
View
@@ -1,5 +1,4 @@
node_modules
-src
bin
.npmignore
View
@@ -2,11 +2,16 @@ fs = require 'fs'
sys = require 'sys'
CoffeeScript = require 'coffee-script'
+walk = require 'walk'
+
compileScriptFile = (from, to) ->
- sys.puts('Compiling script')
+ sys.puts("Compiling script: #{ from }")
script_source = fs.readFileSync(from)
compiled = CoffeeScript.compile(script_source.toString())
- fs.writeFileSync(to, compiled)
+ fs.writeFileSync(to.replace('.coffee', '.js'), compiled)
-task 'build', 'compile src/proto.coffee > lib/proto.js', ->
- compileScriptFile('src/proto.coffee', 'lib/proto.js')
+task 'build', 'Compile src/*.coffee > lib/*.coffee', ->
+ walker = walk.walk('src')
+ walker.on 'file', (root, fileStats, next) ->
+ compileScriptFile('src/' + fileStats.name, 'lib/' + fileStats.name)
+ next()
View
@@ -16,7 +16,8 @@
"gitjs": "0.0.5-3",
"jade": "0.27.6",
"stylus": "0.29.0",
- "restler": "2.0.1"
+ "restler": "2.0.1",
+ "walk": "2.2.1"
},
"bin": {
"proto": "./bin/proto"
View
@@ -0,0 +1 @@
+module.exports = '0.0.5'
View
@@ -0,0 +1,8 @@
+htmlResponse = (response, content, status_code=200) ->
+ response.writeHead status_code,
+ 'Content-Type' : 'text/html'
+ 'Content-Length' : Buffer.byteLength(content).toString()
+ response.end(content)
+
+module.exports =
+ htmlResponse: htmlResponse
View
@@ -6,10 +6,8 @@ express = require 'express'
git = require 'gitjs'
rest = require 'restler'
+renderer = require './renderer'
-CoffeeScript = require 'coffee-script'
-Jade = require 'jade'
-Stylus = require 'stylus'
proto_version = "0.0.4"
@@ -240,88 +238,22 @@ serveProject = (project_name, port) ->
style : project_path + '/style.styl'
settings : project_path + '/settings.json'
- sys.puts("Working on #{ project_name }\n#{ project_path }")
-
- loadSettings = (settings_source_file) ->
- settings_raw = fs.readFileSync(settings_source_file)
- settings = JSON.parse(settings_raw)
- # TODO: Validate settings
- if settings.proto_version isnt proto_version
- quitWithMsg("Error: #{ project_name }'s proto_version (#{ settings.proto_version }) does not match Proto's (#{ proto_version })")
- return settings
-
- compileScriptFile = (script_source_file) ->
- stamp('Compiling script')
-
- script_source = fs.readFileSync(script_source_file)
- return CoffeeScript.compile(script_source.toString())
-
- compileMarkupFile = (markup_source_file) ->
- stamp('Compiling markup')
- markup_source = fs.readFileSync(markup_source_file)
- template = Jade.compile(markup_source.toString())
- return template()
-
- compileStyleFile = (style_source_file) ->
- stamp('Compiling style')
- style_source = fs.readFileSync(style_source_file)
- compiled_style = ''
- # This isn't actually async, just bonkers.
- Stylus.render style_source.toString(), (err, data) ->
- compiled_style = data
- return compiled_style
-
- compileScriptLibraries = (settings) ->
- script_libs = ''
- for lib in settings.script_libraries
- script_libs += "<script src='#{ lib }'></script>"
- return script_libs
-
- compileStyleLibraries = (settings) ->
- style_libs = ''
- for lib in settings.style_libraries
- style_libs += "<link rel='stylesheet' href='#{ lib }' type='text/css'>"
- return style_libs
-
- compositePage = (compiled) ->
- page = """
- <!-- Generated by https://github.com/droptype/proto v#{ proto_version } -->
- <!doctype html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- #{ compiled.script_libraries }
- #{ compiled.style_libraries }
- <style>
- #{ compiled.style }
- </style>
- </head>
- <body>
- #{ compiled.markup }
- <script>
- #{ compiled.script }
- </script>
- </body>
- </html>
- """
- return page
+ stamp("Working on #{ project_name }\n#{ project_path }")
+
+ loadSources = ->
+ source_content = {}
+ for k, v of sources
+ source_content[k] = fs.readFileSync(v)
+ source_content.settings = JSON.parse(source_content.settings)
+ return source_content
doCompilation = ->
- stamp('Compiling all the things')
- settings = loadSettings(sources.settings)
- output = compositePage
- style : compileStyleFile(sources.style)
- script : compileScriptFile(sources.script)
- markup : compileMarkupFile(sources.markup)
- script_libraries : compileScriptLibraries(settings)
- style_libraries : compileStyleLibraries(settings)
+ output = loadSources(sources)
+ output = renderer(output)
return output
serveContent = ->
- # TODO: just use stack (included in cli)
- stamp('Creating server')
app = express()
-
app.get '/', (req, res) ->
res.send(doCompilation())
stamp("Listening on http://localhost:#{ port }")
View
@@ -0,0 +1,71 @@
+CoffeeScript = require 'coffee-script'
+Jade = require 'jade'
+Stylus = require 'stylus'
+
+VERSION = require './VERSION'
+
+
+
+compileScriptFile = (script_source) ->
+ return CoffeeScript.compile(script_source.toString())
+
+compileMarkupFile = (markup_source) ->
+ template = Jade.compile(markup_source.toString())
+ return template()
+
+compileStyleFile = (style_source) ->
+ compiled_style = ''
+ # This isn't actually async, just bonkers.
+ Stylus.render style_source.toString(), (err, data) ->
+ compiled_style = data
+ return compiled_style
+
+compileScriptLibraries = (script_libraries) ->
+ script_libs = ''
+ for lib in script_libraries
+ script_libs += "<script src='#{ lib }'></script>"
+ return script_libs
+
+compileStyleLibraries = (style_libraries) ->
+ style_libs = ''
+ for lib in style_libraries
+ style_libs += "<link rel='stylesheet' href='#{ lib }' type='text/css'>"
+ return style_libs
+
+
+
+compositePage = (compiled) ->
+ page = """
+ <!-- Generated by https://github.com/droptype/proto v#{ VERSION } -->
+ <!doctype html>
+ <html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+ #{ compiled.script_libraries }
+ #{ compiled.style_libraries }
+ <style>
+ #{ compiled.style }
+ </style>
+ </head>
+ <body>
+ #{ compiled.markup }
+ <script>
+ #{ compiled.script }
+ </script>
+ </body>
+ </html>
+ """
+ return page
+
+doCompilation = (sources) ->
+ output = compositePage
+ style : compileStyleFile(sources.style)
+ script : compileScriptFile(sources.script)
+ markup : compileMarkupFile(sources.markup)
+ script_libraries : compileScriptLibraries(sources.settings.script_libraries)
+ style_libraries : compileStyleLibraries(sources.settings.style_libraries)
+ return output
+
+
+module.exports = (sources) ->
+ return doCompilation(sources)
View
@@ -1,21 +1,46 @@
-cli = require 'cli'
+cli = require 'cli'
+
+{ htmlResponse } = require '../src/http_utils'
+renderer = require '../src/renderer'
+rest = require 'restler'
+
+
+
+getGist = (url, cb) ->
+ GIST_API = 'https://api.github.com/gists'
+ post_req = rest.get(GIST_API + url)
+ post_req.on 'complete', (data, response) ->
+ cb(data, response.statusCode)
+
+
handleIndex = (request, response, next) ->
- console.dir(response)
- response.writeHead 200,
- 'Content-Type': 'text/html'
- response.end('Hello World!')
+ if request.url is '/'
+ html_content = 'Hello World!'
+ htmlResponse(response, html_content)
+ else
+ next()
+
+handleRequests = (request, response, next) ->
+ unless request.url is '/favicon.ico'
+ getGist request.url, (data, res_code) ->
+ if res_code is 200
+ content = renderer
+ style : data.files['style.styl'].content
+ script : data.files['script.coffee'].content
+ markup : data.files['markup.jade'].content
+ settings : JSON.parse(data.files['settings.json'].content)
+ htmlResponse(response, content)
+ else
+ content = 'Gist not found'
+ htmlResponse(response, content, 404)
+
+
port = process.env.PORT or 5000
cli.createServer([
handleIndex
+ handleRequests
]).listen(port)
-
-# Extract rendering/serving portion to own module shared by viewer and cli
-
-# Drop express for just Stack.
-# https://github.com/chriso/cli
-# https://github.com/creationix/creationix/blob/master/indexer.js
-# https://github.com/creationix/stack/wiki/Community-Modules

0 comments on commit 25c6f25

Please sign in to comment.