Class: YamlLinksToHtml

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiclious/yaml-links2html.rb

Constant Summary collapse

DEL_ICIO_US_URL =
"http://del.icio.us"
MAX_STYLES_IN_CLOUD =
10
CLOUD_STYLES =
(1..MAX_STYLES_IN_CLOUD).collect{ |size| 'cloud-' + size.to_s }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeYamlLinksToHtml

Returns a new instance of YamlLinksToHtml.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/graphiclious/yaml-links2html.rb', line 57

def initialize
  @include_private = false
  @write_only_diff_tags = true
  @diff_tags = Set.new
  @working_dir = Dir.getwd
  set_style(YamlLinksToHtml.guess_styles[0])
  set_user(YamlLinksToHtml.guess_users_within(@working_dir)[0])
  @link_to_delicious_if_possible = false
  @use_bundles = false
  @include_graph_views = false
  @build_graph_thumbs = @include_graph_views
  @num_files_written = 0
end

Instance Attribute Details

#build_graph_thumbsObject

Returns the value of attribute build_graph_thumbs.



14
15
16
# File 'lib/graphiclious/yaml-links2html.rb', line 14

def build_graph_thumbs
  @build_graph_thumbs
end

#include_graph_viewsObject

Returns the value of attribute include_graph_views.



14
15
16
# File 'lib/graphiclious/yaml-links2html.rb', line 14

def include_graph_views
  @include_graph_views
end

#include_privateObject

Returns the value of attribute include_private.



14
15
16
# File 'lib/graphiclious/yaml-links2html.rb', line 14

def include_private
  @include_private
end

Returns the value of attribute link_to_delicious_if_possible.



14
15
16
# File 'lib/graphiclious/yaml-links2html.rb', line 14

def link_to_delicious_if_possible
  @link_to_delicious_if_possible
end

#use_bundlesObject

Returns the value of attribute use_bundles.



14
15
16
# File 'lib/graphiclious/yaml-links2html.rb', line 14

def use_bundles
  @use_bundles
end

#write_only_diff_tagsObject

Returns the value of attribute write_only_diff_tags.



14
15
16
# File 'lib/graphiclious/yaml-links2html.rb', line 14

def write_only_diff_tags
  @write_only_diff_tags
end

Class Method Details

.guess_stylesObject



21
22
23
# File 'lib/graphiclious/yaml-links2html.rb', line 21

def self.guess_styles
  guess_styles_within(STYLES_FOLDER)
end

.guess_styles_within(folder) ⇒ Object



25
26
27
28
29
30
# File 'lib/graphiclious/yaml-links2html.rb', line 25

def self.guess_styles_within(folder)
  Dir[File.join(folder, '*', 'dot-attributes.yaml')].collect {
    |filename|
    File.dirname(filename)
  }
end

.guess_users_within(folder) ⇒ Object



32
33
34
35
36
37
# File 'lib/graphiclious/yaml-links2html.rb', line 32

def self.guess_users_within(folder)
  Dir[File.join(folder, '*', 'delicious.yaml')].collect {
    |filename|
    File.dirname(filename)
  }
end

Instance Method Details

#check_prerequisitesObject

Lazy require things which some users won’t use



17
18
19
# File 'lib/graphiclious/yaml-links2html.rb', line 17

def check_prerequisites
  require 'RMagick' if @build_graph_thumbs
end

#cloud_name_to_graph_name(cloud) ⇒ Object



357
358
359
# File 'lib/graphiclious/yaml-links2html.rb', line 357

def cloud_name_to_graph_name(cloud)
  UrlGraph.tag2file(cloud) + '__go'
end

#convert_to_thumbnail_filename(filename) ⇒ Object

name.extension will result in name.thumb.extension



315
316
317
318
319
# File 'lib/graphiclious/yaml-links2html.rb', line 315

def convert_to_thumbnail_filename(filename)
  parts = filename.split('.')
  ext = parts.pop
  parts[0..-1].push('thumb').push(ext).join('.')
end

#copy_style_to_userObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/graphiclious/yaml-links2html.rb', line 71

def copy_style_to_user
  ['dot-attributes.yaml', '*.jpg', '*.png', '*.gif', '*.css'].each do
    |pattern|
    Dir[File.join(@style, pattern)].each do
      |template_file|
      write_line_on_protokol("Copy #{template_file} to #{@user}")
      File.copy(template_file, File.join(@working_dir, @user)) 
    end
  end
end

#delicious_tag_url(tag1, *other_tags) ⇒ Object



118
119
120
121
122
# File 'lib/graphiclious/yaml-links2html.rb', line 118

def delicious_tag_url(tag1, *other_tags)
  url = "#{DEL_ICIO_US_URL}/tag/#{tag1}"
  other_tags.each { |tag| url = "#{url}+#{tag}" }
  url + '?settagview=cloud'
end

#delicious_user_url(tag1, *other_tags) ⇒ Object



124
125
126
127
128
# File 'lib/graphiclious/yaml-links2html.rb', line 124

def delicious_user_url(tag1, *other_tags)
  url = "#{DEL_ICIO_US_URL}/#{@user}/#{tag1}"
  other_tags.each { |tag| url = "#{url}+#{tag}" }
  url + '?settagview=cloud'
end

#do_graphviz(tag_graph, cloud_name) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/graphiclious/yaml-links2html.rb', line 321

def do_graphviz(tag_graph, cloud_name)
  return unless @include_graph_views
  name = cloud_name_to_graph_name(cloud_name)
  # Write Graphviz-File
  tmpDotFile = 'graph.dot~'
  tag_graph.print_dotted_on_file(tmpDotFile)
  write_line_on_protokol "Wrote #{tmpDotFile} for temporary use, feel free to delete later."
  
  # Use Graphviz to create HTML with client-side imagemap
  os_gif_file = os_file_join(@html_output_path, "#{name}.gif")
  dot_command = "dot -Tgif #{tmpDotFile} -o #{os_gif_file}" 
  system(dot_command) || fatal("Error can't execute #{dot_command}\nPlease check your graphviz installation")
  write_line_on_protokol "Wrote #{os_gif_file}."
  graph_htm = File.join(@html_output_path, "#{name}.htm")
  os_graph_htm = os_file_join(@html_output_path, "#{name}.htm")
  File.open(graph_htm, 'w') { |fp|
    write_html_head_on(fp)
    fp.puts("<h1>Tag graph: #{cloud_name} </h1>")
    link_index_on(fp, '', @bundles)
    fp.puts '<div class="main">'
    fp.puts '<IMG SRC="' + "#{name}.gif" + '" USEMAP=#UrlGraph>'
    fp.puts '</div>'
  }
  dot_command = "dot -Tcmapx #{tmpDotFile} -o graph.htm~"
  system(dot_command) || fatal("Failed system call: #{dot_command}")
  File.open(graph_htm, 'a') { |fp|
    File.open('graph.htm~'){ |fp2| fp2.each_line { |line| fp.puts(line) }}
    write_html_tail_on(fp)
  }
  write_line_on_protokol "Wrote #{os_graph_htm} (which uses #{os_gif_file})."
  
  return unless @build_graph_thumbs
  gif_file = File.join(@html_output_path, "#{name}.gif")
  thumbnail(gif_file)
end

#downscale(val) ⇒ Object



103
104
105
# File 'lib/graphiclious/yaml-links2html.rb', line 103

def downscale(val)
  (2 * Math.log(val)).round
end

#fatal(msg) ⇒ Object



301
302
303
304
# File 'lib/graphiclious/yaml-links2html.rb', line 301

def fatal(msg)
  write_line_on_protokol(msg)
  exit(1)
end

#get_cloud_style(val, maxval) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/graphiclious/yaml-links2html.rb', line 107

def get_cloud_style(val, maxval)
  quot = downscale(maxval) 
  quot = 1 if quot == 0
  index = (MAX_STYLES_IN_CLOUD - 1) * downscale(val) / quot
  if(index >= MAX_STYLES_IN_CLOUD)
    puts "Corrected index of cloud style for #{val} from #{index} to #{MAX_STYLES_IN_CLOUD}"
    index = MAX_STYLES_IN_CLOUD      
  end
  CLOUD_STYLES[index]
end

#init_tag_graphObject

create an empty graph with dot attributes of chosen style



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/graphiclious/yaml-links2html.rb', line 222

def init_tag_graph
  @tag_graph = UrlGraph.new
  if File.exists?(@dot_attribut_file)
    @tag_graph.fixed_dot_attributes = File.open(@dot_attribut_file, 'r') { |io|
      YAML.load(io)
    }
  else
    # write out the default-attribute-file to make
    # things easy to configure
    File.open(@dot_attribut_file, 'w') { |out|
      YAML.dump(@tag_graph.fixed_dot_attributes, out)
    }
    write_line_on_protokol "Wrote #{@dot_attribut_file}, change to adopt different style."
  end
end


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/graphiclious/yaml-links2html.rb', line 151

