项目作者: gofunky

项目描述 :
parse and transform streaming html using css selectors
高级语言: JavaScript
项目地址: git://github.com/gofunky/trumpet.git
创建时间: 2019-02-17T14:56:13Z
项目社区:https://github.com/gofunky/trumpet

开源协议:MIT License

下载


trumpet

GitHub Workflow Status (branch)
Codecov
Renovate Status
@gofunky%2Ftrumpet"">Libraries.io dependency status for latest release
Snyk Vulnerabilities for npm package
JavaScript Style Guide
CodeFactor
@gofunky/trumpet"">node-current
@gofunky/trumpet"">NPM version
@gofunky/trumpet"">NPM Downloads
GitHub License
GitHub last commit

the maintained version of trumpet

Examples

Replace inner

Input HTML

  1. <table>
  2. <tbody>blah blah blah</tbody>
  3. <tr><td>there</td></tr>
  4. <tr><td>it</td></tr>
  5. <tr><td>is</td></tr>
  6. </table>

Code

  1. const trumpet = require('@gofunky/trumpet')
  2. const tr = trumpet()
  3. tr.pipe(process.stdout)
  4. const ws = tr.select('tbody').createWriteStream()
  5. ws.end('<tr><td>rawr</td></tr>')
  6. const fs = require('fs')
  7. fs.createReadStream(__dirname + '/html/table.html').pipe(tr)

Output

  1. <table>
  2. <tbody><tr><td>rawr</td></tr></tbody>
  3. <tr><td>there</td></tr>
  4. <tr><td>it</td></tr>
  5. <tr><td>is</td></tr>
  6. </table>

Read all

Input HTML

  1. <html>
  2. <head>
  3. <title>beep</title>
  4. </head>
  5. <body>
  6. <div class="a">¡¡¡</div>
  7. <div class="b">
  8. <span>tacos</span>
  9. <span> y </span>
  10. <span>burritos</span>
  11. </div>
  12. <div class="a">!!!</div>
  13. </body>
  14. </html>

Code

  1. const trumpet = require('@gofunky/trumpet')
  2. const tr = trumpet()
  3. tr.selectAll('.b span', (span) => {
  4. span.createReadStream().pipe(process.stdout)
  5. })
  6. const fs = require('fs')
  7. fs.createReadStream(__dirname + '/html/read_all.html').pipe(tr)

Output

  1. tacos y burritos

Read, modify, and write

Input HTML

  1. <html>
  2. <body>
  3. <div class="x">
  4. <span>hack</span>
  5. <span> the </span>
  6. <span>planet</span>
  7. </div>
  8. </body>
  9. </html>

Code

  1. const trumpet = require('@gofunky/trumpet')
  2. const through = require('through2')
  3. const tr = trumpet()
  4. //select all element and apply transformation function to selections
  5. tr.selectAll('.x span', (element) => {
  6. //define function to transform input
  7. const upper = through((buf) => {
  8. this.queue(String(buf).toUpperCase())
  9. })
  10. //create a read/write stream for selected selement
  11. const stream = element.createStream()
  12. //stream the element's inner html to transformation function
  13. //then stream the transformed output back into the element stream
  14. stream.pipe(upper).pipe(stream)
  15. //stream in html to trumpet and stream processed output to stdout
  16. const fs = require('fs')
  17. fs.createReadStream(__dirname + '/html/uppercase.html').pipe(tr).pipe(process.stdout)

Output

  1. <html>
  2. <body>
  3. <div class="x">
  4. <span>HACK</span>
  5. <span> THE </span>
  6. <span>PLANET</span>
  7. </div>
  8. </body>
  9. </html>

Methods

  1. const trumpet = require('@gofunky/trumpet')

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

const elem = tr.select(selector)

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

tr.selectAll(selector, (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

Currently, 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]