text_hyphen_rails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1459e70ff94558309abbf089155d06e620affa62
4
- data.tar.gz: 486dc59178274fd2c839ec091143a1cbb94c0fcf
3
+ metadata.gz: 9c6a77398d5a3b09a1b3742cf1358fa7bcb408e7
4
+ data.tar.gz: 06873e4f1d3e7c34820b457cd83ce4a085fff799
5
5
  SHA512:
6
- metadata.gz: 0c78dc4d8f3dae82236eb8f6ae1ff86648526eccc96ac1e6f901be1d67e4b0f2ce2691cd187a62ca8b1d3836a71d70f6493f7db13565de9015a4649b86a31f67
7
- data.tar.gz: 00def6da1fc907bb478e9e8d7746dfe470aafc7947e4144dd19e95a4ce63bf13b04bccb245c673300a3f8c3906692e44618e8019b2efdb17a979556c59d2cc70
6
+ metadata.gz: 232b1ab239d085f78f1d05ed05357b1b1ce7d3f31617910da0b9fb1f85c36c1aa36389ba6af78b4269ba7c33f96b7a70e8964da026b62f3b410f90232f037268
7
+ data.tar.gz: b2f0954b578f54dbaee87c2afe9cf4895a2152dd9bc0023b5473659d27d6c51cfded1c376c7ada686363a2927e42b3917ece248d9b53f95a0519f449a756844c
@@ -1 +1,4 @@
1
1
  require "text_hyphen_rails/active_record_extension"
2
+ require "text_hyphen_rails/hyphenator"
3
+ require "text_hyphen_rails/html_hyphenator"
4
+ require "text_hyphen_rails/text_hyphenator"
@@ -9,20 +9,28 @@ module TextHyphenRails
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  module ClassMethods
12
+
12
13
  def text_hyphen(*args, **opts)
14
+ thr_create_methods(TextHyphenator, args, opts)
15
+ end
16
+
17
+ def html_hyphen(*args, **opts)
18
+ thr_create_methods(HtmlHyphenator, args, opts)
19
+ end
20
+
21
+ private
22
+
23
+ def thr_create_methods(h_class, args, opts)
13
24
  args.each do |att|
14
- opts = thr_opts att, opts
15
- self.send(:define_method, thr_meth_name(att, opts)) do
25
+ g_opts = thr_g_opts att, opts
26
+ self.send(:define_method, thr_meth_name(att, g_opts)) do
16
27
  str = read_attribute att
17
-
18
- str.gsub opts[:word_regex] do |tok|
19
- thr_hyphenator(opts).visualize tok, opts[:hyphen]
20
- end
28
+ opts = thr_opts g_opts
29
+ h_class.new(str, opts).result
21
30
  end
22
31
  end
23
- end
24
32
 
25
- private
33
+ end
26
34
 
27
35
  def thr_meth_name(att, opts)
28
36
  if opts[:replace_meth]
@@ -32,23 +40,21 @@ module TextHyphenRails
32
40
  end
33
41
  end
34
42
 
35
- def thr_opts(meth, opts)
43
+ def thr_g_opts(meth, opts)
36
44
  raise UnknownOptionError if (opts.keys - TextHyphenRails.settings.keys).size > 0
37
45
  TextHyphenRails.settings.merge opts
38
46
  end
39
47
  end
40
48
 
41
- private
42
-
43
- def thr_hyphenator opts
44
- lang = if opts[:lang_att]
45
- self.send(opts[:lang_att])
46
- else
47
- opts[:lang]
48
- end
49
- ::Text::Hyphen.new(language: lang,
50
- left: opts[:left],
51
- right: opts[:right])
49
+ def thr_opts opts
50
+ if opts[:lang_att]
51
+ lang = self.send(opts[:lang_att])
52
+ nopts = opts.dup
53
+ nopts[:lang] = lang
54
+ nopts
55
+ else
56
+ opts
57
+ end
52
58
  end
53
59
  end
54
60
  end
@@ -0,0 +1,26 @@
1
+ module TextHyphenRails
2
+ class HtmlHyphenator < Hyphenator
3
+ def initialize text, options
4
+ @doc = Nokogiri::HTML.fragment text
5
+ @opts = options
6
+ end
7
+
8
+ def result
9
+ @doc.traverse do |n|
10
+ if n.is_a? Nokogiri::XML::Text
11
+ n.content = n.content.gsub regex do |tok|
12
+ hyphenator.visualize(tok, @opts[:hyphen])
13
+ end
14
+ end
15
+ end
16
+ @doc.to_s
17
+ end
18
+
19
+ private
20
+
21
+ def text_nodes
22
+ # broken
23
+ @doc.xpath('//text()')
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ module TextHyphenRails
2
+ class Hyphenator
3
+
4
+ def initialize
5
+ raise 'don\'t use this class directly'
6
+ end
7
+
8
+ private
9
+
10
+ def regex
11
+ @rx ||= /[[:alpha:]]{#{@opts[:min_word_length]},}/m
12
+ end
13
+
14
+ def hyphenator
15
+ @hyph ||= ::Text::Hyphen.new(language: @opts[:lang],
16
+ left: @opts[:left],
17
+ right: @opts[:right])
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module TextHyphenRails
2
+ class TextHyphenator < Hyphenator
3
+
4
+ def initialize text, options
5
+ @text = text
6
+ @opts = options
7
+ end
8
+
9
+ def result
10
+ @text.gsub regex do |tok|
11
+ hyphenator.visualize(tok, @opts[:hyphen])
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module TextHyphenRails
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -5,7 +5,7 @@ module TextHyphenRails
5
5
  @settings = { hyphen: "\u00ad",
6
6
  lang: :en_uk,
7
7
  lang_att: nil,
8
- word_regex: /[[:alpha:]]{4,}/m,
8
+ min_word_length: 4,
9
9
  left: 1,
10
10
  right: 1,
11
11
  prefix: nil,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: text_hyphen_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thelonius Kort
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-29 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -17,9 +17,6 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
- - - "~>"
21
- - !ruby/object:Gem::Version
22
- version: '4'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +24,6 @@ dependencies:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '3.1'
30
- - - "~>"
31
- - !ruby/object:Gem::Version
32
- version: '4'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: text-hyphen
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -136,10 +130,12 @@ extra_rdoc_files: []
136
130
  files:
137
131
  - config/initializers/active_record_extensions.rb
138
132
  - config/routes.rb
139
- - lib/tasks/text_hyphen_rails_tasks.rake
140
133
  - lib/text_hyphen_rails.rb
141
134
  - lib/text_hyphen_rails/active_record_extension.rb
142
135
  - lib/text_hyphen_rails/engine.rb
136
+ - lib/text_hyphen_rails/html_hyphenator.rb
137
+ - lib/text_hyphen_rails/hyphenator.rb
138
+ - lib/text_hyphen_rails/text_hyphenator.rb
143
139
  - lib/text_hyphen_rails/version.rb
144
140
  homepage: https://github.com/tnt/text_hyphen_rails
145
141
  licenses:
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :text_hyphen_rails do
3
- # # Task goes here
4
- # end