Skip to content

Commit e37bec9

Browse files
authored
Merge pull request #10 from claytron/formatting
Add text transformation options
2 parents 8ac46e2 + 192ac74 commit e37bec9

16 files changed

+6300
-4185
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changes
22

3+
## 1.1.0 (2020-01-24)
4+
5+
- Add ability to transform tweet text with find / replace
6+
- Get full text of tweets, not the default abbreviated version
7+
- Switch to better word wrapping library
8+
39
## 1.0.3 (2020-01-23)
410

511
- Pin Thor version to avoid issue with `options` override

Gemfile.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
PATH
22
remote: .
33
specs:
4-
augury (1.0.3)
5-
facets (~> 3.0)
4+
augury (1.1.0)
65
thor (~> 1.0.0)
76
twitter (~> 7.0)
7+
word_wrap (~> 1.0)
88

99
GEM
1010
remote: https://rubygems.org/
@@ -26,7 +26,6 @@ GEM
2626
unf (>= 0.0.5, < 1.0.0)
2727
e2mmap (0.1.0)
2828
equalizer (0.0.11)
29-
facets (3.1.0)
3029
ffi (1.14.2)
3130
ffi-compiler (1.0.1)
3231
ffi (>= 1.0.0)
@@ -154,6 +153,7 @@ GEM
154153
addressable (>= 2.3.6)
155154
crack (>= 0.3.2)
156155
hashdiff (>= 0.4.0, < 2.0.0)
156+
word_wrap (1.0.0)
157157
yard (0.9.26)
158158

159159
PLATFORMS

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ Option | Description | Default
107107
`retweets` | Include retweets. | `false`
108108
`replies` | Include replies. | `false`
109109
`links` | Include tweets with links in them. | `false`
110+
`remove_links` | Remove all links from tweets before any other `transforms`. If set, this infers `links` is `true`. | `false`
110111
`attribution` | Add an author attribution to each fortune. | `false`
112+
`apply_transforms` | Apply the global and feed specific transforms. | `false`
113+
`transforms` | Transformations to apply to specific user feeds. See [Transforms](#transforms) below. |
114+
`global-transforms` | Transformations to apply to all feeds. Applied after `transforms` See [Transforms](#transforms) below. |
111115

112116
### Twitter Setup
113117

@@ -126,6 +130,65 @@ twitter:
126130
access_token_secret: YOUR_ACCESS_TOKEN_SECRET
127131
```
128132

133+
### Transforms
134+
135+
Global substitutions can be made using regular expressions.
136+
Each transform is a pattern and replacement.
137+
For instance, if you wanted to replace all `Hello`'s with `Hello world`, you could add the following:
138+
139+
```yaml
140+
global-transforms:
141+
-
142+
- !ruby/regexp /(hello)/i
143+
- "\\1 world"
144+
```
145+
146+
Global transforms are applied after all other `transforms`.
147+
Word wrapping is applied after the transforms have been applied.
148+
149+
#### Feed specific
150+
151+
Transforms can also be defined per user feed.
152+
If you wanted to do the same as the global above, but only for `seinfeldtoday`, then add the following:
153+
154+
```yaml
155+
transforms:
156+
seinfeldtoday:
157+
-
158+
- !ruby/regexp /(hello)/i
159+
- "\\1 world"
160+
```
161+
162+
Or a more interesting example using the example earlier for `seinfeldtoday`:
163+
164+
```text
165+
Elaine has no idea what her BF does for a living and it's now too
166+
late to ask. E:"Teacher, I think. Or a doctor? Wait Is
167+
'computers' a job?"
168+
```
169+
170+
Then add the following to make this a bit more readable:
171+
172+
```yaml
173+
transforms:
174+
seinfeldtoday:
175+
-
176+
- !ruby/regexp /(E:\s*)/
177+
- "\n\nElaine: "
178+
-
179+
- !ruby/regexp /BF/
180+
- "boyfriend"
181+
```
182+
183+
Then we end up with this:
184+
185+
```text
186+
Elaine has no idea what her boyfriend does for a living and it's now
187+
too late to ask.
188+
189+
Elaine: "Teacher, I think. Or a doctor? Wait Is 'computers' a job?"
190+
```
191+
129192
## Usage
130193
131194
Create a fortune for the latest *seinfeldtoday* tweets.

augury.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
2323
spec.add_dependency 'thor', '~>1.0.0'
2424
spec.add_dependency 'twitter', '~>7.0'
2525
## For the word_wrap function
26-
spec.add_dependency 'facets', '~>3.0'
26+
spec.add_dependency 'word_wrap', '~>1.0'
2727
end

lib/augury/cli.rb

+14-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CLI < Thor
1616
option :append,
1717
type: :boolean,
1818
aliases: '-a',
19-
desc: 'If set, the target path will be appended to instead of overwritten'
19+
desc: 'If set, the target path will be appended to instead of overwritten. DEFAULT: false'
2020

2121
option :count,
2222
type: :numeric,
@@ -38,11 +38,21 @@ class CLI < Thor
3838
aliases: '-l',
3939
desc: 'Include tweets with links in them. DEFAULT: false'
4040

41+
option :remove_links,
42+
type: :boolean,
43+
aliases: '--remove-links',
44+
desc: 'Remove links from tweets. DEFAULT: false'
45+
4146
option :attribution,
4247
type: :boolean,
4348
aliases: '-A',
4449
desc: 'Add an author attribution to each fortune. DEFAULT: false'
4550

51+
option :apply_transforms,
52+
type: :boolean,
53+
aliases: '-t',
54+
desc: 'Apply transforms from config file. DEFAULT: false'
55+
4656
def generate(username, *path)
4757
path = File.expand_path(path[0] || username)
4858
augury = Augury::Fortune.new(username, path, options)
@@ -63,12 +73,7 @@ def options
6373
defaults = Thor::CoreExt::HashWithIndifferentAccess.new(
6474
{
6575
width: 72,
66-
append: false,
6776
count: 200,
68-
retweets: false,
69-
replies: false,
70-
links: false,
71-
attribution: false,
7277
},
7378
)
7479

@@ -78,6 +83,9 @@ def options
7883
defaults = defaults.merge(config_options)
7984
end
8085

86+
# Enforce implied options
87+
defaults[:links] = true if original_options[:remove_links] || defaults[:remove_links]
88+
8189
Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
8290
end
8391
end

lib/augury/exception.rb

+6
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ def message
66
'No twitter credential configuration found in the augury config'
77
end
88
end
9+
10+
class TransformError < StandardError
11+
def message
12+
'Augury transforms are invalid, check your configuration'
13+
end
14+
end
915
end

lib/augury/fortune.rb

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'cgi'
4-
require 'facets/string/word_wrap'
4+
require 'word_wrap'
55
require 'twitter'
66

77
module Augury
@@ -30,6 +30,7 @@ def retrieve_tweets
3030
@tweets = collect_with_max_id do |max_id|
3131
options = {
3232
count: 200,
33+
tweet_mode: 'extended',
3334
include_rts: @config[:retweets],
3435
exclude_replies: !@config[:replies],
3536
}
@@ -46,7 +47,14 @@ def format_fortune
4647
filtered = @tweets.flat_map(&:full_text).reject do |tweet|
4748
tweet.match(/https?:/) unless @config[:links]
4849
end
49-
formatted = filtered.flat_map { |tweet| CGI.unescapeHTML(tweet).word_wrap(@config[:width]) }
50+
to_transform = transforms
51+
formatted = filtered.flat_map do |tweet|
52+
text = CGI.unescapeHTML(tweet)
53+
to_transform.each do |transform|
54+
text.gsub!(transform[0], transform[1])
55+
end
56+
WordWrap.ww text, @config.fetch(:width, 72)
57+
end
5058
author = @config[:attribution] ? "\n-- #{@twitter.user(@username).name}\n" : ''
5159
formatted.join("#{author}%\n")
5260
end
@@ -75,5 +83,28 @@ def twitter_setup
7583
cfg.access_token_secret = @config[:twitter]['access_token_secret']
7684
end
7785
end
86+
87+
private
88+
89+
def transforms
90+
all_transforms = []
91+
all_transforms << [/https?:\/\/[^\s]+/, ''] if @config[:remove_links]
92+
return all_transforms unless @config[:apply_transforms]
93+
94+
all_transforms.push(*@config.dig('transforms', @username) || [])
95+
all_transforms.push(*@config.fetch('global-transforms', []))
96+
97+
raise Augury::TransformError unless validate_transforms(all_transforms)
98+
99+
all_transforms
100+
end
101+
102+
def validate_transforms(all_transforms)
103+
all_transforms.all? do |transform|
104+
transform.count == 2 &&
105+
[String, Regexp].include?(transform[0].class) &&
106+
transform[1].is_a?(String)
107+
end
108+
end
78109
end
79110
end

lib/augury/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Augury
4-
VERSION = '1.0.3'
4+
VERSION = '1.1.0'
55
end

0 commit comments

Comments
 (0)