Skip to content

Instantly share code, notes, and snippets.

@raggi
Created January 13, 2010 11:27
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 raggi/276118 to your computer and use it in GitHub Desktop.
Save raggi/276118 to your computer and use it in GitHub Desktop.
filthy short + simple parser for Config::General.pm style configs
#!/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