Sha256: bfe03b618b7efdf2b90fbdfd240bb4a3223eb1f792ec4b5de561881efd1b6589

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require "binman/version"

module BinMan
  extend self

  ##
  # Returns content of leading comment header (a contiguous sequence of
  # single-line comments starting at beginning of file and ending at first
  # single blank line) from given source (IO, file name, or string).
  #
  # Comment markers and shebang line (if any) are removed from result.
  #
  def read source=nil
    source = source.read if source.respond_to? :read
    source ||=
      if first_caller = caller.find {|f| not f.start_with? __FILE__ }
        first_caller.sub(/:\d+.*$/, '')
      end
    source = File.read(source) if File.exist? source

    string = source.to_s
    string.split(/^\s*$/, 2).first.sub(/\A#!.+$/, '').gsub(/^# ?/, '').strip
  end

  ##
  # Converts given leading comment header (produced by #read) into roff(7).
  #
  def dump header
    require 'binman/renderer'
    RENDERER.render(header)
  end

  ##
  # Shows leading comment header from given source as UNIX man page.
  #
  def show source=nil
    # try showing existing man page files for given source
    return if source and File.exist? source and
      File.exist? man_path = File.expand_path('../../man', source) and
      system 'man', '-M', man_path, '-a', File.basename(source)

    header = read(source)

    begin
      roff = dump(header)
      IO.popen('man -l -', 'w') {|man| man.puts roff }
    rescue
      puts header
    end
  end

  ##
  # Shows leading comment header from given source as UNIX man page and exits.
  #
  def help source=nil, argv=ARGV
    unless argv.grep(/^(-h|--help)$/).empty?
      show source
      exit
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
binman-0.0.1 lib/binman.rb