Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html_stream wording suggests multiple .loud elements #105

Closed
max opened this issue Jan 14, 2015 · 5 comments
Closed

html_stream wording suggests multiple .loud elements #105

max opened this issue Jan 14, 2015 · 5 comments

Comments

@max
Copy link

max commented Jan 14, 2015

I was trying to come up with a solution that would uppercase multiple .loud elements in any given HTML input. That proved to be quite a bit more difficult than simply transforming a single element. Perhaps the problem statement can be reworded here.

@Noyabronok
Copy link

I absolutely agree. Was going to make the same suggestion. The example solution only uppercases the first matching element, and the input html only has a single loud class to test with.

@swinston100
Copy link

Yes I also noticed this and agree!

Being adventurous, I wrote to try and handle for multiple elements. But, when I add another .loud element to the HTML input it fails with Error: write after end when it tries to transform the second element.

var trumpet = require('trumpet');
var through = require('through2');


var upperCase = function(buffer, enc, next){
  this.push(buffer.toString().toUpperCase());
  next();
}
var upperStream = through(upperCase); //create a transform stream for upperCase

var tr = trumpet(); //create new trumpet stream
process.stdin.pipe(tr); //pipe the input into trumpet

//find elements and transform them
tr.selectAll('.loud', function (elem){

var rs = elem.createReadStream();
var ws = elem.createWriteStream();

rs.pipe(upperStream).pipe(ws); //transform the stream

});

tr.pipe(process.stdout); //pipe trumpet back to output

@max, if can you please help me by pointing out how I can fix to handle all the elements?
Thanks

@swinston100
Copy link

@max - all good I figured it out - I need to move the deceleration of upperStream and upperCase function into the selectAll event handler.

Corrected code:

var trumpet = require('trumpet');
var through = require('through2');



var tr = trumpet(); //create new trumpet stree

process.stdin.pipe(tr); //pipe the input into trumpet

//find elements and transform them
tr.selectAll('.loud', function (elem){

  var upperCase = function(buffer, enc, next){
    this.push(buffer.toString().toUpperCase());
    next();
  }
  var upperStream = through(upperCase); //create a transform stream for upperCase

  var rs = elem.createReadStream();
  var ws = elem.createWriteStream();

  rs.pipe(upperStream).pipe(ws); //transform the stream

});

tr.pipe(process.stdout); //pipe trumpet back to output

@kalinchernev
Copy link
Member

This discussion could be complemented by 3 pull requests doing basically the same as suggested:

Although #69 should be picked as a primary change request being first, #146 looks most thorough, readable and it's the only pull request with no conflicts at the moment.

@ccarruitero
Copy link
Contributor

I'm closing since this it's stale from some time ago. We are planning to update html_stream removing trumpet dependency, join the conversation in #239.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants