jekyll-code-example-tag 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 313440e34ab23b1fc7dbf0f74007afac212cf8d6
4
- data.tar.gz: 1000fa7315fecf654b1b691c3675d0a9ee45f915
3
+ metadata.gz: f78a0b35d2b5412c1ee9037a9d9e00dfd1475544
4
+ data.tar.gz: f88e066633873bc16b1f2f4302977968c5ff4f95
5
5
  SHA512:
6
- metadata.gz: 03cfaf2a1f6389da446bbb38a2001242d77199e80ffc15f51a519c49736de856f5d9952d185bfc9b995007daa193f69c958d08230e0ff7f41ab76a646852b087
7
- data.tar.gz: db4d25d8a1c4e354e89656d0c0f9658a920794ea1b40fdaab2e29a3e7f3cea19cc3fc7ad5abc80722d53cca7a08e1c1c7dd5e69068d307f623a63c64048ad4d8
6
+ metadata.gz: 6aa90895e3fc7074828bd7f7ea02261b2c56b812d52d3403402aa1f120707b755359b8612b587b66be08b6f8aba4201fcb40afc8deeffa4fca608e9688e2e393
7
+ data.tar.gz: 9ce37f352f410ab46dbc01b2cf314a3f05f7f04d69a48630739fdbb948bf82b7a2632df0c97b1183ac06e6b4a0ec6e2192e372252cfe4d713cf45be5ed2dc481
@@ -4,3 +4,6 @@ rvm:
4
4
  - 2.1.5
5
5
  - 2.2.0
6
6
  script: "bundle exec rspec"
7
+ branches:
8
+ only:
9
+ -master
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jekyll-code-example-tag (0.0.3)
4
+ jekyll-code-example-tag (0.0.5)
5
5
  jekyll
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -124,6 +124,43 @@ the resulting markup will include just the Ruby example:
124
124
  </div>
125
125
  </div>
126
126
 
127
+ The *code_example* tag can also support examples organized in sub directories.
128
+ For example, say you would like to organize some of your code examples by
129
+ product and API version:
130
+
131
+ .
132
+ |-code_examples
133
+ |-productA
134
+ |-api_v1
135
+ |-ruby
136
+ |-widget_maker
137
+ |-python
138
+ |-widget_maker
139
+ |-api_v2
140
+ |-ruby
141
+ |-widget_maker
142
+ |-python
143
+ |-widget_maker
144
+ |-productB
145
+ |-api_v1
146
+ |-ruby
147
+ |-authenticating
148
+ |-python
149
+ |-authenticating
150
+ |-ruby
151
+ |-hello_world
152
+ |-python
153
+ |-hello_world
154
+
155
+ With the above directory structure, including any of the following calls to
156
+ code_example will result in including only the relevant code examples from
157
+ the referred to directory:
158
+
159
+ {% code_example productA/api_v1/widget_maker %}
160
+ {% code_example productA/api_v2/widget_maker %}
161
+ {% code_example productB/api_v1/authenticating %}
162
+ {% code_example hello_world %}
163
+
127
164
  ### all_page_code_examples
128
165
 
129
166
  If you have included a few code examples on a page via the *code_example* tag,
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jekyll-code-example-tag'
3
- s.version = '0.0.4'
3
+ s.version = '0.0.5'
4
4
  s.date = '2015-02-03'
5
5
  s.authors = ['GovDelivery']
6
6
  s.email = 'support@govdelivery.com'
@@ -6,9 +6,9 @@ module Jekyll
6
6
  end
7
7
 
8
8
  # Returns a hash of available code examples (per language) for the provided example name
9
- def self.code_examples(example_name, site)
9
+ def self.code_examples(context_path, example_name, site)
10
10
  # Collect all relevant files
11
- examples_root = code_example_dir(site)
11
+ examples_root = File.join(code_example_dir(site), context_path)
12
12
 
13
13
  code_folders = Dir.entries(examples_root).select do |entry|
14
14
  File.directory? File.join(examples_root, entry) and !(entry =='.' || entry == '..')
@@ -55,15 +55,29 @@ EOF
55
55
  "<div class='code-examples'>#{content}</div>"
56
56
  end
57
57
 
58
+ def self.get_example_name_and_context(example_string)
59
+ example_string.strip!
60
+ if example_string.include?('/')
61
+ example_arr = example_string.split('/')
62
+ example_name = example_arr.delete_at(-1)
63
+ context_path = example_arr.join(File::SEPARATOR) + File::SEPARATOR
64
+ else
65
+ context_path = ''
66
+ example_name = example_string
67
+ end
68
+
69
+ return context_path, example_name
70
+ end
71
+
58
72
  class CodeExampleTag < Liquid::Tag
59
- def initialize(tag_name, example_name, tokens)
60
- @example_name = example_name.strip
61
- super
73
+ def initialize(tag_name, example_string, tokens)
74
+ @context_path, @example_name = Jekyll::CodeExampleTags::get_example_name_and_context(example_string)
75
+ super
62
76
  end
63
77
 
64
78
  def render(context)
65
79
 
66
- examples = Jekyll::CodeExampleTags::code_examples(@example_name, context['site'])
80
+ examples = Jekyll::CodeExampleTags::code_examples(@context_path, @example_name, context['site'])
67
81
 
68
82
  # Build the code example elements
69
83
  output = Jekyll::CodeExampleTags::buttons_markup(examples)
