Class: Web2Text::FilePrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/web2text/formatters.rb

Overview

Writes one file per page

Instance Method Summary collapse

Constructor Details

#initialize(crawl, out_dir) ⇒ FilePrinter

Returns a new instance of FilePrinter.



26
27
28
29
30
31
32
33
# File 'lib/web2text/formatters.rb', line 26

def initialize(crawl, out_dir)
  root_path = URI(crawl.url).path.to_s
  root_path = "/" if root_path.empty?

  @crawl_root = Pathname(root_path)
  @out_dir = Pathname(out_dir)
  @out_dir.mkpath
end

Instance Method Details

#append(doc, uri) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/web2text/formatters.rb', line 35

def append(doc, uri)
  path = @out_dir + Pathname(URI(uri).path).relative_path_from(@crawl_root)
  if path.extname == "" then
    path = path + 'index.txt'
  end

  path = path.sub_ext('.txt')

  path.parent.mkpath
  path.open("w") { |f| f.write(doc) }
  self
end

#closeObject



48
49
# File 'lib/web2text/formatters.rb', line 48

def close
end