slacken 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -0
  3. data/README.md +5 -3
  4. data/{sample → examples}/out.txt +0 -0
  5. data/{sample → examples}/source.html +0 -0
  6. data/lib/slacken/document_component.rb +23 -36
  7. data/lib/slacken/dom_container.rb +1 -1
  8. data/lib/slacken/filter.rb +27 -0
  9. data/lib/slacken/filters.rb +19 -0
  10. data/lib/slacken/filters/elim_blanks.rb +41 -0
  11. data/lib/slacken/filters/elim_invalid_links.rb +33 -0
  12. data/lib/slacken/filters/elim_line_breaks.rb +36 -0
  13. data/lib/slacken/filters/extract_img_alt.rb +18 -0
  14. data/lib/slacken/filters/group_indent.rb +28 -0
  15. data/lib/slacken/filters/group_inlines.rb +31 -0
  16. data/lib/slacken/filters/sanitize_headline.rb +46 -0
  17. data/lib/slacken/filters/sanitize_link.rb +48 -0
  18. data/lib/slacken/filters/sanitize_list.rb +57 -0
  19. data/lib/slacken/filters/sanitize_table.rb +44 -0
  20. data/lib/slacken/filters/stringfy_checkbox.rb +26 -0
  21. data/lib/slacken/filters/stringfy_emoji.rb +26 -0
  22. data/lib/slacken/node_type.rb +2 -1
  23. data/lib/slacken/render_element.rb +2 -0
  24. data/lib/slacken/rendering.rb +0 -1
  25. data/lib/slacken/version.rb +1 -1
  26. data/scripts/update_markup_fixture.rb +3 -3
  27. data/spec/slacken/document_component_spec.rb +6 -8
  28. data/spec/slacken/filters/elim_blanks_spec.rb +32 -0
  29. data/spec/slacken/filters/elim_invalid_links_spec.rb +47 -0
  30. data/spec/slacken/filters/elim_line_breaks_spec.rb +39 -0
  31. data/spec/slacken/filters/group_indent_spec.rb +35 -0
  32. data/spec/slacken/filters/group_inlines_spec.rb +31 -0
  33. data/spec/slacken/filters/sanitize_headline_spec.rb +29 -0
  34. data/spec/slacken/filters/sanitize_link_spec.rb +23 -0
  35. data/spec/slacken/filters/sanitize_list_spec.rb +36 -0
  36. data/spec/slacken/filters/sanitize_table_spec.rb +39 -0
  37. data/spec/slacken_spec.rb +3 -0
  38. data/spec/spec_helper.rb +1 -2
  39. metadata +37 -26
  40. data/lib/slacken/document_component/elim_blanks.rb +0 -36
  41. data/lib/slacken/document_component/elim_invalid_links.rb +0 -26
  42. data/lib/slacken/document_component/elim_line_breaks.rb +0 -27
  43. data/lib/slacken/document_component/extract_img_alt.rb +0 -12
  44. data/lib/slacken/document_component/group_indent.rb +0 -27
  45. data/lib/slacken/document_component/group_inlines.rb +0 -26
  46. data/lib/slacken/document_component/sanitize_link_containers.rb +0 -40
  47. data/lib/slacken/document_component/sanitize_special_tag_containers.rb +0 -102
  48. data/lib/slacken/document_component/stringfy_checkbox.rb +0 -20
  49. data/lib/slacken/document_component/stringfy_emoji.rb +0 -20
  50. data/spec/slacken/document_component/elim_blanks_spec.rb +0 -34
  51. data/spec/slacken/document_component/elim_invalid_links_spec.rb +0 -49
  52. data/spec/slacken/document_component/elim_line_breaks_spec.rb +0 -41
  53. data/spec/slacken/document_component/group_indent_spec.rb +0 -37
  54. data/spec/slacken/document_component/group_inlines_spec.rb +0 -33
  55. data/spec/slacken/document_component/sanitize_special_tag_containers_spec.rb +0 -64
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Slacken::Filters::SanitizeLink, dsl: true do
4
+ describe '#valid?' do
5
+ subject { described_class.new.valid?(component) }
6
+
7
+ context 'when a image occurs in a link' do
8
+ let(:component) do
9
+ c(:a, c(:img))
10
+ end
11
+
12
+ it { is_expected.to be_falsey }
13
+ end
14
+
15
+ context 'when a strong occurs in a link' do
16
+ let(:component) do
17
+ c(:a, c(:strong, text('content')))
18
+ end
19
+
20
+ it { is_expected.to be_truthy }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,36 @@
1
+
2
+ require 'spec_helper'
3
+
4
+ describe Slacken::Filters::SanitizeList, dsl: true do
5
+ describe '#valid?' do
6
+ subject { described_class.new.valid?(component) }
7
+
8
+ context 'when a header occurs in a list' do
9
+ let(:component) do
10
+ c(:ul,
11
+ c(:li,
12
+ text('header'),
13
+ c(:indent,
14
+ c(:h1, text('hoge')))))
15
+ end
16
+
17
+ it { is_expected.to be_falsey }
18
+ end
19
+
20
+ context 'when a list occurs in another list' do
21
+ let(:component) do
22
+ c(:ul,
23
+ c(:li,
24
+ text('header'),
25
+ c(:indent,
26
+ c(:dl,
27
+ c(:li,
28
+ text('header2'),
29
+ c(:indent,
30
+ text('fuga')))))))
31
+ end
32
+
33
+ it { is_expected.to be_truthy }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Slacken::Filters::SanitizeTable, dsl: true do
4
+ describe '#valid?' do
5
+ subject { described_class.new.valid?(component) }
6
+
7
+ context 'when a pre tag occurs in a table' do
8
+ let(:component) do
9
+ c(:table,
10
+ c(:thead,
11
+ c(:tr,
12
+ c(:th, text('Header1')),
13
+ c(:th, text('Header2')))),
14
+ c(:tbody,
15
+ c(:tr,
16
+ c(:td, text('Content1')),
17
+ c(:td, c(:pre, (text('Content2')))))))
18
+ end
19
+
20
+ it { is_expected.to be_falsey }
21
+ end
22
+
23
+ context 'when only allowed tags occur in a table' do
24
+ let(:component) do
25
+ c(:table,
26
+ c(:thead,
27
+ c(:tr,
28
+ c(:th, text('Header1')),
29
+ c(:th, c(:span, c(:i, text('Header2')))))),
30
+ c(:tbody,
31
+ c(:tr,
32
+ c(:td, c(:code, text('Content1'))),
33
+ c(:td, c(:strong, text('Content2'))))))
34
+ end
35
+
36
+ it { is_expected.to be_truthy }
37
+ end
38
+ end
39
+ end
data/spec/slacken_spec.rb CHANGED
@@ -5,6 +5,9 @@ describe Slacken do
5
5
  subject { described_class.translate(source) }
6
6
  let(:source) { fixture('example.html') }
7
7
 
8
+ # This test checks whether the behavior of this translation engine is unexpectedly broken.
9
+ # If you change the behavior, you should run `scripts/update_markup_fixture.rb`
10
+ # to update fixture file.
8
11
  it 'translates as expected' do
9
12
  expect(subject).to eq(fixture('markup_text.txt'))
10
13
  end
data/spec/spec_helper.rb CHANGED
@@ -4,8 +4,7 @@ require 'slacken'
4
4
  require 'helpers/document_component_dsl'
5
5
 
6
6
  def fixture(name)
7
- @fixture_cache ||= {}
8
- @fixture_cache[name] ||= File.read(File.expand_path(name, File.expand_path('fixtures', __dir__)))
7
+ File.read(File.join(__dir__, 'fixtures', name))
9
8
  end
10
9
 
11
10
  RSpec.configure do |c|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slacken
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomoya Chiba
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-18 00:00:00.000000000 Z
11
+ date: 2015-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,43 +116,51 @@ extra_rdoc_files: []
116
116
  files:
117
117
  - ".gitignore"
118
118
  - ".rspec"
119
+ - ".travis.yml"
119
120
  - Gemfile
120
121
  - LICENSE.txt
121
122
  - README.md
122
123
  - Rakefile
124
+ - examples/out.txt
125
+ - examples/source.html
123
126
  - lib/slacken.rb
124
127
  - lib/slacken/document_component.rb
125
- - lib/slacken/document_component/elim_blanks.rb
126
- - lib/slacken/document_component/elim_invalid_links.rb
127
- - lib/slacken/document_component/elim_line_breaks.rb
128
- - lib/slacken/document_component/extract_img_alt.rb
129
- - lib/slacken/document_component/group_indent.rb
130
- - lib/slacken/document_component/group_inlines.rb
131
- - lib/slacken/document_component/sanitize_link_containers.rb
132
- - lib/slacken/document_component/sanitize_special_tag_containers.rb
133
- - lib/slacken/document_component/stringfy_checkbox.rb
134
- - lib/slacken/document_component/stringfy_emoji.rb
135
128
  - lib/slacken/dom_container.rb
129
+ - lib/slacken/filter.rb
130
+ - lib/slacken/filters.rb
131
+ - lib/slacken/filters/elim_blanks.rb
132
+ - lib/slacken/filters/elim_invalid_links.rb
133
+ - lib/slacken/filters/elim_line_breaks.rb
134
+ - lib/slacken/filters/extract_img_alt.rb
135
+ - lib/slacken/filters/group_indent.rb
136
+ - lib/slacken/filters/group_inlines.rb
137
+ - lib/slacken/filters/sanitize_headline.rb
138
+ - lib/slacken/filters/sanitize_link.rb
139
+ - lib/slacken/filters/sanitize_list.rb
140
+ - lib/slacken/filters/sanitize_table.rb
141
+ - lib/slacken/filters/stringfy_checkbox.rb
142
+ - lib/slacken/filters/stringfy_emoji.rb
136
143
  - lib/slacken/node_type.rb
137
144
  - lib/slacken/render_element.rb
138
145
  - lib/slacken/rendering.rb
139
146
  - lib/slacken/slack_url.rb
140
147
  - lib/slacken/table_element.rb
141
148
  - lib/slacken/version.rb
142
- - sample/out.txt
143
- - sample/source.html
144
149
  - scripts/update_markup_fixture.rb
145
150
  - slacken.gemspec
146
151
  - spec/fixtures/example.html
147
152
  - spec/fixtures/markup_text.txt
148
153
  - spec/helpers/document_component_dsl.rb
149
- - spec/slacken/document_component/elim_blanks_spec.rb
150
- - spec/slacken/document_component/elim_invalid_links_spec.rb
151
- - spec/slacken/document_component/elim_line_breaks_spec.rb
152
- - spec/slacken/document_component/group_indent_spec.rb
153
- - spec/slacken/document_component/group_inlines_spec.rb
154
- - spec/slacken/document_component/sanitize_special_tag_containers_spec.rb
155
154
  - spec/slacken/document_component_spec.rb
155
+ - spec/slacken/filters/elim_blanks_spec.rb
156
+ - spec/slacken/filters/elim_invalid_links_spec.rb
157
+ - spec/slacken/filters/elim_line_breaks_spec.rb
158
+ - spec/slacken/filters/group_indent_spec.rb
159
+ - spec/slacken/filters/group_inlines_spec.rb
160
+ - spec/slacken/filters/sanitize_headline_spec.rb
161
+ - spec/slacken/filters/sanitize_link_spec.rb
162
+ - spec/slacken/filters/sanitize_list_spec.rb
163
+ - spec/slacken/filters/sanitize_table_spec.rb
156
164
  - spec/slacken_spec.rb
157
165
  - spec/spec_helper.rb
158
166
  homepage: https://rubygems.org/gems/slacken
@@ -183,12 +191,15 @@ test_files:
183
191
  - spec/fixtures/example.html
184
192
  - spec/fixtures/markup_text.txt
185
193
  - spec/helpers/document_component_dsl.rb
186
- - spec/slacken/document_component/elim_blanks_spec.rb
187
- - spec/slacken/document_component/elim_invalid_links_spec.rb
188
- - spec/slacken/document_component/elim_line_breaks_spec.rb
189
- - spec/slacken/document_component/group_indent_spec.rb
190
- - spec/slacken/document_component/group_inlines_spec.rb
191
- - spec/slacken/document_component/sanitize_special_tag_containers_spec.rb
192
194
  - spec/slacken/document_component_spec.rb
195
+ - spec/slacken/filters/elim_blanks_spec.rb
196
+ - spec/slacken/filters/elim_invalid_links_spec.rb
197
+ - spec/slacken/filters/elim_line_breaks_spec.rb
198
+ - spec/slacken/filters/group_indent_spec.rb
199
+ - spec/slacken/filters/group_inlines_spec.rb
200
+ - spec/slacken/filters/sanitize_headline_spec.rb
201
+ - spec/slacken/filters/sanitize_link_spec.rb
202
+ - spec/slacken/filters/sanitize_list_spec.rb
203
+ - spec/slacken/filters/sanitize_table_spec.rb
193
204
  - spec/slacken_spec.rb
194
205
  - spec/spec_helper.rb
@@ -1,36 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module ElimBlanks
3
- # Private: Reject blank elements
4
- def elim_blanks
5
- case type.name
6
- when :pre
7
- self
8
- else
9
- derive(children.reject(&:blank?).map(&:elim_blanks))
10
- end
11
- end
12
-
13
- def blank?
14
- return @is_blank if !@is_blank.nil?
15
- @is_blank =
16
- case type.name
17
- when :pre, :ul, :li, :br, :hr, :img, :checkbox
18
- false
19
- when :text, :emoji
20
- content = attrs[:content]
21
- content.nil? || !content.match(/\A\s*\Z/).nil?
22
- else
23
- children.empty? || children.all?(&:blank?)
24
- end
25
- end
26
-
27
- def has_no_blanks?
28
- case type.name
29
- when :pre
30
- true
31
- else
32
- !blank? && children.all?(&:has_no_blanks?)
33
- end
34
- end
35
- end
36
- end
@@ -1,26 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module ElimInvalidLinks
3
- # Private: Eliminate internal links and blank links
4
- def elim_invalid_links
5
- if invalid_link?
6
- derive(children.map(&:elim_invalid_links), type: :span)
7
- else
8
- derive(children.map(&:elim_invalid_links))
9
- end
10
- end
11
-
12
- def invalid_link?
13
- if type.member_of?(:a)
14
- link = attrs[:href]
15
- link.nil? ||
16
- !link.match(%r{\Ahttps?://})
17
- else
18
- false
19
- end
20
- end
21
-
22
- def has_no_invalid_links?
23
- !invalid_link? && children.all?(&:has_no_invalid_links?)
24
- end
25
- end
26
- end
@@ -1,27 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module ElimLineBreaks
3
- # Private: Reject blank elements
4
- def elim_line_breaks
5
- case type.name
6
- when :text
7
- new_content = attrs[:content].gsub(/[\r\n]/, '')
8
- derive(children, attrs: attrs.merge(content: new_content))
9
- when :pre
10
- self
11
- else
12
- derive(children.map(&:elim_line_breaks))
13
- end
14
- end
15
-
16
- def has_no_line_breaks?
17
- case type.name
18
- when :text
19
- !attrs[:content].match(/[\r\n]/)
20
- when :pre
21
- true
22
- else
23
- children.all?(&:has_no_line_breaks?)
24
- end
25
- end
26
- end
27
- end
@@ -1,12 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module ExtractImgAlt
3
- def extract_img_alt
4
- if type.member_of?(:img)
5
- child = self.class.new(:text, [], content: attrs[:alt] || attrs[:src])
6
- derive([child])
7
- else
8
- derive(children.map(&:extract_img_alt))
9
- end
10
- end
11
- end
12
- end
@@ -1,27 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module GroupIndent
3
- # Private: Wrap contents to indent.
4
- # Childrens of li or dd tags are wrapped.
5
- def group_indent
6
- if type.member_of?(:li, :dd)
7
- head, *tails = children.map(&:group_indent)
8
- derive([head, self.class.new(:indent, tails)])
9
- else
10
- derive(children.map(&:group_indent))
11
- end
12
- end
13
-
14
- def indent_grouped?
15
- if type.member_of?(:li, :dd)
16
- head, tail = children
17
- if tail
18
- tail.type.member_of?(:indent) && tail.indent_grouped?
19
- else
20
- true
21
- end
22
- else
23
- children.all?(&:indent_grouped?)
24
- end
25
- end
26
- end
27
- end
@@ -1,26 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module GroupInlines
3
- # Private: Group inline elements and wrap them in a wrapper node.
4
- # Wrapper nodes are introduced to group inline nodes in a paragraph.
5
- def group_inlines
6
- if block?
7
- new_children = children.map(&:group_inlines).chunk(&:inline?).map do |is_inline, group|
8
- is_inline ? self.class.new(:wrapper, group) : group
9
- end.flatten
10
- derive(new_children)
11
- else
12
- derive(children.map(&:group_inlines))
13
- end
14
- end
15
-
16
- def inlines_grouped?
17
- if type.member_of?(:wrapper)
18
- true
19
- elsif inline?
20
- false
21
- else
22
- children.all?(&:inlines_grouped?)
23
- end
24
- end
25
- end
26
- end
@@ -1,40 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module SanitizeLinkContainers
3
- # Private: Sanitize not allowed tags in links.
4
- def sanitize_link_containers
5
- case type.name
6
- when :img, :a, :iframe
7
- derive(children.map(&:sanitize_in_link))
8
- else
9
- derive(children.map(&:sanitize_link_containers))
10
- end
11
- end
12
-
13
- def sanitize_in_link
14
- if type.allowed_in_link?
15
- derive(children.map(&:sanitize_in_link))
16
- else
17
- # No block tags are allowed.
18
- derive(children.map(&:sanitize_in_link), type: :span)
19
- end
20
- end
21
-
22
- def link_sanitized?
23
- case type.name
24
- when :img, :a, :iframe
25
- children.all?(&:list_containers_sanitized?)
26
- else
27
- children.all?(&:link_sanitized?)
28
- end
29
- end
30
-
31
- def link_container_sanitized?
32
- if type.allowed_in_link?
33
- children.all?(&:link_containers_sanitized?)
34
- else
35
- false
36
- end
37
- end
38
-
39
- end
40
- end
@@ -1,102 +0,0 @@
1
- class Slacken::DocumentComponent
2
- module SanitizeSpecialTagContainers
3
- # Private: Sanitize not allowed tags in list, headline, and table.
4
- def sanitize_special_tag_containers
5
- # NOTE: each special tag (list, headline, and table) is not allowed to occur
6
- # in another type special tags.
7
- case type.name
8
- when /h\d/
9
- derive(children.map(&:sanitize_headline))
10
- when :ul, :ol, :dl
11
- derive(children.map(&:sanitize_list))
12
- when :table
13
- derive(children.map(&:sanitize_table))
14
- else
15
- derive(children.map(&:sanitize_special_tag_containers))
16
- end
17
- end
18
-
19
- def sanitize_headline
20
- if type.allowed_in_headline?
21
- derive(children.map(&:sanitize_headline))
22
- else
23
- # No block tags are allowed.
24
- derive(children.map(&:sanitize_headline), type: :span)
25
- end
26
- end
27
-
28
- def sanitize_list
29
- if type.member_of?(:li, :dd)
30
- head, *tails = children
31
- derive([head.sanitize_list_item, *tails.map(&:sanitize_list)])
32
- elsif type.allowed_in_list?
33
- derive(children.map(&:sanitize_list))
34
- else
35
- derive(children.map(&:sanitize_list), type: block? ? :div : :span)
36
- end
37
- end
38
-
39
- def sanitize_list_item
40
- if type.allowed_as_list_item?
41
- derive(children.map(&:sanitize_list_item))
42
- else
43
- # No block tags are allowed.
44
- derive(children.map(&:sanitize_list_item), type: :span)
45
- end
46
- end
47
-
48
- def sanitize_table
49
- if type.allowed_in_table?
50
- derive(children.map(&:sanitize_table))
51
- else
52
- # No block tags are allowed.
53
- derive(children.map(&:sanitize_table), type: :span)
54
- end
55
- end
56
-
57
- def sanitized?
58
- case type.name
59
- when /h\d/
60
- children.all?(&:headline_sanitized?)
61
- when :ul, :ol, :dl
62
- children.all?(&:list_sanitized?)
63
- when :table
64
- children.all?(&:table_sanitized?)
65
- else
66
- children.all?(&:sanitized?)
67
- end
68
- end
69
-
70
- def headline_sanitized?
71
- if type.allowed_in_headline?
72
- children.all?(&:headline_sanitized?)
73
- else
74
- false
75
- end
76
- end
77
-
78
- def list_sanitized?
79
- if type.allowed_in_list?
80
- children.all?(&:list_sanitized?)
81
- else
82
- false
83
- end
84
- end
85
-
86
- def list_item_sanitized?
87
- if type.allowed_as_list_item?
88
- children.all?(&:list_item_sanitized?)
89
- else
90
- false
91
- end
92
- end
93
-
94
- def table_sanitized?
95
- if type.allowed_in_table?
96
- children.all?(&:table_sanitized?)
97
- else
98
- false
99
- end
100
- end
101
- end
102
- end