Skip to content

Commit

Permalink
Add more filters
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake committed Jan 12, 2014
1 parent 979cd48 commit 3fc3be7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
27 changes: 24 additions & 3 deletions lib/liquid/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,32 @@ Liquid.Template.registerFilter
# 12 January 2014
formatDate(input, '%d %B %Y')

# xml_escape
xml_escape: (input) ->
return input unless input?

# cgi_escape
input.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')

# uri_escape

# http://stackoverflow.com/questions/2824126/whats-the-difference-between-uri-escape-and-cgi-escape
# What's the difference between cgi_escape and uri_escape?
# The latter, URI.escape, is deprecated in Ruby.
#
# encodeURICompnent gives the closest match of the result of CGI.escape,
# except that encodeURIComponent uses %20 to escape space but CGI.espace use
# the + character.
cgi_escape: (input) ->
input or= ''
encodeURIComponent(input).replace(/%20/g, '+')

# same thing happens in encodeURI and URI.escape. URI.escape escapes # into
# %23 but encodeURI ignores it.
uri_escape: (input) ->
input or= ''
encodeURI(input).replace(/#/g, '%23')

# number_of_words

Expand Down
14 changes: 14 additions & 0 deletions lib/liquid/index.js

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

27 changes: 27 additions & 0 deletions test/liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,31 @@ describe('Liquid', function() {
})
})

it('has filter xml_escape', function(done) {
liquid('{{ data | xml_escape }}', {
data: 'How to go home? Taxi -> Train -> Taxi'
}).done(function(res) {
res.should.equal('How to go home? Taxi -&gt; Train -&gt; Taxi')
done()
})
})

it('has filter cgi_escape', function(done) {
liquid('{{ data | cgi_escape }}', {
data: 'http://google.com/foo?bar=at#anchor&title=My Blog & Your Blog'
}).done(function(res) {
res.should.equal('http%3A%2F%2Fgoogle.com%2Ffoo%3Fbar%3Dat%23anchor%26title%3DMy+Blog+%26+Your+Blog')
done()
})
})

it('has filter uri_escape', function(done) {
liquid('{{ data | uri_escape }}', {
data: 'http://google.com/foo?bar=at#anchor&title=My Blog & Your Blog'
}).done(function(res) {
res.should.equal('http://google.com/foo?bar=at%23anchor&title=My%20Blog%20&%20Your%20Blog')
done()
})
})

})
28 changes: 21 additions & 7 deletions test/simple.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
var path = require('path')
var fs = require('fs')
var Site = require('..').Site
var util = require('..').util
var should = require('should')


var site = new Site({
cwd: path.resolve(__dirname, '../test/fixture')
})

site.parse()

site.write()
.fail(function(err) {
util.error('Generation failed because of:')
util.error(err.stack)
describe('Site', function() {

it('should parse posts, pages, and static files', function() {
site.parse()
site.posts.should.not.be.empty
site.pages.should.not.be.empty
})
.done(function() {
util.log('Generating', '... done')

it('should write posts, pages, and static files', function(done) {
site.write()
.fail(function(err) {
util.error('Generation failed because of:')
util.error(err.stack)
})
.done(function() {
util.log('Generating', '... done')
fs.existsSync(site.dest).should.be.ok
done()
})
})
})

0 comments on commit 3fc3be7

Please sign in to comment.