def link_index_on(fp, id='none', bundles={})
  fp.puts '<div class="mainmenu">'|| @checkBundles.checked?
  fp.puts menuitem('cloud (all)', id, 'index.htm')
  unless bundles.empty?
    bundles.keys.sort.each do
      |bundle|
      unless bundle == 'unbundled'
        fp.puts menuitem(bundle, id, "bundle_#{bundle}.htm")
      end
    end
    unless bundles['unbundled'].empty?
      fp.puts menuitem('unbundled', id, "bundle_unbundled.htm") 
    end
  end
  fp.puts '</div>'
end


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/graphiclious/yaml-links2html.rb', line 238

def load_links
  if(@write_only_diff_tags)
    @diff_tags = Set.new
    diff = File.open(@diff_file, 'r') { |io| YAML.load(io) }
    diff.keys.each { |k|
      diff[k]['tags'].each {|t| @diff_tags.add(t) }
    }
  end
  
  # load yaml-file containing links as a hash
  cache = File.open(@input_file, 'r') { |io|
    YAML.load(io)
  }
  links = cache.values
  unless(@include_private)
    links = links.reject { |l| l['shared'] =~ /no/ }
  end
  @links_by_tag = Hash.new
  @links_by_edge = Hash.new
  links.each do
    |link|
    tagsOfLink = link['tags'].reject { |t| t =~ /^for:/ }
    link['tags'] = tagsOfLink
    n = tagsOfLink.size - 1
    (0..n).each do
      |i|
      tag = tagsOfLink[i]
      unless @links_by_tag.has_key?(tag)
        @links_by_tag[tag] = Array.new
        @tag_graph.add_vertex(tag)
      end
      @links_by_tag[tag].push(link)
      (i+1..n).each do
        |j|
        otherTag = tagsOfLink[j]
        # write_line_on_protokol "\t#{tag}<->#{otherTag}"
        @tag_graph.add_edge(tag, otherTag)
        edge_name = UrlGraph.edge_name(tag, otherTag)
        unless @links_by_edge.has_key?(edge_name)
          @links_by_edge[edge_name] = Array.new
        end
        @links_by_edge[edge_name].push(link)
      end
    end
  end
  
  # load bundles file
  @bundles = {}
  if @use_bundles
    @bundles = File.open(@bundles_file, 'r') { |io|
      YAML.load(io)
    }
    set_of_unbundled_tags = {}
    @tag_graph.each { |tag| set_of_unbundled_tags[tag] = tag }
    @bundles.keys.each { |bundle|
      @bundles[bundle].each { |tag|
        set_of_unbundled_tags.delete(tag)
      }
    }
    @bundles['unbundled'] = set_of_unbundled_tags.values.sort
  end
end


143
144
145
146
147
148
149
# File 'lib/graphiclious/yaml-links2html.rb', line 143

def menuitem(name, id, url)
  if(name == id)
    "<div class=\"menuselected\">#{name}</div>"
  else
    "<div class=\"menuitem\"><a href=\"#{url}\">#{name}</a></div>"
  end
end

#os_file_join(dir, file) ⇒ Object

methods for html-generation



95
96
97
98
99
100
101
# File 'lib/graphiclious/yaml-links2html.rb', line 95

def os_file_join(dir, file)
  if RUBY_PLATFORM =~ /win32/
    File.join(dir, file).gsub(File::SEPARATOR, "\\")
  else
    File.join(dir, file)
  end
end

#reset_build_stateObject

make all run-variables garbage collectable



466
467
468
469
470
471
472
# File 'lib/graphiclious/yaml-links2html.rb', line 466

def reset_build_state
  @tag_graph = nil
  @links_by_tag = nil
  @links_by_edge = nil
  @bundles = nil
  @num_files_written = 0
end

#runObject

run the complete batch



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/graphiclious/yaml-links2html.rb', line 475

def run
  unless File.exists?(@input_file)
    raise "Missing input file #{@input_file}"
  end
  check_prerequisites
  Dir.mkdir(@html_output_path) unless File.directory?(@html_output_path)
  copy_style_to_user unless @user == @style
  init_tag_graph
  load_links
  write_all_overviews
  write_single_tag_files    
  write_double_tag_files
  write_line_on_protokol "#{@num_files_written.to_s} Files witten to #{@html_output_path}."
  reset_build_state
  write_line_on_protokol "Finished."
end

#set_protocol_block(aProc) ⇒ Object



82
83
84
# File 'lib/graphiclious/yaml-links2html.rb', line 82

def set_protocol_block(aProc)
  @protocol_block = aProc
end

#set_style(style) ⇒ Object



52
53
54
55
# File 'lib/graphiclious/yaml-links2html.rb', line 52

def set_style style
  @style = style
  @dot_attribut_file = "#{@style}/dot-attributes.yaml"
end

#set_user(user) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/graphiclious/yaml-links2html.rb', line 44

def set_user(user)
  @user = user
  @input_file = File.join(@working_dir, @user, 'delicious.yaml')
  @diff_file = File.join(@working_dir, @user, 'delicious.diff.yaml')
  @html_output_path = File.join(@working_dir, @user, 'html')
  @bundles_file = File.join(@working_dir, @user, 'bundles.yaml')
end

#set_working_dir(new_working_dir) ⇒ Object



39
40
41
42
# File 'lib/graphiclious/yaml-links2html.rb', line 39

def set_working_dir(new_working_dir)
  @working_dir = new_working_dir
  set_user(@user)
end


200
201
202
# File 'lib/graphiclious/yaml-links2html.rb', line 200

def sorted_links(links)
  links.sort { |l1, l2| l1['description'] <=> l2['description'] }
end

#thumbnail(filename) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/graphiclious/yaml-links2html.rb', line 306

def thumbnail(filename)
  thumb_name = convert_to_thumbnail_filename(filename)
  write_line_on_protokol("Writing thumbnail #{thumb_name} for #{filename}")
  img = Magick::Image.read(filename)[0]
  thumb = img.thumbnail(100, 100)
  thumb.write(thumb_name)
end

#write_all_overviewsObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/graphiclious/yaml-links2html.rb', line 361

def write_all_overviews
  # Create a file containing a cloud of all tags
  list = @links_by_tag.keys.sort
  filename = 'index.htm'
  cloud_name = 'cloud (all)'
  write_overview(filename, cloud_name, list, @bundles, @links_by_tag)
  do_graphviz(@tag_graph, cloud_name)
  
  # The same for all bundles
  @bundles.keys.each do 
    | bundle |
    list = @bundles[bundle]
    filename = "bundle_#{bundle}.htm"
    cloud_name = bundle
    write_overview(filename, cloud_name, list, @bundles, @links_by_tag)
    @tag_graph.filter_set = Set.new(list)
    do_graphviz(@tag_graph, cloud_name)
    @tag_graph.filter_set = nil
  end
end

#write_double_tag_filesObject



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/graphiclious/yaml-links2html.rb', line 428

def write_double_tag_files
  # Create one Html-File for each edge between two tags
  @tag_graph.each_edge do
    |tag1,tag2|
    if !@write_only_diff_tags or (@diff_tags.include?(tag1) and @diff_tags.include?(tag2))
      edge_name = UrlGraph.edge_name(tag1, tag2)
      edge_url = UrlGraph.edge_url(tag1, tag2)
      File.open(File.join(@html_output_path, edge_url), 'w') do
        |fp|
        write_html_head_on(fp)
        fp.puts "<h1>Tags: #{tag1} AND #{tag2}</h1>"
        link_index_on(fp, 'none', @bundles)
        fp.puts '<div class="main">'
        ankor1 = UrlGraph.node_ankor(tag1)
        ankor2 = UrlGraph.node_ankor(tag2)
        fp.puts "<h3>Links for tag combination #{ankor1}/#{ankor2}</h3>"
        fp.puts '<ul>'
        sorted_links(@links_by_edge[edge_name]).each do
          |link|
          fp.puts('<li>')
          write_link_on(link, fp, [tag1, tag2])
          fp.puts('<br></li>')
        end
        fp.puts '</ul>'
        if @link_to_delicious_if_possible
          fp.puts("<h3>#{UrlGraph.ankor(DEL_ICIO_US_URL, 'del.icio.us')} links with tag combination #{ankor1}/#{ankor2}</h3>")
          fp.puts(UrlGraph.ankor(delicious_user_url(tag1, tag2), @user))
          fp.puts(UrlGraph.ankor(delicious_tag_url(tag1, tag2), 'all'))
        end
        fp.puts '</div>'
        write_html_tail_on(fp)
      end
    end
  end
  write_line_on_protokol "Wrote one html-file for each combination of two tags."
end

#write_html_head_on(fp) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/graphiclious/yaml-links2html.rb', line 130

def write_html_head_on(fp)
  fp.puts '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
  fp.puts '<html><head>'
  fp.puts '<meta http-equiv="content-type" content="text/html; charset=UTF-8">'
  fp.puts '<link rel="stylesheet" type="text/css" href="../style.css">'
  fp.puts '</head><body>'
end

#write_html_tail_on(fp) ⇒ Object



138
139
140
141
# File 'lib/graphiclious/yaml-links2html.rb', line 138

