Class: Lono::Markdown::Page
- Inherits:
-
Object
- Object
- Lono::Markdown::Page
- Defined in:
- lib/lono/markdown/page.rb
Instance Method Summary collapse
- #cli_name ⇒ Object
- #desc_markdown ⇒ Object
- #description ⇒ Object
- #doc ⇒ Object
- #front_matter ⇒ Object
-
#initialize(command_class, command_name, parent_command_name = nil) ⇒ Page
constructor
A new instance of Page.
-
#long_desc_markdown ⇒ Object
If the Thor long_description is empty then use the description.
-
#long_description ⇒ Object
Use command’s long description as many description.
- #options ⇒ Object
-
#options_markdown ⇒ Object
handles blank options.
- #path ⇒ Object
- #subcommand? ⇒ Boolean
- #subcommand_class ⇒ Object
-
#subcommand_list ⇒ Object
Note: printable_commands are in the form: [ [command_form,command_comment], [command_form,command_comment], ].
- #usage ⇒ Object
- #usage_markdown ⇒ Object
Constructor Details
#initialize(command_class, command_name, parent_command_name = nil) ⇒ Page
Returns a new instance of Page.
3 4 5 6 7 8 |
# File 'lib/lono/markdown/page.rb', line 3 def initialize(command_class, command_name, parent_command_name=nil) @command_class = command_class @command_name = command_name @parent_command_name = parent_command_name @command = @command_class.commands[@command_name] end |
Instance Method Details
#cli_name ⇒ Object
47 48 49 |
# File 'lib/lono/markdown/page.rb', line 47 def cli_name "lono" end |
#desc_markdown ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/lono/markdown/page.rb', line 134 def desc_markdown <<-EOL ## Description #{description} EOL end |
#description ⇒ Object
16 17 18 |
# File 'lib/lono/markdown/page.rb', line 16 def description @command.description end |
#doc ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/lono/markdown/page.rb', line 106 def doc <<-EOL #{front_matter} #{usage_markdown} #{long_desc_markdown} #{subcommand_list} #{} EOL end |
#front_matter ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/lono/markdown/page.rb', line 116 def front_matter command = [cli_name, @parent_command_name, @command_name].compact.join(' ') <<-EOL --- title: #{command} reference: true --- EOL end |
#long_desc_markdown ⇒ Object
If the Thor long_description is empty then use the description.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lono/markdown/page.rb', line 143 def long_desc_markdown return desc_markdown if long_description.empty? <<-EOL ## Description #{description} #{long_description} EOL end |
#long_description ⇒ Object
Use command’s long description as many description
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lono/markdown/page.rb', line 35 def long_description text = @command.long_description return "" if text.nil? # empty description lines = text.split("\n") lines.map do |line| # In the CLI help, we use 2 spaces to designate commands # In Markdown we need 4 spaces. line.sub(/^ \b/, ' ') end.join("\n") end |
#options ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lono/markdown/page.rb', line 20 def shell = Shell.new @command_class.send(:class_options_help, shell, nil => @command..values) text = shell.stdout.string return "" if text.empty? # there are no options lines = text.split("\n")[1..-1] # remove first line wihth "Options: " lines.map! do |line| # remove 2 leading spaces line.sub(/^ /, '') end lines.join("\n") end |
#options_markdown ⇒ Object
handles blank options
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/lono/markdown/page.rb', line 156 def return '' if .empty? <<-EOL ## Options ``` #{} ``` EOL end |
#path ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/lono/markdown/page.rb', line 51 def path full_name = if @parent_command_name "#{cli_name}-#{@parent_command_name}-#{@command_name}" else "#{cli_name}-#{@command_name}" end "docs/_reference/#{full_name}.md" end |
#subcommand? ⇒ Boolean
60 61 62 |
# File 'lib/lono/markdown/page.rb', line 60 def subcommand? @command_class.subcommands.include?(@command_name) end |
#subcommand_class ⇒ Object
64 65 66 |
# File 'lib/lono/markdown/page.rb', line 64 def subcommand_class @command_class.subcommand_classes[@command_name] end |
#subcommand_list ⇒ Object
Note: printable_commands are in the form:
[
[command_form,command_comment],
[command_form,command_comment],
]
It is useful to grab the command form printable_commands as it shows the proper form.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lono/markdown/page.rb', line 77 def subcommand_list return '' unless subcommand? invoking_command = File.basename($0) # could be rspec, etc command_list = subcommand_class.printable_commands .sort_by { |a| a[0] } .map { |a| a[0].sub!(invoking_command, cli_name); a } # replace with proper comand .reject { |a| a[0].include?("help [COMMAND]") } # filter out help # dress up with markdown text = command_list.map do |a| command, comment = a[0], a[1].sub(/^# /,'') subcommand_name = command.split(' ')[2] full_command_path = "#{cli_name}-#{@command_name}-#{subcommand_name}" full_command_name = "#{cli_name} #{@command_name} #{subcommand_name}" link = "_reference/#{full_command_path}.md" # "* [#{command}]({% link #{link} %})" # Example: [lono cfn delete STACK]({% link _reference/lono-cfn-delete.md %}) "* [#{full_command_name}]({% link #{link} %}) - #{comment}" end.join("\n") <<-EOL ## Subcommands #{text} EOL end |
#usage ⇒ Object
10 11 12 13 14 |
# File 'lib/lono/markdown/page.rb', line 10 def usage = @command_class.send(:banner, @command) # banner is protected method invoking_command = File.basename($0) # could be rspec, etc .sub(invoking_command, cli_name) end |
#usage_markdown ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/lono/markdown/page.rb', line 126 def usage_markdown <<-EOL ## Usage #{usage} EOL end |