Class: Lono::Markdown::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/markdown/page.rb

Instance Method Summary collapse

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_nameObject



47
48
49
# File 'lib/lono/markdown/page.rb', line 47

def cli_name
  "lono"
end

#desc_markdownObject



134
135
136
137
138
139
140
# File 'lib/lono/markdown/page.rb', line 134

def desc_markdown
  <<-EOL
## Description

#{description}
EOL
end

#descriptionObject



16
17
18
# File 'lib/lono/markdown/page.rb', line 16

def description
  @command.description
end

#docObject



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}
#{options_markdown}
EOL
end

#front_matterObject



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_markdownObject

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_descriptionObject

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

#optionsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lono/markdown/page.rb', line 20

def options
  shell = Shell.new
  @command_class.send(:class_options_help, shell, nil => @command.options.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_markdownObject

handles blank options



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lono/markdown/page.rb', line 156

def options_markdown
  return '' if options.empty?

  <<-EOL
## Options

```
#{options}
```
EOL
end

#pathObject



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

Returns:

  • (Boolean)


60
61
62
# File 'lib/lono/markdown/page.rb', line 60

def subcommand?
  @command_class.subcommands.include?(@command_name)
end

#subcommand_classObject



64
65
66
# File 'lib/lono/markdown/page.rb', line 64

def subcommand_class
  @command_class.subcommand_classes[@command_name]
end

#subcommand_listObject

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

#usageObject



10
11
12
13
14
# File 'lib/lono/markdown/page.rb', line 10

def usage
  banner = @command_class.send(:banner, @command) # banner is protected method
  invoking_command = File.basename($0) # could be rspec, etc
  banner.sub(invoking_command, cli_name)
end

#usage_markdownObject



126
127
128
129
130
131
132
# File 'lib/lono/markdown/page.rb', line 126

def usage_markdown
  <<-EOL
## Usage

#{usage}
EOL
end