Permalink
Browse files

update

  • Loading branch information...
1 parent 38c9981 commit 242a31b20f6224ce9586b635c9b90c2ddf645979 @youurayy committed Nov 6, 2012
Showing with 68 additions and 8 deletions.
  1. +37 −6 gfms.js
  2. +2 −2 package.json
  3. +29 −0 scratchpad.txt
View
43 gfms.js
@@ -6,6 +6,9 @@ var argv = require('optimist')
.alias('h', 'host')
.describe('h', 'Host address to bind to.')
.default('h', 'localhost')
+ .boolean('a')
+ .describe('a', 'Render using Github API.')
+ .alias('a', 'api')
.argv;
var express = require('express');
@@ -120,17 +123,45 @@ app.get('*', function(req, res, next) {
watched[dir] = true;
}
- res.render('file', {
- file: markdown(fs.readFileSync(dir, 'utf8')),
- title: basename(dir),
- styles: styles,
- fullname: dir
- });
+ var contents = fs.readFileSync(dir, 'utf8');
+ var func = argv.a ? renderWithGithub : renderWithShowdown;
+
+ func(contents, _x(next, true, function(err, res) {
+
+ res.render('file', {
+ file: res,
+ title: basename(dir),
+ styles: styles,
+ fullname: dir
+ });
+
+ }));
}
else
return next();
});
+function renderWithShowdown(contents, cb) { // cb(err, res)
+ var res = markdown(contents);
+ cb(null, res);
+}
+
+function renderWithGithub(contents, cb) { // cb(err, res)
+ var opts = {
+ method: 'post',
+ url: 'https://github.com/markdown',
+ json: {
+ text: contents,
+ mode: 'markdown'
+ },
+ encoding: 'utf8'
+ };
+
+ request(opts, _x(cb, true, function(err, res, body) {
+ cb(null, body);
+ }));
+}
+
process.on('SIGINT', function() {
console.log('\nGFMS exit.');
return process.exit();
View
@@ -2,8 +2,8 @@
"author": "Juraj Vitko (ypocat.com)",
"name": "gfms",
"description": "Github Flavored Markdown Server",
- "keywords": [ "markdown", "server", "local", "reloading" ],
- "version": "0.0.4",
+ "keywords": [ "markdown", "server", "local", "reloading", "github", "render", "gfm" ],
+ "version": "0.0.5",
"homepage": "http://github.com/ypocat/gfms",
"repository": {
"type": "git",
View
@@ -0,0 +1,29 @@
+ /*
+ Ref: http://developer.github.com/v3/oauth/
+ Ref: https://github.com/settings/applications/new
+ Ref: http://developer.github.com/v3/markdown/
+
+ POST to: https://github.com/markdown { text: 'text', mode: 'markdown' }
+ Response: body
+
+ if error:
+ 1. redirect to: https://github.com/login/oauth/authorize
+ ?client_id=APPID
+ &redirect_uri=http://HOST:PORT
+ &state=RANDOM
+
+ 2. catch the ?code= param
+
+ 3. server POST to: https://github.com/login/oauth/access_token
+ ?client_id=APPID
+ &client_secret=APPSECRET
+ &code=CODE
+ &state=RANDOM
+
+ header: Accept: application/json
+ response: { access_token: }
+
+ 4. repeat the POST as /markdown?access_token=ACCESSTOKEN
+
+ 5. if error, go again with the redir, user will bail out as they see fit
+ */

0 comments on commit 242a31b

Please sign in to comment.