Skip to content

Commit b9e02e3

Browse files
authored
Merge pull request gjtorikian#398 from gjtorikian/classify
[BREAKING] `TextFilter` now requires instantiation
2 parents 6e18f1b + b029535 commit b9e02e3

File tree

12 files changed

+119
-89
lines changed

12 files changed

+119
-89
lines changed

CHANGELOG.md

Lines changed: 70 additions & 56 deletions
Large diffs are not rendered by default.

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ gem "awesome_print"
1010
gem "rubocop"
1111
gem "rubocop-standard"
1212

13-
gem "github_changelog_generator", "~> 1.16"
14-
1513
gem "sorbet-runtime"
1614

1715
group :development, :test do

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ ALLOWLIST = {
171171

172172
pipeline = HTMLPipeline.new \
173173
text_filters: [
174-
HTMLPipeline::MarkdownFilter,
174+
HTMLPipeline::TextFilter::ImageFilter.new,
175175
],
176176
convert_filter: HTMLPipeline::ConvertFilter::MarkdownFilter.new,
177177
sanitization_config: ALLOWLIST
@@ -199,7 +199,7 @@ the config:
199199
```ruby
200200
pipeline = HTMLPipeline.new \
201201
text_filters: [
202-
HTMLPipeline::MarkdownFilter,
202+
HTMLPipeline::TextFilter::ImageFilter.new,
203203
],
204204
convert_filter: HTMLPipeline::ConvertFilter::MarkdownFilter.new,
205205
sanitization_config: nil

lib/html_pipeline.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def call(text, context: {}, result: {})
153153

154154
if @text_filters.any?
155155
payload = default_payload({
156-
text_filters: @text_filters.map(&:name),
156+
text_filters: @text_filters.map { |f| f.class.name },
157157
context: context,
158158
result: result,
159159
})
@@ -204,7 +204,7 @@ def call(text, context: {}, result: {})
204204
# Returns the result of the filter.
205205
def perform_filter(filter, doc, context: {}, result: {})
206206
payload = default_payload({
207-
filter: filter.name,
207+
filter: filter.class.name,
208208
context: context,
209209
result: result,
210210
})

lib/html_pipeline/text_filter.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ class HTMLPipeline
44
class TextFilter < Filter
55
attr_reader :text
66

7-
def initialize(text, context: {}, result: {})
8-
raise TypeError, "text must be a String" unless text.is_a?(String)
9-
10-
# Ensure that this is always a string
11-
@text = text.respond_to?(:to_str) ? text.to_str : text.to_s
7+
def initialize(context: {}, result: {})
128
super(context: context, result: result)
139
end
1410

1511
class << self
16-
def call(input, context: {}, result: {})
17-
new(input, context: context, result: result).call
12+
def call(text, context: {}, result: {})
13+
raise TypeError, "text must be a String" unless text.is_a?(String)
14+
15+
# Ensure that this is always a string
16+
text = text.respond_to?(:to_str) ? text.to_str : text.to_s
17+
new(context: context, result: result).call(text)
1818
end
1919
end
2020
end

lib/html_pipeline/text_filter/image_filter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class TextFilter
99
# <img src="http://example.com/test.jpg" alt=""/>.
1010

1111
class ImageFilter < TextFilter
12-
def call
13-
@text.gsub(%r{(https|http)?://.+\.(jpg|jpeg|bmp|gif|png)(\?\S+)?}i) do |match|
12+
def call(text)
13+
text.gsub(%r{(https|http)?://.+\.(jpg|jpeg|bmp|gif|png)(\?\S+)?}i) do |match|
1414
%(<img src="#{match}" alt=""/>)
1515
end
1616
end

lib/html_pipeline/text_filter/plain_text_input_filter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ class TextFilter
55
# Simple filter for plain text input. HTML escapes the text input and wraps it
66
# in a div.
77
class PlainTextInputFilter < TextFilter
8-
def call
9-
"<div>#{CGI.escapeHTML(@text)}</div>"
8+
def call(text)
9+
"<div>#{CGI.escapeHTML(text)}</div>"
1010
end
1111
end
1212
end

lib/html_pipeline/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
class HTMLPipeline
4-
VERSION = "3.0.3"
4+
VERSION = "3.1.0"
55
end

script/generate_changelog

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/html_pipeline/convert_filter/markdown_filter_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def setup
1616
"See http://example.org/ for more info"
1717
@code =
1818
"```\n" \
19-
"def hello()" \
20-
" 'world'" \
19+
"def hello() " \
20+
"'world'" \
2121
"end" \
2222
"```"
2323
@header = <<~DOC

0 commit comments

Comments
 (0)