Class: Jets::Commands::Markdown::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_class:, command_name:) ⇒ Page

Returns a new instance of Page.



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

def initialize(cli_class:, command_name:)
  @cli_class = cli_class # IE: Jets::Commands::Main
  @command_name = command_name # IE: generate

  @cli_name = "jets"
  @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/jets/commands/markdown/page.rb', line 3

def cli_name
  @cli_name
end

Instance Method Details

#desc_markdownObject



90
91
92
93
94
95
96
# File 'lib/jets/commands/markdown/page.rb', line 90

def desc_markdown
  <<-EOL
## Description

#{description}
EOL
end

#descriptionObject



27
28
29
# File 'lib/jets/commands/markdown/page.rb', line 27

def description
  @command.description
end

#docObject



63
64
65
66
67
68
69
70
# File 'lib/jets/commands/markdown/page.rb', line 63

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

#front_matterObject



72
73
74
75
76
77
78
79
80
# File 'lib/jets/commands/markdown/page.rb', line 72

def front_matter
  command = [cli_name, full_command].compact.join(' ')
  <<-EOL
---
title: #{command}
reference: true
---
EOL
end

#full_commandObject



18
19
20
# File 'lib/jets/commands/markdown/page.rb', line 18

def full_command
  [namespace, @command_name].compact.join(':')
end

#long_desc_markdownObject

If the Thor long_description is empty then use the description.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jets/commands/markdown/page.rb', line 99

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 main description



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jets/commands/markdown/page.rb', line 46

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

#namespaceObject



22
23
24
25
# File 'lib/jets/commands/markdown/page.rb', line 22

def namespace
  ns = @cli_class.to_s.sub('Jets::Commands::','').underscore.gsub('/','-')
  ns == 'main' ? nil : ns
end

#optionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jets/commands/markdown/page.rb', line 31

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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jets/commands/markdown/page.rb', line 112

def options_markdown
  return '' if options.empty?

  <<-EOL
## Options

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

#pathObject



58
59
60
61
# File 'lib/jets/commands/markdown/page.rb', line 58

def path
  full_name = [cli_name, namespace, @command_name].compact.join('-')
  "docs/_reference/#{full_name}.md"
end

#usageObject



12
13
14
15
16
# File 'lib/jets/commands/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



82
83
84
85
86
87
88
# File 'lib/jets/commands/markdown/page.rb', line 82

def usage_markdown
  <<-EOL
## Usage

#{usage}
EOL
end