Skip to content

Commit

Permalink
Update liquid-node to 1.0.0; remove homebrewed date filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake committed Jun 23, 2014
1 parent 4a06ade commit 3a2a480
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 173 deletions.
63 changes: 30 additions & 33 deletions bin/darko-serve
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var Site = require('..').Site
var Post = require('..').Post
var Page = require('..').Page
var util = require('..').util
var Promise = require('bluebird')


var site = new Site({
Expand Down Expand Up @@ -207,8 +208,8 @@ function watchOther(dir) {

if (fname) {
debug('Detected ' + e + ' of ' + fname)
if (/\.(md|html)$/.test(fname)) updatePage(fpath)
else if (!/^[._]/.test(fname)) updateStatic(fpath)
if (/\.(md|html|xml)$/.test(fname)) updatePage(fpath)
else if (!/^[\._]/.test(fname)) updateStatic(fpath)
}
else {
debug('What should I do with empty filename?')
Expand Down Expand Up @@ -253,39 +254,35 @@ function _mark() {
}

function updatePage(fpath) {
if (!fs.existsSync(fpath)) {
debug('Removed ' + path.relative(site.cwd, fpath))
}
var Klass = /_posts$/.test(path.dirname(fpath)) ? Post : Page
var page = new Klass({fpath: fpath, site: site })

if (fs.existsSync(fpath)) {
debug('Changed ' + fpath)
if (page.publishable) {
util.replace(site.pages, function(_page) {
return _page.path == page.path ? page : _page
})
site.writeTemplated(page)
.then(function() {
return site.writePages()
})
.then(function() {
util.log('Regenerated', page.path)
Promise.try(function() {
var Klass = /_posts$/.test(path.dirname(fpath)) ? Post : Page
return new Klass({fpath: fpath, site: site })
})
.then(function(page) {
if (fs.existsSync(fpath)) {
debug('Changed ' + fpath)
util.replace(site.pages, function(_page) {
return _page.path == page.path ? page : _page
})
.catch(function(err) {
if (program.trace) util.error(err.stack)
else util.error(err.message)
return site.writeTemplated(page)
}
else {
debug('Removed ' + path.relative(site.cwd, fpath))
util.remove(site.pages, function(_page) {
return _page.path == page.path
})
}
}
else {
debug('Removed ' + fpath)
util.remove(site.pages, function(_page) {
return _page.path == page.path
fs.unlinkSync(page.dest)
util.log('Removed ', path.relative(site.dest, page.dest))
}
})
.then(function(page) {
util.log('Regenerated', page.path)
return site.writePages()
})
.catch(function(err) {
if (program.trace) util.error(err.stack)
else util.error(err.message)
})
fs.unlinkSync(page.dest)
util.log('Removed ', path.relative(site.dest, page.dest))
}
}

function updateStatic(fpath) {
Expand All @@ -299,6 +296,6 @@ function updateStatic(fpath) {
if (program.trace) util.error(err.stack)
})
.on('finish', function() {
debug('Copied file ' + fname)
util.log('Copied', fname)
})
}
62 changes: 5 additions & 57 deletions lib/liquid/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MONTHS = 'January Feburary March April May June July August September October No


toObjectString = Object::toString

isString = (input) ->
toObjectString.call(input) is "[object String]"

Expand All @@ -28,50 +28,6 @@ toString = (input) ->
toObjectString.call input


formatDate = (input, format) ->
return input unless input?

pad = (str, chr, width) ->
str = '' + str
len = Math.max(0, width - str.length)
Array(len + 1).join(chr) + str

# Liquid StandFilters:
# > http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000012
#
# But there's more to implement:
# > http://ruby-doc.org/stdlib-2.1.0/libdoc/date/rdoc/Date.html#method-i-strftime
#
return format.replace /%([a-zA-Z])/g, (m, f) ->
switch f
when 'a' then WEEKDAY_ABBRS[input.getDay()]
when 'A' then WEEKDAYS[input.getDay()]
when 'b' then MONTH_ABBRS[input.getMonth()]
when 'B' then MONTHS[input.getMonth()]
# To be implemented 'd'
when 'd' then pad(input.getDate(), '0', 2)
when 'e' then input.getDate()
when 'H' then pad(input.getHours(), '0', 2)
when 'I' then pad(input.getHours() % 12, '0', 2)
when 'j'
days = (+input - (new Date(input.getFullYear(), 0, 1))) / 1000 / 60 / 60 / 24
pad(days, '0', 3)
when 'm' then pad(input.getMonth() + 1, '0', 2)
when 'M' then pad(input.getMinutes(), '0', 2)
when 'p' then input.getHours() >= 12 ? 'PM' : 'AM'
when 'S' then pad(input.getSeconds(), '0', 2)
# To be implemented 'U' and 'W'
when 'w' then input.getDay()
# To be implemented 'x' and 'X'
when 'y' then input.getFullYear() / 100
when 'Y' then input.getFullYear()
when 'z'
offset = -input.getTimezoneOffset() / 60
prefix = if offset >= 0 then '+' else '-'
prefix + pad(offset, '0', 2) + '00'
else f


engine = new Liquid.Engine

engine.registerTag "block", do ->
Expand Down Expand Up @@ -150,31 +106,23 @@ engine.registerTag "include", do ->
@included.then (i) -> i.render context


# The standard filters that should be provided by liquid-node
engine.registerFilters
capitalize: (input) ->
input && input.replace(/^([a-z])/, (m, chr) -> chr.toUpperCase())

date: formatDate


# The custom filters added by Jekyll
engine.registerFilters
date_to_xmlschema: (input) ->
# 2014-01-12T00:00:00+08:00
formatDate(input, '%Y-%m-%dT%H:%M:%S%z').replace(/00$/, ':00')
@date(input, '%Y-%m-%dT%H:%M:%S%z').replace(/00$/, ':00')

date_to_rfc822: (input) ->
# Sun, 12 Jan 2014 00:00:00 +0800
formatDate(input, '%a, %d %b %Y %H:%M:%S %z')
@date(input, '%a, %d %b %Y %H:%M:%S %z')

date_to_string: (input) ->
# 12 Jan 2014
formatDate(input, '%d %b %Y')
@date(input, '%d %b %Y')

date_to_long_string: (input) ->
# 12 January 2014
formatDate(input, '%d %B %Y')
@date(input, '%d %B %Y')

xml_escape: (input) ->
return input unless input?
Expand Down
78 changes: 5 additions & 73 deletions lib/liquid/index.js

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

5 changes: 3 additions & 2 deletions lib/parsers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var _ = require('lodash')
var util = require('../util')


var PAGE_FORMATS = ['.md', '.html']
var PAGE_FORMATS = ['.md', '.html', '.xml']


function Page(attrs) {
Expand Down Expand Up @@ -36,7 +36,8 @@ function Page(attrs) {

this.url = '/' + (this.path.indexOf(path.sep) >= 0 ? path.dirname(this.path) : '')

this.dest = path.join(this.site.dest, this.site.baseurl.slice(1), this.path.replace(/\.\w+$/, '.html'))
this.dest = path.join(this.site.dest, this.site.baseurl.slice(1),
this.path.replace(/\.\w+$/, this.ext == '.md' ? '.html' : this.ext))
}

Object.defineProperties(Page.prototype, {
Expand Down
2 changes: 1 addition & 1 deletion lib/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Site.prototype.parse = function() {
post = new Post({ fpath: fpath, site: site })
if (post.valid) site.drafts.push(post)
}
else if (/\.(md|html)$/.test(fpath)) {
else if (/\.(md|html|xml)$/.test(fpath)) {
page = new Page({ fpath: fpath, site: site })
if (page.valid) site.pages.push(page)
}
Expand Down
12 changes: 6 additions & 6 deletions lib/writers/templated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var path = require('path')
var yaml = require('yaml-js')
var fs = require('fs')
var mkdirp = require('mkdirp').sync
var engine = require('../liquid')
var md = require('../markdown')
Expand All @@ -11,7 +10,7 @@ var debug = require('debug')('darko')
var util = require('util')
var Promise = require('bluebird')

var readFile = Promise.promisify(fs.readFile)
var fs = Promise.promisifyAll(require('fs'))


function liquid(tpl, site) {
Expand All @@ -21,9 +20,9 @@ function liquid(tpl, site) {
var includePath = path.join(site.cwd, '_includes', subFilepath)

Promise.some([
readFile(includePath, site.encoding),
readFile(includePath + '.md', site.encoding),
readFile(includePath + '.html', site.encoding)
fs.readFileAsync(includePath, site.encoding),
fs.readFileAsync(includePath + '.md', site.encoding),
fs.readFileAsync(includePath + '.html', site.encoding)
], 1)
.then(function(results) {
done(null, results[0])
Expand All @@ -44,7 +43,7 @@ var _layouts = {}

function layout(page) {
if (!page.layoutWas) page.layoutWas = page.layout
if (!page.layout || page.layout_ == page.layout) {
if (!page.layout || page.layout == 'nil' || page.layout_ == page.layout) {
// Finished layout renderring, restore the original layout of current page
page.layout = page.layoutWas
page.layout_ = ''
Expand Down Expand Up @@ -119,6 +118,7 @@ module.exports = function writeTemplated(page) {
.then(render)
.then(function() {
page.content = page.contentWas
return page
})
.catch(function(err) {
util.error('Failed to generate ' + page.path + ':')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"url": "git://github.com/dotnil/darko.git"
},
"dependencies": {
"liquid-node": "0.3.1",
"liquid-node": "~1.0.0",
"lodash": "~2.4.1",
"markit": "~0.1.0",
"mkdirp": "~0.3.5",
Expand Down

0 comments on commit 3a2a480

Please sign in to comment.