Skip to content

Commit

Permalink
1、讲模板引擎修改为jade
Browse files Browse the repository at this point in the history
2、重构
  • Loading branch information
neekey committed Dec 31, 2012
1 parent d9162fe commit 4157d09
Show file tree
Hide file tree
Showing 17 changed files with 786 additions and 270 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules/*
*.iml
*.ipr
*.iws
238 changes: 5 additions & 233 deletions build.js
@@ -1,234 +1,6 @@
var FS = require( 'fs' );
var URL = require( 'url' );
var https = require('https');
var GitHubApi = require("github");
var Mustache = require( 'mustache' );
var Marked = require('marked');

var INDEX_TEMPLATE = './index.mustache';
var INDEX_OUTPUT = './index.html';

/**
* Load user config.
* @type {Object} gistConfig
* @type {Object} gistConfig.username username of Github.
* @type {Object} gistConfig.gist_filter RegExp String to filter gists as post.
*
*/
var gistConfig = require( './gist.json' );
var username = gistConfig.username;
var filter = new RegExp( gistConfig.gist_filter );

/**
* Initialize Github API
* @type {GitHubApi}
*/

var github = new GitHubApi({
version: "3.0.0"
});

/**
* Get gist list.
* ```
* [{
description: 'A test gist for gist-blog',
updated_at: '2012-12-24T01:57:58Z',
forks_url: 'https://api.github.com/gists/4364454/forks',
comments: 0,
public: true,
url: 'https://api.github.com/gists/4364454',
html_url: 'https://gist.github.com/4364454',
git_pull_url: 'https://gist.github.com/4364454.git',
commits_url: 'https://api.github.com/gists/4364454/commits',
comments_url: 'https://api.github.com/gists/4364454/comments',
user:
{
type: 'User',
starred_url: 'https://api.github.com/users/neekey/starred{/owner}{/repo}',
followers_url: 'https://api.github.com/users/neekey/followers',
avatar_url: 'https://secure.gravatar.com/avatar/8896cc725e36350023e7f41f0455b8aa?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png',
following_url: 'https://api.github.com/users/neekey/following',
url: 'https://api.github.com/users/neekey',
repos_url: 'https://api.github.com/users/neekey/repos',
organizations_url: 'https://api.github.com/users/neekey/orgs',
gists_url: 'https://api.github.com/users/neekey/gists{/gist_id}',
gravatar_id: '8896cc725e36350023e7f41f0455b8aa',
login: 'neekey',
events_url: 'https://api.github.com/users/neekey/events{/privacy}',
id: 499870,
received_events_url: 'https://api.github.com/users/neekey/received_events',
subscriptions_url: 'https://api.github.com/users/neekey/subscriptions'
},
created_at: '2012-12-23T17:05:32Z',
git_push_url: 'https://gist.github.com/4364454.git',
id: '4364454',
files: {
'gist-blog-test.md': {
type: 'text/plain',
filename: 'gist-blog-test.md',
raw_url: 'https://gist.github.com/raw/4364454/77508d6306c7f2fe923bec94216c126a85243002/gist-blog-test.md',
size: 164,
language: 'Markdown'
}
}
}]
* ```
*/

github.gists.getFromUser({ user: username }, function( err, res ) {

if( err ){
new Error( err );
}

/**
* The list of gists those be treatd as post.
* @type {Array}
*/
var postGists = [];
res.forEach(function( gist ){

var filename;
for( filename in gist.files ){
if( filename ){
gist.raw_url = gist.files[filename][ 'raw_url' ];
break;
}

}

if( filter.test( filename ) ){
postGists.push( gist );
}
});


/**
* Get gist raw.
*/

var postGistLen = postGists.length;
var postGistRawCount = 0;
postGists.forEach(function( gist ){

GetGistRaw( gist.raw_url, function( err, raw ){
if( err ){
raw = 'Fetching gist raw error: ' + JSON.stringify( err );
}

/**
* The raw content of gist
* @type {String}
*/

gist.raw = raw;

/**
* The HTML of gist.
* @type {String}
*/

gist.html = MDToHTML( raw );

postGistRawCount++;
if( postGistRawCount === postGistLen ){
RenderBlog( postGists, gistConfig, postGists[0].user );
}
});
});
});

