Created
January 13, 2010 11:27
-
-
Save raggi/276118 to your computer and use it in GitHub Desktop.
filthy short + simple parser for Config::General.pm style configs
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
#!/usr/bin/env ruby | |
file = ARGV.first | |
abort "Usage: #{File.basename($0)} file.conf" unless file | |
cfg = [] | |
current_subtree = cfg | |
parent = cfg | |
open(file) do |io| | |
until io.eof? | |
line = io.readline | |
next if line =~ /\s*#.*/ | |
next if line =~ /^\s*$/ | |
if line[0,1] == '<' | |
if line[1,1] == '/' | |
current_subtree = parent | |
next | |
else | |
line = line[/<(.*)>/,1] | |
parts = [] | |
while part = line.slice!(/(")[^"]+"|[^\s]+/) | |
part = part[1..-2] if $1 | |
parts << part | |
end | |
parent = current_subtree | |
subtree = [] | |
parent << [parts, subtree] | |
current_subtree = subtree | |
end | |
else | |
parts = [] | |
while part = line.slice!(/(")[^"]+"|[^\s]+/) | |
part = part[1..-2] if $1 | |
parts << part | |
end | |
current_subtree << parts | |
end | |
end | |
end | |
require 'pp' | |
pp cfg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment