Class: YamlLinksToJavascript
- Inherits:
-
Object
- Object
- YamlLinksToJavascript
- Defined in:
- lib/graphiclious/yaml-links2js.rb
Instance Attribute Summary collapse
-
#include_private ⇒ Object
Returns the value of attribute include_private.
-
#use_bundles ⇒ Object
Returns the value of attribute use_bundles.
-
#write_only_diff_tags ⇒ Object
Returns the value of attribute write_only_diff_tags.
Instance Method Summary collapse
- #as_javascript_string(ruby_string) ⇒ Object
-
#copy_scripts ⇒ Object
copy graphiclious javascripts to working dir.
- #fatal(msg) ⇒ Object
-
#initialize ⇒ YamlLinksToJavascript
constructor
A new instance of YamlLinksToJavascript.
-
#load_bundles ⇒ Object
load bundles file compute virtual bundles ‘full cloud’ and ‘unbundled’.
- #load_links ⇒ Object
-
#run ⇒ Object
run the complete batch.
- #set_protocol_block(aProc) ⇒ Object
-
#set_working_dir(new_working_dir) ⇒ Object
differs from working dir in YamlLinksToHtml because we do not need to know the user.
- #with_virtual_bundles(bundles) ⇒ Object
- #write_all_bundles_file(bundles) ⇒ Object
- #write_all_links_file(links) ⇒ Object
-
#write_js_head_on(fp) ⇒ Object
methods for js-generation.
- #write_line_on_protokol(lineString) ⇒ Object
- #write_link_on(link, fp, skip_tags = []) ⇒ Object
-
#write_single_tag_files ⇒ Object
Create one Javascript-File for each tag.
Constructor Details
#initialize ⇒ YamlLinksToJavascript
Returns a new instance of YamlLinksToJavascript.
22 23 24 25 26 27 28 |
# File 'lib/graphiclious/yaml-links2js.rb', line 22 def initialize @include_private = false @write_only_diff_tags = true @use_bundles = true @diff_tags = Set.new set_working_dir(Dir.getwd) end |
Instance Attribute Details
#include_private ⇒ Object
Returns the value of attribute include_private.
10 11 12 |
# File 'lib/graphiclious/yaml-links2js.rb', line 10 def include_private @include_private end |
#use_bundles ⇒ Object
Returns the value of attribute use_bundles.
10 11 12 |
# File 'lib/graphiclious/yaml-links2js.rb', line 10 def use_bundles @use_bundles end |
#write_only_diff_tags ⇒ Object
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 end |
Instance Method Details
#as_javascript_string(ruby_string) ⇒ Object
62 63 64 |
# File 'lib/graphiclious/yaml-links2js.rb', line 62 def as_javascript_string(ruby_string) ruby_string ? ruby_string.gsub(/'/, "\\\\'").gsub(/\n/, "\\\\n") : '' end |
#copy_scripts ⇒ Object
copy graphiclious javascripts to working dir
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graphiclious/yaml-links2js.rb', line 42 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
129 130 131 132 |
# File 'lib/graphiclious/yaml-links2js.rb', line 129 def fatal(msg) write_line_on_protokol(msg) exit(1) end |
#load_bundles ⇒ Object
load bundles file compute virtual bundles ‘full cloud’ and ‘unbundled’
121 122 123 124 125 126 127 |
# File 'lib/graphiclious/yaml-links2js.rb', line 121 def load_bundles return {} unless @use_bundles bundles = {} bundles = File.open(@bundles_file, 'r') { |io| YAML.load(io) } end |
#load_links ⇒ Object
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 110 111 112 113 114 115 116 117 |
# File 'lib/graphiclious/yaml-links2js.rb', line 77 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| = link['tags'].reject { |t| t =~ /^for:/ } link['tags'] = n = .size - 1 (0..n).each do |i| tag = [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 |
#run ⇒ Object
run the complete batch
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/graphiclious/yaml-links2js.rb', line 202 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 if @use_bundles && File.exists?(@bundles_file) bundles = load_bundles else bundles = {} end write_all_bundles_file bundles write_line_on_protokol "Finished." end |
#set_protocol_block(aProc) ⇒ Object
30 31 32 |
# File 'lib/graphiclious/yaml-links2js.rb', line 30 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 20 |
# 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') @bundles_file = File.join(@working_dir, 'bundles.yaml') @diff_file = File.join(@working_dir, 'delicious.diff.yaml') @js_output_path = File.join(@working_dir, 'js') end |
#with_virtual_bundles(bundles) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/graphiclious/yaml-links2js.rb', line 161 def with_virtual_bundles bundles = {} @links_by_tag.keys.each { |tag| [tag] = tag } bundles['full cloud'] = .values.sort bundles.keys.each { |bundle| bundles[bundle].each { |tag| .delete(tag) } } unless .empty? bundles['unbundled'] = .values.sort end bundles end |
#write_all_bundles_file(bundles) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/graphiclious/yaml-links2js.rb', line 178 def write_all_bundles_file bundles unless bundles.empty? bundle_names = bundles.keys.sort bundle_names.unshift('full cloud'); bundle_names.push('unbundled') if bundles['unbundled']; all_bundles = with_virtual_bundles bundles else bundle_names = Array.new all_bundles = bundles end File.open(File.join(@js_output_path, "_bundles.js"), 'w') do |fp| fp.puts '/*---javascript generated by graphiclious/yaml-links2js.rb---*/' fp.puts fp.puts "var bundle_names = new Array('#{bundle_names.join("','")}');" fp.puts 'var bundles = new Array();' bundle_names.each { |name| = "\n'#{bundles[name].join("',\n'")}'\n" fp.puts "bundles['#{name}'] = new Array(#{});" } end end |
#write_all_links_file(links) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/graphiclious/yaml-links2js.rb', line 151 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
55 56 57 58 59 60 |
# File 'lib/graphiclious/yaml-links2js.rb', line 55 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
34 35 36 37 38 39 |
# File 'lib/graphiclious/yaml-links2js.rb', line 34 def write_line_on_protokol(lineString) puts lineString unless @protocol_block.nil? @protocol_block.call(lineString) end end |
#write_link_on(link, fp, skip_tags = []) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/graphiclious/yaml-links2js.rb', line 66 def write_link_on(link, fp, = []) fp.puts "links.push(new Link(" fp.puts "\t'#{link['href']}'," = link['tags'].reject { |t| .include?(t) } fp.puts "\tnew Array(" + .collect{|t| "'#{t}'"}.join(", ") + ")," desc = as_javascript_string(link['description']) fp.puts "\t'#{desc}', " ext = as_javascript_string(link['extended']) fp.puts "\t'#{ext}'));" end |
#write_single_tag_files ⇒ Object
Create one Javascript-File for each tag
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/graphiclious/yaml-links2js.rb', line 135 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 |