Class: Florby::Plugins::WikiLinkReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/florby/plugins/wiki_link_replacer.rb

Constant Summary collapse

/\[\[\s?([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s?\]\]/

Instance Method Summary collapse

Constructor Details

#initialize(collection:) ⇒ WikiLinkReplacer

Returns a new instance of WikiLinkReplacer.



8
9
10
# File 'lib/florby/plugins/wiki_link_replacer.rb', line 8

def initialize(collection:)
  @collection = collection
end

Instance Method Details

#replace!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/florby/plugins/wiki_link_replacer.rb', line 12

def replace!
  @collection.all_pages.each do |page|
    page.meta['backlinks'] ||= []

    page.content =
      page.content.gsub(WIKI_LINK_REGEXP) do |match|

        title = CGI.unescapeHTML(Regexp.last_match(1))
        destination = @collection.find(title)

        unless page.exclude_from_collections?
          destination.meta['backlinks'] ||= []
          destination.meta['backlinks'] << page
        end

        raise "Page not found: #{title}. #{@collection.titles}" unless destination

        "<a href='#{destination.permalink}'>#{destination.title}</a>"
      end
  end
end