public
Last active

a jekyll tag to parse json files and objects for use as maps subsequently

  • Download Gist
jsonball.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
require 'json'
 
# JSON parser tag, creating map for use in jekyll markdown
# Alex.Heneveld @ Cloudsoft Corp (remove spaces and add the .com)
# Released under APL 2.0
 
# usage: {% jsonball varname from TYPE PARAM %}
#
# where TYPE is one of {data,var,file,page}, described below
 
# drop this into your _plugins/ folder, then you can write, e.g.
#
# {% jsonball foo from data { "a": "b" } %}
#
# and then later refer to {{ foo.a }} to get b inserted
 
# more usefully, you can load it from a variable x, e.g.
# {% capture x %}{% include toc.json %}{% endcapture %}
#
# {% jsonball foo from var x %}
 
# even better, to read from a file, say toc.json
# (absolute, or relative to the page being jekylled):
#
# {% jsonball foo from file toc.json %}
#
# then e.g. {% for record in jsonball %} {{ record.name }} {% endfor %}
# to print out all the name entries (or build a fancy TOC sidebar)
 
# and finally, if that json file might itself contain liquid tags,
# or need jekylling, treat it as a page and it will get jekylled
# (we use this for toc.json reading from subdirs' toc.json files):
#
# {% jsonball foo from page toc.json %}
 
module JekyllJsonball
class JsonballTag < Liquid::Tag
 
def initialize(tag_name, text, tokens)
super
@text = text
end
 
def render(context)
if /(.+) from var (.+)/.match(@text)
context[$1] = JSON context[$2]
return ''
end
if /(.+) from data (.+)/.match(@text)
context[$1] = JSON $2
return ''
end
if /(.+) from file (.+)/.match(@text)
context[$1] = JSON page_relative_file_contents(context, $2.strip)
return ''
end
if /(.+) from page (.+)/.match(@text)
context[$1] = JSON jekylled_page_relative_file_contents(context, $2.strip)
return ''
end
# syntax error
return 'ERROR:bad_jsonball_syntax'
end
 
def page_relative_file_contents(context, filename)
jekyllSite = context.registers[:site]
dir = jekyllSite.source+'/'+File.dirname(context['page']['url'])
if !filename.match(/\/.*/)
filename = dir + '/' + filename
end
file = File.open(filename, "rb")
return file.read
end
 
def jekylled_page_relative_file_contents(context, filename)
jekyllSite = context.registers[:site]
targetPage = Jekyll::Page.new(jekyllSite, jekyllSite.source, File.dirname(context['page']['url']), filename)
targetPage.render(jekyllSite.layouts, jekyllSite.site_payload)
targetPage.output
end
 
end
end
 
Liquid::Template.register_tag('jsonball', JekyllJsonball::JsonballTag)

I forked and updated this gist to enable retrieving json from a remote URL. The forked gist is available here: https://gist.github.com/geekles/6081967

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.