Module: Hexp

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

Defined Under Namespace

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

Constant Summary collapse

ROOT =
Pathname(__FILE__).dirname.parent
Error =

Base class for exceptions raised by Hexp

Class.new(StandardError)
VERSION =
'0.4.6'

Class Method Summary collapse

Class Method Details

.Array(arg) ⇒ Array

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:



39
40
41
42
43
44
45
# File 'lib/hexp.rb', line 39

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

.build(*args) {|| ... } ⇒ Hexp::Builder

Use builder syntax to create a Hexp

Examples:

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

Parameters:

Yield Parameters:

Returns:

See Also:



88
89
90
# File 'lib/hexp.rb', line 88

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

.included(klazz) ⇒ Class

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)


20
21
22
# File 'lib/hexp.rb', line 20

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

.parse(html) ⇒ Hexp::Node

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:



62
63
64
65
66
# File 'lib/hexp.rb', line 62

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