Module: Irgat::Help

Included in:
Irgat
Defined in:
lib/irgat/help.rb

Instance Method Summary collapse

Instance Method Details

#show_help(pattern, exit_irgat = false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/irgat/help.rb', line 6

def show_help(pattern, exit_irgat = false)
  help_file = IRGAT_LANG + "/help_cmd_#{ @config[:lang] }.rb"
  if ! test(?e, help_file)
    puts "Help file #{ help_file } not found. Check the install or choose another language in your irgat config file"
    puts "Loading default Lang"
    help_file = IRGAT_LANG + "/help_cmd_en.rb"
  end

  #get comments in file
  comment = File.open(help_file) do |file|
    RDoc.find_comment(file)
  end

  # parse it, and create needed objects
  comment        = comment.gsub(/^\s*#/, '')
  markup         = SM::SimpleMarkup.new
  flow_convertor = SM::ToFlow.new
  flow           = markup.convert(comment, flow_convertor)

  #define output format
  format = "plain"
  #index help if nothing was wrotten
  pattern = (pattern.length == 0 ? 'index' : pattern.to_s)

  #i don't want output in these...
  $stdout       = File.new('/dev/null',"w+")
  searched_flow = RDoc.extract_sections(flow, pattern)
  #retake output
  $stdout       = STDOUT

  # i don't like the rdoc way to return the things... it is assumed, that if you don't found your desired section, you want to see all!! O_o
  if flow == searched_flow
    puts "[ Error ] No section founded for your petition #{ pattern }"
    puts "Try a diffent pattern search or try $ irgat help without search pattern to view a list of avaibles sections"
  else
    #we have found, render it
    options = RI::Options.instance
    if args = ENV["RI"]
      options.parse(args.split)
    end
    formatter = options.formatter.new(options, "")
    formatter.display_flow(searched_flow)
  end
end