trumpet
parse and transform streaming html using css selectors
Last updated 9 years ago by substack .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install trumpet 
SYNC missed versions from official npm registry.

trumpet

parse and transform streaming html using css selectors

build status

trumpet

example

table

input html:

<table>
  <tbody>blah blah blah</tbody>
  <tr><td>there</td></tr>
  <tr><td>it</td></tr>
  <tr><td>is</td></tr>
</table>
var trumpet = require('trumpet');
var tr = trumpet();
tr.pipe(process.stdout);
 
var ws = tr.select('tbody').createWriteStream();
ws.end('<tr><td>rawr</td></tr>');

var fs = require('fs');
fs.createReadStream(__dirname + '/html/table.html').pipe(tr);

output:

<table>
  <tbody><tr><td>rawr</td></tr></tbody>
  <tr><td>there</td></tr>
  <tr><td>it</td></tr>
  <tr><td>is</td></tr>
</table>

read all

input html:

<html>
  <head>
    <title>beep</title>
  </head>
  <body>
    <div class="a">¡¡¡</div>
    <div class="b">
      <span>tacos</span>
      <span> y </span>
      <span>burritos</span>
    </div>
    <div class="a">!!!</div>
  </body>
</html>
var trumpet = require('trumpet');
var tr = trumpet();

tr.selectAll('.b span', function (span) {
    span.createReadStream().pipe(process.stdout);
});

var fs = require('fs');
fs.createReadStream(__dirname + '/html/read_all.html').pipe(tr);

output:

tacos y burritos

methods

var trumpet = require('trumpet')

var tr = trumpet(opts)

Create a new trumpet stream. This stream is readable and writable. Pipe an html stream into tr and get back a transformed html stream.

Parse errors are emitted by tr in an 'error' event.

var elem = tr.select(selector)

Return a result object elem for the first element matching selector.

tr.selectAll(selector, function (elem) {})

Get a result object elem for every element matching selector.

elem.getAttribute(name, cb)

When the selector for elem matches, query the case-insensitive attribute called name with cb(value).

Returns elem.

elem.getAttributes(name, cb)

Get all the elements in cb(attributes) as an object attributes with lower-case keys.

Returns elem.

elem.setAttribute(name, value)

When the selector for elem matches, replace the case-insensitive attribute called name with value.

If the attribute doesn't exist, it will be created in the output stream.

Returns elem.

elem.removeAttribute(name)

When the selector for elem matches, remove the attribute called name if it exists.

Returns elem.

elem.createReadStream(opts)

Create a new readable stream with the inner html content under elem.

To use the outer html content instead of the inner, set opts.outer to true.

elem.createWriteStream(opts)

Create a new write stream to replace the inner html content under elem.

To use the outer html content instead of the inner, set opts.outer to true.

elem.createStream(opts)

Create a new readable writable stream that outputs the content under elem and replaces the content with the data written to it.

To use the outer html content instead of the inner, set opts.outer to true.

tr.createStream(sel, opts)

Short-hand for tr.select(sel).createStream(opts).

tr.createReadStream(sel, opts)

Short-hand for tr.select(sel).createReadStream(opts).

tr.createWriteStream(sel, opts)

Short-hand for tr.select(sel).createWriteStream(opts).

attributes

elem.name

The element name as a lower-case string. For example: 'div'.

selector syntax

Presently these css selectors work:

  • E
  • E F
  • E > F
  • E + F
  • E.class
  • E#id
  • E[attr=value]
  • E[attr~=search]
  • E[attr|=prefix]
  • E[attr^=prefix]
  • E[attr$=suffix]
  • E[attr*=search]

install

With npm do:

npm install trumpet

license

MIT

Current Tags

  • 1.7.2                                ...           latest (9 years ago)

48 Versions

  • 1.7.2                                ...           9 years ago
  • 1.7.1                                ...           10 years ago
  • 1.7.0                                ...           10 years ago
  • 1.6.6                                ...           10 years ago
  • 1.6.5                                ...           10 years ago
  • 1.6.4                                ...           10 years ago
  • 1.6.3                                ...           11 years ago
  • 1.6.2                                ...           11 years ago
  • 1.6.1                                ...           11 years ago
  • 1.6.0                                ...           11 years ago
  • 1.5.6                                ...           11 years ago
  • 1.5.5                                ...           11 years ago
  • 1.5.4                                ...           11 years ago
  • 1.5.3                                ...           11 years ago
  • 1.5.2                                ...           11 years ago
  • 1.5.1                                ...           11 years ago
  • 1.5.0                                ...           11 years ago
  • 1.4.1                                ...           11 years ago
  • 1.4.0                                ...           11 years ago
  • 1.3.1                                ...           11 years ago
  • 1.3.0                                ...           11 years ago
  • 1.2.3                                ...           11 years ago
  • 1.2.2                                ...           11 years ago
  • 1.2.1                                ...           11 years ago
  • 1.2.0                                ...           11 years ago
  • 1.1.0                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
  • 0.4.2                                ...           11 years ago
  • 0.4.1                                ...           11 years ago
  • 0.4.0                                ...           12 years ago
  • 0.3.6                                ...           12 years ago
  • 0.3.5                                ...           12 years ago
  • 0.3.4                                ...           12 years ago
  • 0.3.3                                ...           12 years ago
  • 0.3.2                                ...           12 years ago
  • 0.3.1                                ...           12 years ago
  • 0.3.0                                ...           13 years ago
  • 0.2.7                                ...           13 years ago
  • 0.2.6                                ...           13 years ago
  • 0.2.5                                ...           13 years ago
  • 0.2.4                                ...           13 years ago
  • 0.2.3                                ...           13 years ago
  • 0.2.2                                ...           13 years ago
  • 0.2.1                                ...           13 years ago
  • 0.2.0                                ...           13 years ago
  • 0.1.1                                ...           13 years ago
  • 0.1.0                                ...           13 years ago
  • 0.0.0                                ...           13 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (6)
Dev Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |