Skip to content

Instantly share code, notes, and snippets.

@myun2
Last active October 24, 2017 02:49
Show Gist options
  • Save myun2/2137acc66d4f7e1f537c81aec8c9ed88 to your computer and use it in GitHub Desktop.
Save myun2/2137acc66d4f7e1f537c81aec8c9ed88 to your computer and use it in GitHub Desktop.
class @MeaveCss
constructor: () ->
@content = ''
add: (selector, prop, value) ->
@content += selector + "{" + prop + ":" + value + "}"
render: () ->
@content
// Generated by CoffeeScript 1.12.7
(function() {
this.MeaveCss = (function() {
function MeaveCss() {
this.content = '';
}
MeaveCss.prototype.add = function(selector, prop, value) {
return this.content += selector + "{" + prop + ":" + value + "}";
};
MeaveCss.prototype.render = function() {
return this.content;
};
return MeaveCss;
})();
}).call(this);
class @MeaveHtml
constructor: (body = '', head = '') ->
@head = head
@body = body
render: () ->
'<!DOCTYPE html><html><head>' + @head +
'</head><body>' + @body + '</body></html>'
// Generated by CoffeeScript 1.12.7
(function() {
this.MeaveHtml = (function() {
function MeaveHtml(body, head) {
if (body == null) {
body = '';
}
if (head == null) {
head = '';
}
this.head = head;
this.body = body;
}
MeaveHtml.prototype.render = function() {
return '<!DOCTYPE html><html><head>' + this.head + '</head><body>' + this.body + '</body></html>';
};
return MeaveHtml;
})();
}).call(this);
class @Meave
constructor: () ->
@document = document
// Generated by CoffeeScript 1.12.7
(function() {
this.Meave = (function() {
function Meave() {
this.document = document;
}
return Meave;
})();
}).call(this);
MeaveCss = require('./css') .MeaveCss
MeaveTag = require('./tag') .MeaveTag
MeaveHtml = require('./html').MeaveHtml
class MeaveServer
constructor: (port, host = "localhost") ->
@port = port
@host = host
start: () ->
http = require('http')
server = http.createServer() 
server.on 'request', (q, r) ->
r.writeHead 200, {'Content-Type': 'text/html'}
css = new MeaveCss()
css.add('*','margin', 0)
css.add('*','padding', 0)
style_tag = new MeaveTag('style', css.render()).render()
body = new MeaveTag('h1', 'hello world').render()
r.write new MeaveHtml(body, style_tag).render()
console.log q.headers.host + ' -> hello world'
r.end()
server.listen @port, @host
server = new MeaveServer(8906, "0.0.0.0")
server.start()
console.log "Started at 0.0.0.0:8906"
// Generated by CoffeeScript 1.12.7
(function() {
var MeaveCss, MeaveHtml, MeaveServer, MeaveTag, server;
MeaveCss = require('./css').MeaveCss;
MeaveTag = require('./tag').MeaveTag;
MeaveHtml = require('./html').MeaveHtml;
MeaveServer = (function() {
function MeaveServer(port, host) {
if (host == null) {
host = "localhost";
}
this.port = port;
this.host = host;
}
MeaveServer.prototype.start = function() {
var http, server;
http = require('http');
server = http.createServer();
server.on('request', function(q, r) {
var body, css, style_tag;
r.writeHead(200, {
'Content-Type': 'text/html'
});
css = new MeaveCss();
css.add('*', 'margin', 0);
css.add('*', 'padding', 0);
style_tag = new MeaveTag('style', css.render()).render();
body = new MeaveTag('h1', 'hello world').render();
r.write(new MeaveHtml(body, style_tag).render());
console.log(q.headers.host + ' -> hello world');
return r.end();
});
return server.listen(this.port, this.host);
};
return MeaveServer;
})();
server = new MeaveServer(8906, "0.0.0.0");
server.start();
console.log("Started at 0.0.0.0:8906");
}).call(this);
class @MeaveTag
constructor: (name, content, attributes = "") ->
@name = name
@content = content
@attributes = if attributes then " " + attributes else ""
render: () ->
"<#{@name}#{@attributes}>#{@content}</#{@name}>"
// Generated by CoffeeScript 1.12.7
(function() {
this.MeaveTag = (function() {
function MeaveTag(name, content, attributes) {
if (attributes == null) {
attributes = "";
}
this.name = name;
this.content = content;
this.attributes = attributes ? " " + attributes : "";
}
MeaveTag.prototype.render = function() {
return "<" + this.name + this.attributes + ">" + this.content + "</" + this.name + ">";
};
return MeaveTag;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment