Giter VIP home page Giter VIP logo

node-trumpet's Introduction

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).

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.

elem.removeAttribute(name)

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

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).

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

node-trumpet's People

Contributors

emkay avatar joshwnj avatar juliangruber avatar kesla avatar maff- avatar raynos avatar refset avatar timoxley avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.