Sha256: 4a96069aab1501939bb2faac1cc0a5853386acd9269fc358c5436ea814f89362

Contents?: true

Size: 933 Bytes

Versions: 1

Compression:

Stored size: 933 Bytes

Contents

require 'cgi'

module Florby
  module Plugins
    class WikiLinkReplacer
      WIKI_LINK_REGEXP = /\[\[\s?([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s?\]\]/

      def initialize(collection:)
        @collection = collection
      end

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
florby-0.1.0 lib/florby/plugins/wiki_link_replacer.rb