diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-02-26 12:04:29 +0100 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-03-17 07:39:15 +0100 |
commit | 3c493c24c70f7c8dc8e1f3bcf29e18d1ef0944a7 (patch) | |
tree | 1f89261db74631a92f2ef5187ff0785a41b49333 /lib | |
parent | efd8251751c85285e6519fd3dd756f74579da15b (diff) | |
download | gitlab-ce-3c493c24c70f7c8dc8e1f3bcf29e18d1ef0944a7.tar.gz |
Add reference unfold pipeline used when moving issue
Diffstat (limited to 'lib')
-rw-r--r-- | lib/banzai/filter/reference_unfold_filter.rb | 46 | ||||
-rw-r--r-- | lib/banzai/pipeline/reference_unfold_pipeline.rb | 9 |
2 files changed, 55 insertions, 0 deletions
diff --git a/lib/banzai/filter/reference_unfold_filter.rb b/lib/banzai/filter/reference_unfold_filter.rb new file mode 100644 index 00000000000..a6145261651 --- /dev/null +++ b/lib/banzai/filter/reference_unfold_filter.rb @@ -0,0 +1,46 @@ +module Banzai + module Filter + ## + # Filter than unfolds local references. + # + # Replaces all local references with project cross reference version + # in all objects passed to this filter in context. + # + # Requires objects array with each element implementing `Referable`. + # + class ReferenceUnfoldFilter < ReferenceFilter + def initialize(*) + super + + @objects = context[:objects] + @project = context[:project] + + unless @objects.all? { |object| object.respond_to?(:to_reference) } + raise StandardError, "No `to_reference` method implemented in one of the objects !" + end + + unless @project.kind_of?(Project) + raise StandardError, 'No valid project passed in context!' + end + end + + def call + @objects.each do |object| + pattern = /#{Regexp.escape(object.to_reference)}/ + replace_text_nodes_matching(pattern) do |content| + content.gsub(pattern, object.to_reference(@project)) + end + end + + doc + end + + private + + def validate + needs :project + needs :objects + end + end + end +end diff --git a/lib/banzai/pipeline/reference_unfold_pipeline.rb b/lib/banzai/pipeline/reference_unfold_pipeline.rb new file mode 100644 index 00000000000..5b555d7c2d7 --- /dev/null +++ b/lib/banzai/pipeline/reference_unfold_pipeline.rb @@ -0,0 +1,9 @@ +module Banzai + module Pipeline + class ReferenceUnfoldPipeline < BasePipeline + def self.filters + [Filter::ReferenceUnfoldFilter] + end + end + end +end |