@@ -80,7 +94,8 @@ EOF
80
94
  def render(context)
81
95
  examples = {}
82
96
  context['page']['content'].scan(/\{%\s*code_example (\S+)\s*%\}/) do |name|
83
- more_examples = Jekyll::CodeExampleTags::code_examples(name[0], context['site'])
97
+ context_path, example_name = Jekyll::CodeExampleTags::get_example_name_and_context(name[0])
98
+ more_examples = Jekyll::CodeExampleTags::code_examples(context_path, example_name, context['site'])
84
99
  examples.merge!(more_examples){|key, pre_example, new_example| "#{pre_example}\n#{new_example}"}
85
100
  end
86
101
 
@@ -1,33 +1,63 @@
1
1
  require_relative './spec_helper.rb'
2
2
 
3
+ def check_code_example_conditions(o_obj)
4
+ expect(o_obj.xpath('/div[@class="code-examples"]/div[@class="buttons examples"]/ul/li/a[@class="button"][@target="ruby"][.="Ruby"]')).not_to be_empty
5
+ expect(o_obj.xpath('/div[@class="code-examples"]/div[@class="buttons examples"]/ul/li/a[@class="button"][@target="python"][.="Python"]')).not_to be_empty
6
+ ruby_example = o_obj.xpath('/div[@class="code-examples"]/div[@class="highlight example ruby"]/pre/code[@class="language ruby"][@data-lang="ruby"]')
7
+ expect(ruby_example).not_to be_empty
8
+ expect(ruby_example.first.content).to eq 'puts "Hello World"'
9
+ python_example = o_obj.xpath('/div[@class="code-examples"]/div[@class="highlight example python"]/pre/code[@class="language python"][@data-lang="python"]')
10
+ expect(python_example).not_to be_empty
11
+ expect(python_example.first.content).to eq 'print "Hello World"'
12
+ end
13
+
3
14
  describe 'code_example', fakefs: true do
4
15
 
5
16
  let(:page) { "{% code_example hello_world %}" }
6
17
 
7
18
  before(:all) do
8
19
  FakeFS.activate!
20
+ end
21
+
22
+ after(:all) do
23
+ FakeFS.deactivate!
24
+ end
9
25
 
26
+ it 'can be used' do
10
27
  FileUtils.mkdir_p('code_examples/ruby')
11
28
  FileUtils.mkdir_p('code_examples/python')
12
29
  File.open('code_examples/ruby/hello_world', 'w') { |f| f << 'puts "Hello World"'}
13
30
  File.open('code_examples/python/hello_world', 'w') { |f| f << 'print "Hello World"'}
31
+
32
+ t = Liquid::Template.parse(page)
33
+ o = t.render!({'site' => {}})
34
+ o_obj = Nokogiri::XML.parse(o)
35
+ check_code_example_conditions(o_obj)
14
36
  end
15
37
 
16
- after(:all) do
17
- FakeFS.deactivate!
38
+ it 'can be configured via "code_example_dir"' do
39
+ FileUtils.mkdir_p('examples/ruby')
40
+ FileUtils.mkdir_p('examples/python')
41
+ File.open('examples/ruby/hello_world', 'w') { |f| f << 'puts "Hello World"'}
42
+ File.open('examples/python/hello_world', 'w') { |f| f << 'print "Hello World"'}
43
+
44
+ t = Liquid::Template.parse(page)
45
+ o = t.render!({'site' => {'code_example_dir' => 'examples'}})
46
+ o_obj = Nokogiri::XML.parse(o)
47
+ check_code_example_conditions(o_obj)
18
48
  end
19
49
 
20
- it 'can be used' do
50
+ it 'allows for dividing examples via context' do
51
+ FileUtils.mkdir_p('code_examples/v1/ruby')
52
+ FileUtils.mkdir_p('code_examples/v1/python')
53
+ File.open('code_examples/v1/ruby/hello_world', 'w') { |f| f << 'puts "Hello World"'}
54
+ File.open('code_examples/v1/python/hello_world', 'w') { |f| f << 'print "Hello World"'}
55
+
56
+ page = "{% code_example v1/hello_world %}"
57
+
21
58
  t = Liquid::Template.parse(page)
22
59
  o = t.render!({'site' => {}})
23
60
  o_obj = Nokogiri::XML.parse(o)
24
- expect(o_obj.xpath('/div[@class="code-examples"]/div[@class="buttons examples"]/ul/li/a[@class="button"][@target="ruby"][.="Ruby"]')).not_to be_empty
25
- expect(o_obj.xpath('/div[@class="code-examples"]/div[@class="buttons examples"]/ul/li/a[@class="button"][@target="python"][.="Python"]')).not_to be_empty
26
- ruby_example = o_obj.xpath('/div[@class="code-examples"]/div[@class="highlight example ruby"]/pre/code[@class="language ruby"][@data-lang="ruby"]')
27
- expect(ruby_example).not_to be_empty
28
- expect(ruby_example.first.content).to eq 'puts "Hello World"'
29
- python_example = o_obj.xpath('/div[@class="code-examples"]/div[@class="highlight example python"]/pre/code[@class="language python"][@data-lang="python"]')
30
- expect(python_example).not_to be_empty
31
- expect(python_example.first.content).to eq 'print "Hello World"'
61
+ check_code_example_conditions(o_obj)
32
62
  end
33
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-code-example-tag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - GovDelivery