jekyll-jsonball 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-jsonball.rb +85 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30947a567f2bf541ea8fcde6daee0acd44fed445
4
+ data.tar.gz: 8b0a734b9b474994238a505fdc29c6bb02369432
5
+ SHA512:
6
+ metadata.gz: cb6bcb7ca96298bc157a8da3bdf3c17d5813e23ecbc1a3d4b5a56f64362508d23037c91bf8be65786a0c7c371cb3012b7189a9ac163a22015b2688ef0fb17fa2
7
+ data.tar.gz: 4ddfc30be47d5fba983baf6fb38ae61651e3da356a851da640a1d8732b544ac1735a673307da2da71ebc3c171a4fa92a0edd32840055614fc207b0d5d0bd672f
@@ -0,0 +1,85 @@
1
+ require 'json'
2
+
3
+ # JSON parser tag, creating map for use in jekyll markdown
4
+ # Alex.Heneveld @ Cloudsoft Corp (remove spaces and add the .com)
5
+ # Released under APL 2.0
6
+
7
+ # usage: {% jsonball varname from TYPE PARAM %}
8
+ #
9
+ # where TYPE is one of {data,var,file,page}, described below
10
+
11
+ # drop this into your _plugins/ folder, then you can write, e.g.
12
+ #
13
+ # {% jsonball foo from data { "a": "b" } %}
14
+ #
15
+ # and then later refer to {{ foo.a }} to get b inserted
16
+
17
+ # more usefully, you can load it from a variable x, e.g.
18
+ # {% capture x %}{% include toc.json %}{% endcapture %}
19
+ #
20
+ # {% jsonball foo from var x %}
21
+
22
+ # even better, to read from a file, say toc.json
23
+ # (absolute, or relative to the page being jekylled):
24
+ #
25
+ # {% jsonball foo from file toc.json %}
26
+ #
27
+ # then e.g. {% for record in jsonball %} {{ record.name }} {% endfor %}
28
+ # to print out all the name entries (or build a fancy TOC sidebar)
29
+
30
+ # and finally, if that json file might itself contain liquid tags,
31
+ # or need jekylling, treat it as a page and it will get jekylled
32
+ # (we use this for toc.json reading from subdirs' toc.json files):
33
+ #
34
+ # {% jsonball foo from page toc.json %}
35
+
36
+ module JekyllJsonball
37
+ class JsonballTag < Liquid::Tag
38
+
39
+ def initialize(tag_name, text, tokens)
40
+ super
41
+ @text = text
42
+ end
43
+
44
+ def render(context)
45
+ if /(.+) from var (.+)/.match(@text)
46
+ context[$1] = JSON context[$2]
47
+ return ''
48
+ end
49
+ if /(.+) from data (.+)/.match(@text)
50
+ context[$1] = JSON $2
51
+ return ''
52
+ end
53
+ if /(.+) from file (.+)/.match(@text)
54
+ context[$1] = JSON page_relative_file_contents(context, $2.strip)
55
+ return ''
56
+ end
57
+ if /(.+) from page (.+)/.match(@text)
58
+ context[$1] = JSON jekylled_page_relative_file_contents(context, $2.strip)
59
+ return ''
60
+ end
61
+ # syntax error
62
+ return 'ERROR:bad_jsonball_syntax'
63
+ end
64
+
65
+ def page_relative_file_contents(context, filename)
66
+ jekyllSite = context.registers[:site]
67
+ dir = jekyllSite.source+'/'+File.dirname(context['page']['url'])
68
+ if !filename.match(/\/.*/)
69
+ filename = dir + '/' + filename
70
+ end
71
+ file = File.open(filename, "rb")
72
+ return file.read
73
+ end
74
+
75
+ def jekylled_page_relative_file_contents(context, filename)
76
+ jekyllSite = context.registers[:site]
77
+ targetPage = Jekyll::Page.new(jekyllSite, jekyllSite.source, File.dirname(context['page']['url']), filename)
78
+ targetPage.render(jekyllSite.layouts, jekyllSite.site_payload)
79
+ targetPage.output
80
+ end
81
+
82
+ end
83
+ end
84
+
85
+ Liquid::Template.register_tag('jsonball', JekyllJsonball::JsonballTag)
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-jsonball
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Heneveld (ahgittin)
8
+ - Dmitrii Cucleschin (dmitryfd)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-08 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: JSON parser extension for Jekyll
15
+ email: dmitrii.cucleschin@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/jekyll-jsonball.rb
21
+ homepage: https://gist.github.com/ahgittin/1895282/
22
+ licenses:
23
+ - APL 2.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: jsonball
45
+ test_files: []