support jsonify filter
This commit is contained in:
parent
58ae8bc0cd
commit
f971aebfb1
|
@ -1,4 +1,5 @@
|
|||
require 'uri'
|
||||
require 'json'
|
||||
|
||||
module Jekyll
|
||||
module Filters
|
||||
|
@ -148,6 +149,15 @@ module Jekyll
|
|||
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
|
||||
def time(input)
|
||||
case input
|
||||
|
|
|
@ -173,6 +173,17 @@ common tasks easier.
|
|||
</p>
|
||||
</td>
|
||||
</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>
|
||||
</table>
|
||||
</div>
|
||||
|
|
|
@ -98,5 +98,16 @@ class TestFilters < Test::Unit::TestCase
|
|||
should "escape space as %20" do
|
||||
assert_equal "my%20things", @filter.uri_escape("my things")
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue