Class: Pilgrim

Inherits:
Object
  • Object
show all
Defined in:
lib/blume/pilgrim.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_url, site_directory, assets) ⇒ Pilgrim

Returns a new instance of Pilgrim.



2
3
4
5
6
7
8
9
# File 'lib/blume/pilgrim.rb', line 2

def initialize(root_url, site_directory, assets)
	@root_url = root_url
	@site_directory = site_directory
	@assets = assets
	@asset_folders = Dir.glob(@assets).
		reject{|f| not File.directory?(f)}.
		map{|d| "/" + d + "/"}
end

Instance Method Details

#compressObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blume/pilgrim.rb', line 22

def compress()
	Find.find(@site_directory) do |f_name|
		ext = File.extname(f_name)
		if ext == '.html' || ext == '.js' || ext == '.css'
			File.open(f_name,'rb') do |f|
				File.open(f_name + ".gz", 'w') do |gz_f|
					gz = Zlib::GzipWriter.new(gz_f)
					gz.write(f.read)
					gz.close
				end
			end
		end
	end
end

#generate(compression_on = true) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/blume/pilgrim.rb', line 11

def generate(compression_on = true)
	visited = Set.new
	unvisited = Set.new [@root_url]
	FileUtils.rm_rf(@site_directory)
	while !unvisited.empty?
		parse_page(visited, unvisited)
	end
	FileUtils.cp_r(Dir[@assets + "/*"], @site_directory)
	if compression_on then compress end
end

#parse_page(visited, unvisited) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/blume/pilgrim.rb', line 37

def parse_page(visited, unvisited)
	current = unvisited.take(1)
	visited << current[0]

	page = Nokogiri::HTML(open(current[0]))
	page.css('a').each do |p|
		if p['href'].start_with? "/"
			unless visited.include? @root_url + p['href'] or @asset_folders.any?{|d| p['href'].start_with? d}
				unvisited << @root_url + p['href']
			end
		end
	end
	save_file(page, current)
	unvisited.subtract(current)
end

#save_file(page, file) ⇒ Object



53
54
55
56
57
# File 'lib/blume/pilgrim.rb', line 53

def save_file(page, file)
	path = @site_directory + file[0].sub(@root_url, "/")
	FileUtils.mkpath(path)
	File.open(path + "/index.html", "w"){|f| f.write(page.to_html)}
end