Class: Brrowser::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/brrowser/renderer.rb

Constant Summary collapse

ANSI_RE =
/\e\[[0-9;]*m/.freeze
SKIP_ELEMENTS =
%w[script style noscript svg head].freeze
BLOCK_ELEMENTS =
%w[
  p div section article aside main header footer nav
  h1 h2 h3 h4 h5 h6 ul ol li dl dt dd
  blockquote pre figure figcaption address
  table thead tbody tfoot tr
  form fieldset details summary hr br
].freeze
IMG_RESERVE =

Blank lines reserved for each image

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width) ⇒ Renderer

Returns a new instance of Renderer.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/brrowser/renderer.rb', line 19

def initialize(width)
  @width    = width
  @links    = []
  @images   = []
  @forms    = []
  @output   = []
  @line     = ""
  @col      = 0
  @indent   = 0
  @pre      = false
  @ol_count = []
  @current_form = nil
end

Instance Attribute Details

#formsObject (readonly)

Returns the value of attribute forms.



17
18
19
# File 'lib/brrowser/renderer.rb', line 17

def forms
  @forms
end

#imagesObject (readonly)

Returns the value of attribute images.



17
18
19
# File 'lib/brrowser/renderer.rb', line 17

def images
  @images
end

Returns the value of attribute links.



17
18
19
# File 'lib/brrowser/renderer.rb', line 17

def links
  @links
end

Instance Method Details

#render(html, base_url = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/brrowser/renderer.rb', line 33

def render(html, base_url = nil)
  @base_url = base_url
  @links    = []
  @images   = []
  @forms    = []
  @output   = []
  @line     = ""
  @col      = 0

  doc = Nokogiri::HTML(html)
  title = doc.at_css("title")&.text&.strip || ""

  body = doc.at_css("body") || doc
  walk(body)
  flush_line

  site_colors = extract_site_colors(doc)
  { text: @output.join("\n"), links: @links, images: @images, forms: @forms, title: title, colors: site_colors }
end