module Anchored::Linker

Constants

regexps for determining context, used high-volume

BRACKETS
PUNCTUATION_RE

Public Instance Methods

remove_target_if_local(href, domain, options) click to toggle source

remove_target_if_local(“same.com/x”, “same.com”, target: “_blank”)

> <a href=“http://same.com/xsame.com/x”>http://same.com/x>

remove_target_if_local(“same.com/x”, “different.com”, target: “_blank”)

> <a href=“same.com/x” target=“_blank”>same.com/x>

modifies options in place

# File lib/anchored/linker.rb, line 19
def remove_target_if_local(href, domain, options)
  return unless options[:target]
  options.delete(:target) if href.include?("//#{domain}")
end

Private Instance Methods

anchor_attrs(options) click to toggle source
# File lib/anchored/linker.rb, line 76
def anchor_attrs(options)
  options.map { |k, v| %(#{k}="#{v}") }.unshift("").join(" ")
end
anchor_tag(href, text, options) click to toggle source
# File lib/anchored/linker.rb, line 80
def anchor_tag(href, text, options)
  options = options.dup
  if (domain = options.delete(:domain))
    remove_target_if_local href, domain, options
  end
  %(<a href="#{href}"#{anchor_attrs(options)}>#{text}</a>)
end
auto_linked?(match) click to toggle source

Detects already linked context or position in the middle of a tag Note: this changes the current Regexp

# File lib/anchored/linker.rb, line 69
def auto_linked?(match)
  left = match.pre_match
  right = match.post_match
  (left =~ AUTO_LINK_CRE[0] && right =~ AUTO_LINK_CRE[1]) ||
    (left.rindex(AUTO_LINK_CRE[2]) && Regexp.last_match.post_match !~ AUTO_LINK_CRE[3])
end