# coding: utf-8 # Rakefile for Markdown documents. DIRMAP_MD = '.dirmap.md' ENCODING = "UTF-8" HTML2PDF = "wkhtmltopdf" TEX2IMAGE = "tex2image" HTML2PDF_OPTIONS = "-B 1cm -L 1.5cm -R 1.5cm -T 1.5cm -s A4 --encoding #{ENCODING} " CONVERT_COMMAND = "convert -alpha deactivate -density 150x150" require "pp" require "fileutils" require "pathname" ## .dirmap.md # dirmap コマンドは必ず実行なので task タスク。 # これを file にすると存在するときに実行されない。 # 生成物の .dirmap.md から task タスクへの依存を設定すると、 # .dirmap.md に依存する file タスクに「必ず実行」が伝播して必ず実行になってしまう。 # DIRMAP_MD というファイルに対する file タスクへの依存として扱うことで、 # .dirmap.md に依存する file タスクに「必ず実行」が伝播するのを防いでいる。 desc "update .dirmap.md if directory changed." file DIRMAP_MD => :dirmap_command task :dirmap_command do sh "dirmap" end ## *.html md_files = FileList["*.md"] html_files = md_files.ext("html") html_tasks = [] html_files.each do |html_file| md_file = html_file.ext("md") md_path = Pathname.new( md_file) dirpath = md_path.dirname src = FileList[md_file, DIRMAP_MD] file html_file => [DIRMAP_MD, md_file] do sh "madowu -O -s .dirmap.md -c madowu.css -C UTF-8 #{md_file}" end html_tasks << html_file end desc "make *.html from *.md" task :md2html => [DIRMAP_MD, * html_tasks] desc "make *.pdf from *.html" pdf_files = html_files.ext("pdf") task :html2pdf => FileList[pdf_files] pdf_files.each do |pdf_file| html_file = pdf_file.ext("html") file pdf_file => html_file do sh "#{HTML2PDF} #{HTML2PDF_OPTIONS} #{html_file} #{pdf_file}" end end desc "make .png from .eps." eps_files = FileList["*.eps"] png_files = eps_files.ext("png") task :eps2png => FileList[png_files] png_files.each do |png_file| eps_file = png_file.ext("eps") t = [eps_file] file png_file => t do sh "#{CONVERT_COMMAND} #{eps_file} #{png_file}" end end desc "make *.png from *.tex" tex_files = FileList["*.tex"] png_files = tex_files.ext("png") task :tex2png => FileList[png_files] png_files.each do |png_file| tex_file = png_file.ext("tex") file png_file => tex_file do #pp png_file, tex_file; exit sh "#{TEX2IMAGE} #{tex_file}" end end # recursive だと、サブディレクトリの Rakefile も # recursive ターゲットを持っているという前提が必要。 desc "execute 'rake' in all subdirs with Rakefile" rakefiles = FileList["**/Rakefile"] dirs = rakefiles.map{|path| Pathname.new(path).dirname.to_s} dirs.delete_if {|i| i == '.' } dirs.map!{|path| File.absolute_path(path)} task :subdir do dirs.each do |dir| Dir.chdir dir system "rake" end end task :all => [:md2html, :subdir] task :pdf => :html2pdf #task :default => [:tree, :tex2png, :eps2png] task :default => [:md2html, :tex2png, :eps2png] require "rake/clean" CLEAN.include( [ html_files, pdf_files, ])