Class: Banzai::Filter::ProjectReferenceFilter

Inherits:
ReferenceFilter
  • Object
show all
Defined in:
lib/banzai/filter/project_reference_filter.rb

Overview

HTML filter that replaces project references with links.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ReferenceFilter

call, #call_and_update_nodes, #data_attribute, #each_node, #element_node?, #group, #ignore_ancestor_query, #initialize, #nodes, #project, #reference_class, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #skip_project_check?, #text_node?, #user, #validate, #yield_valid_link

Methods included from OutputSafety

#escape_once

Methods included from RequestStoreReferenceCache

#cached_call, #get_or_set_cache

Constructor Details

This class inherits a constructor from Banzai::Filter::ReferenceFilter

Class Method Details

.references_in(text) ⇒ Object

Public: Find `namespace/project>` project references in text

ProjectReferenceFilter.references_in(text) do |match, project|
  "<a href=...>#{project}></a>"
end

text - String text to search.

Yields the String match, and the String project name.

Returns a String replaced with the return of the block.


20
21
22
23
24
# File 'lib/banzai/filter/project_reference_filter.rb', line 20

def self.references_in(text)
  text.gsub(Project.markdown_reference_pattern) do |match|
    yield match, "#{$~[:namespace]}/#{$~[:project]}"
  end
end

Instance Method Details

#callObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/banzai/filter/project_reference_filter.rb', line 26

def call
  ref_pattern = Project.markdown_reference_pattern
  ref_pattern_start = /\A#{ref_pattern}\z/

  nodes.each_with_index do |node, index|
    if text_node?(node)
      replace_text_when_pattern_matches(node, index, ref_pattern) do |content|
        project_link_filter(content)
      end
    elsif element_node?(node)
      yield_valid_link(node) do |link, inner_html|
        if link =~ ref_pattern_start
          replace_link_node_with_href(node, index, link) do
            project_link_filter(link, link_content: inner_html)
          end
        end
      end
    end
  end

  doc
end

Replace `namespace/project>` project references in text with links to the referenced project page.

text - String text to replace references in. link_content - Original content of the link being replaced.

Returns a String with `namespace/project>` references replaced with links. All links have `gfm` and `gfm-project` class names attached for styling.


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/banzai/filter/project_reference_filter.rb', line 57

def project_link_filter(text, link_content: nil)
  self.class.references_in(text) do |match, project_path|
    cached_call(:banzai_url_for_object, match, path: [Project, project_path.downcase]) do
      if project = projects_hash[project_path.downcase]
        link_to_project(project, link_content: link_content) || match
      else
        match
      end
    end
  end
end

#projectsObject

Returns all projects referenced in the current document.


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/banzai/filter/project_reference_filter.rb', line 82

def projects
  refs = Set.new

  nodes.each do |node|
    node.to_html.scan(Project.markdown_reference_pattern) do
      refs << "#{$~[:namespace]}/#{$~[:project]}"
    end
  end

  refs.to_a
end

#projects_hashObject

Returns a Hash containing all Project objects for the project references in the current document.

The keys of this Hash are the project paths, the values the corresponding Project objects.


74
75
76
77
78
79
# File 'lib/banzai/filter/project_reference_filter.rb', line 74

def projects_hash
  @projects ||= Project.eager_load(:route, namespace: [:route])
                       .where_full_path_in(projects)
                       .index_by(&:full_path)
                       .transform_keys(&:downcase)
end