polite_text 0.1.3 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +11 -45
- data/lib/polite_text/base.rb +8 -9
- data/lib/polite_text/text_cleaner.rb +2 -3
- data/lib/polite_text/text_scanner.rb +2 -3
- data/lib/polite_text/version.rb +1 -1
- data/lib/polite_text.rb +4 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 574591328155e7985751a85bfdb2610376f9c976b4d0a589c1d735ec9bfb9816
|
4
|
+
data.tar.gz: b0f61d51a49c6882dbd666a495b20b2e0f12a5a2ae716c20350b2739e3910afe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d678889edb60886cfb83f6a186335e6371238cd45669eec435f1cf0f52f981cd074cfa04cbb8181243314edb68c371b0e9ca45805d9184719a1b8f1518839064
|
7
|
+
data.tar.gz: 705cbb3d6f397a4292aa858fc734d3dea33d7fa4c6989d4d848b3968948b2f9b57ca367c076a00968eafb9d931b58ab823b24e3c0b2421594ee4c5f13ceeba2f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -5,9 +5,9 @@
|
|
5
5
|
|
6
6
|
PoliteText is making your users' input polite 👀
|
7
7
|
|
8
|
-
👉 Replace the swear words or execute callbacks if the input is not polite
|
8
|
+
👉 Replace the swear words or execute callbacks if the input is not polite.
|
9
9
|
|
10
|
-
## Installation
|
10
|
+
## Installation
|
11
11
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
13
|
|
@@ -23,7 +23,7 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
$ gem install polite_text
|
25
25
|
|
26
|
-
## Usage
|
26
|
+
## Usage
|
27
27
|
|
28
28
|
### Swear words 🔞
|
29
29
|
PoliteText has a default list of forbidden words, this list includes the most common swear words used on the web. You can acces the list of swear words [here](https://github.com/OpenGems/polite_text/blob/master/lib/locales/en.yml).
|
@@ -35,6 +35,7 @@ include PoliteText
|
|
35
35
|
str = "This gem is a fucking big shit but let's try it"
|
36
36
|
|
37
37
|
PoliteText.be_polite!(str)
|
38
|
+
|
38
39
|
=> "This gem is a *** big *** but let's try it"
|
39
40
|
```
|
40
41
|
|
@@ -45,6 +46,7 @@ include PoliteText
|
|
45
46
|
str = "This gem is a fucking big shit but let's try it"
|
46
47
|
|
47
48
|
PoliteText.is_polite?(str)
|
49
|
+
|
48
50
|
=> false
|
49
51
|
```
|
50
52
|
|
@@ -62,7 +64,7 @@ PoliteText.is_polite?(str)
|
|
62
64
|
#
|
63
65
|
|
64
66
|
class Article < ApplicationRecord
|
65
|
-
include
|
67
|
+
include Politetext
|
66
68
|
|
67
69
|
# Callbacks
|
68
70
|
before_save :make_text_polite
|
@@ -74,53 +76,17 @@ class Article < ApplicationRecord
|
|
74
76
|
end
|
75
77
|
```
|
76
78
|
|
77
|
-
## Usage with custom swear words list 🌞
|
78
|
-
|
79
|
-
### Custom swear words list 🔞
|
80
|
-
Create a custom list in a **YAML** file following this format :
|
81
|
-
```
|
82
|
-
# my_custom_swear_words.yml
|
83
|
-
|
84
|
-
swear_words:
|
85
|
-
- gem
|
86
|
-
- big
|
87
|
-
- but
|
88
|
-
```
|
89
|
-
|
90
|
-
Place it where you want in your app, we recommend here : `./lib/polite_text/my_custom_swear_words.yml`
|
91
|
-
|
92
|
-
👉 You just need to pass the path to this file as a second argument.
|
93
|
-
|
94
|
-
### Example for an Article model
|
95
|
-
|
96
|
-
### Remove or detect swear words 🤬 🙅♂️ 🙅♀️
|
97
|
-
```
|
98
|
-
# Inside a random_model.rb
|
99
|
-
|
100
|
-
include PoliteText
|
101
|
-
|
102
|
-
str = "This gem is a fucking big shit but let's try it"
|
103
|
-
|
104
|
-
current_dir = __dir__
|
105
|
-
custom_path = "#{current_dir}/../lib/polite_text/fr.yml"
|
106
|
-
|
107
|
-
PoliteText.be_polite!(str, custom_path)
|
108
|
-
=> "This *** is a fucking *** shit *** let's try it"
|
109
|
-
|
110
|
-
PoliteText.is_polite?(str, custom_path)
|
111
|
-
=> false
|
112
|
-
```
|
113
|
-
|
114
|
-
|
115
79
|
## Contributing
|
116
80
|
|
117
81
|
Bug reports and pull requests are welcome on GitHub at https://github.com/OpenGems/polite_text.
|
118
82
|
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
|
119
83
|
|
120
84
|
### Todo 💪
|
121
|
-
- [ ]
|
122
|
-
- [ ]
|
123
|
-
- [ ]
|
85
|
+
- [ ] Allow users to add their own swear words list.
|
86
|
+
- [ ] Allow users to remove swear words from the default list.
|
87
|
+
- [ ] Manage locales with I18N.
|
88
|
+
- [ ] Add swear words from multiple languages.
|
89
|
+
- [ ] Improve swear words list and keeping it short.
|
124
90
|
|
125
91
|
## License
|
126
92
|
|
data/lib/polite_text/base.rb
CHANGED
@@ -2,13 +2,16 @@ require 'yaml'
|
|
2
2
|
|
3
3
|
module PoliteText
|
4
4
|
class Base
|
5
|
-
def swear_words
|
6
|
-
@swear_words ||= /\b(#{Regexp.union(swear_words_list
|
5
|
+
def swear_words
|
6
|
+
@swear_words ||= /\b(#{Regexp.union(swear_words_list).source})\b/
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
9
|
+
def swear_words_load_path
|
10
|
+
"#{__dir__}/../locales/#{locale}.yml"
|
11
|
+
end
|
12
|
+
|
13
|
+
def swear_words_list
|
14
|
+
YAML.load_file(swear_words_load_path)['swear_words']
|
12
15
|
end
|
13
16
|
|
14
17
|
private
|
@@ -17,9 +20,5 @@ module PoliteText
|
|
17
20
|
def locale
|
18
21
|
@locale ||= 'en'
|
19
22
|
end
|
20
|
-
|
21
|
-
def swear_words_load_path
|
22
|
-
"#{__dir__}/../locales/#{locale}.yml"
|
23
|
-
end
|
24
23
|
end
|
25
24
|
end
|
@@ -2,15 +2,14 @@ module PoliteText
|
|
2
2
|
class TextCleaner < Base
|
3
3
|
attr_reader :text
|
4
4
|
|
5
|
-
def initialize(text
|
5
|
+
def initialize(text)
|
6
6
|
raise ArgumentError.new('The text can not be nil') if text.nil?
|
7
7
|
|
8
8
|
@text = text.to_s
|
9
|
-
@custom_swear_words_path = custom_swear_words_path
|
10
9
|
end
|
11
10
|
|
12
11
|
def clean!
|
13
|
-
text.gsub!(swear_words
|
12
|
+
text.gsub!(swear_words, '***')
|
14
13
|
end
|
15
14
|
end
|
16
15
|
end
|
@@ -2,15 +2,14 @@ module PoliteText
|
|
2
2
|
class TextScanner < Base
|
3
3
|
attr_reader :text
|
4
4
|
|
5
|
-
def initialize(text
|
5
|
+
def initialize(text)
|
6
6
|
raise ArgumentError.new('The text can not be nil') if text.nil?
|
7
7
|
|
8
8
|
@text = text.to_s
|
9
|
-
@custom_swear_words_path = custom_swear_words_path
|
10
9
|
end
|
11
10
|
|
12
11
|
def match_swear_word?
|
13
|
-
!@text.match?(swear_words
|
12
|
+
!@text.match?(swear_words)
|
14
13
|
end
|
15
14
|
end
|
16
15
|
end
|
data/lib/polite_text/version.rb
CHANGED
data/lib/polite_text.rb
CHANGED
@@ -4,12 +4,12 @@ require 'polite_text/text_cleaner'
|
|
4
4
|
|
5
5
|
module PoliteText
|
6
6
|
class << self
|
7
|
-
def be_polite!(text
|
8
|
-
PoliteText::TextCleaner.new(text
|
7
|
+
def be_polite!(text)
|
8
|
+
PoliteText::TextCleaner.new(text).clean!
|
9
9
|
end
|
10
10
|
|
11
|
-
def is_polite?(text
|
12
|
-
PoliteText::TextScanner.new(text
|
11
|
+
def is_polite?(text)
|
12
|
+
PoliteText::TextScanner.new(text).match_swear_word?
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|