Module: Jekyll::URLMetadata

Defined in:
lib/jekyll-url-metadata.rb,
lib/jekyll-url-metadata/main.rb,
lib/jekyll-url-metadata/version.rb

Constant Summary collapse

VERSION =
"1.0.3"

Instance Method Summary collapse

Instance Method Details

#cacheObject



16
17
18
# File 'lib/jekyll-url-metadata/main.rb', line 16

def cache
  @@cache ||= Jekyll::Cache.new("Jekyll::URLMetadata")
end

#exists(obj) ⇒ Object



115
116
117
# File 'lib/jekyll-url-metadata/main.rb', line 115

def exists(obj)
  !obj.nil? && obj != ""
end

#generate_hashmap(input, testCase = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll-url-metadata/main.rb', line 29

def generate_hashmap(input, testCase = false)
  # parse HTML from URL
  begin
    doc = Nokogiri::HTML(URI.open(input, { 
      :open_timeout => url_config["open_timeout"].nil? ? 1 : url_config["open_timeout"],
      :read_timeout => url_config["read_timeout"].nil? ? 1 : url_config["read_timeout"]
    }))
  rescue
    log("Failed to parse HTML from '#{input}'. Please double check for URL validity.") if !testCase
    return
  end

  hash = Hash.new

  # add first <title> tag's value to the hash
  doc.search("head title").each do | title |
    break if exists(hash["title"])
    hash["title"] = title.content
  end

  # add possible <meta> tag attribute's value to the hash
  doc.search("meta").each do | meta |
    name = get(meta, "name")
    property = get(meta, "property")
    charset = get(meta, "charset")
    content = get(meta, "content")

    if exists(name)
      hash[name] = content
    elsif exists(property)
      hash[property] = content
    elsif exists(charset)
      hash["charset"] = charset
    end
  end

  # add possible <link> tag attribute's value to the hash
  doc.search("link").each do | link |
    hash[get(link, "rel")] = get(link, "href")
  end
  
  hash
end

#get(obj, attr) ⇒ Object



119
120
121
# File 'lib/jekyll-url-metadata/main.rb', line 119

def get(obj, attr)
  obj.get_attribute(attr)
end

#is_config_validObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jekyll-url-metadata/main.rb', line 99

def is_config_valid()
  if !url_config["open_timeout"].nil? && !url_config["open_timeout"].is_a?(Integer)
    log("Expected an 'Integer' value for config 'open_timeout'. Got '#{url_config["open_timeout"].class}'.")

    return false
  end

  if !url_config["read_timeout"].nil? && !url_config["read_timeout"].is_a?(Integer)
    log("Expected an 'Integer' value for config 'read_timeout'. Got '#{url_config["read_timeout"].class}'.")

    return false
  end

  true
end

#is_input_valid(input, testCase = false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jekyll-url-metadata/main.rb', line 73

def is_input_valid(input, testCase = false)
  if !input.is_a?(String)
    log("Expected input type 'String'. Got '#{input.class}'.") if !testCase

    return false
  end

  if input.nil? || input == ""
    log("Empty input string.") if !testCase

    return false
  end

  begin
    if URI.parse(input).kind_of?(URI::HTTP)
      return true
    else
      log("'#{input}' does not seem to be a valid URL.") if !testCase
    end
  rescue
    log("'#{input}' does not seem to be a valid URL.") if !testCase
  end

  false
end

#log(msg) ⇒ Object



123
124
125
# File 'lib/jekyll-url-metadata/main.rb', line 123

def log(msg)
  Jekyll.logger.error "URL Metadata:", msg
end

#metadata(input) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/jekyll-url-metadata/main.rb', line 20

def (input)
  return if !is_input_valid(input) || !is_config_valid()

  # The getset() API implements a cache first strategy
  cache.getset(input) do
    generate_hashmap(input)
  end
end

#url_configObject



7
8
9
10
11
12
13
14
# File 'lib/jekyll-url-metadata/main.rb', line 7

def url_config
  begin
    @@url_config ||= @context.registers[:site].config['url_metadata'].nil? ? {} : 
      @context.registers[:site].config['url_metadata']
  rescue
    {}
  end
end