8000 Add ability to set XML parsing options by rybesh · Pull Request #12 · noflo/noflo · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/components/Output.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
noflo = require "noflo"
util = require "util"

class Output extends noflo.Component

description: "This component receives input on a single inport, and sends the data items directly to console.log"

constructor: ->
@options =
showHidden: false
depth: 2
colors: false

@inPorts =
in: new noflo.ArrayPort()
options: new noflo.Port()

@outPorts = {}

@inPorts.in.on "data", (data) ->
console.log data
@inPorts.in.on "data", (data) =>
@log data

@inPorts.options.on "data", (data) =>
@setOptions data

setOptions: (options) ->
throw "Options is not an object" unless typeof options is "object"
for own key, value of options
@options[key] = value

log: (data) ->
console.log util.inspect data,
@options.showHidden, @options.depth, @options.colors

exports.getComponent = ->
new Output()
16 changes: 15 additions & 1 deletion src/components/ParseXml.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ xml2js = require "xml2js"

class ParseXml extends noflo.Component
constructor: ->
@options = # defaults recommended by xml2js docs
normalize: false
trim: false
explicitRoot: true

@inPorts =
in: new noflo.Port()
options: new noflo.Port()
@outPorts =
out: new noflo.Port()

Expand All @@ -15,9 +21,17 @@ class ParseXml extends noflo.Component
@parseXml xml
xml = ""

@inPorts.options.on "data", (data) =>
@setOptions data

setOptions: (options) ->
throw "Options is not an object" unless typeof options is "object"
for own key, value of options
@options[key] = value

parseXml: (xml) ->
target = @outPorts.out
parser = new xml2js.Parser
parser = new xml2js.Parser(@options)
parser.on "end", (parsed) ->
target.send parsed
target.disconnect()
Expand Down
27 changes: 27 additions & 0 deletions src/components/Replace.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
noflo = require "noflo"

class Replace extends noflo.Component
constructor: ->
@pattern = null
@replacement = ""

@inPorts =
in: new noflo.Port()
pattern: new noflo.Port()
replacement: new noflo.Port()
@outPorts =
out: new noflo.Port()

@inPorts.pattern.on "data", (data) =>
@pattern = new RegExp(data, 'g')
@inPorts.replacement.on "data", (data) =>
@replacement = data
@inPorts.in.on "data", (data) =>
string = data
if @pattern?
string = data.replace @pattern, @replacement
@outPorts.out.send string
@inPorts.in.on "disconnect", =>
@outPorts.out.disconnect()

exports.getComponent = -> new Replace
48 changes: 48 additions & 0 deletions src/components/ScrapeHtml.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
noflo = require "noflo"
jsdom = require "jsdom"

class ScrapeHtml extends noflo.Component
constructor: ->
@html = ""
@textSelector = ""
@crapSelectors = []

@inPorts =
in: new noflo.Port()
textSelector: new noflo.Port()
crapSelector: new noflo.ArrayPort()
@outPorts =
out: new noflo.Port()
error: new noflo.Port()

html = ""
@inPorts.in.on "data", (data) =>
html += data
@inPorts.in.on "disconnect", =>
@html = html
html = ""
@scrapeHtml()

@inPorts.textSelector.on "data", (data) =>
@textSelector = data
@inPorts.textSelector.on "disconnect", =>
@scrapeHtml()

@inPorts.crapSelector.on "data", (data) =>
@crapSelectors.push data

scrapeHtml: ->
return unless @html.length
return unless @textSelector.length
target = @outPorts.out
jsdom.env @html, ['http://code.jquery.com/jquery.min.js'], (err, win) =>
if err
@outPorts.error.send err
return @outPorts.error.disconnect()
win.$(crap).remove() for crap in @crapSelectors
for text in (win.$(@textSelector).map -> win.$(this).text())
@outPorts.out.send text
@outPorts.out.disconnect()
@html = ""

exports.getComponent = -> new ScrapeHtml
0