Add FilterArray class to Banzai
This commit is contained in:
parent
6aa50165b0
commit
c9b1132217
11 changed files with 76 additions and 13 deletions
27
lib/banzai/filter_array.rb
Normal file
27
lib/banzai/filter_array.rb
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
module Banzai
|
||||||
|
class FilterArray < Array
|
||||||
|
# Insert a value immediately after another value
|
||||||
|
#
|
||||||
|
# If the preceding value does not exist, the new value is added to the end
|
||||||
|
# of the Array.
|
||||||
|
def insert_after(after_value, value)
|
||||||
|
i = index(after_value) || length - 1
|
||||||
|
|
||||||
|
insert(i + 1, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Insert a value immediately before another value
|
||||||
|
#
|
||||||
|
# If the succeeding value does not exist, the new value is added to the
|
||||||
|
# beginning of the Array.
|
||||||
|
def insert_before(before_value, value)
|
||||||
|
i = index(before_value) || -1
|
||||||
|
|
||||||
|
if i < 0
|
||||||
|
unshift(value)
|
||||||
|
else
|
||||||
|
insert(i, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,7 +4,7 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class BasePipeline
|
class BasePipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
[]
|
FilterArray[]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.transform_context(context)
|
def self.transform_context(context)
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class BroadcastMessagePipeline < DescriptionPipeline
|
class BroadcastMessagePipeline < DescriptionPipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
@filters ||= [
|
@filters ||= FilterArray[
|
||||||
Filter::MarkdownFilter,
|
Filter::MarkdownFilter,
|
||||||
Filter::SanitizationFilter,
|
Filter::SanitizationFilter,
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Banzai
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.filters
|
def self.filters
|
||||||
pipelines.flat_map(&:filters)
|
FilterArray.new(pipelines.flat_map(&:filters))
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.transform_context(context)
|
def self.transform_context(context)
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class GfmPipeline < BasePipeline
|
class GfmPipeline < BasePipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
@filters ||= [
|
@filters ||= FilterArray[
|
||||||
Filter::SyntaxHighlightFilter,
|
Filter::SyntaxHighlightFilter,
|
||||||
Filter::SanitizationFilter,
|
Filter::SanitizationFilter,
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class PlainMarkdownPipeline < BasePipeline
|
class PlainMarkdownPipeline < BasePipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
[
|
FilterArray[
|
||||||
Filter::MarkdownFilter
|
Filter::MarkdownFilter
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class PostProcessPipeline < BasePipeline
|
class PostProcessPipeline < BasePipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
[
|
FilterArray[
|
||||||
Filter::RelativeLinkFilter,
|
Filter::RelativeLinkFilter,
|
||||||
Filter::RedactorFilter
|
Filter::RedactorFilter
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class ReferenceExtractionPipeline < BasePipeline
|
class ReferenceExtractionPipeline < BasePipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
[
|
FilterArray[
|
||||||
Filter::ReferenceGathererFilter
|
Filter::ReferenceGathererFilter
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class SingleLinePipeline < GfmPipeline
|
class SingleLinePipeline < GfmPipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
@filters ||= [
|
@filters ||= FilterArray[
|
||||||
Filter::SanitizationFilter,
|
Filter::SanitizationFilter,
|
||||||
|
|
||||||
Filter::EmojiFilter,
|
Filter::EmojiFilter,
|
||||||
|
|
|
@ -4,11 +4,8 @@ module Banzai
|
||||||
module Pipeline
|
module Pipeline
|
||||||
class WikiPipeline < FullPipeline
|
class WikiPipeline < FullPipeline
|
||||||
def self.filters
|
def self.filters
|
||||||
@filters ||= begin
|
@filters ||= super.insert_after(Filter::TableOfContentsFilter,
|
||||||
filters = super
|
Filter::GollumTagsFilter)
|
||||||
toc = filters.index(Filter::TableOfContentsFilter)
|
|
||||||
filters.insert(toc + 1, Filter::GollumTagsFilter)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
39
spec/lib/banzai/filter_array_spec.rb
Normal file
39
spec/lib/banzai/filter_array_spec.rb
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Banzai::FilterArray do
|
||||||
|
describe '#insert_after' do
|
||||||
|
it 'inserts an element after a provided element' do
|
||||||
|
filters = described_class.new(%w(a b c))
|
||||||
|
|
||||||
|
filters.insert_after('b', '1')
|
||||||
|
|
||||||
|
expect(filters).to eq %w(a b 1 c)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'inserts an element at the end when the provided element does not exist' do
|
||||||
|
filters = described_class.new(%w(a b c))
|
||||||
|
|
||||||
|
filters.insert_after('d', '1')
|
||||||
|
|
||||||
|
expect(filters).to eq %w(a b c 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#insert_before' do
|
||||||
|
it 'inserts an element before a provided element' do
|
||||||
|
filters = described_class.new(%w(a b c))
|
||||||
|
|
||||||
|
filters.insert_before('b', '1')
|
||||||
|
|
||||||
|
expect(filters).to eq %w(a 1 b c)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'inserts an element at the beginning when the provided element does not exist' do
|
||||||
|
filters = described_class.new(%w(a b c))
|
||||||
|
|
||||||
|
filters.insert_before('d', '1')
|
||||||
|
|
||||||
|
expect(filters).to eq %w(1 a b c)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue