Skip to content

Commit

Permalink
support rss and change time function
Browse files Browse the repository at this point in the history
  • Loading branch information
zmmbreeze committed Jan 7, 2013
1 parent 2c7c147 commit ab2d4d4
Show file tree
Hide file tree
Showing 41 changed files with 2,291 additions and 41 deletions.
4 changes: 3 additions & 1 deletion blogin.json
Expand Up @@ -3,5 +3,7 @@
"description": "A static blog base on blogin.",
"keywords": "website,blog,framework,static,blogin",
"author": "MZhou / @zhoumm",
"email": "zmmbreeze0825@gmail.com"
"email": "zmmbreeze0825@gmail.com",
"siteUrl": "http://nodejs.in",
"favicon": "https://github.com//favicon.ico"
}
4 changes: 4 additions & 0 deletions data/info
@@ -0,0 +1,4 @@
{
pages: [],
posts: []
}
9 changes: 6 additions & 3 deletions lib/file.coffee
Expand Up @@ -64,7 +64,10 @@ copy = exports.copy = (src, dest) ->

readJSON = exports.readJSON = (src) ->
content = read(src)
JSON.parse(content)
if content
return JSON.parse(content)
else
return ''

getFileName = exports.getFileName = (filePath) ->
filePath.replace(path.dirname(filePath) + '/', '')
Expand Down Expand Up @@ -107,12 +110,12 @@ exports.pathToUrl = (filePath, root) ->

exports.getCTime = (filePath, format) ->
stat = fs.statSync(filePath)
format = format || 'YYYY-MM-DD'
format = format || 'YYYY-MM-DD hh:mm:ss'
moment(stat.ctime).format(format)

exports.getMTime = (filePath, format) ->
stat = fs.statSync(filePath)
format = format || 'YYYY-MM-DD'
format = format || 'YYYY-MM-DD hh:mm:ss'
moment(stat.mtime).format(format)

exports.mdToHtml = (filePath) ->
Expand Down
10 changes: 7 additions & 3 deletions lib/file.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 99 additions & 14 deletions lib/update.coffee
Expand Up @@ -7,10 +7,11 @@ moment = require('moment')
file = require('./file')
usage = require('./usage')
parseArg = require('./arg').parse
RSS = require('rss')

templateDir = './public/template/'
projectDir = './'

projectInfo = {}

fileApi =
getJadeFile: (type) ->
Expand All @@ -20,9 +21,12 @@ fileApi =
switch type
when 'page'
path.resolve(projectDir, 'data/pages')
else
when 'post'
path.resolve(projectDir, 'data/posts')
when 'archive'
path.resolve(projectDir, 'data/posts')


getDestFile: (type) ->
switch type
when 'archive'
Expand All @@ -31,8 +35,12 @@ fileApi =
path.resolve(projectDir, 'post')
when 'page'
path.resolve(projectDir, 'page')
else
when 'index'
path.resolve(projectDir, 'index.html')
when 'rss'
path.resolve(projectDir, 'rss.xml')
else
projectDir

srcToDest: (type, srcFilePath) ->
relativePath = path.relative(this.getSrcFile(type), srcFilePath)
Expand All @@ -44,6 +52,24 @@ fileApi =
fileUrl = path.resolve(this.getDestFile(type), relativePath)
file.pathToUrl(file.mdToHtml(fileUrl), projectDir)

getInfo: (type, filePath) ->
list = projectInfo[type]
filePath = path.relative(projectDir, filePath)
list.forEach (item) =>
if (item.file is filePath)
return item

getMTime: (type, filePath) ->
info = this.getInfo(type, filePath)
if info then info.mtime

getCTime: (type, filePath) ->
info = this.getInfo(type, filePath)
if info then info.ctime

sortByCreateTime: (type, files) ->
return files.sort (a, b) =>
return this.getCTime(type, a) < this.getCTime(type, b)


dataApi =
Expand All @@ -52,7 +78,7 @@ dataApi =
fileList = file.dir(postDir)

items = []
fileList = file.sortByCreateTime(fileList)
fileList = fileApi.sortByCreateTime('post', fileList)
fileList.forEach (filePath) =>
if not file.isMd(filePath)
return;
Expand All @@ -66,7 +92,7 @@ dataApi =
fileList = file.dir(pageDir)

items = []
fileList = file.sortByCreateTime(fileList)
fileList = fileApi.sortByCreateTime('page', fileList)
fileList.forEach (filePath) =>
if not file.isMd(filePath)
return;
Expand All @@ -80,7 +106,8 @@ dataApi =
archiveList = file.dir(archiveDir, true)

items = []
archiveList = file.sortByCreateTime(archiveList)
archiveList = archiveList.sort (a, b) =>
return a < b
archiveList.forEach (filePath) =>
items.push
title: file.getFileName(filePath)
Expand All @@ -92,7 +119,7 @@ dataApi =
fileList = file.dir(postDir)

