require 'rake' # build man pages before building ruby gem using bundler if defined? Bundler::GemHelper %w[build install release].each {|t| task t => :md2man } end #----------------------------------------------------------------------------- desc 'Build manual pages from Markdown files in man/.' task :md2man => ['md2man:man', 'md2man:web'] #----------------------------------------------------------------------------- mkds = FileList['man/**/*.{markdown,mkd,md}'] mans = mkds.pathmap('%X') webs = mans.pathmap('%p.html') render_file_task = lambda do |src, dst, renderer| directory dir = dst.pathmap('%d') file dst => [dir, src] do input = File.read(src) output = renderer.call(input) File.open(dst, 'w') {|f| f << output } end end #----------------------------------------------------------------------------- desc 'Build UNIX manual pages from Markdown files in man/.' task 'md2man:man' => mans #----------------------------------------------------------------------------- mkds.zip(mans).each do |src, dst| render_file_task.call src, dst, lambda {|input| require 'md2man/engine' Md2Man::ENGINE.render(input) } end #----------------------------------------------------------------------------- desc 'Build HTML manual pages from Markdown files in man/.' task 'md2man:web' => 'man/index.html' #----------------------------------------------------------------------------- wrap_html_template = lambda do |title, content| < #{title} #{content} WRAP_HTML_TEMPLATE end parse_manpage_name = lambda do |html_file_name| html_file_name.pathmap('%n').sub(/\.(.+)$/, '(\1)') end parse_manpage_info = lambda do |html_file_body| html_file_body.scan(%r{NAME(.+?)/, '') # strip HTML end file 'man/index.html' => webs do |t| buffer = ['
'] webs.group_by {|web| web.pathmap('%d') }.each do |dir, dir_webs| subdir = dir.sub('man/', '') buffer << %{

#{subdir}

} dir_webs.each do |web| name = parse_manpage_name.call(web) info = parse_manpage_info.call(File.read(web)) link = %{#{name}} buffer << %{
#{link}
#{info}
} end end buffer << '
' content = buffer.join title = t.name.pathmap('%X') output = wrap_html_template.call(title, content) File.open(t.name, 'w') {|f| f << output } end mkds.zip(webs).each do |src, dst| render_file_task.call src, dst, lambda {|input| require 'md2man/html/engine' output = Md2Man::HTML::ENGINE.render(input) name = parse_manpage_name.call(dst) info = parse_manpage_info.call(output) title = [name, info].join(' — ') subdir = dst.pathmap('%d').sub('man/', '') ascend = '../' * subdir.count('/').next content = [ '', '
', '
', output, '
', '
', ].join wrap_html_template.call title, content } end