diff options
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 |