social_snippet 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjJkOWViMTBkNjE5NDJiMTEzN2I1MTkwMjRiOTkxMjY0NzdiYzY5ZQ==
4
+ MThiMzVkNmUyMjI0MjdhY2M4ZmU3YjM4OWZlM2Y1MzIyYjQzYjc3MQ==
5
5
  data.tar.gz: !binary |-
6
- MWM0MGM0ZjYxOTc1OTI5MmU4MmI0MDkyM2RmZDU2MTMyM2FiOWU5Mw==
6
+ Njk0NzQzZWI1NWI3MzgyNWJmYWVmY2I1ZGI2MDU4ZmFlNWM3ZWVjOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- Y2NlYmU0MDc0ZTQ0NWI4NTU2YjVlYzYzYzkwZWYyM2ZkMGQ0NTY3NmZkZTY2
10
- MDdlZjgzZDg3NWM0YWZlZDVkOGM0YWU2YWI1YTc2MTYxZGIyZTNkY2MyNzhh
11
- NjM5MDEzOWEzNWEwODAzMDJhZjIzYTdkY2M3NTZjMTc5Y2IwOGI=
9
+ OWEwYTA2MGEyYmI1YmMyMjAxZWMzYzljZjg2NDRhYTJmNWYwNTgyNGNlZTUw
10
+ ZDg0M2I2YWI5MjQxYjQxMjNkZmRjYjJhODgxYTQ4N2FmM2U5YWE2ODgyNTQ5
11
+ N2EyN2JjYjdmZDdiNzAzYzcyMzg4NjRlNmZkYzYzMTQ3NTg1YjU=
12
12
  data.tar.gz: !binary |-
13
- MTc5ODRjYWUzM2Y5NjJmOGY0NDRhYjZmNDRkYzA4ODg2OGYzN2M3MmI1N2Zh
14
- ZmM3MGNhNmE0ZmVmYjE1ZjM4MTc2ZWRmOGQ0Y2RmODBkYTYwMjcwNDc2ZGU5
15
- NDI2ZDlhYWIyZTg5OTRiNWZhY2VjYThhNTljZjdjZDBjMTdkNGY=
13
+ YTZkMjI0MTAwYzBjNzZkZmI0ZWY5N2FlMjk1OGZiMmQ4MWNlNDY0YmJkYjQ3
14
+ OGY4NWYwMDZkNTUxNTYyYzY5ZmIxNjJlNDIyNzRlODE0NmViMTNmYzZjMjAw
15
+ ZWRkYjFhN2U1YmVhZTA3YmYzOTFiYjkyZmU3NzAyODhiMmIxODU=
@@ -10,6 +10,7 @@ require "optparse"
10
10
  require "uri"
11
11
  require "wisper"
12
12
  require "securerandom"
13
+ require "css_parser"
13
14
 
14
15
  module SocialSnippet; end
15
16
  require_relative "social_snippet/core"
@@ -88,6 +88,10 @@ class SocialSnippet::Config
88
88
  # config helpers
89
89
  #
90
90
 
91
+ def snippet_css
92
+ ::File.join home, "snippet.css"
93
+ end
94
+
91
95
  def document_path
92
96
  ::File.join home, "documents.yml"
93
97
  end
@@ -16,10 +16,51 @@ module SocialSnippet
16
16
  end
17
17
 
18
18
  def init_options
19
- options[:margin_bottom] = options.fetch(:margin_bottom, 0)
19
+ # apply local snippet.css
20
+ parse_snippet_css "snippet.css"
21
+
22
+ # apply global snippet.css
23
+ parse_snippet_css core.config.snippet_css
24
+
25
+ # use default value
26
+ options[:margin_bottom] = options.fetch(:margin_bottom, 1)
20
27
  options[:margin_top] = options.fetch(:margin_top, 0)
21
28
  end
22
29
 
30
+ def convert_to_option_key(prop)
31
+ case prop
32
+ when "margin-bottom"
33
+ :margin_bottom
34
+ when "margin-top"
35
+ :margin_top
36
+ else
37
+ prop.to_sym
38
+ end
39
+ end
40
+
41
+ # Example of snippet.css
42
+ # ```css
43
+ # snippet {
44
+ # margin-top: 3;
45
+ # margin-bottom: 3;
46
+ # }
47
+ # ```
48
+ def parse_snippet_css(path)
49
+ return unless core.storage.file?(path)
50
+ parser = ::CssParser::Parser.new
51
+ parser.add_block! core.storage.read(path)
52
+ apply_styles parser, "snippet"
53
+ end
54
+
55
+ def apply_styles(parser, selector)
56
+ style = parser.find_by_selector(selector).first
57
+ rules = ::CssParser::RuleSet.new(nil, style)
58
+ rules.each_declaration do |prop, val, imp|
59
+ key = convert_to_option_key(prop)
60
+ options[key] = options.fetch(key, val.to_i)
61
+ end
62
+ end
63
+
23
64
  # Insert snippets to given text
24
65
  #
25
66
  # @param text [String] The text of source code
@@ -1,6 +1,6 @@
1
1
  module SocialSnippet
2
2
 
3
- VERSION = "0.0.13"
3
+ VERSION = "0.0.14"
4
4
 
5
5
  module Version
6
6
 
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_runtime_dependency "highline", "~> 1.7.0"
25
25
  spec.add_runtime_dependency "wisper", "~> 1.6.0"
26
26
  spec.add_runtime_dependency "social_snippet-supports-git", "~> 0.1.0"
27
+ spec.add_runtime_dependency "css_parser", "~> 1.3.6"
27
28
 
28
29
  spec.add_development_dependency "bundler"
29
30
  spec.add_development_dependency "rake"
@@ -4,6 +4,16 @@ module SocialSnippet
4
4
 
5
5
  describe Api::InsertSnippetApi do
6
6
 
7
+ before do
8
+ # prepare snippet.css (global)
9
+ fake_core.storage.write fake_core.config.snippet_css, [
10
+ "snippet {",
11
+ " margin-top: 0;",
12
+ " margin-bottom: 0;",
13
+ "}",
14
+ ].join($/)
15
+ end
16
+
7
17
  describe "#insert_snippet()", :without_fakefs => true do
8
18
 
9
19
  class FakeDriver < ::SocialSnippet::Repository::Drivers::DriverBase
@@ -88,6 +88,7 @@ module SocialSnippet
88
88
  ' return 42;',
89
89
  '}',
90
90
  '',
91
+ '',
91
92
  'int main() {',
92
93
  ' std::cout << get_42() << std::endl;',
93
94
  ' return 0;',
@@ -26,6 +26,16 @@ describe SocialSnippet::Resolvers::InsertResolver do
26
26
  end
27
27
  end
28
28
 
29
+ before do
30
+ # prepare snippet.css (global)
31
+ fake_core.storage.write fake_core.config.snippet_css, [
32
+ "snippet {",
33
+ " margin-top: 0;",
34
+ " margin-bottom: 0;",
35
+ "}",
36
+ ].join($/)
37
+ end
38
+
29
39
  describe "@begin_cut / @end_cut" do
30
40
 
31
41
  describe "ruby's require()" do
@@ -191,6 +201,248 @@ describe SocialSnippet::Resolvers::InsertResolver do
191
201
 
192
202
  end # :margin_bottom
193
203
 
204
+ describe "snippet.css", :current => true do
205
+
206
+ context "snippet { margin-bottom: 3 }" do
207
+
208
+ before do
209
+ fake_core.storage.write "snippet.css", [
210
+ "snippet { margin-bottom: 3; }"
211
+ ].join($/)
212
+ end
213
+
214
+ let(:resolver) do
215
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core, ::Hash.new)
216
+ end
217
+
218
+ let(:input) do
219
+ [
220
+ "// @snip <my-repo-1:path/to/file.d>",
221
+ "// @snip <my-repo-2:path/to/file.d>",
222
+ "// @snip <my-repo-3:path/to/file.d>",
223
+ ].join($/)
224
+ end
225
+
226
+ let(:expected) do
227
+ [
228
+ "// @snippet <my-repo-1:path/to/file.d>",
229
+ "my-repo-1",
230
+ "",
231
+ "",
232
+ "",
233
+ "// @snippet <my-repo-2:path/to/file.d>",
234
+ "my-repo-2",
235
+ "",
236
+ "",
237
+ "",
238
+ "// @snippet <my-repo-3:path/to/file.d>",
239
+ "my-repo-3",
240
+ "",
241
+ "",
242
+ "",
243
+ ].join($/)
244
+ end
245
+
246
+ subject { resolver.insert input }
247
+ it { should eq expected }
248
+
249
+ end # snippet { margin-bottom: 3 }
250
+
251
+ context "snippet { margin-top: 3; margin-bottom: 3 }" do
252
+
253
+ before do
254
+ fake_core.storage.write "snippet.css", [
255
+ "snippet {",
256
+ " margin-top: 3;",
257
+ " margin-bottom: 3;",
258
+ "}",
259
+ ].join($/)
260
+ end
261
+
262
+ let(:resolver) do
263
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core, ::Hash.new)
264
+ end
265
+
266
+ let(:input) do
267
+ [
268
+ "// @snip <my-repo-1:path/to/file.d>",
269
+ "// @snip <my-repo-2:path/to/file.d>",
270
+ "// @snip <my-repo-3:path/to/file.d>",
271
+ ].join($/)
272
+ end
273
+
274
+ let(:expected) do
275
+ [
276
+ "",
277
+ "",
278
+ "",
279
+ "// @snippet <my-repo-1:path/to/file.d>",
280
+ "my-repo-1",
281
+ "",
282
+ "",
283
+ "",
284
+ "",
285
+ "",
286
+ "",
287
+ "// @snippet <my-repo-2:path/to/file.d>",
288
+ "my-repo-2",
289
+ "",
290
+ "",
291
+ "",
292
+ "",
293
+ "",
294
+ "",
295
+ "// @snippet <my-repo-3:path/to/file.d>",
296
+ "my-repo-3",
297
+ "",
298
+ "",
299
+ "",
300
+ ].join($/)
301
+ end
302
+
303
+ subject { resolver.insert input }
304
+ it { should eq expected }
305
+
306
+ end # snippet { margin-top: 3; margin-bottom: 3 }
307
+
308
+ context "global - snippet { margin-bottom: 3 }" do
309
+
310
+ before do
311
+ fake_core.storage.write fake_core.config.snippet_css, [
312
+ "snippet{ margin-top: 3; margin-bottom: 0 }"
313
+ ].join($/)
314
+ end
315
+
316
+ let(:resolver) do
317
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core, ::Hash.new)
318
+ end
319
+
320
+ let(:input) do
321
+ [
322
+ "// @snip <my-repo-1:path/to/file.d>",
323
+ "// @snip <my-repo-2:path/to/file.d>",
324
+ "// @snip <my-repo-3:path/to/file.d>",
325
+ ].join($/)
326
+ end
327
+
328
+ let(:expected) do
329
+ [
330
+ "",
331
+ "",
332
+ "",
333
+ "// @snippet <my-repo-1:path/to/file.d>",
334
+ "my-repo-1",
335
+ "",
336
+ "",
337
+ "",
338
+ "// @snippet <my-repo-2:path/to/file.d>",
339
+ "my-repo-2",
340
+ "",
341
+ "",
342
+ "",
343
+ "// @snippet <my-repo-3:path/to/file.d>",
344
+ "my-repo-3",
345
+ ].join($/)
346
+ end
347
+
348
+ subject { resolver.insert input }
349
+ it { should eq expected }
350
+
351
+ end
352
+
353
+ context "local css > global css" do
354
+
355
+ before do
356
+ fake_core.storage.write fake_core.config.snippet_css, [
357
+ "snippet{ margin-top: 0; margin-bottom: 0 }"
358
+ ].join($/)
359
+
360
+ fake_core.storage.write "snippet.css", [
361
+ "snippet{ margin-top: 3; margin-bottom: 0 }"
362
+ ].join($/)
363
+ end
364
+
365
+ let(:resolver) do
366
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core, ::Hash.new)
367
+ end
368
+
369
+ let(:input) do
370
+ [
371
+ "// @snip <my-repo-1:path/to/file.d>",
372
+ "// @snip <my-repo-2:path/to/file.d>",
373
+ "// @snip <my-repo-3:path/to/file.d>",
374
+ ].join($/)
375
+ end
376
+
377
+ let(:expected) do
378
+ [
379
+ "",
380
+ "",
381
+ "",
382
+ "// @snippet <my-repo-1:path/to/file.d>",
383
+ "my-repo-1",
384
+ "",
385
+ "",
386
+ "",
387
+ "// @snippet <my-repo-2:path/to/file.d>",
388
+ "my-repo-2",
389
+ "",
390
+ "",
391
+ "",
392
+ "// @snippet <my-repo-3:path/to/file.d>",
393
+ "my-repo-3",
394
+ ].join($/)
395
+ end
396
+
397
+ subject { resolver.insert input }
398
+ it { should eq expected }
399
+
400
+ end # local css > global css
401
+
402
+ context "multi-selector case" do
403
+
404
+ before do
405
+ fake_core.storage.write fake_core.config.snippet_css, [
406
+ "abcde, snippet, .test, #testtest { margin-top: 2; margin-bottom: 0 }"
407
+ ].join($/)
408
+ end
409
+
410
+ let(:resolver) do
411
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core, ::Hash.new)
412
+ end
413
+
414
+ let(:input) do
415
+ [
416
+ "// @snip <my-repo-1:path/to/file.d>",
417
+ "// @snip <my-repo-2:path/to/file.d>",
418
+ "// @snip <my-repo-3:path/to/file.d>",
419
+ ].join($/)
420
+ end
421
+
422
+ let(:expected) do
423
+ [
424
+ "",
425
+ "",
426
+ "// @snippet <my-repo-1:path/to/file.d>",
427
+ "my-repo-1",
428
+ "",
429
+ "",
430
+ "// @snippet <my-repo-2:path/to/file.d>",
431
+ "my-repo-2",
432
+ "",
433
+ "",
434
+ "// @snippet <my-repo-3:path/to/file.d>",
435
+ "my-repo-3",
436
+ ].join($/)
437
+ end
438
+
439
+ subject { resolver.insert input }
440
+ it { should eq expected }
441
+
442
+ end # multi-selectors case
443
+
444
+ end # snippet.css
445
+
194
446
  end # test styling
