Class: YamlLinksToJavascript

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeYamlLinksToJavascript

Returns a new instance of YamlLinksToJavascript.



21
22
23
24
25
26
# File 'lib/graphiclious/yaml-links2js.rb', line 21

def initialize
  @include_private = false
  @write_only_diff_tags = true
  @diff_tags = Set.new
  set_working_dir(Dir.getwd)
end

Instance Attribute Details

#include_privateObject

Returns the value of attribute include_private.



10
11
12
# File 'lib/graphiclious/yaml-links2js.rb', line 10

def include_private
  @include_private
end

#write_only_diff_tagsObject

Returns the value of attribute write_only_diff_tags.



10
11
12
# File 'lib/graphiclious/yaml-links2js.rb', line 10

def write_only_diff_tags
  @write_only_diff_tags
end

Instance Method Details

#copy_scriptsObject

copy graphiclious javascripts to working dir



40
41
42
43
44
45
46
47
48
49
# File 'lib/graphiclious/yaml-links2js.rb', line 40

def copy_scripts
  ['*.js', '*.htm', '*.css'].each do
    |pattern|
    Dir[File.join(JS_TEMPLATES_FOLDER, pattern)].each do
      |template_file|
      write_line_on_protokol("Copy #{template_file}")
      File.copy(template_file, @js_output_path) 
    end
  end
end

#fatal(msg) ⇒ Object



111
112
113
114
# File 'lib/graphiclious/yaml-links2js.rb', line 111

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


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/graphiclious/yaml-links2js.rb', line 69

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.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
      end
      @links_by_tag[tag].push(link)
    end
  end
  
  @links_by_tag.keys.each { |tag|
    @links_by_tag[tag] = @links_by_tag[tag].sort { |l1, l2|
      l1['description'] <=> l2['description']
    }
  }
  links
end

#runObject

run the complete batch



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/graphiclious/yaml-links2js.rb', line 144

def run
  unless File.exists?(@input_file)
    raise "Missing input file #{@input_file}"
  end
  Dir.mkdir(@js_output_path) unless File.directory?(@js_output_path)
  copy_scripts
  links = load_links
  write_all_links_file links
  # write_single_tag_files    
  write_line_on_protokol "Finished."
end

#set_protocol_block(aProc) ⇒ Object



28
29
30
# File 'lib/graphiclious/yaml-links2js.rb', line 28

def set_protocol_block(aProc)
  @protocol_block = aProc
end

#set_working_dir(new_working_dir) ⇒ Object

differs from working dir in YamlLinksToHtml because we do not need to know the user



14
15
16
17
18
19
# File 'lib/graphiclious/yaml-links2js.rb', line 14

def set_working_dir(new_working_dir)
  @working_dir = new_working_dir
  @input_file = File.join(@working_dir, 'delicious.yaml')
  @diff_file = File.join(@working_dir, 'delicious.diff.yaml')
  @js_output_path = File.join(@working_dir, 'js')
end


133
134
135
136
137
138
139
140
141
# File 'lib/graphiclious/yaml-links2js.rb', line 133

def write_all_links_file links
  File.open(File.join(@js_output_path, "_all.js"), 'w') do
    |fp|
    write_js_head_on(fp)
    links.each { |link|
      write_link_on(link, fp, [])
    }
  end
end

#write_js_head_on(fp) ⇒ Object

methods for js-generation



53
54
55
56
57
58
# File 'lib/graphiclious/yaml-links2js.rb', line 53

def write_js_head_on(fp)
  fp.puts '/*---javascript generated by graphiclious/yaml-links2js.rb---*/'
  fp.puts
  fp.puts 'var links = new Array();' 
  fp.puts
end

#write_line_on_protokol(lineString) ⇒ Object



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

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


60
61
62
63
64
65
66
67
# File 'lib/graphiclious/yaml-links2js.rb', line 60

def write_link_on(link, fp, skip_tags = [])
  fp.puts "links.push(new Link("
  fp.puts "\t'#{link['href']}',"
  other_tags = link['tags'].reject { |t| skip_tags.include?(t) }
  fp.puts "\tnew Array(" + other_tags.collect{|t| "'#{t}'"}.join(", ") + "),"
  desc = link['description'].gsub(/'/, "\\'")
  fp.puts "\t'#{desc}'));"
end

#write_single_tag_filesObject

Create one Javascript-File for each tag



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/graphiclious/yaml-links2js.rb', line 117

def write_single_tag_files
  @links_by_tag.keys.each do
    |tag|
    if !@write_only_diff_tags or @diff_tags.include?(tag)
      File.open(File.join(@js_output_path, "#{UrlGraph.tag2file(tag)}.js"), 'w') do
        |fp|
        write_js_head_on(fp)
        @links_by_tag[tag].each { |link|
          write_link_on(link, fp, [tag])
        }
      end
    end
  end
  write_line_on_protokol "Wrote one js-file for each tag to directory #{@js_output_path}."
end