lib/binman.rb in binman-3.4.1 vs lib/binman.rb in binman-4.0.0

- old
+ new

@@ -13,11 +13,11 @@ # # (2) First embedded document delimited by `=begin` and `=end` lines. # # Comment markers and shebang/encoding comments are omitted from result. # - def load source=nil + def snip source=nil header = read(source) # strip shebang and encoding comments header.sub! /\A#!.+\n?/, '' header.sub! /\A#.*coding:.+\n?/, '' @@ -28,26 +28,20 @@ else header[/^=begin\b.*?$(.*?)^=end\b.*?$/m, 1].to_s end.strip end - # Converts given markdown(7) source into roff(7). - def conv source=nil - require_md2man - require 'md2man/roff/engine' - Md2Man::Roff::ENGINE.render(read(source)) - end - # Extracts leading comment header content from given # source and returns the roff(7) conversion thereof. def dump source=nil - conv load(source) + conv snip(source) end # Shows leading comment header 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 leading comment header as-is. + # Tries to display a pre-rendered UNIX man page under ./man/ if possible. def show source=nil, query=nil # try showing existing man page files for given source if file = find(source) and File.exist? file man_page = File.basename(file) man_path = File.expand_path('../../man', file) @@ -67,11 +61,11 @@ # close STDERR to avoid interference with the fall back below return if view query, '-M', man_path, '-a', man_page, 2 => :close end # fall back to showing leading comment header as-is - header = load(source) + header = snip(source) begin roff = conv(header) require 'tempfile' Tempfile.open 'binman' do |temp| @@ -96,19 +90,10 @@ show source, argv[index + 1] exit end end - # Requires that the correct version of Md2Man is available on this system. - def require_md2man - require 'rubygems' unless respond_to? :gem - gem 'md2man', '~> 3.0' if respond_to? :gem - require 'md2man/version' - rescue LoadError - raise 'Run `gem install md2man --version "~> 3.0"` to use BinMan::conv().' - end - private # Launches man(1) with the given arguments and then tries to search for the # query (if given) within. If man(1) is not able to launch with the search # capability, then it tries launching man(1) without the search capability. @@ -118,9 +103,17 @@ # See https://www.debian-administration.org/article/246/ for pager list. query and %w[ pager less most more ].any? do |pager| # the `-s` and `+/pattern` options are universally supported by pagers system 'man', '-P', "#{pager} -s +/#{query.shellescape}", *argv end or system 'man', *argv + end + + # Converts given markdown(7) source into roff(7). + def conv source=nil + require 'md2man/roff/engine' + Md2Man::Roff::ENGINE.render(read(source)) + rescue LoadError + raise 'Run `gem install md2man` to use BinMan::dump() or BinMan::show().' end # Returns contents of given source I/O, file name, or string. def read source=nil if source.respond_to? :read