summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-03-07 13:09:53 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-03-17 07:39:15 +0100
commitfd8394faae25b54c4d9ac485a0ce746cffec3a0f (patch)
treecb6d282e96e97421f365c62ed2ee0928f0586967 /lib
parentcd0f19450843b5b9245ae82302d3f9d9003cb899 (diff)
downloadgitlab-ce-fd8394faae25b54c4d9ac485a0ce746cffec3a0f.tar.gz
Move reference unfolder for GFM to separate class
Diffstat (limited to 'lib')
-rw-r--r--lib/banzai/filter/reference_unfold_filter.rb42
-rw-r--r--lib/banzai/pipeline/reference_unfold_pipeline.rb21
-rw-r--r--lib/gitlab/gfm/reference_unfolder.rb31
-rw-r--r--lib/gitlab/reference_extractor.rb8
4 files changed, 38 insertions, 64 deletions
diff --git a/lib/banzai/filter/reference_unfold_filter.rb b/lib/banzai/filter/reference_unfold_filter.rb
deleted file mode 100644
index 70a485ba2ae..00000000000
--- a/lib/banzai/filter/reference_unfold_filter.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require 'html/pipeline/filter'
-
-module Banzai
- module Filter
- ##
- # Filter than unfolds local references.
- #
- #
- class ReferenceUnfoldFilter < HTML::Pipeline::Filter
- def initialize(*)
- super
-
- unless result[:references].is_a?(Hash)
- raise StandardError, 'References not processed!'
- end
-
- @text = context[:text].dup
- @new_project = context[:new_project]
- @referables = result[:references].values.flatten
- end
-
- def call
- @referables.each do |referable|
- next unless referable.respond_to?(:to_reference)
-
- pattern = /#{Regexp.escape(referable.to_reference)}/
- @text.gsub!(pattern, referable.to_reference(@new_project))
- end
-
- @text
- end
-
- private
-
- def validate
- needs :project
- needs :new_project
- needs :text
- end
- end
- end
-end
diff --git a/lib/banzai/pipeline/reference_unfold_pipeline.rb b/lib/banzai/pipeline/reference_unfold_pipeline.rb
deleted file mode 100644
index 597bf7befd1..00000000000
--- a/lib/banzai/pipeline/reference_unfold_pipeline.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module Banzai
- module Pipeline
- class ReferenceUnfoldPipeline < BasePipeline
- def self.filters
- FullPipeline.filters +
- [Filter::ReferenceGathererFilter,
- Filter::ReferenceUnfoldFilter]
- end
-
- def self.call(text, context = {})
- context = context.merge(text: text)
- super
- end
-
- class << self
- alias_method :to_document, :call
- alias_method :to_html, :call
- end
- end
- end
-end
diff --git a/lib/gitlab/gfm/reference_unfolder.rb b/lib/gitlab/gfm/reference_unfolder.rb
new file mode 100644
index 00000000000..4969e8ebe85
--- /dev/null
+++ b/lib/gitlab/gfm/reference_unfolder.rb
@@ -0,0 +1,31 @@
+module Gitlab
+ module Gfm
+ ##
+ # Class than unfolds local references in text.
+ #
+ #
+ class ReferenceUnfolder
+ def initialize(text, project)
+ @text = text
+ @project = project
+ end
+
+ def unfold(from_project)
+ referables.each_with_object(@text.dup) do |referable, text|
+ next unless referable.respond_to?(:to_reference)
+
+ pattern = /#{Regexp.escape(referable.to_reference)}/
+ text.gsub!(pattern, referable.to_reference(from_project))
+ end
+ end
+
+ private
+
+ def referables
+ extractor = Gitlab::ReferenceExtractor.new(@project)
+ extractor.analyze(@text)
+ extractor.all
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb
index 4d830aa45e1..8c698d43bef 100644
--- a/lib/gitlab/reference_extractor.rb
+++ b/lib/gitlab/reference_extractor.rb
@@ -1,6 +1,7 @@
module Gitlab
# Extract possible GFM references from an arbitrary String for further processing.
class ReferenceExtractor < Banzai::ReferenceExtractor
+ REFERABLES = %i(user issue label milestone merge_request snippet commit commit_range)
attr_accessor :project, :current_user, :author
def initialize(project, current_user = nil, author = nil)
@@ -17,7 +18,7 @@ module Gitlab
super(text, context.merge(project: project))
end
- %i(user label milestone merge_request snippet commit commit_range).each do |type|
+ REFERABLES.each do |type|
define_method("#{type}s") do
@references[type] ||= references(type, reference_context)
end
@@ -31,6 +32,11 @@ module Gitlab
end
end
+ def all
+ REFERABLES.each { |referable| send(referable.to_s.pluralize) }
+ @references.values.flatten
+ end
+
private
def reference_context