Module: Hexp

Included in:
Builder, Node::Rewriter
Defined in:
lib/hexp.rb,
lib/hexp/dom.rb,
lib/hexp/dsl.rb,
lib/hexp/node.rb,
lib/hexp/list.rb,
lib/hexp/errors.rb,
lib/hexp/builder.rb,
lib/hexp/node/pp.rb,
lib/hexp/version.rb,
lib/hexp/text_node.rb,
lib/hexp/node/domize.rb,
lib/hexp/css_selector.rb,
lib/hexp/node/selector.rb,
lib/hexp/node/children.rb,
lib/hexp/node/rewriter.rb,
lib/hexp/node/normalize.rb,
lib/hexp/node/attributes.rb,
lib/hexp/nokogiri/reader.rb,
lib/hexp/nokogiri/equality.rb,
lib/hexp/node/css_selection.rb,
lib/hexp/css_selector/parser.rb,
lib/hexp/sass/selector_parser.rb,
lib/hexp/css_selector/sass_parser.rb

Defined Under Namespace

Modules: CssSelector, DOM, DSL, Nokogiri, Sass Classes: Builder, FormatError, List, Node, ParseError, TextNode

Constant Summary

Error =
Class.new(StandardError)
VERSION =
'0.2.0'

Class Method Summary (collapse)

Class Method Details

+ (Array) Array(arg)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Variant of ::Array with slightly modified semantics

Array() is often used to wrap a value in an Array, unless it's already an array. However if your object implements #to_a, then Array() will use that value. Because of this objects that aren't Array-like will get converted as well, such as Struct objects.

This implementation relies on #to_ary, which signals that the Object is a drop-in replacement for an actual Array.

Parameters:

  • arg (Object)

Returns:



47
48
49
50
51
52
53
# File 'lib/hexp.rb', line 47

def self.Array(arg)
  if arg.respond_to? :to_ary
    arg.to_ary
  else
    [ arg ]
  end
end

+ (Hexp::Builder) build(*args, &block)

Use builder syntax to create a Hexp

(see Hexp::Builder)

Examples:

list = Hexp.build do
  ul do
   3.times do |i|
     li i.to_s
   end
  end
end

Parameters:

Returns:



91
92
93
# File 'lib/hexp.rb', line 91

def self.build(*args, &block)
  Hexp::Builder.new(*args, &block)
end

+ (Object) deep_freeze(*args)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deep freeze an object

Delegates to IceNine

Parameters:

  • args (Array)

    arguments to pass on

Returns:

  • Object



29
30
31
# File 'lib/hexp.rb', line 29

def self.deep_freeze(*args)
  IceNine.deep_freeze(*args)
end

+ (Class) included(klazz)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inject the Hexp::DSL module into classes that include Hexp

Parameters:

  • klazz (Class)

    The class that included Hexp

Returns:

  • (Class)


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

def self.included(klazz)
  klazz.send(:include, Hexp::DSL)
end

+ (Hexp::Node) parse(html)

Parse HTML to Hexp

The input have a single root element. If there are multiple only the first will be converted. If there is no root element (e.g. an empty document, or only a DTD or comment) then an error is raised

Examples:

Hexp.parse('<div>hello</div>') #=> H[:div, "hello"]

Parameters:

  • html (String)

    A HTML document

Returns:

Raises:



68
69
70
71
72
# File 'lib/hexp.rb', line 68

def self.parse(html)
  root = Nokogiri(html).root
  raise Hexp::ParseError, "Failed to parse HTML : no document root" if root.nil?
  Hexp::Nokogiri::Reader.new.call(root)
end