Class: Brigit::ConfigParser
- Inherits:
-
Object
- Object
- Brigit::ConfigParser
- Defined in:
- lib/brigit/config_parser.rb
Overview
Reworked from Sam Ruby’s Ruby “PythonConfigParser,” intertwingly.net/code/mars/planet/config.rb which in turn is a port of Python’s ConfigParser, docs.python.org/lib/module-ConfigParser.html
Defined Under Namespace
Classes: Section
Class Method Summary collapse
Instance Method Summary collapse
-
#parse(lines) ⇒ Object
FIXME: This is way ugly.
Class Method Details
.option_pattern ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/brigit/config_parser.rb', line 32 def self.option_pattern @option_pattern ||= / ([^:=\s][^:=]*) # very permissive! \s*[:=]\s* # any number of space chars, # followed by separator # (either : or =), followed # by any # space chars (.*)$ # everything up to eol /x end |
.section_pattern ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/brigit/config_parser.rb', line 24 def self.section_pattern @section_pattern ||= / \[ # [ ([^\]]+) # very permissive! \] # ] /x end |
Instance Method Details
#parse(lines) ⇒ Object
FIXME: This is way ugly
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/brigit/config_parser.rb', line 45 def parse(lines) sections = Hash.new { |_, name| Section.new(name) } section = nil option = nil lines.each_with_index do |line, number| next if skip? line if line =~ self.class.option_pattern # option line option, value = $1, $2 option = option.downcase.strip value.sub!(/\s;.*/, '') value = '' if value == '""' section[option] = clean value elsif line =~ /^\s/ && section && option # continuation line value = line.strip section[option] = section[option] ? (section[option] << "\n#{clean value}") : clean(value) elsif line =~ self.class.section_pattern section = sections[$1] sections[$1] = section option = nil elsif !section raise SyntaxError, 'Missing section header' else raise SyntaxError, "Invalid syntax on line #{number}" end end sections end |