195
447
 
196
448
  end # prepare stubs
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe ::SocialSnippet::Snippet, :current => true do
4
+
5
+ describe "snippet.css" do
6
+
7
+ context "prepare snippet" do
8
+
9
+ let(:snippet) do
10
+ ::SocialSnippet::Snippet.new_text fake_core, [
11
+ "line-1",
12
+ "line-2",
13
+ "line-3",
14
+ ].join($/)
15
+ end
16
+
17
+ context "no styles" do
18
+ subject { snippet.lines }
19
+ let(:expected) do
20
+ [
21
+ "line-1",
22
+ "line-2",
23
+ "line-3",
24
+ ]
25
+ end
26
+ it { should eq expected }
27
+ end
28
+
29
+ end # prepare snippet
30
+
31
+ end # snippet.css
32
+
33
+ end # ::SocialSnippet::Snippet
34
+
@@ -50,6 +50,16 @@ describe SocialSnippet::Api::InsertSnippetApi do
50
50
  end
51
51
  end
52
52
 
53
+ before do
54
+ # prepare snippet.css (global)
55
+ fake_core.storage.write fake_core.config.snippet_css, [
56
+ "snippet {",
57
+ " margin-top: 0;",
58
+ " margin-bottom: 0;",
59
+ "}",
60
+ ].join($/)
61
+ end
62
+
53
63
  describe "#insert_snippet" do
54
64
 
55
65
  context "use commit id" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: social_snippet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki Sano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-25 00:00:00.000000000 Z
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: version_sorter
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: css_parser
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.6
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.6
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: bundler
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -318,6 +332,7 @@ files:
318
332
  - spec/lib/repository/package_spec.rb
319
333
  - spec/lib/repository/repository_manager_spec.rb
320
334
  - spec/lib/repository/repository_spec.rb
335
+ - spec/lib/snippet_spec.rb
321
336
  - spec/lib/tag_parser_spec.rb
322
337
  - spec/lib/tag_spec.rb
323
338
  - spec/lib/yaml_document_spec.rb
@@ -385,6 +400,7 @@ test_files:
385
400
  - spec/lib/repository/package_spec.rb
386
401
  - spec/lib/repository/repository_manager_spec.rb
387
402
  - spec/lib/repository/repository_spec.rb
403
+ - spec/lib/snippet_spec.rb
388
404
  - spec/lib/tag_parser_spec.rb
389
405
  - spec/lib/tag_spec.rb
390
406
  - spec/lib/yaml_document_spec.rb