social_snippet 0.0.6 → 0.0.7

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
- NDExZWFlYjUyMzUyOGQwYWFkNjg0YjRhZDg0ZDdmMzA1MTViMjIxMA==
4
+ NzU5NTJjOWRhNjY0MGQ1Yjk2MzY0YjA3ZmU3YTc3NzkzYjBjYWM4Mg==
5
5
  data.tar.gz: !binary |-
6
- OGMyMTAyZjdmODQ2NzJiNzM4YzAyMGJlZDRmMjQwZjEyYmIxMmJiZQ==
6
+ ZWIxMDM3ODY5MTgxYzEyMGU0ZWM4ZWNjNWFjZWJkMjg5ODgyYWQ5OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTg1MWU1OGM5ODUxMjdhNTZlZDhhMmU5ZTMzYmM2N2ExMWNjNDNmYjM2MmM2
10
- MTE2MzU0NzYyNGU4NWU1NDljNmRjNzE5MTI5NmQ3Njc2Y2NhZGRlNzNiNThj
11
- YzRmNTA0ZDVhYmVlNmI4ZGZjMWI0MjNmYzE1ZjVhNjk0ZThmMWQ=
9
+ ZDlhNWY2ZDMyOWZhNzRiNGQ2NzUxZGNkOWUyNGNlNGE5ZmE1MDBiZTBkMGVj
10
+ ZGU0ZjViN2MxMDU0ZjlhYmZkZDcwYTI1MzYyYWVkMTUwMDM5NDJjMjA3ODlj
11
+ MTk0ZGYyOTVkNWQ4Yjk4OGZhMzFmOWQxNmVjMzhkN2NlMTY2Y2U=
12
12
  data.tar.gz: !binary |-
13
- YjY0NjczODE3NWUxZjRhNGFmMzliY2IyYTFmODJjM2I3OGU5MTJjZmVjNGUz
14
- NTY4MTE1Y2ZlN2FlMGU1ZmNjMTZlMjc2ZGJkNGFjNjAyM2M3MjZkN2JjZGZm
15
- MmY3MmFhYWM4ZGExNjA0YWZlYjI4ZDU0MDI0MGI1MDRjNGFlNjQ=
13
+ YTJiY2ZjZWFjYWFkNTZmNGIxMzA4Yjk1Mzc0NzgwNWRhZjFiMjM5OTFhZGZm
14
+ Mzk3NTg0NmUwZTY2MzIwYjI0NzI3MzZkNTE5MjkwOGFlNDI4MzUzYjdjNDMw
15
+ ODNlNjkzMTdkODU4ZTc3NzNkNTUyOTk1YmNiMWQzZmI0MjY0Mjg=
@@ -6,6 +6,7 @@ module SocialSnippet::Api::InsertSnippetApi
6
6
  # @param src [String] The text of source code
7
7
  #
8
8
  def insert_snippet(src, options = {})
9
+ raise "must be passed string" unless src.is_a?(String)
9
10
  resolver = ::SocialSnippet::Resolvers::InsertResolver.new(social_snippet, options)
10
11
  res = resolver.insert(src)
11
12
  output res
@@ -39,7 +39,7 @@ module SocialSnippet::Repository
39
39
  not deps(repo_name, repo_ref).empty?
40
40
  end
41
41
 
42
- # Get snippet
42
+ # Get snippet by context and tag
43
43
  #
44
44
  # @param context [::SocialSnippet::Context] The context of snippet
45
45
  # @param tag [::SocialSnippet::Tag] The tag of snippet
@@ -15,11 +15,13 @@ module SocialSnippet
15
15
 
16
16
  # Call block each snip tags
17
17
  #
18
- # @param src [Array<String>] The text of source code
19
- # @param context [SocialSnippet::Context] The context of current code
20
- # @param base_tag [SocialSnippet::Tag]
21
- def each_snip_tags(src, context, base_tag)
22
- TagParser.find_snip_tags(src).each do |tag_info|
18
+ # @param snippet [Snippet]
19
+ # @param context [Context] The context of current code
20
+ # @param base_tag [Tag]
21
+ def each_child_snippet(snippet, context, base_tag)
22
+ raise "must be passed snippet" unless snippet.is_a?(Snippet)
23
+
24
+ snippet.snip_tags.each do |tag_info|
23
25
  t = tag_info[:tag].set_by_tag(base_tag)
24
26
  new_context = context.clone
25
27
 
@@ -28,42 +30,16 @@ module SocialSnippet
28
30
  update_tag_path_by_context! new_context, t
29
31
  resolve_tag_repo_ref! t
30
32
 
31
- snippet = social_snippet.repo_manager.get_snippet(context, t)
33
+ child_snippet = social_snippet.repo_manager.get_snippet(context, t)
32
34
 
33
35
  if block_given?
34
- yield(
35
- tag_info[:tag],
36
- tag_info[:line_no],
37
- snippet,
38
- new_context
39
- )
36
+ yield tag_info[:tag], tag_info[:line_no], child_snippet, new_context
40
37
  end
41
38
  end
42
39
  end
43
40
 
44
- # @param lines [Array<String>]
45
- def filter(lines)
46
- lines = cut_filter(lines)
47
- lines
48
- end
49
-
50
41
  private
51
42
 
52
- def cut_filter(lines)
53
- cut_level = 0
54
- lines.select do |line|
55
- if Tag.is_begin_cut?(line)
56
- cut_level += 1
57
- false
58
- elsif Tag.is_end_cut?(line)
59
- cut_level -= 1
60
- false
61
- else
62
- cut_level === 0
63
- end
64
- end
65
- end
66
-
67
43
  def move_context_by_tag!(context, tag)
68
44
  if tag.has_repo?
69
45
  if tag.has_ref?
@@ -14,11 +14,13 @@ module SocialSnippet
14
14
 
15
15
  # Find all missing depended snippets
16
16
  #
17
- # @param src [Array<String>] The text of source code
17
+ # @param snippet [Snippet] The text of source code
18
18
  # @param context_from [SocialSnippet::Context] The context of previous code
19
19
  # @param tag_from [SocialSnippet::Tag] The previous tag
20
- def find(src, context_from, tag_from)
21
- found_tags = find_func src, context_from, tag_from
20
+ def find(snippet, context_from, tag_from)
21
+ raise "must be passed snippet" unless snippet.is_a?(Snippet)
22
+
23
+ found_tags = find_func snippet, context_from, tag_from
22
24
  found_tags.each do |tag_info|
23
25
  # remove self from deps graph
24
26
  tag = tag_info[:tag]
@@ -35,11 +37,13 @@ module SocialSnippet
35
37
  dep_to[tag_from.to_path].add tag_to.to_path
36
38
  end
37
39
 
38
- def find_func(src, context_from, tag_from)
40
+ def find_func(snippet, context_from, tag_from)
41
+ raise "must be passed snippet" unless snippet.is_a?(Snippet)
42
+
39
43
  found_tags = []
40
44
  context = context_from.clone
41
45
 
42
- each_snip_tags(src, context_from, tag_from) do |tag, line_no, snippet, new_context|
46
+ each_child_snippet(snippet, context_from, tag_from) do |tag, line_no, child_snippet, new_context|
43
47
  next if is_visited(tag)
44
48
  visit tag
45
49
 
@@ -48,13 +52,7 @@ module SocialSnippet
48
52
  :tag => tag,
49
53
  :context => new_context,
50
54
  })
51
- # find more deps
52
- if snippet.is_a?(Snippet)
53
- snippet_lines = snippet.lines
54
- else
55
- snippet_lines = snippet.split($/)
56
- end
57
- found_tags.push *find_func(snippet_lines, new_context, tag)
55
+ found_tags.push *find_func(child_snippet, new_context, tag)
58
56
  end
59
57
 
60
58
  return found_tags
@@ -22,35 +22,39 @@ module SocialSnippet
22
22
 
23
23
  # Insert snippets to given text
24
24
  #
25
- # @param src [Array<String>] The text of source code
26
- def insert(src)
25
+ # @param text [String] The text of source code
26
+ def insert(text)
27
+ raise "must be passed string" unless text.is_a?(String)
28
+
27
29
  context = Context.new("")
28
- lines = filter(src.split $/)
30
+ snippet = Snippet.new_text(text)
29
31
 
30
- TagParser.find_snippet_tags(lines).each do |tag_info|
32
+ snippet.snippet_tags.each do |tag_info|
31
33
  visit tag_info[:tag]
32
34
  end
33
35
 
34
- dest = insert_func(lines, context)
36
+ dest = insert_func(snippet, context)
35
37
  return dest.join($/)
36
38
  end
37
39
 
38
40
  private
39
41
 
40
- def insert_func(code, context_from, base_tag = nil)
41
- inserter = Inserter.new(code)
42
+ def insert_func(snippet, context_from, base_tag = nil)
43
+ raise "must be passed snippet" unless snippet.is_a?(Snippet)
44
+
45
+ inserter = Inserter.new(snippet.lines)
42
46
  context = context_from.clone
43
47
 
44
48
  # replace each @snip tags
45
- each_snip_tags(code, context, base_tag) do |tag, line_no, snippet, new_context|
49
+ each_child_snippet(snippet, context, base_tag) do |tag, line_no, child_snippet, new_context|
46
50
  inserter.set_index line_no
47
51
  inserter.ignore
48
52
 
49
53
  visit(tag) if is_self(tag, context)
50
54
  next if is_visited(tag)
51
55
 
52
- insert_depended_snippets! inserter, snippet, new_context, tag
53
- insert_by_tag_and_context! inserter, snippet, new_context, tag
56
+ insert_depended_snippets! inserter, child_snippet, new_context, tag
57
+ insert_by_tag_and_context! inserter, child_snippet, new_context, tag
54
58
  end
55
59
 
56
60
  inserter.set_index_last
@@ -58,12 +62,10 @@ module SocialSnippet
58
62
  end
59
63
 
60
64
  # Insert snippet by tag and context
61
- def insert_by_tag_and_context!(inserter, snippet_str, context, tag)
62
- if snippet_str.is_a?(Snippet)
63
- src = insert_func(snippet_str.lines, context, tag)
64
- else
65
- src = insert_func(snippet_str.split($/), context, tag)
66
- end
65
+ def insert_by_tag_and_context!(inserter, snippet, context, tag)
66
+ raise "must be passed snippet" unless snippet.is_a?(Snippet)
67
+
68
+ src = insert_func(snippet, context, tag)
67
69
 
68
70
  options[:margin_top].times { inserter.insert "" }
69
71
  inserter.insert tag.to_snippet_tag # @snip -> @snippet
@@ -75,7 +77,9 @@ module SocialSnippet
75
77
 
76
78
  # Insert depended snippet
77
79
  def insert_depended_snippets!(inserter, snippet, context, tag)
78
- dep_tags = deps_resolver.find(snippet.lines, context, tag)
80
+ raise "must be passed snippet" unless snippet.is_a?(Snippet)
81
+
82
+ dep_tags = deps_resolver.find(snippet, context, tag)
79
83
  dep_tags = sort_dep_tags_by_dep(dep_tags)
80
84
 
81
85
  dep_tags.each do |tag_info|
@@ -85,8 +89,8 @@ module SocialSnippet
85
89
  visit(tag) if is_self(tag, context)
86
90
  next if is_visited(sub_t)
87
91
 
88
- sub_snippet = social_snippet.repo_manager.get_snippet(sub_c, sub_t)
89
- insert_by_tag_and_context! inserter, sub_snippet, sub_c, sub_t
92
+ next_snippet = social_snippet.repo_manager.get_snippet(sub_c, sub_t)
93
+ insert_by_tag_and_context! inserter, next_snippet, sub_c, sub_t
90
94
  end
91
95
  end
92
96
 
@@ -1,14 +1,75 @@
1
1
  class SocialSnippet::Snippet
2
2
 
3
- attr_reader :path
3
+ attr_reader :filepath
4
4
  attr_reader :code
5
- attr_reader :lines
6
5
 
7
6
  # Constructor
8
7
  def initialize(snippet_path)
9
- @path = snippet_path
10
- @code = ::File.read(path)
11
- @lines = code.split($/)
8
+ @filepath = snippet_path
9
+ read_file unless filepath.nil?
10
+ end
11
+
12
+ def read_file
13
+ @code = ::File.read(filepath).split($/)
14
+ end
15
+
16
+ def read_text(s)
17
+ raise "must be passed string" unless s.is_a?(String)
18
+ @code = s.split($/)
19
+ end
20
+
21
+ def lines
22
+ @lines ||= new_lines
23
+ end
24
+
25
+ def snippet_tags
26
+ ::SocialSnippet::TagParser.find_snippet_tags lines
27
+ end
28
+
29
+ def snip_tags
30
+ ::SocialSnippet::TagParser.find_snip_tags lines
31
+ end
32
+
33
+ class << self
34
+
35
+ # Create instance by text
36
+ def new_text(s)
37
+ raise "must be passed string" unless s.is_a?(String)
38
+ snippet = self.new(nil)
39
+ snippet.read_text s
40
+ snippet
41
+ end
42
+
43
+ end
44
+
45
+ private
46
+
47
+ # Return filtered and styled lines
48
+ def new_lines
49
+ tmp = code.clone
50
+ tmp = filter(tmp)
51
+ tmp
52
+ end
53
+
54
+ # @param lines [Array<String>]
55
+ def filter(lines)
56
+ lines = cut_filter(lines)
57
+ lines
58
+ end
59
+
60
+ def cut_filter(lines)
61
+ cut_level = 0
62
+ lines.select do |line|
63
+ if ::SocialSnippet::Tag.is_begin_cut?(line)
64
+ cut_level += 1
65
+ false
66
+ elsif ::SocialSnippet::Tag.is_end_cut?(line)
67
+ cut_level -= 1
68
+ false
69
+ else
70
+ cut_level === 0
71
+ end
72
+ end
12
73
  end
13
74
 
14
75
  end
@@ -1,6 +1,6 @@
1
1
  module SocialSnippet
2
2
 
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
 
5
5
  module Version
6
6
 
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = SocialSnippet::VERSION
9
9
  spec.authors = ["Hiroyuki Sano"]
10
10
  spec.email = ["sh19910711@gmail.com"]
11
- spec.summary = %q{Share and use snippet libraries for the online judges}
11
+ spec.summary = %q{A snippet manager to share and use snippet library for the online judges; Google Code Jam, Codeforces, ACM/ICPC and etc...}
12
12
  spec.homepage = "https://github.com/social-snippet/social-snippet"
13
13
  spec.license = "MIT"
14
14
 
@@ -9,7 +9,7 @@ describe SocialSnippet::Resolvers::InsertResolver do
9
9
  t.repo
10
10
  end
11
11
  allow(fake_social_snippet.repo_manager).to receive(:get_snippet) do |c, t|
12
- t.repo
12
+ ::SocialSnippet::Snippet.new_text(t.repo)
13
13
  end
14
14
  end
15
15
 
@@ -19,11 +19,11 @@ describe SocialSnippet::Resolvers::InsertResolver do
19
19
 
20
20
  before do
21
21
  allow(fake_social_snippet.repo_manager).to receive(:get_snippet) do |c, t|
22
- [
22
+ ::SocialSnippet::Snippet.new_text([
23
23
  "def foo",
24
24
  " 42",
25
25
  "end",
26
- ].join($/)
26
+ ].join $/)
27
27
  end
28
28
  end # prepare snippet
29
29
 
data/test/core_test.rb CHANGED
@@ -2086,6 +2086,85 @@ describe SocialSnippet::Core do
2086
2086
 
2087
2087
  end # more duplicate cases
2088
2088
 
