Skip to content

Commit

Permalink
Add #4, local server support.
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Oct 6, 2014
1 parent 776a5ac commit c239369
Show file tree
Hide file tree
Showing 15 changed files with 9,046 additions and 10 deletions.
8 changes: 5 additions & 3 deletions bin/markline
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ commander
.usage('[options]')
.option('-v, --version', 'output the version number')
.option('build', 'build markdown to markline page.')
.option('server', 'build markdown to markline page.')
.option('server', 'preview markdown to markline page.')
.option('-p, --port [port]', 'server port for preview markline.')
.option('-w, --watch', 'watch markdown file change, and auto reload for preview markline.')
.parse(process.argv);


Expand All @@ -22,8 +24,8 @@ if (commander.build) {
markline.build(options);
process.exit();
} else if (commander.server){
markline.server(options);
process.exit();
markline.server(cwd, commander);
return;
}

// output help and exit if no args found
Expand Down
67 changes: 60 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
var fs = require("fs");
var path = require("path");

function build(options){
console.log(options.src)
console.log(options.dist)
}

function server(options){
var http = require('http');
var app = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
var DEFAULT_PORT = 8000;
var DEFAULT_ENCODE = "utf-8";

function server(cwd, options){
var port = options.port || DEFAULT_PORT;
var encode = options.encode || DEFAULT_ENCODE;

var fileName = options.args.join("");
if (!fs.existsSync(fileName)) {
console.error("Not Found:", fileName);
return;
}

var http = require('http').createServer(function (req, res) {
var url = req.url;

if (url === "/") {
fs.readFile(path.join(__dirname, "template", "index.html"), encode, function(err, html){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html.replace("{FILE_NANE}", fileName));
});
} else if (url === "/" + fileName) {
fs.readFile(path.join(cwd, fileName), encode, function(err, markdown){
res.writeHead(200, {'Content-Type': 'text/markdown'});
res.end(markdown);
});
} else {

var MIME_TYPE = {
"html": "text/html",
"js": "text/javascript",
"css": "text/css"
};

if (url === "/") {
url = "index.html";
}

var ext = url.split(".");
ext = ext[ext.length - 1];

var filePath = path.join(__dirname, "template", url);
if (MIME_TYPE.hasOwnProperty(ext) && fs.existsSync(filePath)) {

fs.readFile(filePath, encode, function(err, html){
res.writeHead(200, {'Content-Type': MIME_TYPE[ext]});
res.end(html.replace("{FILE_NANE}", fileName));
});

} else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('404\n');
}

}
}).listen(port).on("error", function(error){
console.error(error);
});
app.listen(8080);
console.log("Server Started 127.0.0.1:8080")
console.log("Server Started 127.0.0.1:" + port);
}

function watch(){
Expand Down
18 changes: 18 additions & 0 deletions template/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name = $(shell cat package.json | grep '"name"' | awk -F'"' '{print $$4}')
version = $(shell cat package.json | grep '"version"' | awk -F'"' '{print $$4}')
seajs_version = $(shell cat package.json | grep '"seajs"' | awk -F'"' '{print $$4}')
markline_version = $(shell cat package.json | grep '"markline"' | awk -F'"' '{print $$4}')

install:
@spm install

build: clear
@spm build --with-deps
@mkdir -p ./assets/seajs/$(seajs_version)
@cp ./spm_modules/seajs/$(seajs_version)/dist/* ./assets/seajs/$(seajs_version)
@sed 's/markline\/[0-9\.]\{1,\}/markline\/$(markline_version)/g' index.html > index-tmp.html
@rm -rf index.html
@mv index-tmp.html index.html

clear:
@rm -rf assets
Loading

0 comments on commit c239369

Please sign in to comment.