diff --git a/.gitignore b/.gitignore index 7dccd97..6cd408e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,10 +6,11 @@ lib-cov *.out *.pid *.gz +.DS_Store pids logs results node_modules -npm-debug.log \ No newline at end of file +npm-debug.log diff --git a/examples/blog/assets/images/nodejs.png b/examples/blog-only/assets/images/nodejs.png similarity index 100% rename from examples/blog/assets/images/nodejs.png rename to examples/blog-only/assets/images/nodejs.png diff --git a/examples/blog/assets/styles/blog.css b/examples/blog-only/assets/styles/blog.css similarity index 100% rename from examples/blog/assets/styles/blog.css rename to examples/blog-only/assets/styles/blog.css diff --git a/examples/blog/assets/styles/code-fixes.css b/examples/blog-only/assets/styles/code-fixes.css similarity index 100% rename from examples/blog/assets/styles/code-fixes.css rename to examples/blog-only/assets/styles/code-fixes.css diff --git a/examples/blog/assets/styles/code.css b/examples/blog-only/assets/styles/code.css similarity index 100% rename from examples/blog/assets/styles/code.css rename to examples/blog-only/assets/styles/code.css diff --git a/examples/blog/assets/styles/preview.css b/examples/blog-only/assets/styles/preview.css similarity index 100% rename from examples/blog/assets/styles/preview.css rename to examples/blog-only/assets/styles/preview.css diff --git a/examples/blog/blog.json b/examples/blog-only/blog.json similarity index 100% rename from examples/blog/blog.json rename to examples/blog-only/blog.json diff --git a/examples/blog/first-post/post.json b/examples/blog-only/first-post/post.json similarity index 100% rename from examples/blog/first-post/post.json rename to examples/blog-only/first-post/post.json diff --git a/examples/blog/first-post/post.md b/examples/blog-only/first-post/post.md similarity index 94% rename from examples/blog/first-post/post.md rename to examples/blog-only/first-post/post.md index 4a9b2bd..5f95354 100644 --- a/examples/blog/first-post/post.md +++ b/examples/blog-only/first-post/post.md @@ -2,7 +2,7 @@ Some introduction to the blog would be here, followed by the gory details. -![nodejs](blog/assets/images/nodejs.png) +![nodejs](assets/images/nodejs.png) ## Inlined snippet diff --git a/examples/blog/first-post/snippets/simple.js b/examples/blog-only/first-post/snippets/simple.js similarity index 100% rename from examples/blog/first-post/snippets/simple.js rename to examples/blog-only/first-post/snippets/simple.js diff --git a/examples/blog-site/blog/assets/images/nodejs.png b/examples/blog-site/blog/assets/images/nodejs.png new file mode 100644 index 0000000..b206e75 Binary files /dev/null and b/examples/blog-site/blog/assets/images/nodejs.png differ diff --git a/examples/blog-site/blog/assets/styles/blog.css b/examples/blog-site/blog/assets/styles/blog.css new file mode 100644 index 0000000..bb5946f --- /dev/null +++ b/examples/blog-site/blog/assets/styles/blog.css @@ -0,0 +1,19 @@ +.blog-post p > code { + background: #F0F8F7; + border-radius: 3px; + padding: 2px 5px 2px 5px; + top: -2px; + position: relative; +} + +/* block quotes */ +.blog-post pre { + font-style: italic; + padding-left: 40px; +} + +/* Don't apply above block quote style to code samples */ +.blog-post pre .syntaxhighlighter { + font-style: initial; + margin-left: -40px; +} diff --git a/examples/blog-site/blog/assets/styles/code-fixes.css b/examples/blog-site/blog/assets/styles/code-fixes.css new file mode 100644 index 0000000..284e7a6 --- /dev/null +++ b/examples/blog-site/blog/assets/styles/code-fixes.css @@ -0,0 +1,30 @@ + +.syntaxhighlighter .gutter .line { + border-right: 1px solid rgba(67, 189, 67, 0.26) !important; + padding-left: 5px; + padding-right: 5px; +} + +.syntaxhighlighter .gutter { + padding-right: 8px; +} + +.syntaxhighlighter { + width: 735px; + border-radius: 8px; + padding-left: 5px; + padding-right: 5px; + padding-top: 20px; + padding-bottom: 20px; +} + + +.syntaxhighlighter { + background-color: #FAF6DF !important; +} +.syntaxhighlighter .line.alt1 { + background-color: transparent !important; +} +.syntaxhighlighter .line.alt2 { + background-color: transparent !important; +} diff --git a/examples/blog-site/blog/assets/styles/code.css b/examples/blog-site/blog/assets/styles/code.css new file mode 100644 index 0000000..2942c4a --- /dev/null +++ b/examples/blog-site/blog/assets/styles/code.css @@ -0,0 +1,101 @@ +.syntaxhighlighter { + background-color: white !important; +} +.syntaxhighlighter .line.alt1 { + background-color: white !important; +} +.syntaxhighlighter .line.alt2 { + background-color: white !important; +} +.syntaxhighlighter .line.highlighted.alt1, .syntaxhighlighter .line.highlighted.alt2 { + background-color: #e0e0e0 !important; +} +.syntaxhighlighter .line.highlighted.number { + color: black !important; +} +.syntaxhighlighter table caption { + color: black !important; +} +.syntaxhighlighter .gutter { + color: #afafaf !important; +} +.syntaxhighlighter .gutter .line { + border-right: 3px solid #6ce26c !important; +} +.syntaxhighlighter .gutter .line.highlighted { + background-color: #6ce26c !important; + color: white !important; +} +.syntaxhighlighter.printing .line .content { + border: none !important; +} +.syntaxhighlighter.collapsed { + overflow: visible !important; +} +.syntaxhighlighter.collapsed .toolbar { + color: blue !important; + background: white !important; + border: 1px solid #6ce26c !important; +} +.syntaxhighlighter.collapsed .toolbar a { + color: blue !important; +} +.syntaxhighlighter.collapsed .toolbar a:hover { + color: red !important; +} +.syntaxhighlighter .toolbar { + color: white !important; + background: #6ce26c !important; + border: none !important; +} +.syntaxhighlighter .toolbar a { + color: white !important; +} +.syntaxhighlighter .toolbar a:hover { + color: black !important; +} +.syntaxhighlighter .plain, .syntaxhighlighter .plain a { + color: black !important; +} +.syntaxhighlighter .comments, .syntaxhighlighter .comments a { + color: #008200 !important; +} +.syntaxhighlighter .string, .syntaxhighlighter .string a { + color: blue !important; +} +.syntaxhighlighter .keyword { + color: #006699 !important; +} +.syntaxhighlighter .preprocessor { + color: gray !important; +} +.syntaxhighlighter .variable { + color: #aa7700 !important; +} +.syntaxhighlighter .value { + color: #009900 !important; +} +.syntaxhighlighter .functions { + color: #ff1493 !important; +} +.syntaxhighlighter .constants { + color: #0066cc !important; +} +.syntaxhighlighter .script { + font-weight: bold !important; + color: #006699 !important; + background-color: none !important; +} +.syntaxhighlighter .color1, .syntaxhighlighter .color1 a { + color: gray !important; +} +.syntaxhighlighter .color2, .syntaxhighlighter .color2 a { + color: #ff1493 !important; +} +.syntaxhighlighter .color3, .syntaxhighlighter .color3 a { + color: red !important; +} + +.syntaxhighlighter .keyword { + font-weight: bold !important; +} diff --git a/examples/blog-site/blog/assets/styles/preview.css b/examples/blog-site/blog/assets/styles/preview.css new file mode 100644 index 0000000..7a9a11d --- /dev/null +++ b/examples/blog-site/blog/assets/styles/preview.css @@ -0,0 +1,38 @@ +.blog-post { + margin-left: auto; + margin-right: auto; + width: 750px; + background: #E4FFF3; + padding: 5px 20px 20px 20px; + border-radius: 10px +} + +.blog-post h1 { + background: #F3EFCD; + border-top: 1px; + border-left: 1px; + border-right: 1px; + border-bottom: 1px; + border-color: #CAC59D; + border-style: solid; + border-radius: 5px 5px 1px 1px; + padding: 10px; +} + +.blog-post h2 { + border-top: 0px; + border-left: 0px; + border-right: 0px; + border-bottom: 1px; + border-color: #B0D4E2; + border-style: solid; + padding-bottom: 8px; +} + +.blog-post hr { + margin-left: -20px; + margin-right: -20px; + border-style: solid; + border-color: white; + border-width: 10px; +} diff --git a/examples/blog-site/blog/blog.json b/examples/blog-site/blog/blog.json new file mode 100644 index 0000000..6bbdc73 --- /dev/null +++ b/examples/blog-site/blog/blog.json @@ -0,0 +1,19 @@ +{ + "posts": [ + "disclaimer", + "first-post" + ], + "tags": [ + "blog", + "markdown", + "warning", + "example", + "best-practice" + ], + "styles": [ + "code", + "code-fixes", + "blog", + "preview" + ] +} \ No newline at end of file diff --git a/examples/blog-site/blog/disclaimer/post.json b/examples/blog-site/blog/disclaimer/post.json new file mode 100644 index 0000000..c1129cf --- /dev/null +++ b/examples/blog-site/blog/disclaimer/post.json @@ -0,0 +1,11 @@ +{ + "created": "2012-09-04T01:10:00.420Z", + "updated": "2012-09-04T01:37:34.098Z", + "tags": [ + "warning", + "example", + "best-practice" + ], + "name": "disclaimer", + "title": "Disclaimer, Please Read First!" +} \ No newline at end of file diff --git a/examples/blog-site/blog/disclaimer/post.md b/examples/blog-site/blog/disclaimer/post.md new file mode 100644 index 0000000..28f8fae --- /dev/null +++ b/examples/blog-site/blog/disclaimer/post.md @@ -0,0 +1,15 @@ +# Disclaimer + +Demonstrates how to use the dog provider in order to serve the blog via a website. + +Please keep in mind that I favored simplicity over correctness and robustness. + +I used shorter (and somewhat naive) implementations and avoided using libraries to let this example stand on its own. + +## Improvments needed + +The following should be improved if this was to be used for real: + +- implement a more robust router or use libraries like director or express +- the server and blog initialization need more robust error handling +- the code should be modularized diff --git a/examples/blog-site/blog/first-post/post.json b/examples/blog-site/blog/first-post/post.json new file mode 100644 index 0000000..5a647e1 --- /dev/null +++ b/examples/blog-site/blog/first-post/post.json @@ -0,0 +1,10 @@ +{ + "created": "2012-09-03T17:33:04.764Z", + "updated": "2012-09-03T17:33:04.764Z", + "tags": [ + "blog", + "markdown" + ], + "name": "first-post", + "title": "My First Post" +} \ No newline at end of file diff --git a/examples/blog-site/blog/first-post/post.md b/examples/blog-site/blog/first-post/post.md new file mode 100644 index 0000000..9d93692 --- /dev/null +++ b/examples/blog-site/blog/first-post/post.md @@ -0,0 +1,51 @@ +# Blog Uno + +Some introduction to the blog would be here, followed by the gory details. + +![nodejs](/blog/assets/images/nodejs.png) + +## Inlined snippet + +```javascript + var some = 'javascript'; +``` +The `var` ensures we don't pollute the global space. + + +## Snippet pulled in + +This is some explanation about the snippet to follow. + +{{ snippet: simple.js }} + +The moral of the snippet here: + + - moral one + - another moral + +*** + +## Styles + +Certain markdown directives should be recognized. + +### Italic + +This *should be italic* if it works. + +#### *Italic Header* + +Amazing what you can do with just markdown. + +### Bold + +This **should be bold** if it works. + +### Mixed + +This ***should be italic-bold*** if that also works. + +## Indentation + + Indented to generate a block quote + Second line diff --git a/examples/blog-site/blog/first-post/snippets/simple.js b/examples/blog-site/blog/first-post/snippets/simple.js new file mode 100644 index 0000000..e40963d --- /dev/null +++ b/examples/blog-site/blog/first-post/snippets/simple.js @@ -0,0 +1,3 @@ +var simple = 1; + +console.log('this is as simple as 10', simple); diff --git a/examples/blog-site/site/README.md b/examples/blog-site/site/README.md new file mode 100644 index 0000000..9285e35 --- /dev/null +++ b/examples/blog-site/site/README.md @@ -0,0 +1,22 @@ +# Blog-Site example + +Demonstrates how to use the dog provider in order to serve the blog via a website. + +## Disclaimer + +Please keep in mind that I favored simplicity over correctness and robustness. + +I used shorter (and somewhat naive) implementations and avoided using libraries to let this example stand on its own. + +## Improvments needed + +The following should be improved if this was to be used for real: + +- implement a more robust router or use libraries like director or express +- the server and blog initialization need more robust error handling +- the code should be modularized + +## Running the example + +Alright, enough of the warnings, here is how you run the example: + diff --git a/examples/blog-site/site/app.js b/examples/blog-site/site/app.js new file mode 100644 index 0000000..8503306 --- /dev/null +++ b/examples/blog-site/site/app.js @@ -0,0 +1,151 @@ +/* + * Simple Server Example + * ===================== + * + * This example demonstrates how to use the dog provider in order to serve the blog via a website. + * + * Please keep in mind that I favored simplicity over correctness and robustness. + * I used shorter (and somewhat naive) implementations and avoided using libraries to let this example stand on its own. + * + * The following should be improved if this was to be used for real: + * + * - implement a more robust router or use libraries like director or express + * - the server and blog initialization need more robust error handling + * - the code should be modularized + * + */ + +var dog = require('./../../..') + , provider = dog.provider + , log = require('npmlog') + , http = require('http') + , path = require('path') + , fs = require('fs') + , pagegen = require('./page-generator') + , blogdir = path.join(__dirname, '..', 'blog') + , staticdir = path.join(__dirname, 'static') + , imagesdir = path.join(staticdir, 'images') + , nodejsImg = path.join(imagesdir, 'nodejs.png') + , stylesdir = path.join(staticdir, 'styles') + , blogstyleFile = path.join(stylesdir, 'blog.css') + , PORT = 3000 + , postsNames + , posts + ; + + +function serveSite () { + http + .createServer(function (req, res) { + + function respond(data, type) { + res.writeHead( + 200 + , { + 'Content-Type' : type + , 'Content-Length' : data.length + }); + res.end(data); + } + + function fail(status) { + res.writeHead(status); + res.end(); + } + + log.info('blog-server', '%s %s', req.method, req.url); + + switch(req.url) { + case '/': + var postlinks = postsNames.map(function (postname) { + log.info('blog-server','map', posts[postname].metadata); + return '' + posts[postname].metadata.title + ''; + }) + .join('\n') + , html = [ + '

