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 +4 -4
- data/config/initializers/active_record_extensions.rb +3 -0
- data/lib/text_hyphen_rails/active_record_extension.rb +26 -20
- data/lib/text_hyphen_rails/html_hyphenator.rb +26 -0
- data/lib/text_hyphen_rails/hyphenator.rb +20 -0
- data/lib/text_hyphen_rails/text_hyphenator.rb +15 -0
- data/lib/text_hyphen_rails/version.rb +1 -1
- data/lib/text_hyphen_rails.rb +1 -1
- metadata +5 -9
- data/lib/tasks/text_hyphen_rails_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c6a77398d5a3b09a1b3742cf1358fa7bcb408e7
|
4
|
+
data.tar.gz: 06873e4f1d3e7c34820b457cd83ce4a085fff799
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 232b1ab239d085f78f1d05ed05357b1b1ce7d3f31617910da0b9fb1f85c36c1aa36389ba6af78b4269ba7c33f96b7a70e8964da026b62f3b410f90232f037268
|
7
|
+
data.tar.gz: b2f0954b578f54dbaee87c2afe9cf4895a2152dd9bc0023b5473659d27d6c51cfded1c376c7ada686363a2927e42b3917ece248d9b53f95a0519f449a756844c
|
@@ -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
|
-
|
15
|
-
self.send(:define_method, thr_meth_name(att,
|
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
|
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
|
-
|
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
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
data/lib/text_hyphen_rails.rb
CHANGED
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.
|
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-
|
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:
|