Skip to content

Instantly share code, notes, and snippets.

@novogrammer
Created February 28, 2014 19:29
Show Gist options
  • Save novogrammer/9277981 to your computer and use it in GitHub Desktop.
Save novogrammer/9277981 to your computer and use it in GitHub Desktop.
#encoding: utf-8
#erbテンプレートをhtmlに変換する
require 'erb'
require 'fileutils'
task :default => [:erb2html,:scss2css]
def Page(partials,erb)
pageBase=Class.new do|klass|
extend ERB::DefMethod
partials.each do|partial|
method_name=File.basename(partial,".html.erb").gsub(/^_/,"")
def_erb_method(method_name,partial)#partialをメソッドとして定義
end
end
Class.new(pageBase) do|klass|
extend ERB::DefMethod
define_method("initialize") do
@id=erb.gsub(%r|^erb/|,"").gsub(/\.html.erb$/,"")
@styles=[
"css/reset-min.css",
"css/fonts-min.css",
"css/common.css",
]
@scripts=[
"js/jquery.min.js",
"js/page_top.js",
]
@keywords=[
"あいうえお",
"かきくけこ",
]
@description="description"
end
def path_to_root
"./"+"../"*(@id.split("/").length-1)
end
def_erb_method("to_html",erb)
end
end
task :erb2html do|t|
Dir.chdir( File.dirname( __FILE__ ) ) do
#アンダーバーで始まる.erbを列挙
partials=Dir.glob("erb/**/_*.erb")
#アンダーバーで始まらない.erbを列挙
Dir.glob("erb/**/[^_]*.erb") do|file|
if file=~%r|^erb/(.*).erb$|
base=$1
page=Page(partials,file).new
html="./html/#{base}"
FileUtils.mkpath(File.dirname(html))
File.open(html,"w") do|f|
f.write(page.to_html)
end
end
end
end
end
task :scss2css do|t|
Dir.chdir( File.dirname( __FILE__ ) ) do
`sass --style expanded --update ./scss:./html/css`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment