Class: Wraptext::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/wraptext/parser.rb

Constant Summary collapse

BLOCK_TAGS =
%w"table thead tfoot caption col colgroup tbody tr td th div dl dd dt
ul ol li pre select option form map area blockquote address math style input hr
fieldset legend section article aside hgroup header footer nav p
figure figcaption details menu summary h1 h2 h3 h4 h5 h6 script"
BLOCK_TAGS_LOOKUP =
Hash[*BLOCK_TAGS.map {|e| [e, 1]
NO_WRAP_IN =
%w"h1 h2 h3 h4 h5 h6"
NO_WRAP_IN_LOOKUP =
STRAIGHT_COPY_TAGS =
%w"script pre textarea style"
STRAIGHT_COPY_TAGS_LOOKUP =
MULTIPLE_NEWLINES_REGEX =
/(\r\n|\n){2,}/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text_or_nokogiri_doc) ⇒ Parser

Returns a new instance of Parser.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wraptext/parser.rb', line 20

def initialize(text_or_nokogiri_doc)
  @doc = if text_or_nokogiri_doc.is_a? Nokogiri::XML::Document
    text_or_nokogiri_doc
  elsif text_or_nokogiri_doc.is_a? String
    Nokogiri::HTML text_or_nokogiri_doc
  else
    raise "#initialize requires a string or Nokogiri document"
  end
  @root = Nokogiri::HTML "<body></body>"
  reparent_nodes @root.xpath("/html/body").first, @doc.xpath("/html/body").first
  replace_single_breaks
  strip_empty_paragraphs!
end

Class Method Details

.parse(text) ⇒ Object



16
17
18
# File 'lib/wraptext/parser.rb', line 16

def self.parse(text)
  new(text).to_html
end

Instance Method Details

#to_docObject



38
39
40
# File 'lib/wraptext/parser.rb', line 38

def to_doc
  @doc_out ||= @root.xpath("/html/body").first
end

#to_htmlObject



34
35
36
# File 'lib/wraptext/parser.rb', line 34

def to_html
  @html ||= @root.xpath("/html/body").inner_html
end