nanoc-filesystem-i18n 0.1.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +25 -0
- data/README.rdoc +109 -0
- data/Rakefile +59 -0
- data/lib/nanoc3/data_sources/filesystem_i18n.rb +399 -0
- data/lib/nanoc3/data_sources/filesystem_i18n/version.rb +12 -0
- data/lib/nanoc3/extra/i18n.rb +50 -0
- data/test/helper.rb +90 -0
- data/test/test_filesystem.rb +347 -0
- data/test/test_filesystem_i18n.rb +61 -0
- data/test/test_filesystem_unified.rb +461 -0
- data/test/test_filesystem_verbose.rb +260 -0
- metadata +129 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'nanoc3'
|
4
|
+
require 'i18n'
|
5
|
+
|
6
|
+
module I18n
|
7
|
+
class Config
|
8
|
+
|
9
|
+
def localized_site?
|
10
|
+
!available_locales.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def exclude_list(kind)
|
14
|
+
@exclude ||= {}
|
15
|
+
@exclude[kind] ||= localized_site? ? [] : ['*']
|
16
|
+
end
|
17
|
+
|
18
|
+
def exclude_list=(exclude)
|
19
|
+
@exclude = exclude
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def load_config(dir_or_config_hash)
|
26
|
+
if dir_or_config_hash.is_a? String
|
27
|
+
# Read config from config.yaml in given dir
|
28
|
+
config_path = File.join(dir_or_config_hash, 'config.yaml')
|
29
|
+
config_data = YAML.load_file(config_path).symbolize_keys[:locale] rescue {}
|
30
|
+
else
|
31
|
+
# Use passed config hash
|
32
|
+
config_data = dir_or_config_hash || {}
|
33
|
+
end
|
34
|
+
|
35
|
+
config.available_locales = config_data[:availables] ? config_data[:availables].map {|code, data| code.to_sym } : []
|
36
|
+
if I18n.localized_site?
|
37
|
+
config.default_locale = begin config_data[:availables].find{|code, data| data[:default] }[0] rescue config_data[:availables].first[0] end
|
38
|
+
config.exclude_list = config_data[:exclude]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def exclude_list(kind)
|
43
|
+
config.exclude_list(kind)
|
44
|
+
end
|
45
|
+
|
46
|
+
def localized_site?
|
47
|
+
config.localized_site?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Load unit testing stuff
|
4
|
+
begin
|
5
|
+
require 'minitest/unit'
|
6
|
+
require 'minitest/spec'
|
7
|
+
require 'minitest/mock'
|
8
|
+
require 'mocha'
|
9
|
+
rescue => e
|
10
|
+
$stderr.puts "To run the nanoc unit tests, you need minitest and mocha."
|
11
|
+
raise e
|
12
|
+
end
|
13
|
+
|
14
|
+
# Load nanoc
|
15
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
|
16
|
+
require 'nanoc3'
|
17
|
+
require 'nanoc3/cli'
|
18
|
+
require 'nanoc3/tasks'
|
19
|
+
require 'nanoc3/data_sources/filesystem_i18n'
|
20
|
+
|
21
|
+
# Load miscellaneous requirements
|
22
|
+
require 'stringio'
|
23
|
+
|
24
|
+
module Nanoc3::TestHelpers
|
25
|
+
|
26
|
+
def if_have(*libs)
|
27
|
+
libs.each do |lib|
|
28
|
+
begin
|
29
|
+
require lib
|
30
|
+
rescue LoadError
|
31
|
+
skip "requiring #{lib} failed"
|
32
|
+
return
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
yield
|
37
|
+
end
|
38
|
+
|
39
|
+
def setup
|
40
|
+
# Clean up
|
41
|
+
GC.start
|
42
|
+
|
43
|
+
# Go quiet
|
44
|
+
unless ENV['QUIET'] == 'false'
|
45
|
+
$stdout = StringIO.new
|
46
|
+
$stderr = StringIO.new
|
47
|
+
end
|
48
|
+
|
49
|
+
# Enter tmp
|
50
|
+
FileUtils.mkdir_p('tmp')
|
51
|
+
FileUtils.cd('tmp')
|
52
|
+
end
|
53
|
+
|
54
|
+
def teardown
|
55
|
+
# Exit tmp
|
56
|
+
FileUtils.cd('..')
|
57
|
+
FileUtils.rm_rf('tmp')
|
58
|
+
|
59
|
+
# Go unquiet
|
60
|
+
unless ENV['QUIET'] == 'false'
|
61
|
+
$stdout = STDOUT
|
62
|
+
$stderr = STDERR
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Adapted from http://github.com/lsegal/yard-examples/tree/master/doctest
|
67
|
+
def assert_examples_correct(object)
|
68
|
+
P(object).tags(:example).each do |example|
|
69
|
+
begin
|
70
|
+
# Get input and output
|
71
|
+
parts = example.text.split(/# ?=>/).map { |s| s.strip }
|
72
|
+
code = parts[0].strip
|
73
|
+
expected_out_raw = parts[1].strip
|
74
|
+
|
75
|
+
# Evaluate
|
76
|
+
expected_out = eval(parts[1])
|
77
|
+
actual_out = instance_eval("#{code}")
|
78
|
+
rescue Exception => e
|
79
|
+
e.message << " (code: #{code}; expected output: #{expected_out_raw})"
|
80
|
+
raise e
|
81
|
+
end
|
82
|
+
|
83
|
+
assert_equal expected_out, actual_out,
|
84
|
+
"Incorrect example: #{code}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
MiniTest::Unit.autorun
|
@@ -0,0 +1,347 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'test/helper'
|
4
|
+
|
5
|
+
class Nanoc3::DataSources::FilesystemTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
include Nanoc3::TestHelpers
|
8
|
+
|
9
|
+
class SampleFilesystemDataSource < Nanoc3::DataSources::FilesystemI18n
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_setup
|
13
|
+
# Create data source
|
14
|
+
data_source = SampleFilesystemDataSource.new(nil, nil, nil, nil)
|
15
|
+
|
16
|
+
# Remove files to make sure they are recreated
|
17
|
+
FileUtils.rm_rf('content')
|
18
|
+
FileUtils.rm_rf('layouts/default')
|
19
|
+
FileUtils.rm_rf('lib/default.rb')
|
20
|
+
|
21
|
+
# Mock VCS
|
22
|
+
vcs = mock
|
23
|
+
vcs.expects(:add).times(3) # One time for each directory
|
24
|
+
data_source.vcs = vcs
|
25
|
+
|
26
|
+
# Recreate files
|
27
|
+
data_source.setup
|
28
|
+
|
29
|
+
# Ensure essential files have been recreated
|
30
|
+
assert(File.directory?('content/'))
|
31
|
+
assert(File.directory?('layouts/'))
|
32
|
+
assert(File.directory?('lib/'))
|
33
|
+
|
34
|
+
# Ensure no non-essential files have been recreated
|
35
|
+
assert(!File.file?('content/index.html'))
|
36
|
+
assert(!File.file?('layouts/default.html'))
|
37
|
+
assert(!File.file?('lib/default.rb'))
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_items
|
41
|
+
# Create data source
|
42
|
+
data_source = SampleFilesystemDataSource.new(nil, nil, nil, nil)
|
43
|
+
|
44
|
+
# Check
|
45
|
+
data_source.expects(:load_objects).with('content', 'item', Nanoc3::Item)
|
46
|
+
data_source.items
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_layouts
|
50
|
+
# Create data source
|
51
|
+
data_source = SampleFilesystemDataSource.new(nil, nil, nil, nil)
|
52
|
+
|
53
|
+
# Check
|
54
|
+
data_source.expects(:load_objects).with('layouts', 'layout', Nanoc3::Layout)
|
55
|
+
data_source.layouts
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_create_item
|
59
|
+
# Create data source
|
60
|
+
data_source = SampleFilesystemDataSource.new(nil, nil, nil, nil)
|
61
|
+
|
62
|
+
# Check
|
63
|
+
data_source.expects(:create_object).with('content', 'the content', 'the attributes', 'the identifier', {})
|
64
|
+
data_source.create_item('the content', 'the attributes', 'the identifier')
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_create_layout
|
68
|
+
# Create data source
|
69
|
+
data_source = SampleFilesystemDataSource.new(nil, nil, nil, nil)
|
70
|
+
|
71
|
+
# Check
|
72
|
+
data_source.expects(:create_object).with('layouts', 'the content', 'the attributes', 'the identifier', {})
|
73
|
+
data_source.create_layout('the content', 'the attributes', 'the identifier')
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_all_split_files_in_allowing_periods_in_identifiers
|
77
|
+
# Create data source
|
78
|
+
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, { :allow_periods_in_identifiers => true })
|
79
|
+
|
80
|
+
# Write sample files
|
81
|
+
FileUtils.mkdir_p('foo')
|
82
|
+
%w( foo.html foo.yaml bar.entry.html foo/qux.yaml ).each do |filename|
|
83
|
+
File.open(filename, 'w') { |io| io.write('test') }
|
84
|
+
end
|
85
|
+
|
86
|
+
# Write stray files
|
87
|
+
%w( foo.html~ foo.yaml.orig bar.entry.html.bak ).each do |filename|
|
88
|
+
File.open(filename, 'w') { |io| io.write('test') }
|
89
|
+
end
|
90
|
+
|
91
|
+
# Get all files
|
92
|
+
output_expected = {
|
93
|
+
'./foo' => [ 'yaml', 'html' ],
|
94
|
+
'./bar.entry' => [ nil, 'html' ],
|
95
|
+
'./foo/qux' => [ 'yaml', nil ]
|
96
|
+
}
|
97
|
+
output_actual = data_source.send :all_split_files_in, '.'
|
98
|
+
|
99
|
+
# Check
|
100
|
+
assert_equal output_expected, output_actual
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_all_split_files_in_disallowing_periods_in_identifiers
|
104
|
+
# Create data source
|
105
|
+
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, nil)
|
106
|
+
|
107
|
+
# Write sample files
|
108
|
+
FileUtils.mkdir_p('foo')
|
109
|
+
%w( foo.html foo.yaml bar.html.erb foo/qux.yaml ).each do |filename|
|
110
|
+
File.open(filename, 'w') { |io| io.write('test') }
|
111
|
+
end
|
112
|
+
|
113
|
+
# Write stray files
|
114
|
+
%w( foo.html~ foo.yaml.orig bar.entry.html.bak ).each do |filename|
|
115
|
+
File.open(filename, 'w') { |io| io.write('test') }
|
116
|
+
end
|
117
|
+
|
118
|
+
# Get all files
|
119
|
+
output_expected = {
|
120
|
+
'./foo' => [ 'yaml', 'html' ],
|
121
|
+
'./bar' => [ nil, 'html.erb' ],
|
122
|
+
'./foo/qux' => [ 'yaml', nil ]
|
123
|
+
}
|
124
|
+
output_actual = data_source.send :all_split_files_in, '.'
|
125
|
+
|
126
|
+
# Check
|
127
|
+
assert_equal output_expected, output_actual
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_all_split_files_in_with_multiple_content_files
|
131
|
+
# Create data source
|
132
|
+
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, nil)
|
133
|
+
|
134
|
+
# Write sample files
|
135
|
+
%w( foo.html foo.xhtml foo.txt foo.yaml bar.html qux.yaml ).each do |filename|
|
136
|
+
File.open(filename, 'w') { |io| io.write('test') }
|
137
|
+
end
|
138
|
+
|
139
|
+
# Check
|
140
|
+
assert_raises RuntimeError do
|
141
|
+
data_source.send :all_split_files_in, '.'
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_basename_of_allowing_periods_in_identifiers
|
146
|
+
# Create data source
|
147
|
+
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, { :allow_periods_in_identifiers => true })
|
148
|
+
|
149
|
+
# Get input and expected output
|
150
|
+
expected = {
|
151
|
+
'/' => '/',
|
152
|
+
'/foo' => '/foo',
|
153
|
+
'/foo.html' => '/foo',
|
154
|
+
'/foo.xyz.html' => '/foo.xyz',
|
155
|
+
'/foo/' => '/foo/',
|
156
|
+
'/foo.xyz/' => '/foo.xyz/',
|
157
|
+
'/foo/bar' => '/foo/bar',
|
158
|
+
'/foo/bar.html' => '/foo/bar',
|
159
|
+
'/foo/bar.xyz.html' => '/foo/bar.xyz',
|
160
|
+
'/foo/bar/' => '/foo/bar/',
|
161
|
+
'/foo/bar.xyz/' => '/foo/bar.xyz/',
|
162
|
+
'/foo.xyz/bar.xyz/' => '/foo.xyz/bar.xyz/'
|
163
|
+
}
|
164
|
+
|
165
|
+
# Check
|
166
|
+
expected.each_pair do |input, expected_output|
|
167
|
+
actual_output = data_source.send(:basename_of, input)
|
168
|
+
assert_equal(
|
169
|
+
expected_output, actual_output,
|
170
|
+
"basename_of(#{input.inspect}) should equal #{expected_output.inspect}, not #{actual_output.inspect}"
|
171
|
+
)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_basename_of_disallowing_periods_in_identifiers
|
176
|
+
# Create data source
|
177
|
+
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, nil)
|
178
|
+
|
179
|
+
# Get input and expected output
|
180
|
+
expected = {
|
181
|
+
'/' => '/',
|
182
|
+
'/foo' => '/foo',
|
183
|
+
'/foo.html' => '/foo',
|
184
|
+
'/foo.xyz.html' => '/foo',
|
185
|
+
'/foo/' => '/foo/',
|
186
|
+
'/foo.xyz/' => '/foo.xyz/',
|
187
|
+
'/foo/bar' => '/foo/bar',
|
188
|
+
'/foo/bar.html' => '/foo/bar',
|
189
|
+
'/foo/bar.xyz.html' => '/foo/bar',
|
190
|
+
'/foo/bar/' => '/foo/bar/',
|
191
|
+
'/foo/bar.xyz/' => '/foo/bar.xyz/',
|
192
|
+
'/foo.xyz/bar.xyz/' => '/foo.xyz/bar.xyz/'
|
193
|
+
}
|
194
|
+
|
195
|
+
# Check
|
196
|
+
expected.each_pair do |input, expected_output|
|
197
|
+
actual_output = data_source.send(:basename_of, input)
|
198
|
+
assert_equal(
|
199
|
+
expected_output, actual_output,
|
200
|
+
"basename_of(#{input.inspect}) should equal #{expected_output.inspect}, not #{actual_output.inspect}"
|
201
|
+
)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_ext_of_allowing_periods_in_identifiers
|
206
|
+
# Create data source
|
207
|
+
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, { :allow_periods_in_identifiers => true })
|
208
|
+
|
209
|
+
# Get input and expected output
|
210
|
+
expected = {
|
211
|
+
'/' => '',
|
212
|
+
'/foo' => '',
|
213
|
+
'/foo.html' => '.html',
|
214
|
+
'/foo.xyz.html' => '.html',
|
215
|
+
'/foo/' => '',
|
216
|
+
'/foo.xyz/' => '',
|
217
|
+
'/foo/bar' => '',
|
218
|
+
'/foo/bar.html' => '.html',
|
219
|
+
'/foo/bar.xyz.html' => '.html',
|
220
|
+
'/foo/bar/' => '',
|
221
|
+
'/foo/bar.xyz/' => '',
|
222
|
+
'/foo.xyz/bar.xyz/' => ''
|
223
|
+
}
|
224
|
+
|
225
|
+
# Check
|
226
|
+
expected.each_pair do |input, expected_output|
|
227
|
+
actual_output = data_source.send(:ext_of, input)
|
228
|
+
assert_equal(
|
229
|
+
expected_output, actual_output,
|
230
|
+
"basename_of(#{input.inspect}) should equal #{expected_output.inspect}, not #{actual_output.inspect}"
|
231
|
+
)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_ext_of_disallowing_periods_in_identifiers
|
236
|
+
# Create data source
|
237
|
+
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, nil)
|
238
|
+
|
239
|
+
# Get input and expected output
|
240
|
+
expected = {
|
241
|
+
'/' => '',
|
242
|
+
'/foo' => '',
|
243
|
+
'/foo.html' => '.html',
|
244
|
+
'/foo.xyz.html' => '.xyz.html',
|
245
|
+
'/foo/' => '',
|
246
|
+
'/foo.xyz/' => '',
|
247
|
+
'/foo/bar' => '',
|
248
|
+
'/foo/bar.html' => '.html',
|
249
|
+
'/foo/bar.xyz.html' => '.xyz.html',
|
250
|
+
'/foo/bar/' => '',
|
251
|
+
'/foo/bar.xyz/' => '',
|
252
|
+
'/foo.xyz/bar.xyz/' => ''
|
253
|
+
}
|
254
|
+
|
255
|
+
# Check
|
256
|
+
expected.each_pair do |input, expected_output|
|
257
|
+
actual_output = data_source.send(:ext_of, input)
|
258
|
+
assert_equal(
|
259
|
+
expected_output, actual_output,
|
260
|
+
"basename_of(#{input.inspect}) should equal #{expected_output.inspect}, not #{actual_output.inspect}"
|
261
|
+
)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_parse_embedded_invalid_2
|
266
|
+
# Create a file
|
267
|
+
File.open('test.html', 'w') do |io|
|
268
|
+
io.write "-----\n"
|
269
|
+
io.write "blah blah\n"
|
270
|
+
end
|
271
|
+
|
272
|
+
# Create data source
|
273
|
+
data_source = Nanoc3::DataSources::FilesystemCombined.new(nil, nil, nil, nil)
|
274
|
+
|
275
|
+
# Parse it
|
276
|
+
assert_raises(RuntimeError) do
|
277
|
+
data_source.instance_eval { parse('test.html', nil, 'foobar') }
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_parse_embedded_full_meta
|
282
|
+
# Create a file
|
283
|
+
File.open('test.html', 'w') do |io|
|
284
|
+
io.write "-----\n"
|
285
|
+
io.write "foo: bar\n"
|
286
|
+
io.write "-----\n"
|
287
|
+
io.write "blah blah\n"
|
288
|
+
end
|
289
|
+
|
290
|
+
# Create data source
|
291
|
+
data_source = Nanoc3::DataSources::FilesystemCombined.new(nil, nil, nil, nil)
|
292
|
+
|
293
|
+
# Parse it
|
294
|
+
result = data_source.instance_eval { parse('test.html', nil, 'foobar') }
|
295
|
+
assert_equal({ 'foo' => 'bar' }, result[0])
|
296
|
+
assert_equal('blah blah', result[1])
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_parse_embedded_empty_meta
|
300
|
+
# Create a file
|
301
|
+
File.open('test.html', 'w') do |io|
|
302
|
+
io.write "-----\n"
|
303
|
+
io.write "-----\n"
|
304
|
+
io.write "blah blah\n"
|
305
|
+
end
|
306
|
+
|
307
|
+
# Create data source
|
308
|
+
data_source = Nanoc3::DataSources::FilesystemCombined.new(nil, nil, nil, nil)
|
309
|
+
|
310
|
+
# Parse it
|
311
|
+
result = data_source.instance_eval { parse('test.html', nil, 'foobar') }
|
312
|
+
assert_equal({}, result[0])
|
313
|
+
assert_equal('blah blah', result[1])
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_parse_embedded_no_meta
|
317
|
+
content = "blah\n" \
|
318
|
+
"blah blah blah\n" \
|
319
|
+
"blah blah\n"
|
320
|
+
|
321
|
+
# Create a file
|
322
|
+
File.open('test.html', 'w') { |io| io.write(content) }
|
323
|
+
|
324
|
+
# Create data source
|
325
|
+
data_source = Nanoc3::DataSources::FilesystemCombined.new(nil, nil, nil, nil)
|
326
|
+
|
327
|
+
# Parse it
|
328
|
+
result = data_source.instance_eval { parse('test.html', nil, 'foobar') }
|
329
|
+
assert_equal({}, result[0])
|
330
|
+
assert_equal(content, result[1])
|
331
|
+
end
|
332
|
+
|
333
|
+
def test_parse_external
|
334
|
+
# Create a file
|
335
|
+
File.open('test.html', 'w') { |io| io.write("blah blah") }
|
336
|
+
File.open('test.yaml', 'w') { |io| io.write("foo: bar") }
|
337
|
+
|
338
|
+
# Create data source
|
339
|
+
data_source = Nanoc3::DataSources::FilesystemCombined.new(nil, nil, nil, nil)
|
340
|
+
|
341
|
+
# Parse it
|
342
|
+
result = data_source.instance_eval { parse('test.html', 'test.yaml', 'foobar') }
|
343
|
+
assert_equal({ "foo" => "bar"}, result[0])
|
344
|
+
assert_equal("blah blah", result[1])
|
345
|
+
end
|
346
|
+
|
347
|
+
end
|