items = []
fileList = file.sortByCreateTime(fileList)
fileList = fileApi.sortByCreateTime('post', fileList)
fileList.forEach (filePath) =>
if not file.isMd(filePath)
return;
Expand All @@ -102,9 +129,12 @@ dataApi =
return items

getLocals: (type, arg1) ->
locals =
locals =
site: file.readJSON(path.resolve(projectDir, './blogin.json'))
pageName: ''
siteUrl = locals.site.siteUrl
rssPath = if (siteUrl[siteUrl.length-1] == '/') then 'rss.xml' else '/rss.xml'
locals.site.rssUrl = siteUrl + rssPath

switch type
when 'index'
Expand Down Expand Up @@ -162,6 +192,8 @@ rendApi =
)

archives.forEach (archivePath) =>
if (archivePath[0] === '.')
return
archiveName = file.getFileName(archivePath)
archiveDestFile = path.resolve(destDir, archiveName, 'index.html')
file.write(archiveDestFile, compile(dataApi.getLocals('archive', archiveName)))
Expand All @@ -170,7 +202,6 @@ rendApi =

page: (keepQuiet) ->
srcDir = fileApi.getSrcFile('page')
destDir = fileApi.getDestFile('page')
pages = file.dir(srcDir, true)
compile = jade.compile(fileApi.getJadeFile('page'),
filename: path.resolve(templateDir, 'includes')
Expand All @@ -179,20 +210,18 @@ rendApi =
pages.forEach (pagePath) =>
if not file.isMd(pagePath)
return
pageName = file.getFileName(pagePath).slice(0, -3)
pageTitle = file.pathToTitle(pagePath)
entry =
title: pageTitle
content: file.readMdToHtml(pagePath)
time: file.getMTime(pagePath)
time: fileApi.getMTime('page', pagePath)
pageFile = fileApi.srcToDest('page', pagePath)
file.write(pageFile, compile(dataApi.getLocals('page', entry)))
if not keepQuiet
util.puts('File ' + pageFile + ' created.')

post: (keepQuiet) ->
srcDir = fileApi.getSrcFile('post')
destDir = fileApi.getDestFile('post')
posts = file.dir(srcDir)
compile = jade.compile(fileApi.getJadeFile('post'),
filename: path.resolve(templateDir, 'includes')
Expand All @@ -201,31 +230,87 @@ rendApi =
posts.forEach (postPath) =>
if not file.isMd(postPath)
return
postName = file.getFileName(postPath).slice(0, -3)
postTitle = file.pathToTitle(postPath)
entry =
title: postTitle
content: file.readMdToHtml(postPath)
time: file.getMTime(postPath)
time: fileApi.getMTime('post', postPath)
postFile = fileApi.srcToDest('post', postPath)
file.write(postFile, compile(dataApi.getLocals('post', entry)))
if not keepQuiet
util.puts('File ' + postFile + ' created.')

rss: (keepQuiet) ->
srcDir = fileApi.getSrcFile('post')
posts = file.dir(srcDir)
posts = file.sortByCreateTime(posts)
locals = dataApi.getLocals('index')
feedFile = fileApi.getDestFile('rss')
feed = new RSS
title: locals.site.name
description: locals.site.description
feed_url: path.join(locals.site.siteUrl, '/rss.xml')
site_url: locals.site.siteUrl
image_url: locals.site.favicon
author: locals.site.author

posts.forEach (postPath) =>
if not file.isMd(postPath)
return
postTitle = file.pathToTitle(postPath)
feed.item
title: postTitle
description: file.readMdToHtml(postPath)
url: fileApi.srcToUrl('post', postPath)
date: fileApi.getMTime('post', postPath)

file.write(feedFile, feed.xml())
if not keepQuiet
util.puts('File ' + feedFile + ' created.')




module.exports = (args) ->
arg = parseArg(args)
projectDir = path.resolve('./', arg.req[0] || './')
infoFile = path.resolve(projectDir, 'data/info')
templateDir = path.resolve(projectDir, './public/template/')

# rewrite info file
infos = {};
infos.post = [];
posts = file.dir(fileApi.getSrcFile('post'))
posts.forEach (filePath) =>
post = {};
post.file = path.relative(projectDir, filePath)
post.ctime = file.getCTime(filePath)
post.mtime = file.getMTime(filePath)
infos.post.push(post)

infos.page = [];
pages = file.dir(fileApi.getSrcFile('page'))
pages.forEach (filePath) =>
page = {};
page.file = path.relative(projectDir, filePath)
page.ctime = file.getCTime(filePath)
page.mtime = file.getMTime(filePath)
infos.page.push(page)

file.write(infoFile, JSON.stringify(infos))


projectInfo = file.readJSON(infoFile)
if not fs.existsSync(templateDir)
usage.puts('update')
return
# rend html
keepQuiet = arg.opt.indexOf('q') > 0
rendApi.index(keepQuiet)
rendApi.archive(keepQuiet)
rendApi.page(keepQuiet)
rendApi.post(keepQuiet)
rendApi.rss(keepQuiet)



0 comments on commit ab2d4d4

Please sign in to comment.