Module: AsFoo::AsHtml

Included in:
String
Defined in:
lib/as_foo/as_html.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_pagerObject



7
8
9
10
11
# File 'lib/as_foo/as_html.rb', line 7

def available_pager
  @@_as_foo_html_pager ||= %i(w3m elinks lynx links).find {|command|
    system("which #{command}", out: "/dev/null", err: "/dev/null")
  }
end

Instance Method Details

#as_html(with: nil, options: nil) ⇒ String

Returns converted string.

Returns:

  • (String)

    converted string



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/as_foo/as_html.rb', line 15

def as_html(with: nil, options: nil)
  pager = with || AsHtml.available_pager

  raise "could not find available pager command" unless pager

  case pager
  when :w3m
    Open3.popen3("w3m -dump -T text/html") do |stdin, stdout, stderr|
      stdin.puts self.to_s
      stdin.close
      stdout.read
    end
  when :lynx
    Open3.popen3("lynx -dump -nonumbers -nolist -stdin") do |stdin, stdout, stderr|
      stdin.puts self.to_s
      stdin.close
      stdout.read
    end
  when :links
    Tempfile.open ["as_foo", ".html"] do |src|
      src.puts self.to_s
      src.flush

      `links -dump #{src.path}`
    end
  when :elinks
    Open3.popen3("elinks -dump -no-numbering -no-references") do |stdin, stdout, stderr|
      stdin.puts self.to_s
      stdin.close
      stdout.read
    end
  else
    raise ArgumentError.new("unexpected method #{pager}")
  end
end