Welcome!

' + , '

Please select a post from below

' + , postlinks + ] + .join('\n'); + + respond(pagegen.wrap(html), 'text/html'); + break; + case '/blog/first-post': + respond(pagegen.wrap(posts['first-post'].html), 'text/html'); + break; + case '/blog/disclaimer': + respond(pagegen.wrap(posts['disclaimer'].html), 'text/html'); + break; + + case '/blog/assets/styles/blog.css': + fs.readFile(blogstyleFile, 'utf8', function (err, data) { + if (err) { fail(505); return; } + respond(data, 'text/css'); + }); + break; + + case '/blog/assets/images/nodejs.png': + fs.readFile(nodejsImg, function (err, data) { + if (err) { fail(505); return; } + respond(data, 'image/png'); + }); + break; + + default: + log.error('blog-server', 'Unknown route: ', req.url); + fail(404); + break; + } + }) + .listen(PORT, 'localhost'); + + log.info('blog-server', 'Listening at localhost:', PORT); + log.info('blog-server', 'Please point your browser to http://localhost:%s', PORT); +} + +function initializeBlog (initialized) { + provider.provideFrom(blogdir); + + initImages(); + + function initImages () { + provider.copyImages(imagesdir, function (err) { + if (err) { log.error('blog-server', err); } + initStyles(); + }); + } + + function initStyles () { + provider.concatenateStyles(function (err, css) { + if (err) { log.error('blog-server', err); } + fs.writeFile(blogstyleFile, css, 'utf8', function (err) { + if (err) { log.error('blog-server', err); } + initPosts(); + }); + }); + } + + function initPosts () { + provider.getAllPosts(function (err, postnamesArg) { + if (err) { log.error('blog-server', err); } + postsNames = postnamesArg + + provider.provideAll(function (err, metadata) { + if (err) { log.error('blog-server', err); } + posts = {}; + + metadata.forEach(function (meta) { + posts[meta.name] = meta; + log.info('blog-server', 'Providing: %s\n', meta.name, meta.metadata); + }); + + log.info('blog-server', 'Initialized posts', postsNames); + + initialized(); + }); + }); + } +} + +initializeBlog(serveSite); diff --git a/examples/blog-site/site/page-generator.js b/examples/blog-site/site/page-generator.js new file mode 100644 index 0000000..4ea10c5 --- /dev/null +++ b/examples/blog-site/site/page-generator.js @@ -0,0 +1,20 @@ +function wrap (html) { +return [ + '' + , '' + , '' + , ' ' + , ' Dog Blog' + , ' ' + , '' + , '' + , html + , '' + , '' , '' , ' ' - , ' Page of Self' - , ' ' - , ' ' - , ' ' - , ' ' + , ' Dog Preview' + , ' ' + , ' ' + , ' ' + , ' ' , '' , '' , rendered , '' , '