node-trumpet

parse and transform streaming html using css selectors

  • 所有者: substack/node-trumpet
  • 平台:
  • 许可证: Other
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

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

主要指标

概览
名称与所有者substack/node-trumpet
主编程语言JavaScript
编程语言JavaScript (语言数: 2)
平台
许可证Other
所有者活动
创建于2012-03-11 23:57:10
推送于2016-11-28 01:34:13
最后一次提交2015-10-02 15:40:04
发布数48
最新版本名称1.7.2 (发布于 2015-10-02 15:40:04)
第一版名称0.0.0 (发布于 2012-03-11 19:17:23)
用户参与
星数629
关注者数16
派生数65
提交数402
已启用问题?
问题数45
打开的问题数23
拉请求数4
打开的拉请求数10
关闭的拉请求数19
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?