/**
* Render gists to posts.
*
* @param {Object} posts
* @param {String} posts.description
* @param {String} posts.updated_at
* @param {String} posts.html
* @param {Object} [meta]
* @param {Object} meta.title
* @param {Object} meta.description
* @param {Object} [other]
* @return {String}
*/

function RenderBlog( posts, meta, user, other ){
var blogData = {
posts: posts,
user: user,
meta: meta || gistConfig
};

// render gist to post.
var HTML = Mustache.render( FS.readFileSync( INDEX_TEMPLATE ).toString(), blogData );
FS.writeFileSync( INDEX_OUTPUT, HTML );

return HTML;
}

/**
* Render MD to HTML
* @param {String} raw Markdown source.
* @return {String} html
*/

function MDToHTML( raw ){
Marked.setOptions({
gfm: true,
pedantic: false,
sanitize: false
});
return Marked( raw );
}

/**
* Use `https` to fetch gist raw.
* @param url
* @param {Function} next callback
* @param {Object=null} next.err
* @param {String} next.raw
*/
function GetGistRaw( url, next ){

var urlObj = URL.parse( url );

var options = {
hostname: urlObj.host,
port: 443,
path: urlObj.path,
method: 'GET',
headers: {
"host": 'gist.github.com',
"user-agent": 'NodeJS HTTP Client',
"content-length": "0"
}
};

var req = https.request(options, function(res) {
res.setEncoding( 'utf8' );
var data = '';
res.on( 'data', function(chunk) {
data += chunk;
});
res.on( 'end', function() {
if (res.statusCode >= 400 && res.statusCode < 600 || res.statusCode < 10) {
new Error(data, res.statusCode);
}
else {
next( null, data )
}
});
});

req.on("error", function(e) {
next( e );
});

req.end();
}

function GetUserInfo( username, next ){
github
}
var Data = require( './lib/data' );
var BlogRender = require( './lib/blogRender' );

Data.fetch( function( data ){
BlogRender.render( data );
});
1 change: 1 addition & 0 deletions gist.json
Expand Up @@ -2,5 +2,6 @@
"title": "Neekey",
"description": "在此记录我的学习&生活.",
"username": "neekey",
"site_url": "http://blog.neekey.net/gist-blog",
"gist_filter": "^gist-blog-.*.md"
}
42 changes: 7 additions & 35 deletions index.html
@@ -1,23 +1,4 @@
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<link href="./assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
<link href="./assets/common/markdown.css" rel="stylesheet"/>
<link href="./assets/common/screen.css" rel="stylesheet"/>
</head>
<body>
<header>
<div class="avatar"><img src="https://secure.gravatar.com/avatar/8896cc725e36350023e7f41f0455b8aa?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png"></div>
<h1 class="site-title">Neekey</h1>
<p class="site-description">在此记录我的学习&amp;生活.</p>
</header>
<section class="post-list">
<div class="post-block">
<p class="post-title">浏览器错误捕捉总结</p>
<p class="update-date">2012-12-25T00:37:38Z</p>
<div class="post-md-content"><p>捕捉浏览器中的JS运行时错误,主要通过监听window.onerror来实现。但是对于不同的脚本执行方式以及不同的浏览器,能捕获到的信息会有区别。
<head><meta charst="utf-8"/><title>GistBlog</title><link href="http://blog.neekey.net/gist-blog/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"/><link href="http://blog.neekey.net/gist-blog/assets/common/markdown.css" rel="stylesheet"/><link href="http://blog.neekey.net/gist-blog/assets/common/screen.css" rel="stylesheet"/></head><body><header><div class="avatar"><img src="https://secure.gravatar.com/avatar/8896cc725e36350023e7f41f0455b8aa?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png"/></div><h1 class="site-title"><a href="http://blog.neekey.net/gist-blog">Neekey</a></h1><p class="site-description">在此记录我的学习&amp;生活.</p></header><section class="post-list"><div class="post-block"><p class="post-title"><a href="posts/gist-blog-browser-js-error-catch-summary.html" target="_blank">浏览器错误捕捉总结</a></p><p class="update-date">2012-12-26T01:38:35Z</p><div class="post-md-content"><p>捕捉浏览器中的JS运行时错误,主要通过监听window.onerror来实现。但是对于不同的脚本执行方式以及不同的浏览器,能捕获到的信息会有区别。

</p>
<p>window.onerror 讲接收3个参数:
Expand Down Expand Up @@ -45,7 +26,7 @@ <h1 class="site-title">Neekey</h1>
<p>下面列一下各浏览器对与上面集中脚本执行的捕获情况(Markdown对于table的支持不是很好,我是直接在页面上copy HTML进来的,维护可以看下 <a href="https://www.evernote.com/shard/s43/sh/a36fc90e-9f72-43b5-8710-1476340d14ac/1160f515582d297f22f495924ba10d3d">https://www.evernote.com/shard/s43/sh/a36fc90e-9f72-43b5-8710-1476340d14ac/1160f515582d297f22f495924ba10d3d</a> )

</p>
<h2>Chrome</h2>
<h2>Chrome (23.0.1271.101)</h2>
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tbody>
<tr>
Expand Down Expand Up @@ -87,7 +68,7 @@ <h2>Chrome</h2>
</tbody>
</table>

<h3>Firefox</h3>
<h3>Firefox (16.0.2)</h3>
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tbody>
<tr>
Expand Down Expand Up @@ -129,7 +110,7 @@ <h3>Firefox</h3>
</tbody>
</table>

<h3>Safari</h3>
<h3>Safari (6.0.2 (8536.26.17))</h3>
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tbody>
<tr>
Expand Down Expand Up @@ -171,7 +152,7 @@ <h3>Safari</h3>
</tbody>
</table>

<h3>Opera</h3>
<h3>Opera (12.11)</h3>
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tbody>
<tr>
Expand Down Expand Up @@ -380,12 +361,7 @@ <h3>IE6</h3>
</tr>
</tbody>
</table>
</div>
</div>
<div class="post-block">
<p class="post-title">Cordova(PhoneGap)插件编写</p>
<p class="update-date">2012-12-24T10:02:08Z</p>
<div class="post-md-content"><p>PhoneGap的插件机制为我们提供了自由拓展Native API的可能。但是网络上和PhoneGap插件开发相关的资料较少,本文介绍插件开发的简单过程。
</div></div><div class="post-block"><p class="post-title"><a href="posts/gist-blog-Cordova(PhoneGap)-plugin.html" target="_blank">Cordova(PhoneGap)插件编写</a></p><p class="update-date">2012-12-24T10:02:08Z</p><div class="post-md-content"><p>PhoneGap的插件机制为我们提供了自由拓展Native API的可能。但是网络上和PhoneGap插件开发相关的资料较少,本文介绍插件开发的简单过程。

</p>
<p>参考资料:
Expand Down Expand Up @@ -424,8 +400,4 @@ <h3>解决方案</h3>
<p>而这种解决方案也就是PhoneGap提供的插件方式。

</p>
</div>
</div>
<section>
</body>
</html>
</div></div></section></body>
19 changes: 19 additions & 0 deletions lib/MDToHTML.js
@@ -0,0 +1,19 @@
/**
* Render Markdown to HTML
*/
var Marked = require('marked');

/**
* Render MD to HTML
* @param {String} raw Markdown source.
* @return {String} html
*/

module.exports.render = function( raw ){
Marked.setOptions({
gfm: true,
pedantic: false,
sanitize: false
});
return Marked( raw );
};

0 comments on commit 4157d09

Please sign in to comment.