Class: GLI::Commands::MarkdownDocumentListener

Inherits:
Object
  • Object
show all
Defined in:
lib/hook/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
# File 'lib/hook/markdown_document_listener.rb', line 9

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

Instance Method Details

#beginningObject



20
21
# File 'lib/hook/markdown_document_listener.rb', line 20

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



111
112
113
114
115
116
117
118
119
120
# File 'lib/hook/markdown_document_listener.rb', line 111

def command(name, aliases, desc, long_desc, arg_name, arg_options)
  arg_name_fmt = @arg_name_formatter.format(arg_name, arg_options, [])
  @io.puts header("`$ #{@exe}` <mark>`#{([name] + aliases).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



104
105
106
107
108
# File 'lib/hook/markdown_document_listener.rb', line 104

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



129
130
131
# File 'lib/hook/markdown_document_listener.rb', line 129

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



123
124
125
126
# File 'lib/hook/markdown_document_listener.rb', line 123

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

#end_commandsObject



133
134
135
# File 'lib/hook/markdown_document_listener.rb', line 133

def end_commands
  decrement_nest
end

#end_optionsObject



101
102
# File 'lib/hook/markdown_document_listener.rb', line 101

def end_options
end

#endingObject

Called when processing has completed



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hook/markdown_document_listener.rb', line 24

def ending
  if File.exist?('CREDITS.md')
    @io.puts IO.read('CREDITS.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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hook/markdown_document_listener.rb', line 73

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



63
64
65
66
67
68
69
70
# File 'lib/hook/markdown_document_listener.rb', line 63

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



40
41
42
43
44
45
# File 'lib/hook/markdown_document_listener.rb', line 40

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

#program_long_desc(desc) ⇒ Object



47
48
49
50
# File 'lib/hook/markdown_document_listener.rb', line 47

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



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hook/markdown_document_listener.rb', line 87

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(_) + "`" }.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



53
54
55
56
57
58
59
60
61
# File 'lib/hook/markdown_document_listener.rb', line 53

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