Created
November 13, 2012 16:30
-
-
Save cyberwombat/4066782 to your computer and use it in GitHub Desktop.
Fix for NodeJS imagemagick module not parsing IM identify output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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