support jsonify filter

This commit is contained in:
liufengyun 2013-10-22 18:04:32 +08:00
parent 58ae8bc0cd
commit f971aebfb1
3 changed files with 32 additions and 0 deletions

View File

@ -1,4 +1,5 @@
require 'uri' require 'uri'
require 'json'
module Jekyll module Jekyll
module Filters module Filters
@ -148,6 +149,15 @@ module Jekyll
end end
end end
# Convert the input into json string
#
# input - The Array or Hash to be converted
#
# Returns the converted json string
def jsonify(input)
input.to_json
end
private private
def time(input) def time(input)
case input case input

View File

@ -173,6 +173,17 @@ common tasks easier.
</p> </p>
</td> </td>
</tr> </tr>
<tr>
<td>
<p class='name'><strong>Data To JSON</strong></p>
<p>Convert Hash or Array to JSON.</p>
</td>
<td class='align-center'>
<p>
<code class='filter'>{% raw %}{{ site.data.projects | jsonify }}{% endraw %}</code>
</p>
</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -98,5 +98,16 @@ class TestFilters < Test::Unit::TestCase
should "escape space as %20" do should "escape space as %20" do
assert_equal "my%20things", @filter.uri_escape("my things") assert_equal "my%20things", @filter.uri_escape("my things")
end end
context "jsonify filter" do
should "convert hash to json" do
assert_equal "{\"age\":18}", @filter.jsonify({:age => 18})
end
should "convert array to json" do
assert_equal "[1,2]", @filter.jsonify([1, 2])
assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}])
end
end
end end
end end