Class: CliMarkdown::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_class:, cli_name:, command_name:, parent_command_name: nil) ⇒ Page

Returns a new instance of Page.



4
5
6
7
8
9
10
# File 'lib/cli_markdown/page.rb', line 4

def initialize(cli_class:, cli_name:, command_name:, parent_command_name: nil)
  @cli_class = cli_class # IE: Jets::Commands::Main
  @cli_name = cli_name # IE: lono
  @command_name = command_name # IE: generate
  @parent_command_name = parent_command_name # IE: cfn
  @command = @cli_class.commands[@command_name]
end

Instance Attribute Details

#cli_nameObject (readonly)

Returns the value of attribute cli_name.



3
4
5
# File 'lib/cli_markdown/page.rb', line 3

def cli_name
  @cli_name
end

Instance Method Details

#desc_markdownObject



132
133
134
135
136
137
138
# File 'lib/cli_markdown/page.rb', line 132

def desc_markdown
  <<-EOL
## Description

#{description}
EOL
end

#descriptionObject



18
19
20
# File 'lib/cli_markdown/page.rb', line 18

def description
  @command.description
end

#docObject



104
105
106
107
108
109
110
111
112
# File 'lib/cli_markdown/page.rb', line 104

def doc
  <<-EOL
#{front_matter}
#{usage_markdown}
#{long_desc_markdown}
#{subcommand_list}
#{options_markdown}
EOL
end

#front_matterObject



114
115
116
117
118
119
120
121
122
# File 'lib/cli_markdown/page.rb', line 114

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.



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cli_markdown/page.rb', line 141

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cli_markdown/page.rb', line 37

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



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cli_markdown/page.rb', line 22

def options
  shell = Shell.new
  @cli_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



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cli_markdown/page.rb', line 154

def options_markdown
  return '' if options.empty?

  <<-EOL
## Options

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

#pathObject



49
50
51
52
53
54
55
56
# File 'lib/cli_markdown/page.rb', line 49

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)


58
59
60
# File 'lib/cli_markdown/page.rb', line 58

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

#subcommand_classObject



62
63
64
# File 'lib/cli_markdown/page.rb', line 62

def subcommand_class
  @cli_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.



75
76
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
# File 'lib/cli_markdown/page.rb', line 75

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



12
13
14
15
16
# File 'lib/cli_markdown/page.rb', line 12

def usage
  banner = @cli_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



124
125
126
127
128
129
130
# File 'lib/cli_markdown/page.rb', line 124

def usage_markdown
  <<-EOL
## Usage

#{usage}
EOL
end