Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 66 lines (55 sloc) 1.53 KB
#!/usr/bin/env ruby
require 'yard'
require 'fileutils'
require 'rdiscount'
require 'erb'
ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
LAYOUTS = {}
module Helpers
def markdown(text)
RDiscount.new(text).to_html
end
def erb(__file)
LAYOUTS[__file] ||= File.read(File.join(ROOT, 'templates', "#{__file}.erb"))
ERB.new(LAYOUTS[__file], 0, '<>').result(binding)
end
end
class Generator
include Helpers
attr_accessor :url, :root_path, :layout, :content, :title
def initialize(url, layout, root_path = '.')
self.root_path = root_path.empty? ? '.' : root_path.gsub(%r{/$}, '')
self.url = url
self.layout = layout.to_sym
yield(self) if block_given?
end
def to_html
erb(layout)
end
def write
path = File.join(ROOT, 'public', url + '.html')
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') {|f| f.write to_html }
end
end
def map(url, layout, &block)
Generator.new(url, layout, '../' * url.count('/'), &block).write
end
Dir[File.join(ROOT, 'site/**/*.{md,html}')].each do |file|
rel = File.relative_path(File.dirname(file), ROOT)
rel = '.' if rel.empty?
url = file.sub(/^#{ROOT}\/site\/*/, '').sub(/\.(html|md)$/, '')
content = File.read(file)
gen = Generator.new(url, file =~ %r{/guides/} ? :guide : :regular, rel)
gen.content = content
case file
when /\.md$/
gen.title = content[/\A\s*#\s*(.+)/, 1]
gen.content = gen.markdown(content)
else
gen.content = content
end
gen.write
end
map 'index', :home
map 'guides/index', :guides