Class: MHT::MhtmlGenerator
- Inherits:
-
Object
- Object
- MHT::MhtmlGenerator
- Defined in:
- lib/mht.rb
Overview
generate mhtml file
uri target uri
return mhtml file mhtml = MHT::MhtmlGenerator.generate(“rubygems.org/”) open(“output.mht”, “w+”){|f| f.write mhtml }
Instance Attribute Summary collapse
-
#conf ⇒ Object
Returns the value of attribute conf.
Class Method Summary collapse
Instance Method Summary collapse
- #add_html_content(cid) ⇒ Object
- #attach_contents ⇒ Object
- #convert(filename_or_uri) ⇒ Object
- #download_finished? ⇒ Boolean
-
#initialize ⇒ MhtmlGenerator
constructor
A new instance of MhtmlGenerator.
- #start_download_thread(num = 5) ⇒ Object
Constructor Details
#initialize ⇒ MhtmlGenerator
Returns a new instance of MhtmlGenerator.
27 28 29 30 31 32 33 34 |
# File 'lib/mht.rb', line 27 def initialize @contents = {} @src = StringIO.new @boundary = "mimepart_#{Digest::MD5.hexdigest(Time.now.to_s)}" @threads = [] @queue = Queue.new @conf = { :base64_except=>["html"]} end |
Instance Attribute Details
#conf ⇒ Object
Returns the value of attribute conf.
22 23 24 |
# File 'lib/mht.rb', line 22 def conf @conf end |
Class Method Details
.generate(uri) ⇒ Object
23 24 25 26 |
# File 'lib/mht.rb', line 23 def MhtmlGenerator.generate(uri) generateror = MhtmlGenerator.new return generateror.convert(uri) end |
Instance Method Details
#add_html_content(cid) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mht.rb', line 117 def add_html_content(cid) filename = File.basename(URI(@contents[cid][:uri]).path) @src.puts "--#{@boundary}" @src.puts "Content-Disposition: inline; filename=" + filename @src.puts "Content-Type: #{@contents[cid][:content_type]}" @src.puts "Content-Location: #{@contents[cid][:uri]}" @src.puts "Content-Transfer-Encoding: Base64" @src.puts "Content-Id: #{cid}" @src.puts "" @src.puts "#{Base64.encode64(@contents[cid][:body])}" @src.puts "" return end |
#attach_contents ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/mht.rb', line 108 def attach_contents #prepeare_queue @contents.each{|k,v| @queue.push k} #start download threads self.start_download_thread # wait until download finished. @threads.each{|t|t.join} @contents.each{|k,v|self.add_html_content(k)} end |
#convert(filename_or_uri) ⇒ Object
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mht.rb', line 35 def convert(filename_or_uri) f = open(filename_or_uri) html = f.read @parser = Nokogiri::HTML html @src.puts "Subject: " + @parser.search("title").text() @src.puts "Content-Type: multipart/related; boundary=#{@boundary}" @src.puts "Content-Location: #{filename_or_uri}" @src.puts "Date: #{Time.now.to_s}" @src.puts "MIME-Version: 1.0" @src.puts "" @src.puts "mime mhtml content" @src.puts "" #imgs @parser.search('img').each{|i| uri = i.attr('src'); uri = URI::join( filename_or_uri, uri).to_s uid = Digest::MD5.hexdigest(uri) @contents[uid] = uri i.set_attribute('src',"cid:#{uid}") } #styles @parser.search('link[rel=stylesheet]').each{|i| uri = i.attr('href'); uri = URI::join( filename_or_uri, uri).to_s uid = Digest::MD5.hexdigest(uri) @contents[uid] = uri i.set_attribute('href',"cid:#{uid}") } #scripts @parser.search('script').map{ |i| next unless i.attr('src'); uri = i.attr('src'); uri = URI::join( filename_or_uri, uri).to_s uid = Digest::MD5.hexdigest(uri) @contents[uid] = uri i.set_attribute('src',"cid:#{uid}") } @src.puts "--#{@boundary}" @src.puts "Content-Disposition: inline; filename=default.htm" @src.puts "Content-Type: #{f.['content-type']}" @src.puts "Content-Id: #{Digest::MD5.hexdigest(filename_or_uri)}" @src.puts "Content-Location: #{filename_or_uri}" @src.puts "Content-Transfer-Encoding: 8bit" if @conf[:base64_except].find("html") @src.puts "Content-Transfer-Encoding: Base64" unless @conf[:base64_except].find("html") @src.puts "" #@src.puts html @src.puts "#{html}" if @conf[:base64_except].find("html") #@src.puts "#{Base64.encode64(html)}" unless @conf[:base64_except].find("html") @src.puts "" self.attach_contents @src.puts "--#{@boundary}--" @src.rewind return @src.read end |
#download_finished? ⇒ Boolean
105 106 107 |
# File 'lib/mht.rb', line 105 def download_finished? @contents.find{|k,v| v.class != Hash } == nil end |
#start_download_thread(num = 5) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mht.rb', line 89 def start_download_thread(num=5) num.times{ t = Thread.start{ while(@queue.empty? == false) k = @queue.pop v = @contents[k] next if v.class == Hash f = open(v) = f. @contents[k] = { :body=>f.read, :uri=> v, :content_type=> f.["content-type"] } end } @threads.push t } return @threads end |