Class: HrefTag::ExternalHref

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
href.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, command_line, tokens) ⇒ void

Constructor.

Parameters:

  • tag_name (String)

    is the name of the tag, which we already know.

  • command_line (Hash, String, Liquid::Tag::Parser)

    the arguments from the web page.

  • tokens (Liquid::ParseContext)

    tokenized command line



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'href.rb', line 54

def initialize(tag_name, command_line, tokens)
  super

  @follow = " rel='nofollow'"
  @match = false
  @target = " target='_blank'"

  tokens = command_line.strip.split(" ")

  followIndex = tokens.index("follow")
  if followIndex then
    tokens.delete_at(followIndex)
    @follow = ""
  end

  targetIndex = tokens.index("notarget")
  if targetIndex then
    tokens.delete_at(targetIndex)
    @target = ""
  end

  matchIndex = tokens.index("match")
  if matchIndex then
    tokens.delete_at(matchIndex)
    @follow = ""
    @match = true
    @target = ""
  end

  @link = tokens.shift

  @text = tokens.join(" ").strip
  if @text.empty? then 
    @text = "<code>" + @link + "</code>" 
    @link = "https://" + @link
  end

  unless @link.start_with? "http"
    @follow = ""
    @target = ""
  end
end

Instance Method Details

#match(context) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'href.rb', line 97

def match(context)
  site = context.registers[:site]
  config = site.config['href']
  die_if_nomatch = !config.nil? && config['nomatch'] && config['nomatch']=='fatal'

  path, fragment = @link.split('#')

  # puts "@link=#{@link}"
  # puts "site.posts[0].url = #{site.posts.docs[0].url}"
  # puts "site.posts[0].path = #{site.posts.docs[0].path}"
  posts = site.posts.docs.select { |x| x.url.include?(path) }
  case posts.length
  when 0
    if die_if_nomatch then
      abort "href error: No url matches '#{@link}'"
    else
      @link = "#"
      @text = "<i>#{@link} is not available</i>"
    end
  when 1
    @link = "#{@link}\##{fragment}" if fragment
  else
    abort "Error: More than one url matched: #{ matches.join(", ")}"
  end
end

#render(context) ⇒ String

Method prescribed by the Jekyll plugin lifecycle.

Returns:

  • (String)


134
135
136
137
138
139
# File 'href.rb', line 134

def render(context)
  if (@match) then match(context) end
  link = replaceVars(context, @link)  # puts "@link=#{@link}; link=#{link}"

  "<a href='#{link}'#{@target}#{@follow}>#{@text}</a>"
end

#replaceVars(context, link) ⇒ Object



123
124
125
126
127
128
129
130
# File 'href.rb', line 123

def replaceVars(context, link)
  variables = context.registers[:site].config['plugin-vars']
  variables.each do |name, value|
    #puts "#{name}=#{value}"
    link = link.gsub("{{#{name}}}", value)
  end
  link
end