diff --git a/imagemagick.js b/imagemagick.js index b846c0c..1536d22 100644 --- a/imagemagick.js +++ b/imagemagick.js @@ -99,36 +99,43 @@ function exec2(file, args /*, options, callback */) { function parseIdentify(input) { - var lines = input.split("\n"), - prop = {}, - props = [prop], - prevIndent = 0, - indents = [indent], - currentLine, comps, indent, i; - - lines.shift(); //drop first line (Image: name.jpg) - - for (i in lines) { - currentLine = lines[i]; - indent = currentLine.search(/\S/); - if (indent >= 0) { - comps = currentLine.split(': '); - if (indent > prevIndent) indents.push(indent); - while (indent < prevIndent && props.length) { - indents.pop(); - prop = props.pop(); - prevIndent = indents[indents.length - 1]; - } - if (comps.length < 2) { - props.push(prop); - prop = prop[currentLine.split(':')[0].trim().toLowerCase()] = {}; - } else { - prop[comps[0].trim().toLowerCase()] = comps[1].trim() - } - prevIndent = indent; + var lines = input.split("\n"), + prop = {}, + props = [prop], + prevIndent = 0, + indents = [indent], + currentLine, comps, indent, i, nextIndent; + + lines.shift(); //drop first line (Image: name.jpg) + + for (i in lines) { + currentLine = lines[i]; + indent = nextIndent || currentLine.search(/\S/); + if (indent >= 0) { + comps = currentLine.split(': '); + if (indent > prevIndent) indents.push(indent); + while (indent < prevIndent && props.length) { + indents.pop(); + prop = props.pop(); + prevIndent = indents[indents.length - 1]; + } + nextIndent = getLineIndent(parseInt(i) + 1, lines); + if (comps.length < 2 || nextIndent > indent) { + props.push(prop); + prop = prop[currentLine.split(':')[0].trim().toLowerCase()] = {}; + } else { + prop[comps[0].trim().toLowerCase()] = comps[1].trim() + } + prevIndent = indent; + } + } + return prop; +}; + +function getLineIndent(lineNum, lines){ + if(lineNum < lines.length){ + return lines[lineNum].search(/\S/); } - } - return prop; }; exports.identify = function(pathOrArgs, callback) { @@ -140,8 +147,8 @@ exports.identify = function(pathOrArgs, callback) { isData = true; pathOrArgs = args[args.length-1]; args[args.length-1] = '-'; - if (!pathOrArgs.data) - throw new Error('first argument is missing the "data" member'); + if (!pathOrArgs.data && !pathOrArgs.stream) + throw new Error('first argument must contain "data" or "stream" member'); } else if (typeof pathOrArgs === 'function') { args[args.length-1] = '-'; callback = pathOrArgs; @@ -165,7 +172,9 @@ exports.identify = function(pathOrArgs, callback) { callback(err, result); }); if (isData) { - if ('string' === typeof pathOrArgs.data) { + if (pathOrArgs.stream) { + pathOrArgs.stream.pipe(proc.stdin); + } else if ('string' === typeof pathOrArgs.data) { proc.stdin.setEncoding('binary'); proc.stdin.write(pathOrArgs.data, 'binary'); proc.stdin.end();