Skip to content

Instantly share code, notes, and snippets.

@cyberwombat
Created November 13, 2012 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberwombat/4066782 to your computer and use it in GitHub Desktop.
Save cyberwombat/4066782 to your computer and use it in GitHub Desktop.
Fix for NodeJS imagemagick module not parsing IM identify output
function parseIdentify(input) {
var lines = input.split("\n"),
prop = {},
props = [prop],
prevIndent = 0,
indent = 0,
indents = [indent],
currentLine, comps;
lines.shift(); //drop first line (Image: name.jpg)
for (i in lines) {
currentLine = lines[i];
if (currentLine.length > 0) {
comps = currentLine.split(': ');
indent = currentLine.search(/\S/);
while (indent <= prevIndent) {
indents.pop();
prop = props.pop();
prevIndent = indents[indents.length - 1];
}
if(comps.length < 2) {
indents.push(indent);
props.push(prop);
prop = prop[comps[0].trim().toLowerCase()] = {};
prevIndent = indent;
}
else {
prop[comps[0].trim().toLowerCase()] = comps[1].trim();
}
}
}
return props[0];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment