Class: MemoRack::Builder

Inherits:
Core
  • Object
show all
Defined in:
lib/memorack/builder.rb

Constant Summary collapse

DEFAULT_BUILD_OPTIONS =
{
	output:		'_site',
	prefix:		'/',
	suffix:		'.html',
	uri_escape:	true,
	env:		ENV,
}
DEFAULT_KEEPS =
['.git', '.hg', '.svn', '.csv']

Constants inherited from Core

Core::DEFAULT_APP_OPTIONS, Core::DEFAULT_LOCALS, Core::DEFAULT_OPTIONS, Core::DEFAULT_TEMPLATE_OPTIONS

Instance Attribute Summary

Attributes inherited from Core

#options_chain, #root, #suffix, #themes

Instance Method Summary collapse

Methods inherited from Core

#add_config_chain, app, #collect_formats, #content_name, #contents, #css_exts, #default_locals, #embed_macro, #embed_macro_for_array, #enable_exts, #extnames, #file_search, #folder, #initialize, #load_plugin, #load_plugins, #pages, #plugins_folders, #read_config, #read_data, #render, #render_content, #render_css, #render_menu, #render_page, #render_with_mustache, #require_plugin, #split_extname, template, template_method, #theme_path, #to_sym_keys, #update_locale, #use_engine, #value_to_locals

Constructor Details

This class inherits a constructor from MemoRack::Core

Instance Method Details

#build_index(directories, output, &callback) ⇒ Object

サブディレクトリの index.html を出力する



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/memorack/builder.rb', line 180

def build_index(directories, output, &callback)
	# . は取除く
	directories.delete('.')

	# index.html を出力する
	directories.each { |path|
		callback.call(path) if callback

		content_write(path, '/index.html', output) { |template|
			render_content template, {path_info: path}, nil
		}
	}
end

#content_write(template, suffix, output) ⇒ Object

コンテンツをファイルに出力する



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/memorack/builder.rb', line 118

def content_write(template, suffix, output)
	begin
		content = yield(template)
		return unless content

		path = template
		path = path.sub(/\.[^.]*$/, '') if path.kind_of?(String)

		to = path.to_s + suffix
		to = File.join(output, to)

		FileUtils.mkpath(File.dirname(to))
		File.write(to, content)
	rescue
	end
end

#copy_file(path, output, &callback) ⇒ Object

ファイルをコピーする



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/memorack/builder.rb', line 136

def copy_file(path, output, &callback)
	return if File.directory?(path)
	return if @templates.include?(path)

	callback.call(path) if callback

	if output.kind_of?(Array)
		to = File.join(*output)
	else
		to = File.join(output, path)
	end

	FileUtils.mkpath(File.dirname(to))
	FileUtils.copy_entry(path, to)
end

#copy_public(views, output, &callback) ⇒ Object

テーマの公開ファイルをコピーする



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/memorack/builder.rb', line 162

def copy_public(views, output, &callback)
	public_files.each { |path_info|
		callback.call(path_info) if callback

		ext = split_extname(path_info)[1]

		if css_exts.include?(ext)
			content_write(path_info, '.css', output) { |template|
				content = render_css(template)
			}
		else
			fullpath = file_search(path_info, {views: views}, nil)
			copy_file(fullpath, [output, path_info]) if fullpath
		end
	}
end

#copy_statics(dir, output, &callback) ⇒ Object

静的ファイルをコピーする



153
154
155
156
157
158
159
# File 'lib/memorack/builder.rb', line 153

def copy_statics(dir, output, &callback)
	Dir.chdir(dir) { |dir|
		Dir.glob('**/*') { |path|
			copy_file(path, output, &callback)
		}
	}
end

#dir_init(dir, keeps = []) ⇒ Object

ディレクトリを初期化する



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/memorack/builder.rb', line 79

def dir_init(dir, keeps = [])
	if Dir.exists?(dir)
		Dir.glob(File.join(dir, '*'), File::FNM_DOTMATCH) { |path|
			fname = File.basename(path)

			next if /^\.{1,2}$/ =~ fname
			next if keeps.member?(fname)

			FileUtils.remove_entry_secure(path)
		}
	else
		FileUtils.mkpath(dir)
	end
end

#generate(options = {}, &callback) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/memorack/builder.rb', line 23

def generate(options = {}, &callback)
	options = DEFAULT_BUILD_OPTIONS.merge(options)
	keeps = options[:keep] || @options[:keeps] || DEFAULT_KEEPS

	url = @site[:url]
	options[:prefix] = File.join(url, options[:prefix]) unless url.empty?
	@suffix = options[:suffix]

	output = File.expand_path(options[:output])
	dir_init(output, keeps)

	@contents = contents(options)
	@templates = Set.new @contents.files.collect { |file| file[:path] }
	@directories = Set.new @templates.collect { |path| File.dirname(path) }
	@directories.delete('.')

	# ロケールの更新
	update_locale(options[:env])

	# トップページを作成する
	content_write(:index, '.html', output) { |template|
		render_with_mustache template, :markdown
	}

	suffix = options[:suffix]
	suffix = '/index.html' if ['', '/'].member?(suffix)

	# サブディレクトリの index.html を出力する
	build_index(@directories, output, &callback) if options[:index] || @options[:index]

	# 固定ページのレンダリングを行う
	pages.each { |path_info, path|
		callback.call(path_info) if callback

		content_write(path_info, suffix, output) { |template|
			render_page template, {path_info: path_info}
		}
	}

	# コンテンツのレンダリングを行う
	@templates.each { |path|
		callback.call(path) if callback

		content_write(path, suffix, output) { |template|
			render_content template, {path_info: path}, nil
		}
	}

	# テーマの公開ファイルをコピー
	copy_public(@themes, output, &callback)

	# 静的ファイルをコピーする
	copy_statics(@root, output, &callback)
end

#public_filesObject

テーマから公開用のファイルを収集する



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/memorack/builder.rb', line 95

def public_files
	unless @public_files
		@public_files = Set.new

		@public.each { |path_info|
			if path_info[-1] == '/'
				@themes.each { |theme|
					if Dir.exists?(File.join(theme, path_info))
						Dir.chdir(theme) { |dir|
							@public_files += Dir.glob(File.join(path_info, '**/*'))
						}
					end
				}
			else
				@public_files << path_info
			end
		}
	end

	@public_files
end