Module: BinMan

Extended by:
BinMan
Included in:
BinMan
Defined in:
lib/binman.rb,
lib/binman/version.rb

Constant Summary collapse

VERSION =
"5.1.0"

Instance Method Summary collapse

Instance Method Details

#help(source = nil, argv = nil) ⇒ Object

Shows embedded manpage source from given source as UNIX man page and exits if the given argument vector contains ‘-h’ or ‘–help’, except after ‘–’, optionally followed by a regular expression argument that specifies text to search for and, if found, jump to inside the displayed UNIX man page.



66
67
68
69
70
71
72
73
74
# File 'lib/binman.rb', line 66

def help source=nil, argv=nil
  argv = Array(argv || ARGV)
  limit = argv.index('--') || argv.length
  index = [argv.index('-h'), argv.index('--help')].compact.min
  if index and index < limit
    show source, argv[index + 1]
    exit
  end
end

#html(source = nil) ⇒ Object

Renders embedded manpage source from given source as HTML man page.



40
41
42
# File 'lib/binman.rb', line 40

def html source=nil
  to_html text(source)
end

#roff(source = nil) ⇒ Object

Renders embedded manpage source from given source as UNIX man page.



35
36
37
# File 'lib/binman.rb', line 35

def roff source=nil
  to_roff text(source)
end

#show(source = nil, query = nil) ⇒ Object

Shows embedded manpage source from given source as UNIX man page and optionally jumps to first match of query regular expression if given. If not possible, falls back to showing embedded manpage source as-is. Tries to display a pre-rendered UNIX man page under ./man/ if possible.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/binman.rb', line 48

def show source=nil, query=nil
  # try showing existing man page files for given source
  if file = find(source) and File.file? file and file.respond_to? :to_str
    man_page = File.basename(file)
    man_path = File.expand_path('../../man', file)
    return if show_man(man_path, man_page, query)
  end

  # fall back to rendering embedded manpage source or showing it as-is
  header = text(source)
  return if show_str(header, query)
  puts header
end

#text(source = nil) ⇒ Object

Extracts content of embedded manpage source (which can be one of the following two choices) from given source (IO, file name, or string).

(1) A contiguous sequence of single-line comments starting at the

beginning of the file (after shebang and encoding comments plus
optional blank lines) and ending at the first single blank line.

(2) First embedded document delimited by ‘=begin` and `=end` lines.

Comment markers and shebang/encoding comments are omitted from result.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/binman.rb', line 19

def text source=nil
  header = read(source)

  # strip shebang and encoding comments
  header.sub! /\A#!.+\n?/, ''
  header.sub! /\A#.*coding:.+\n?/, ''

  # extract the embedded manpage source
  if header =~ /\A\s*^#/
    header.split(/^\s*$/, 2).first.gsub(/^# ?/, '')
  else
    header[/^=begin\b.*?$(.*?)^=end\b.*?$/m, 1].to_s
  end.strip
end