Class: GLI::Commands::MarkdownDocumentListener

Inherits:
Object
  • Object
show all
Defined in:
lib/doing/markdown_document_listener.rb

Overview

DocumentListener class for GLI documentation generator

Instance Method Summary collapse

Constructor Details

#initialize(_global_options, _options, _arguments, app) ⇒ MarkdownDocumentListener

Returns a new instance of MarkdownDocumentListener.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/doing/markdown_document_listener.rb', line 9

def initialize(_global_options, _options, _arguments, app)
  @exe = app.exe_name
  if File.exist?('COMMANDS.md') # Back up existing README
    FileUtils.mv('COMMANDS.md', 'COMMANDS.bak')
    $stderr.puts "Backing up existing COMMANDS.md"
  end
  @io = File.new('COMMANDS.md', 'w')
  @nest = '#'
  @arg_name_formatter = GLI::Commands::HelpModules::ArgNameFormatter.new
  @parent_command = []
end

Instance Method Details

#beginningObject



21
22
# File 'lib/doing/markdown_document_listener.rb', line 21

def beginning
end

#command(name, aliases, desc, long_desc, arg_name, arg_options) ⇒ Object

Gives you a command in the current context and creates a new context of this command



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/doing/markdown_document_listener.rb', line 118

def command(name, aliases, desc, long_desc, arg_name, arg_options)
  @parent_command.push ([name] + aliases).join('|')
  arg_name_fmt = @arg_name_formatter.format(arg_name, arg_options, [])
  arg_name_fmt = " `#{arg_name_fmt.strip}`" if arg_name_fmt
  @io.puts header("`$ #{@exe}` <mark>`#{@parent_command.join(' ')}`</mark>#{arg_name_fmt}", 1)
  @io.puts
  @io.puts "*#{String(desc).strip}*"
  @io.puts
  cmd_desc = String(long_desc).strip.split("\n").map { |_| "> #{_}" }.join("\n")
  @io.puts "#{cmd_desc}\n\n" unless cmd_desc.length == 0
  increment_nest
end

#commandsObject



111
112
113
114
115
# File 'lib/doing/markdown_document_listener.rb', line 111

def commands
  @io.puts header("Commands", 1)
  @io.puts
  increment_nest
end

#default_command(name) ⇒ Object

Gives you the name of the current command in the current context



139
140
141
# File 'lib/doing/markdown_document_listener.rb', line 139

def default_command(name)
  @io.puts "#### [Default Command] #{name}" unless name.nil?
end

#end_command(_name) ⇒ Object

Ends a command, and "pops" you back up one context



132
133
134
135
136
# File 'lib/doing/markdown_document_listener.rb', line 132

def end_command(_name)
  @parent_command.pop
  decrement_nest
  @io.puts "* * * * * *\n\n" unless @nest.size > 2
end

#end_commandsObject



143
144
145
# File 'lib/doing/markdown_document_listener.rb', line 143

def end_commands
  decrement_nest
end

#end_optionsObject



108
109
# File 'lib/doing/markdown_document_listener.rb', line 108

def end_options
end

#endingObject

Called when processing has completed



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/doing/markdown_document_listener.rb', line 25

def ending
  if File.exist?('CREDITS.md')
    @io.puts IO.read('CREDITS.md')
    @io.puts
  end

  if File.exist?('AUTHORS.md')
    @io.puts IO.read('AUTHORS.md')
    @io.puts
  end

  if File.exist?('LICENSE.md')
    @io.puts IO.read('LICENSE.md')
    @io.puts
  end
  @io.puts
  @io.puts "Documentation generated #{Time.now.strftime('%Y-%m-%d %H:%M')}"
  @io.puts
  @io.close
end

#flag(name, aliases, desc, long_desc, default_value, arg_name, must_match, _type) ⇒ Object

Gives you a flag in the current context



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/doing/markdown_document_listener.rb', line 80

def flag(name, aliases, desc, long_desc, default_value, arg_name, must_match, _type)
  invocations = ([name] + Array(aliases)).map { |_| "`" + add_dashes(_) + "`" }.join(' | ')
  usage = "#{invocations} #{arg_name || 'arg'}"
  @io.puts header(usage, 2)
  @io.puts
  @io.puts String(desc).strip
  @io.puts "\n*Default Value:* `#{default_value || 'None'}`\n" unless default_value.nil?
  @io.puts "\n*Must Match:* `#{must_match.to_s}`\n" unless must_match.nil?
  cmd_desc = String(long_desc).strip
  @io.puts "> #{cmd_desc}\n" unless cmd_desc.length == 0
  @io.puts
end

#optionsObject



70
71
72
73
74
75
76
77
# File 'lib/doing/markdown_document_listener.rb', line 70

def options
  if @nest.size == 1
    @io.puts "## Global Options"
  else
    @io.puts header("Options", 1)
  end
  @io.puts
end

#program_desc(desc) ⇒ Object

Gives you the program description



47
48
49
50
51
52
# File 'lib/doing/markdown_document_listener.rb', line 47

def program_desc(desc)
  @io.puts "# #{@exe} CLI"
  @io.puts
  @io.puts desc
  @io.puts
end

#program_long_desc(desc) ⇒ Object



54
55
56
57
# File 'lib/doing/markdown_document_listener.rb', line 54

def program_long_desc(desc)
  @io.puts "> #{desc}"
  @io.puts
end

#switch(name, aliases, desc, long_desc, negatable) ⇒ Object

Gives you a switch in the current context



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/doing/markdown_document_listener.rb', line 94

def switch(name, aliases, desc, long_desc, negatable)
  if negatable
    name = "[no-]#{name}" if name.to_s.length > 1
    aliases = aliases.map { |_|  _.to_s.length > 1 ? "[no-]#{_}" : _ }
  end
  invocations = ([name] + aliases).map { |_| "`" + add_dashes(_).strip + "`" }.join('|')
  @io.puts header("#{invocations}", 2)
  @io.puts
  @io.puts String(desc).strip
  cmd_desc = String(long_desc).strip
  @io.puts "\n> #{cmd_desc}\n" unless cmd_desc.length == 0
  @io.puts
end

#version(version) ⇒ Object

Gives you the program version



60
61
62
63
64
65
66
67
68
# File 'lib/doing/markdown_document_listener.rb', line 60

def version(version)
  @io.puts "*v#{version}*"
  @io.puts
  # Hacking in the overview file
  if File.exist?('OVERVIEW.md')
    @io.puts IO.read('OVERVIEW.md')
    @io.puts
  end
end