move comment parser to utils

This commit is contained in:
Christoph Hartmann 2015-10-06 13:50:25 +02:00
parent df9411e529
commit 949496776e
2 changed files with 27 additions and 23 deletions

View file

@ -23,4 +23,27 @@ module ContentParser
'shell' => x.at(6), 'shell' => x.at(6),
} }
end end
def parse_comment_line(raw, opts)
idx_nl = raw.index("\n")
idx_comment = raw.index(opts[:comment_char])
idx_nl = raw.length if idx_nl.nil?
idx_comment = idx_nl + 1 if idx_comment.nil?
line = ''
# is a comment inside this line
if idx_comment < idx_nl && idx_comment != 0
line = raw[0..(idx_comment - 1)]
# in case we don't allow comments at the end
# of an assignment/statement, ignore it and fall
# back to treating this as a regular line
if opts[:standalone_comments] && !is_empty_line(line)
line = raw[0..(idx_nl - 1)]
end
# if there is no comment in this line
elsif idx_comment > idx_nl && idx_nl != 0
line = raw[0..(idx_nl - 1)]
end
[line, idx_nl]
end
end end

View file

@ -4,7 +4,11 @@
# author: Dominik Richter # author: Dominik Richter
# author: Christoph Hartmann # author: Christoph Hartmann
require 'utils/parser'
class SimpleConfig class SimpleConfig
include ContentParser
attr_reader :params, :groups attr_reader :params, :groups
def initialize(raw_data, opts = {}) def initialize(raw_data, opts = {})
parse(raw_data, opts) parse(raw_data, opts)
@ -49,29 +53,6 @@ class SimpleConfig
values values
end end
def parse_comment_line(rest, opts)
idx_nl = rest.index("\n")
idx_comment = rest.index(opts[:comment_char])
idx_nl = rest.length if idx_nl.nil?
idx_comment = idx_nl + 1 if idx_comment.nil?
line = ''
# is a comment inside this line
if idx_comment < idx_nl && idx_comment != 0
line = rest[0..(idx_comment - 1)]
# in case we don't allow comments at the end
# of an assignment/statement, ignore it and fall
# back to treating this as a regular line
if opts[:standalone_comments] && !is_empty_line(line)
line = rest[0..(idx_nl - 1)]
end
# if there is no comment in this line
elsif idx_comment > idx_nl && idx_nl != 0
line = rest[0..(idx_nl - 1)]
end
[line, idx_nl]
end
def parse_params_line(line, opts) def parse_params_line(line, opts)
# now line contains what we are interested in parsing # now line contains what we are interested in parsing
# check if it is an assignment # check if it is an assignment