github.com/echohead/hub@v2.2.1+incompatible/Rakefile (about)

     1  desc "Show man page"
     2  task :man => "man:build" do
     3    exec "man man/hub.1"
     4  end
     5  
     6  desc "Build man pages"
     7  task "man:build" => ["man/hub.1", "man/hub.1.html"]
     8  
     9  # split readme in sections
    10  # and return the specified section
    11  def split_readme(file, index)
    12    File.read(file).split(/^-{4,}$/)[index].strip
    13  end
    14  
    15  def extract_configs(readme_file)
    16    configs = split_readme(readme_file, 4)
    17    configs.gsub!(/\*\*(.+?)\*\*/, '<\1>')      # replace **xx** with <xx>
    18    configs.sub!(/\n+.+\Z/, '')                 # remove last line
    19    configs
    20  end
    21  
    22  def extract_examples(readme_file)
    23    examples = split_readme(readme_file, 3)
    24    examples.sub!(/^.+?(###)/m, '\1')  # strip intro paragraph
    25    examples.sub!(/\n+.+\Z/, '')       # remove last line
    26    examples
    27  end
    28  
    29  # inject configs and examples from README file to .ronn source
    30  def compiled_source(source, readme)
    31    configs = extract_configs(readme)
    32    examples = extract_examples(readme)
    33    compiled = File.read(source)
    34    compiled.sub!('{{CONFIGS}}', configs)
    35    compiled.sub!('{{README}}', examples)
    36    compiled
    37  end
    38  
    39  # generate man page with ronn
    40  def compile_ronn(destination, type, contents)
    41    File.popen("ronn --pipe --#{type} --organization=GITHUB --manual='Hub Manual'", 'w+') { |io|
    42      io.write contents
    43      io.close_write
    44      File.open(destination, 'w') { |f| f << io.read }
    45    }
    46    abort "ronn --#{type} conversion failed" unless $?.success?
    47  end
    48  
    49  file "man/hub.1" => ["man/hub.1.ronn", "README.md"] do |task|
    50    contents = compiled_source(*task.prerequisites)
    51    compile_ronn(task.name, 'roff', contents)
    52    compile_ronn("#{task.name}.html", 'html', contents)
    53  end
    54  
    55  file "man/hub.1.html" => ["man/hub.1.ronn", "README.md"] do |task|
    56    Rake::Task["man/hub.1"].invoke
    57  end