module ApacheVhostsParser

Public Class Methods

parseDirectory(dir='/etc/apache2/sites-enabled/') click to toggle source
# File lib/apache-vhosts-parser.rb, line 88
def self.parseDirectory dir='/etc/apache2/sites-enabled/'
        parseString Dir.glob(File.join(dir, '*')).map(&File.method(:read)).join "\n"
end
parseString(str) click to toggle source
# File lib/apache-vhosts-parser.rb, line 45
def self.parseString str
        vhosts = []

        tag = {
                children: []
        }

        str.split(/\n+/).map(&:strip).each do |line|
                if line =~ /^$/
                        #ignore the line
                elsif m = line.match(/<\s*(\w+)(.*?)>$/)
                        opener, rest = m[1], m[2].to_s.strip.split(/\s+/)
                        new_tag = {
                                name: opener.downcase,
                                type: 'tag',
                                original_name: opener,
                                arguments: rest,
                                parent: tag,
                                children: []
                        }
                        tag[:children] << new_tag
                        tag = new_tag
                elsif closer = line[/<\/\s*(\w+)>$/, 1]
                        if closer.downcase == tag[:name]
                                tag = tag[:parent]
                        else
                                throw "Mismatched closing tag: #{closer} for #{tag[:original_name]}"
                        end
                else
                        directive, *arguments = line.split(/\s+/)
                        tag[:children] << {
                                name: directive.downcase,
                                arguments: arguments,
                                type: 'directive',
                                original_name: directive,
                                parent: tag
                        }
                end
        end

        return Config.new(tag)
end