From 949496776e02b146f01411e6afb2dceb2dfd856a Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 6 Oct 2015 13:50:25 +0200 Subject: [PATCH] move comment parser to utils --- lib/utils/parser.rb | 23 +++++++++++++++++++++++ lib/utils/simpleconfig.rb | 27 ++++----------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/utils/parser.rb b/lib/utils/parser.rb index 01c147d72..8935e4d99 100644 --- a/lib/utils/parser.rb +++ b/lib/utils/parser.rb @@ -23,4 +23,27 @@ module ContentParser 'shell' => x.at(6), } 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 diff --git a/lib/utils/simpleconfig.rb b/lib/utils/simpleconfig.rb index 281a8dd0a..ba44d955c 100644 --- a/lib/utils/simpleconfig.rb +++ b/lib/utils/simpleconfig.rb @@ -4,7 +4,11 @@ # author: Dominik Richter # author: Christoph Hartmann +require 'utils/parser' + class SimpleConfig + include ContentParser + attr_reader :params, :groups def initialize(raw_data, opts = {}) parse(raw_data, opts) @@ -49,29 +53,6 @@ class SimpleConfig values 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) # now line contains what we are interested in parsing # check if it is an assignment