def write_html_tail_on(fp)
  fp.puts '</body></html>'
  @num_files_written = @num_files_written + 1
end

#write_line_on_protokol(lineString) ⇒ Object



86
87
88
89
90
91
# File 'lib/graphiclious/yaml-links2html.rb', line 86

def write_line_on_protokol(lineString)
  puts lineString
  unless @protocol_block.nil?
    @protocol_block.call(lineString)
  end
end


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/graphiclious/yaml-links2html.rb', line 204

def write_link_on(link, fp, skip_tags = [])
  fp.puts(UrlGraph.ankor(link['href'], link['description']))
  info = link['extended']
  unless info.nil? || info.empty?
    fp.puts('<br>')
    fp.puts("#{info}")
  end
  other_tags = link['tags'].reject { |t| skip_tags.include?(t) }
  unless other_tags.empty?
    fp.puts('<br>')
    fp.puts('More tags: ')
    other_tags.each { |t|
      fp.puts(UrlGraph.node_ankor(t))
    }
  end
end

#write_overview(filename, cloud_name, list, bundles, links_by_tag) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/graphiclious/yaml-links2html.rb', line 168

def write_overview(filename, cloud_name, list, bundles, links_by_tag)
  valid_list = list.reject { |tag|
    links_by_tag[tag].nil? # doesn't make sense but seems to be possible
  }
  File.open(File.join(@html_output_path, filename), 'w') do
    | fp |
    write_html_head_on(fp)
    fp.puts "<h1>Tag overview: #{cloud_name}</h1>"
    link_index_on(fp, cloud_name, bundles)
    fp.puts '<div class="main">'
    fp.puts "<h3>Tags, size representing number of links</h3>"
    max_num_links = valid_list.collect{ |tag| links_by_tag[tag].size }.max
    valid_list.each do
      |tag|
      style = get_cloud_style(links_by_tag[tag].size, max_num_links)
      fp.puts(UrlGraph.node_ankor(tag, style))
    end
    if(@include_graph_views)
      name = cloud_name_to_graph_name(cloud_name)
      fp.puts "<h3>Graph of tag relations</h3>"
      
      link_desc = if(@build_graph_thumbs)
        "<img src=\"#{convert_to_thumbnail_filename(name + '.gif')}\">"
      else
        'view it'
      end 
      fp.puts "<a href=\"#{name}.htm\">#{link_desc}</a>"
      fp.puts '</div>'
    end 
  end
end

#write_single_tag_filesObject

Create one Html-File for each tag



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/graphiclious/yaml-links2html.rb', line 383

def write_single_tag_files
  @links_by_tag.keys.sort.each do
    |tag|
    if !@write_only_diff_tags or @diff_tags.include?(tag)
      File.open(File.join(@html_output_path, UrlGraph.node_url(tag)), 'w') do
        |fp|
        write_html_head_on(fp)
        fp.puts "<h1>Tag: #{tag}</h1>"
        link_index_on(fp, 'none', @bundles)
        fp.puts '<div class="main">'
        fp.puts "<h3>Subcategories for tag #{tag}</h3>"
        subcloud = @tag_graph.adjacent_vertices(tag).sort
        max_num_links = subcloud.collect{ |n| @links_by_edge[UrlGraph.edge_name(tag, n)].size }.max
        subcloud.each do
          |neighbor|
          edge_name = UrlGraph.edge_name(tag, neighbor)
          edgePage = UrlGraph.edge_url(tag, neighbor)
          style = get_cloud_style(@links_by_edge[edge_name].size, max_num_links)
          fp.puts(UrlGraph.intern_ankor(UrlGraph.edge_url(tag, neighbor), neighbor, style))
        end
        list = @links_by_tag[tag].select { |l| l['tags'].size == 1 }
        unless list.empty?
          fp.puts("<h3>Links with the single tag #{tag}</h3>")
          fp.puts('<ul>')
          sorted_links(list).each do
            |link|
            fp.puts('<li>')
            write_link_on(link, fp, [tag])
            fp.puts('</li>')
          end
          fp.puts('</ul>')
        end
        if @link_to_delicious_if_possible
          fp.puts("<h3>#{UrlGraph.ankor(DEL_ICIO_US_URL, 'del.icio.us')} links with tag #{tag}</h3>")
          fp.puts(UrlGraph.ankor(delicious_user_url(tag), "#{@user}"))
          fp.puts(UrlGraph.ankor(delicious_tag_url(tag), "all"))
        end
        fp.puts '</div>'
        write_html_tail_on(fp)
      end
    end
  end
  write_line_on_protokol "Wrote one html-file for each tag."
end