Class | Tags::DownloadTag |
In: |
lib/webgen/plugins/tags/download.rb
|
Parent: | DefaultTag |
UNIT_NAMES | = | ['Byte', 'KiB', 'MiB', 'GiB', 'TiB'] |
# File lib/webgen/plugins/tags/download.rb, line 49 49: def initialize( plugin_manager ) 50: super 51: @plugin_manager['Core/ResourceManager'].append_data( 'webgen-css', ' 52: /* START webgen download tag */ 53: .webgen-file-icon, .webgen-download-icon { vertical-align: middle; } 54: /* STOP webgen download tag */ 55: ' ) 56: @default_mapping = load_mapping( File.join( Webgen.data_dir, 'icon_mapping.yaml' ) ) 57: end
# File lib/webgen/plugins/tags/download.rb, line 59 59: def process_tag( tag, chain ) 60: url = param( 'url' ) 61: return '' if url.nil? 62: 63: mapping = @default_mapping.dup 64: mapping.update( load_mapping( param( 'mappingFile' ) ) ) if File.exists?( param( 'mappingFile' ) || '' ) 65: 66: icon = file_icon( File.extname( url ), mapping, chain.last ) 67: output = '' 68: output << download_icon if param( 'alwaysShowDownloadIcon' ) || icon.nil? 69: output << icon unless icon.nil? 70: output << file_link( url, chain.last, chain.first ) 71: output << file_size( url, chain.first ) 72: end
# File lib/webgen/plugins/tags/download.rb, line 78 78: def download_icon 79: "<img class=\"webgen-download-icon\" src=\"{resource: webgen-icons-download}\" alt=\"Download icon\" />" 80: end
# File lib/webgen/plugins/tags/download.rb, line 82 82: def file_icon( ext, mapping, node ) 83: data = mapping[ext] 84: src = param( 'icon' ) 85: if src.nil? && !data.nil? 86: if data[0] == :resource 87: src = "{resource: #{data[1]}}" 88: else 89: icon_node = Node.root( node ).node_for_string( data[1] ) 90: src = node.route_to( icon_node ) unless icon_node.nil? 91: end 92: end 93: (src.nil? ? nil : "<img class=\"webgen-file-icon\" src=\"#{src}\" alt=\"File icon\" />") 94: end
# File lib/webgen/plugins/tags/download.rb, line 96 96: def file_link( url, node, ref_node ) 97: link = if URI.parse( url ).absolute? 98: url 99: else 100: file_node = ref_node.resolve_node( url ) 101: (file_node.nil? ? '' : node.route_to( file_node )) 102: end 103: "<a href=\"#{link}\">#{File.basename( url )}</a>" 104: end
# File lib/webgen/plugins/tags/download.rb, line 108 108: def file_size( url, ref_node ) 109: size = nil 110: catch :size do 111: begin 112: if URI.parse( url ).absolute? 113: open( url, :content_length_proc => proc {|size| throw :size} ) {|f| } 114: else 115: file_node = ref_node.resolve_node( url ) 116: size = File.size( file_node.node_info[:src] ) 117: end 118: rescue 119: end 120: end 121: 122: if size.nil? 123: log(:warn) { "Could not get file size information for file <#{url}>" } 124: '' 125: else 126: size, unit = [size.to_f, 0] 127: size, unit = [size / 1024, unit + 1] while size > 1024 128: format_str = if unit == 0 129: " (%d %s)" 130: else 131: " (%.2f %s)" 132: end 133: format_str % [size, UNIT_NAMES[unit]] 134: end 135: end
# File lib/webgen/plugins/tags/download.rb, line 137 137: def load_mapping( file ) 138: data = YAML::load( File.read( file ) ) 139: mapping = {} 140: if data['resource-mapping'] 141: data['resource-mapping'].each do |icon, exts| 142: exts.each {|ext| mapping[ext] = [:resource, icon]} 143: end 144: end 145: if data['file-mapping'] 146: data['file-mapping'].each do |icon, exts| 147: exts.each {|ext| mapping[ext] = [:file, icon]} 148: end 149: end 150: mapping 151: end