Class: Ghostwriter::Writer

Inherits:
Object
  • Object
show all
Includes:
ListWriter, TableWriter
Defined in:
lib/ghostwriter/writer.rb

Overview

Main Ghostwriter converter object.

Defined Under Namespace

Modules: ListWriter, TableWriter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TableWriter

#add_table_header_underline, #calculate_column_sizes, #replace_table_nodes, #replace_tables

Methods included from ListWriter

#replace_list_items, #replace_lists

Constructor Details

#initialize(link_base: '', heading_marker: '--', ul_marker: '-', ol_marker: '1', table_column: '|', table_row: '-', table_corner: '|') ⇒ Writer

Creates a new ghostwriter

Parameters:

  • link_base (String) (defaults to: '')

    the url to prefix relative links with



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ghostwriter/writer.rb', line 11

def initialize(link_base: '', heading_marker: '--', ul_marker: '-', ol_marker: '1',
               table_column: '|', table_row: '-', table_corner: '|')
   @link_base      = link_base
   @heading_marker = heading_marker
   @ul_marker      = ul_marker
   @ol_marker      = ol_marker
   @table_column   = table_column
   @table_row      = table_row
   @table_corner   = table_corner

   freeze
end

Instance Attribute Details

#heading_markerObject (readonly)

Returns the value of attribute heading_marker.



6
7
8
# File 'lib/ghostwriter/writer.rb', line 6

def heading_marker
  @heading_marker
end

Returns the value of attribute link_base.



6
7
8
# File 'lib/ghostwriter/writer.rb', line 6

def link_base
  @link_base
end

#ol_markerObject (readonly)

Returns the value of attribute ol_marker.



6
7
8
# File 'lib/ghostwriter/writer.rb', line 6

def ol_marker
  @ol_marker
end

#table_columnObject (readonly)

Returns the value of attribute table_column.



6
7
8
# File 'lib/ghostwriter/writer.rb', line 6

def table_column
  @table_column
end

#table_cornerObject (readonly)

Returns the value of attribute table_corner.



6
7
8
# File 'lib/ghostwriter/writer.rb', line 6

def table_corner
  @table_corner
end

#table_rowObject (readonly)

Returns the value of attribute table_row.



6
7
8
# File 'lib/ghostwriter/writer.rb', line 6

def table_row
  @table_row
end

#ul_markerObject (readonly)

Returns the value of attribute ul_marker.



6
7
8
# File 'lib/ghostwriter/writer.rb', line 6

def ul_marker
  @ul_marker
end

Instance Method Details

#textify(html) ⇒ Object

Strips HTML down to plain text.

Parameters:

  • html (String)

    the HTML to be convert to text

Returns:

  • converted text



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ghostwriter/writer.rb', line 29

def textify(html)
   doc = Nokogiri::HTML(html.gsub(/\s+/, ' '))

   doc.search('style, script').remove

   replace_anchors(doc)
   replace_images(doc)

   simple_replace(doc, '*[role="presentation"]', "\n")

   replace_headers(doc)
   replace_lists(doc)
   replace_tables(doc)

   simple_replace(doc, 'hr', "\n----------\n\n")
   simple_replace(doc, 'br', "\n")
   simple_replace(doc, 'p', "\n\n")

   normalize_lines(doc)
end