Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/html2fortitude/html.rb

Overview

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#converted_to_fortitudeBoolean

Whether this node has already been converted to Fortitude. Only used for text nodes and elements.

Returns:

  • (Boolean)


19
20
21
# File 'lib/html2fortitude/html.rb', line 19

def converted_to_fortitude
  @converted_to_fortitude
end

Instance Method Details

#to_fortitude(tabs, options) ⇒ Object

Returns the Fortitude representation of the given node.

Parameters:

  • tabs (Fixnum)

    The indentation level of the resulting Fortitude.

Options Hash (options):

  • :class_name (String) — default: required

    The name of the class to generate

  • :superclass (String) — default: required

    The name of the superclass for this widget

  • :method (String) — default: required

    The name of the method to generate (usually 'content')

  • :assigns (Symbol) — default: required

    Can be one of +:needs_defaulted_to_nil+ (generate +needs+ declarations with defaults of +nil+), +:required_needs+ (generate +needs+ declarations with no defaults), +:instance_variables+ (generate +needs+ declarations with defaults of +nil+, but reference them using instance variables, not methods), or +:no_needs+ (omit any +needs+ declarations entirely -- requires that you have +extra_assigns :use+ set on your widget, or it won't work)

  • :do_end (Boolean) — default: false

    Use 'do ... end' rather than '{ ... }' for tag content

  • :new_style_hashes (Boolean) — default: false

    Use Ruby 1.9-style Hashes



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/html2fortitude/html.rb', line 25

def to_fortitude(tabs, options)
  return "" if converted_to_fortitude

  # Eliminate whitespace that has no newlines
  as_string = self.to_s
  return "" if as_string.strip.empty? && as_string !~ /[\r\n]/mi

  if as_string.strip.empty?
    # If we get here, it's whitespace, but containing newlines; eliminate trailing indentation
    as_string = $1 if as_string =~ /^(.*?[\r\n])[ \t]+$/mi
    return as_string
  end

  # We have actual content if we get here; deal with leading and trailing newline/whitespace combinations properly
  text = uninterp(as_string)
  if text =~ /\A((?:\s*[\r\n])*)(.*?)((?:\s*[\r\n])*)\Z/mi
    prefix, middle, suffix = $1, $2, $3
    middle = parse_text_with_interpolation(middle, tabs)
    return prefix + middle + suffix
  else
    return parse_text_with_interpolation(text, tabs)
  end
end