Module: ApacheVhostsParser

Defined in:
lib/apache-vhosts-parser.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Class Method Details

.parseDirectory(dir = '/etc/apache2/sites-enabled/') ⇒ Object



88
89
90
# 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) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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