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/roff/engine' Md2Man::Roff::ENGINE.render(input) } end #----------------------------------------------------------------------------- desc 'Build HTML manual pages from Markdown files in man/.' task 'md2man:web' => ['man/index.html', 'man/style.css'] #----------------------------------------------------------------------------- wrap_html_template = lambda do |title, content, ascent| require 'md2man/version' < #{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| if html_file_body =~ %r{

.+?\s-\s+(.+?)

} $1.gsub(/<.+?>/, '') # strip HTML tags end end directory 'man' file 'man/index.html' => ['man'] + webs do |t| buffer = ['
'] webs.sort.group_by {|web| web.pathmap('%d').sub('man/', '') }.each do |subdir, dir_webs| 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, nil) File.open(t.name, 'w') {|f| f << output } end file 'man/style.css' => ['man', __FILE__.pathmap('%X/style.css')] do |t| cp t.prerequisites.last, t.name if t.needed? 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). # deactivate external manual page cross-references gsub(/(?<=', '', '', '
', output, '
', ].join wrap_html_template.call title, content, ascent } end