• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

plexus / hexp / 36 / 3
93%
master: 100%

Build:
Build:
LAST BUILD BRANCH: v0.4.4
DEFAULT BRANCH: master
Ran 26 Jun 2013 08:03PM UTC
Files 37
Run time –
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

26 Jun 2013 08:01PM UTC coverage: 93.791%. First build
2.0.0

push

travis-ci

plexus
Experiment with chainable selectors and rewriters

This commit introduces a first attempt at coming up with a chainable interface
for describing transformations of Hexp trees.

The idea is to have certain methods that select certain nodes in a tree. At
this point there is one such method : select, which takes a block that receives
a node and returns a boolean.

  node.select {|child_node| child_node.class? 'strong' }

The result is a Hexp::Node::Selector, it is Enumerable, so one can do #to_a, #map,
etc and to loop over the nodes in the tree that have class="strong".

This can be combined with the concept of "rewriting" a tree, here the block receives
a node, and the return value of the block takes the place of the original node in the
new, rewritten tree.

This return value can be a single node or an array of nodes (zero or more). Nil is
treated special in that it is consider a no-op. When the block returns nil, the
original node is simply kept in the tree unaltered.

This example will give every <p> the class "para".

  node.rewrite do |child_node|
    if child_node.tag == :p
      child_node.attr('class', 'para')}
    end
  end

Nodes and Selectors both respond to #rewrite, in the latter case only the nodes
that match the selection criterium are passed to the block, so the former example
can be rewritten as

  node.select  {|child| child.tag == :p}
      .rewrite {|child| child_node.attr('class', 'para') }

The result is an instance of Hexp::Node::Rewriter, which lazily evaluates the
transformation when converted to a Hexp. It includes Hexp::DSL, so it can be
used as any Hexp::Node (tag, attributes, to_html, etc).

These two can form the basic building blocks for a richer API. As a tryout they
already implement #wrap and #attr, so to take every paragraph, give it the class
'para', and wrap it in a div with class 'wrapper' you can write

  node.select {|child| child.tag == :p}
      .attr('class', 'para')
      .wrap(:div, 'class' => 'wrapper')

The... (continued)

710 of 757 relevant lines covered (93.79%)

7.94 hits per line

Source Files on job 36.3 (2.0.0)
  • Tree
  • List 0
  • Changed 0
  • Source Changed 0
  • Coverage Changed 0
Coverage ∆ File Lines Relevant Covered Missed Hits/Line
  • Back to Build 36
  • Travis Job 36.3
  • 7d1ad9f3 on github
  • Next Job for 2.0.0 on chainable_api_rfc (#38.3)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc