Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 68fd0a7b4679798b39ea5df7a4f3a895638a0911 @youurayy committed Jan 29, 2012
Showing with 1,694 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +45 −0 README.md
  3. +3 −0 bin/gfms
  4. +134 −0 gfms.js
  5. +36 −0 package.json
  6. +11 −0 public/gfms.js
  7. +2 −0 public/style.css
  8. +1,426 −0 showdown.js
  9. +7 −0 views/directory.jade
  10. +9 −0 views/file.jade
  11. +12 −0 views/layout.jade
  12. +7 −0 views/style.styl
@@ -0,0 +1,2 @@
+node_modules
+/log
@@ -0,0 +1,45 @@
+# Github Flavored Markdown Server (GFMS)
+
+### (based on Node.js, Express.js, Jade, Stylus, Socket.io and GFM for JavaScript)
+
+---
+I could not find a tool that would allow me to preview Github Flavored Markdown files offline, so I wrote one. (Well it's not completely offline - it loads the Github CSS from Akamai and JQuery from Google's CDN, but you know how to cache them if you need to.)
+
+Basically the only way how to preview GFM somewhat properly, was to commit the file to the Github repo and reload the browser page. However committing just for preview unecessarily pollutes GIT history of the file, and it's a tedious process.
+
+Sure, there are various good Markdown editors for all platforms, but only a few support GFM, and to my knowledge, none properly supports the syntax-colored code blocks denoted by three apostrophes, e.g.
+
+```js
+function(arg) {
+ // some code here
+}
+```
+
+And none would show the result looking (almost) exactly like at Github.
+
+## Limitations
+
+Well, the current implementation of GFMS doesn't color the source code blocks, but it at least renders them correctly. I had to modify the `showdown.js` from `github-flavored-markdown`. I made a copy of that file, and fork & pull-request to the original project is on my `TODO` list. GFMS has `github-flavored-markdown` as a dependency despite it's not needed, so that the heritage is obvious in NPM.
+
+Another possible shortcoming is that the [Github CSS](http://a248.e.akamai.net/assets.github.com/stylesheets/bundles/github-34a19ad407ee058e180249a0f19030a3731ed4d8.css) file is loaded from Akamai, and when its name changes, this tool will need to be updated.
+
+## Usage
+
+ [sudo] npm install gfms -g
+ cd your-github-project-dir
+ gfms -p 1234
+
+Browse to `http://localhost:1234`, and select the `.md` or `.markdown` file to view.
+
+When you save the source Markdown file in your editor, it will be automatically updated in your browser. So perhaps a good setup is to have both your editor window and your browser window visible at the same time, so that you don't have to switch in between.
+
+## License
+(The MIT License)
+
+Copyright (c) 2012 Juraj Vitko (http://ypocat.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('../gfms');
134 gfms.js
@@ -0,0 +1,134 @@
+var argv = require('optimist')
+ .usage('\nGithub Flavored Markdown Server.\nRun in your project\'s root directory.\nUsage: $0')
+ .demand('p')
+ .alias('p', 'port')
+ .describe('p', 'Port number to listen at.')
+ .alias('h', 'host')
+ .describe('h', 'Host address to bind to.')
+ .default('h', 'localhost')
+ .argv;
+
+var sio = require('socket.io'), io;
+var express = require('express');
+var stylus = require('stylus');
+var nib = require('nib');
+var app = express.createServer();
+var markdown = //require('github-flavored-markdown').parse;
+ require('./showdown.js').parse;
+var _ = require('underscore');
+var fs = require('fs');
+
+var laeh = require('laeh2').leanStacks(true);
+var _e = laeh._e;
+var _x = laeh._x;
+
+var watched = {};
+
+app.configure(function() {
+
+ var pub = __dirname + '/public';
+ var views = __dirname + '/views';
+ app.set('views', views);
+ app.set('view engine', 'jade');
+ app.set('view options', { layout: false });
+
+
+ if(process.env.NODE_ENV === 'development') {
+ // only use Stylus in development, because when gfms is installed
+ // globally with sudo, and then run by an user, it cannot create
+ // the generate .css files (and I'm too tired to look for a solution now).
+ app.use(stylus.middleware({
+ src: views,
+ dest: pub,
+ compile: function(str, path) {
+ return stylus(str)
+ .set('filename', path)
+ .set('compress', true)
+ .use(nib())
+ .import('nib');
+ }
+ }));
+ }
+
+ app.use(express.favicon());
+ app.use(app.router);
+ app.use(express.static(pub));
+ app.use(express.errorHandler({ dump: true, stack: true }));
+});
+
+app.configure('development', function() {
+ require('utilz').watchFile(__filename);
+});
+
+function basename(fn) {
+ var m = fn.match(/.*?([^\/]+)\/?$/);
+ return m ? m[1] : fn;
+}
+
+function is_markdown(v) {
+ return v.match(/.*?(?:\.md|\.markdown)$/) ? true : false;
+}
+
+app.get('*', function(req, res, next) {
+
+ var base = req.path.replace('..', 'DENIED').replace(/\/$/, '');
+ var dir = process.cwd() + base;
+
+ var stat;
+ try {
+ stat = fs.statSync(dir);
+ }
+ catch(e) {
+ return next();
+ }
+
+ if(stat.isDirectory()) {
+
+ var files = _.chain(fs.readdirSync(dir)).filter(function(v) {
+ var stat = fs.statSync(dir + '/' + v);
+ return stat.isDirectory() || (stat.isFile() && is_markdown(v));
+ }).map(function(v) {
+ return {
+ url: base + '/' + v,
+ name: v
+ };
+ }).value();
+
+ res.render('directory', {
+ files: files,
+ dir: dir,
+ title: basename(dir)
+ });
+ }
+ else if(is_markdown(dir)) {
+
+ if(!watched[dir]) {
+ fs.watchFile(dir, { interval: 500 }, function(curr, prev) {
+ if(curr.mtime.getTime() !== prev.mtime.getTime()) {
+ console.log('file ' + dir + ' has changed');
+ io.sockets.json.send({ update: dir, content: markdown(fs.readFileSync(dir, 'utf8')) });
+ }
+ });
+ watched[dir] = true;
+ }
+
+ res.render('file', {
+ file: markdown(fs.readFileSync(dir, 'utf8')),
+ title: basename(dir),
+ fullname: dir
+ });
+ }
+ else
+ return next();
+});
+
+app.listen(argv.p, argv.h);
+io = sio.listen(app);
+io.set('log level', 1);
+
+console.log('GFMS serving ' + process.cwd() + ' at http://' + argv.h + ':' + argv.p + '/ - press CTRL+C to exit.');
+
+process.on('SIGINT', function() {
+ console.log('\nGFMS exit.');
+ return process.exit();
+});
@@ -0,0 +1,36 @@
+{
+ "author": "Juraj Vitko (ypocat.com)",
+ "name": "gfms",
+ "description": "Github Flavored Markdown Server",
+ "keywords": [ "markdown", "server", "local", "reloading" ],
+ "version": "0.0.1",
+ "homepage": "http://github.com/ypocat/gfms",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/ypocat/gfms.git"
+ },
+ "main": "gfms.js",
+ "directories": {
+ "lib": "./lib"
+ },
+ "bin": {
+ "gfms": "./bin/gfms"
+ },
+ "engines": {
+ "node": ">= 0.6.0"
+ },
+ "dependencies": {
+ "laeh2": "0.2.0",
+ "express": "2.5.6",
+ "optimist": "0.2.x",
+ "github-flavored-markdown": "1.0.0",
+ "utilz": "0.1.3",
+ "jade": "0.20.0",
+ "stylus": "0.22.6",
+ "nib": "0.3.2",
+ "underscore": "1.3.1",
+ "socket.io": "0.8.7"
+ },
+ "devDependencies": {
+ }
+}
@@ -0,0 +1,11 @@
+$(function() {
+
+ var filename = $('#content').data('filename');
+
+ var socket = io.connect();
+ socket.on('message', function(data) {
+ if(filename === data.update)
+ $('div.markdown-body').html(data.content);
+ });
+
+});
@@ -0,0 +1,2 @@
+#content{padding:50px 0}
+#content{width:920px;margin:0 auto}
Oops, something went wrong.

0 comments on commit 68fd0a7

Please sign in to comment.