jekyll-code-example-tag 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +37 -0
- data/jekyll-code-example-tag.gemspec +1 -1
- data/lib/jekyll-code-example-tag.rb +22 -7
- data/spec/code_example_spec.rb +41 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f78a0b35d2b5412c1ee9037a9d9e00dfd1475544
|
4
|
+
data.tar.gz: f88e066633873bc16b1f2f4302977968c5ff4f95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6aa90895e3fc7074828bd7f7ea02261b2c56b812d52d3403402aa1f120707b755359b8612b587b66be08b6f8aba4201fcb40afc8deeffa4fca608e9688e2e393
|
7
|
+
data.tar.gz: 9ce37f352f410ab46dbc01b2cf314a3f05f7f04d69a48630739fdbb948bf82b7a2632df0c97b1183ac06e6b4a0ec6e2192e372252cfe4d713cf45be5ed2dc481
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
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,
|
@@ -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,
|
60
|
-
|
61
|
-
|
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
|
-
|
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
|
|
data/spec/code_example_spec.rb
CHANGED
@@ -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
|
-
|
17
|
-
|
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 '
|
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
|
-
|
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
|