Class: Chisel::SiteDirectory

Inherits:
Pathname
  • Object
show all
Defined in:
lib/chisel/site_directory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_dir) ⇒ SiteDirectory

Returns a new instance of SiteDirectory.



11
12
13
14
15
16
17
# File 'lib/chisel/site_directory.rb', line 11

def initialize(site_dir)
	super
	config_path = self.join('_config.yml')
	if config_path.exist?
		@config = YAML::load_file(config_path) || {}
	end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/chisel/site_directory.rb', line 7

def config
  @config
end

Class Method Details

.create(dest) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/chisel/site_directory.rb', line 91

def self.create(dest)
	dir = Pathname.new(__FILE__).dirname
	src = dir.join('new_site')
	dest = Pathname.new(dest)

	FileUtils.cp_r(src.to_s + '/.', dest)

	dest.join('_resources').mkpath
	dest.join('_views', '_resource').mkpath
end

Instance Method Details

#clear_output_dirObject



44
45
46
47
# File 'lib/chisel/site_directory.rb', line 44

def clear_output_dir
	FileUtils.rm_r(output_dir) if File.exists?(output_dir) and @config['replace_output_dir'] != false
	FileUtils.mkdir_p(output_dir)
end

#create_resource(name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chisel/site_directory.rb', line 102

def create_resource(name)
	name = name.underscore if name.camelized?
	
	# Create resource.rb file
	
	resource_template_filename = Pathname.new(__FILE__).dirname.join('templates', 'resource.rb')
	resource_template = File.read(resource_template_filename)
	resource_template.gsub!('{NAME}', name.camelize)
	
	resource_filename = self.join('_resources', "#{name}.rb")
	File.open(resource_filename, 'w') { |f| f.write(resource_template) }
	
	# Create resource index.html file
	
	self.join('_views', name).mkpath
	
	index_template_filename = Pathname.new(__FILE__).dirname.join('templates', 'resource_index.html.erb')
	index_template = File.read(index_template_filename)
	index_template.gsub!('{NAME}', name.camelize)
	
	index_filename = self.join('_views', name, 'index.html.erb')
	File.open(index_filename, 'w') { |f| f.write(index_template) }
	
end

#layout_dirObject



40
41
42
# File 'lib/chisel/site_directory.rb', line 40

def layout_dir
	self.view_dir.join('_layout')
end

#layout_view_path(layout_name) ⇒ Object



69
70
71
# File 'lib/chisel/site_directory.rb', line 69

def layout_view_path(layout_name)
	self.layout_dir.join("#{view_with_extension(layout_name).to_s}.erb")
end

#output_dirObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chisel/site_directory.rb', line 19

def output_dir
	if @config['output_dir']
		dir = Pathname.new(@config['output_dir']).expand_path(self)
		if dir.absolute?
		return dir
		else
		return self.join(dir)
		end
	else
		return self.join('_output')
	end
end

#page_output_path(page, view_output_dir) ⇒ Object



64
65
66
67
# File 'lib/chisel/site_directory.rb', line 64

def page_output_path(page, view_output_dir)
	page = view_with_extension(page)
	self.site_relative_path(page, view_output_dir)
end

#require_resourcesObject



49
50
51
52
53
54
# File 'lib/chisel/site_directory.rb', line 49

def require_resources
	path = self.resource_dir.join("*.rb").expand_path
	Dir[path].each do |resource_filename|
		require resource_filename
	end
end

#resource_dirObject



32
33
34
# File 'lib/chisel/site_directory.rb', line 32

def resource_dir
	self.join('_resources')
end

#resource_type_view_path(resource_type, view) ⇒ Object



56
57
58
# File 'lib/chisel/site_directory.rb', line 56

def resource_type_view_path(resource_type, view)
	self.view_dir.join(resource_type.to_s, "#{view_with_extension(view)}.erb")
end

#resource_view_output_path(resource, view) ⇒ Object



60
61
62
# File 'lib/chisel/site_directory.rb', line 60

def resource_view_output_path(resource, view)
	self.output_dir.join(*resource.id, view_with_extension(view))
end

#site_relative_path(relative_path, view_output_dir) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/chisel/site_directory.rb', line 77

def site_relative_path(relative_path, view_output_dir)
	if relative_path[0] == '/'
		self.output_dir.join(relative_path[1..-1])
	else
		view_output_dir.join(relative_path)
	end
end

#view_dirObject



36
37
38
# File 'lib/chisel/site_directory.rb', line 36

def view_dir
	self.join('_views')
end

#view_path(view) ⇒ Object



73
74
75
# File 'lib/chisel/site_directory.rb', line 73

def view_path(view)
	self.view_dir.join("#{view_with_extension(view)}.erb")
end

#view_with_extension(view) ⇒ Object



85
86
87
88
89
# File 'lib/chisel/site_directory.rb', line 85

def view_with_extension(view)
	view = "#{view}index" if view[-1] == '/'
	return "#{view}.html" if File.basename(view).split('.').count < 2
	view
end