2089
+ context "filters" do
2090
+
2091
+ context "range cut (simple)" do
2092
+
2093
+ before do
2094
+ FileUtils.touch "./file1.cpp"
2095
+ File.write "./file1.cpp", [
2096
+ "// @begin_cut",
2097
+ "#include <path/to/lib>",
2098
+ "// @end_cut",
2099
+ "void func() {",
2100
+ "}",
2101
+ ].join($/)
2102
+ end
2103
+
2104
+ let(:input) do
2105
+ [
2106
+ "// @snip <./file1.cpp>",
2107
+ ].join($/)
2108
+ end
2109
+
2110
+ let(:output) do
2111
+ [
2112
+ "// @snippet <file1.cpp>",
2113
+ "void func() {",
2114
+ "}",
2115
+ ].join($/)
2116
+ end
2117
+
2118
+ subject { fake_social_snippet.api.insert_snippet(input) }
2119
+ it { should eq output }
2120
+
2121
+ end
2122
+
2123
+ context "range cut (nested snippet)" do
2124
+
2125
+ before do
2126
+ FileUtils.touch "./file1.cpp"
2127
+ File.write "./file1.cpp", [
2128
+ "// @begin_cut",
2129
+ "#include <path/to/lib>",
2130
+ "// @end_cut",
2131
+ "// @snip <./file2.cpp>",
2132
+ "void func1() {",
2133
+ "}",
2134
+ ].join($/)
2135
+ File.write "./file2.cpp", [
2136
+ "// @begin_cut",
2137
+ "#include <path/to/lib>",
2138
+ "// @end_cut",
2139
+ "void func2() {",
2140
+ "}",
2141
+ ].join($/)
2142
+ end
2143
+
2144
+ let(:input) do
2145
+ [
2146
+ "// @snip <./file1.cpp>",
2147
+ ].join($/)
2148
+ end
2149
+
2150
+ let(:output) do
2151
+ [
2152
+ "// @snippet <file2.cpp>",
2153
+ "void func2() {",
2154
+ "}",
2155
+ "// @snippet <file1.cpp>",
2156
+ "void func1() {",
2157
+ "}",
2158
+ ].join($/)
2159
+ end
2160
+
2161
+ subject { fake_social_snippet.api.insert_snippet(input) }
2162
+ it { should eq output }
2163
+
2164
+ end
2165
+
2166
+ end # filters
2167
+
2089
2168
  end # insert_snippet
2090
2169
 
2091
2170
  end # SocialSnippet::Core
@@ -1,10 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
- module SocialSnippet::Resolvers
3
+ module SocialSnippet
4
4
 
5
- describe BaseResolver do
6
-
7
- let(:resolver) { BaseResolver.new(fake_social_snippet) }
5
+ describe Snippet do
8
6
 
9
7
  describe "#filter()" do
10
8
 
@@ -31,13 +29,16 @@ module SocialSnippet::Resolvers
31
29
  ]
32
30
  end
33
31
 
34
- subject { resolver.filter(input) }
32
+ let(:snippet) { Snippet.new nil }
33
+ before { snippet.read_text input.join($/) }
34
+ subject { snippet.lines }
35
35
  it { should eq expected }
36
36
 
37
37
  end
38
38
 
39
39
  end
40
40
 
41
- end # BaseResolver
41
+ end # Snippet
42
+
43
+ end # SocialSnippet
42
44
 
43
- end # SocialSnippet::Resolvers
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.6
4
+ version: 0.0.7
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-01-27 00:00:00.000000000 Z
11
+ date: 2015-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: version_sorter
@@ -295,7 +295,6 @@ files:
295
295
  - spec/lib/tag_spec.rb
296
296
  - spec/spec_helper.rb
297
297
  - test/base_repository_test.rb
298
- - test/base_resolver_test.rb
299
298
  - test/command_test.rb
300
299
  - test/config_test.rb
301
300
  - test/context_test.rb
@@ -303,6 +302,7 @@ files:
303
302
  - test/git_repository_test.rb
304
303
  - test/install_command_test.rb
305
304
  - test/repository_manager_test.rb
305
+ - test/snippet_test.rb
306
306
  - test/tag_parser_test.rb
307
307
  - test/tag_test.rb
308
308
  - test/version_test.rb
@@ -329,7 +329,8 @@ rubyforge_project:
329
329
  rubygems_version: 2.4.5
330
330
  signing_key:
331
331
  specification_version: 4
332
- summary: Share and use snippet libraries for the online judges
332
+ summary: A snippet manager to share and use snippet library for the online judges;
333
+ Google Code Jam, Codeforces, ACM/ICPC and etc...
333
334
  test_files:
334
335
  - spec/helpers/codeclimate_helper.rb
335
336
  - spec/helpers/fakefs_helper.rb
@@ -355,7 +356,6 @@ test_files:
355
356
  - spec/lib/tag_spec.rb
356
357
  - spec/spec_helper.rb
357
358
  - test/base_repository_test.rb
358
- - test/base_resolver_test.rb
359
359
  - test/command_test.rb
360
360
  - test/config_test.rb
361
361
  - test/context_test.rb
@@ -363,6 +363,7 @@ test_files:
363
363
  - test/git_repository_test.rb
364
364
  - test/install_command_test.rb
365
365
  - test/repository_manager_test.rb
366
+ - test/snippet_test.rb
366
367
  - test/tag_parser_test.rb
367
368
  - test/tag_test.rb
368
369
  - test/version_test.rb