Module: Indexer::Importer::HTMLImportation

Included in:
Indexer::Importer
Defined in:
lib/indexer/importer/html.rb

Overview

Import metadata from a HTML source using microformats.

NOTE: The implementation using css selectors is fairly slow. If we even think it important to speed up then we might try traversing instead.

Instance Method Summary collapse

Instance Method Details

#import(source) ⇒ Object

YAML import procedure.



16
17
18
19
20
21
22
23
24
25
# File 'lib/indexer/importer/html.rb', line 16

def import(source)
  if File.file?(source)
    case File.extname(source)
    when '.html'
      load_html(source)
      return true
    end
  end
  super(source) if defined?(super)
end

#load_html(file) ⇒ Object

Import metadata from HTML file.



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
# File 'lib/indexer/importer/html.rb', line 30

def load_html(file)
  require 'nokogiri'

  case file
  when Nokogiri::XML::Document
    doc = file
  when File
    doc = Nokogiri::HTML(file)
  else
    doc = Nokogiri::HTML(File.new(file))
  end

  data = {}

  %w{version summary description created}.each do |field|
    load_html_simple(field, doc, data)
  end

  load_html_name(doc, data)
  load_html_title(doc, data)
  load_html_authors(doc, data)
  load_html_organizations(doc, data)
  load_html_requirements(doc, data)
  load_html_resources(doc, data)
  load_html_repositories(doc, data)
  load_html_copyrights(doc, data)
  load_html_categories(doc, data)

  .merge!(data)
end

#load_html_authors(doc, data) ⇒ Object

Class is iauthor.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/indexer/importer/html.rb', line 178

def load_html_authors(doc, data)
  nodes = doc.css('.iauthor')
  return if (nodes.nil? or nodes.empty?)

  data['authors'] ||= []

  nodes.each do |node|
    entry = {}

    if n = (node.at_css('.name') || node.at_css('.nickname'))
      entry['name'] = n.content.strip
    end

    if n = node.at_css('.email')
      text = n.attr(:href) || n.content.strip
      text = text.sub(/^mailto\:/i, '')
      entry['email'] = text
    end

    if n = node.at_css('.website') || node.at_css('.uri') || node.at_css('.url')
      text = n.attr(:href) || n.content.strip
      entry['website'] = text
    end

    data['authors'] << entry if entry['name']
  end
end

#load_html_categories(doc, data) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/indexer/importer/html.rb', line 104

def load_html_categories(doc, data)
  nodes = doc.css('.icategory')
  return if (nodes.nil? or nodes.empty?)

  data['categories'] ||= []

  nodes.each do |node|
    entry = node.content.strip
    data['categories'] << entry unless entry == ""
  end
end

#load_html_copyrights(doc, data) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/indexer/importer/html.rb', line 260

def load_html_copyrights(doc, data)
  nodes = doc.css('.icopyright')
  return if (nodes.nil? or nodes.empty?)

  data['copyrights'] ||= []

  nodes.each do |node|
    entry = {}

    if n = node.at_css('.holder')
      entry['holder'] = n.content.strip
    end

    if n = node.at_css('.year')
      entry['year'] = n.content.strip
    end

    if n = node.at_css('.license')
      text = n.content.strip
      text = text.sub(/license$/i,'').strip
      entry['license'] = text
    end

    data['copyrights'] << entry
  end
end

#load_html_name(doc, data) ⇒ Object

Load name, and use it for title too if not already set.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/indexer/importer/html.rb', line 74

def load_html_name(doc, data)
  nodes = doc.css(".iname")
  return if (nodes.nil? or nodes.empty?)
  text = nodes.first.content.strip

  unless .title
    data['title'] = text.capitalize
  end

  data['name'] = text
end

#load_html_organizations(doc, data) ⇒ Object

Class is iorg.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/indexer/importer/html.rb', line 209

def load_html_organizations(doc, data)
  nodes = doc.css('.iorg')
  return if (nodes.nil? or nodes.empty?)

  data['organizations'] ||= []

  nodes.each do |node|
    entry = {}

    if n = node.at_css('.name')
      entry['name'] = n.content.strip
    end

    if n = node.at_css('.email')
      text = n.attr(:href) || n.content.strip
      text = text.sub(/^mailto\:/i, '')
      entry['email'] = text
    end

    if n = node.at_css('.website') || node.at_css('.uri') || node.at_css('.url')
      text = n.attr(:href) || n.content.strip
      entry['website'] = text
    end

    data['organizations'] << entry if entry['name']
  end
end

#load_html_repositories(doc, data) ⇒ Object

Class is irepo.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/indexer/importer/html.rb', line 240

def load_html_repositories(doc, data)
  nodes = doc.css('.irepo')
  return if (nodes.nil? or nodes.empty?)

  data['repositories'] ||= []

  nodes.each do |node|
    entry = {}

    entry['uri']   = node.attr('href')
    entry['type']  = node.attr('name') || node.attr('title')  # best choice for this?
    entry['label'] = node.content.strip

    data['resources'] << entry if entry['uri']
  end
end

#load_html_requirements(doc, data) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/indexer/importer/html.rb', line 139

def load_html_requirements(doc, data)
  nodes = doc.css('.irequirement')
  return if (nodes.nil? or nodes.empty?)

  data['requirements'] ||= []

  nodes.each do |node|
    entry = {}

    if n = node.at_css('.name')
      entry['name'] = n.content.strip
    end

    if n = node.at_css('.version')
      entry['version'] = n.content.strip
    end

    # TODO: better approach to optional field?
    if n = node.at_css('.optional')
      entry['optional'] = true  #n.content.strip != "false"
    end

    if n = (node.at_css('.groups') || node.at_css('.group'))
      text = n.content.strip
      text = text.sub(/^[(]/, '').sub(/[)]$/, '').strip
      entry['groups'] = text.split(/\s+/)

      if %w{test build document development}.any?{ |g| entry['groups'].include?(g) }
        entry['development'] = true
      end
    end

    data['requirements'] << entry if entry['name']
  end
end

#load_html_resources(doc, data) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/indexer/importer/html.rb', line 119

def load_html_resources(doc, data)
  nodes = doc.css('.iresource')
  return if (nodes.nil? or nodes.empty?)

  data['resources'] ||= []

  nodes.each do |node|
    entry = {}

    entry['uri']   = node.attr('href')
    entry['type']  = node.attr('name') || node.attr('title')  # best choice for this?
    entry['label'] = node.content.strip

    data['resources'] << entry if entry['uri']
  end
end

#load_html_simple(field, doc, data) ⇒ Object

Load a simple field value.



64
65
66
67
68
69
# File 'lib/indexer/importer/html.rb', line 64

def load_html_simple(field, doc, data)
  nodes = doc.css(".i#{field}")
  return if (nodes.nil? or nodes.empty?)
  text = nodes.first.content.strip
  data[field] = text
end

#load_html_title(doc, data) ⇒ Object

Load title, and use it for name too if not already set.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/indexer/importer/html.rb', line 89

def load_html_title(doc, data)
  nodes = doc.css(".ititle")
  return if (nodes.nil? or nodes.empty?)
  text = nodes.first.content.strip

  unless .name
    data['name'] = text.downcase.gsub(/\s+/, '_')
  end

  data['title'] = text
end