cli_markdown 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec2eb95b5cd0c7a7295f1b31195979c9d9fad652676517a0e59166875b76e330
4
+ data.tar.gz: 0cf8d886a34c22b26ffa8f8fe49f12ab68a30e36286ddfcb149646394939dac9
5
+ SHA512:
6
+ metadata.gz: 65805b1221655539b07cfb7144f4ed86e52446f7ee8c5e1d04972b5e5615e13f3dabcecfb0face82f17f1db4433795d4edf859749304e5cf010ae40a55b9c75b
7
+ data.tar.gz: a6b7bf93e2763b2e3557a0d0f8c6e57cba0081be9476267059b0c4b66a26d89da0fb84355165edfe679d74601e9ad3beb3682a3eb94403fbddbcbd06c34db6d3
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /docs
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ # Change Log
2
+
3
+ ## [0.1.0]
4
+ - Initial release
5
+ - Extract cli doc generation out of lono
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cli_markdown.gemspec
6
+ gemspec
@@ -0,0 +1,129 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cli_markdown (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (5.1.5)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (~> 0.7)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ aws-partitions (1.67.0)
15
+ aws-sdk-cloudformation (1.3.0)
16
+ aws-sdk-core (~> 3)
17
+ aws-sigv4 (~> 1.0)
18
+ aws-sdk-core (3.16.0)
19
+ aws-partitions (~> 1.0)
20
+ aws-sigv4 (~> 1.0)
21
+ jmespath (~> 1.0)
22
+ aws-sdk-kms (1.5.0)
23
+ aws-sdk-core (~> 3)
24
+ aws-sigv4 (~> 1.0)
25
+ aws-sdk-s3 (1.8.2)
26
+ aws-sdk-core (~> 3)
27
+ aws-sdk-kms (~> 1)
28
+ aws-sigv4 (~> 1.0)
29
+ aws-sigv4 (1.0.2)
30
+ coderay (1.1.2)
31
+ colorize (0.8.1)
32
+ concurrent-ruby (1.0.5)
33
+ diff-lcs (1.3)
34
+ ffi (1.9.23)
35
+ filesize (0.1.1)
36
+ formatador (0.2.5)
37
+ graph (2.8.2)
38
+ guard (2.14.2)
39
+ formatador (>= 0.2.4)
40
+ listen (>= 2.7, < 4.0)
41
+ lumberjack (>= 1.0.12, < 2.0)
42
+ nenv (~> 0.1)
43
+ notiffany (~> 0.0)
44
+ pry (>= 0.9.12)
45
+ shellany (~> 0.0)
46
+ thor (>= 0.18.1)
47
+ guard-cloudformation (0.0.3)
48
+ colorize
49
+ guard
50
+ guard-compat (1.2.1)
51
+ guard-lono (1.0.1)
52
+ colorize
53
+ guard
54
+ guard-compat
55
+ hashie (3.5.7)
56
+ i18n (0.9.5)
57
+ concurrent-ruby (~> 1.0)
58
+ jmespath (1.3.1)
59
+ json (2.1.0)
60
+ listen (3.1.5)
61
+ rb-fsevent (~> 0.9, >= 0.9.4)
62
+ rb-inotify (~> 0.9, >= 0.9.7)
63
+ ruby_dep (~> 1.2)
64
+ lono (4.0.6)
65
+ activesupport
66
+ aws-sdk-cloudformation
67
+ aws-sdk-s3
68
+ colorize
69
+ filesize
70
+ graph
71
+ guard
72
+ guard-cloudformation
73
+ guard-lono
74
+ hashie
75
+ json
76
+ rb-fsevent
77
+ render_me_pretty
78
+ thor
79
+ lumberjack (1.0.12)
80
+ method_source (0.9.0)
81
+ minitest (5.11.3)
82
+ nenv (0.3.0)
83
+ notiffany (0.1.1)
84
+ nenv (~> 0.1)
85
+ shellany (~> 0.0)
86
+ pry (0.11.3)
87
+ coderay (~> 1.1.0)
88
+ method_source (~> 0.9.0)
89
+ rake (12.3.0)
90
+ rb-fsevent (0.10.2)
91
+ rb-inotify (0.9.10)
92
+ ffi (>= 0.5.0, < 2)
93
+ render_me_pretty (0.7.1)
94
+ activesupport
95
+ colorize
96
+ tilt
97
+ rspec (3.7.0)
98
+ rspec-core (~> 3.7.0)
99
+ rspec-expectations (~> 3.7.0)
100
+ rspec-mocks (~> 3.7.0)
101
+ rspec-core (3.7.1)
102
+ rspec-support (~> 3.7.0)
103
+ rspec-expectations (3.7.0)
104
+ diff-lcs (>= 1.2.0, < 2.0)
105
+ rspec-support (~> 3.7.0)
106
+ rspec-mocks (3.7.0)
107
+ diff-lcs (>= 1.2.0, < 2.0)
108
+ rspec-support (~> 3.7.0)
109
+ rspec-support (3.7.1)
110
+ ruby_dep (1.5.0)
111
+ shellany (0.0.1)
112
+ thor (0.20.0)
113
+ thread_safe (0.3.6)
114
+ tilt (2.0.8)
115
+ tzinfo (1.2.5)
116
+ thread_safe (~> 0.1)
117
+
118
+ PLATFORMS
119
+ ruby
120
+
121
+ DEPENDENCIES
122
+ bundler
123
+ cli_markdown!
124
+ lono
125
+ rake
126
+ rspec
127
+
128
+ BUNDLED WITH
129
+ 1.16.1
@@ -0,0 +1,43 @@
1
+ # CliMarkdown
2
+
3
+ Library generates markdown from a Thor CLI class. The markdown is meant to be served as part of a Jekyll static website.
4
+
5
+ ## Usage
6
+
7
+ To use, add the following to your `Rakefile`:
8
+
9
+ ```ruby
10
+ require_relative "lib/your_tool"
11
+ require "cli_markdown"
12
+ desc "Generates cli reference docs as markdown"
13
+ task :docs do
14
+ CliMarkdown::Creator.create_all(cli_class: YourTool::CLI, cli_name: "your_tool")
15
+ end
16
+ ```
17
+
18
+ * The `YourTool::CLI` class must be a Thor class.
19
+ * The cli_name is the name of the cli command used for your tool.
20
+
21
+ To generate docs, you can call:
22
+
23
+ rake docs
24
+
25
+ This will generate docs in the `docs/_reference` folder.
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'cli_markdown'
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cli_markdown.
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
3
+
4
+ # In a real project the require will be the project itself.
5
+ # This ask is only here to pass specs.
6
+ require "lono"
7
+ require "./lib/cli_markdown"
8
+ desc "Generates cli reference docs as markdown"
9
+ task :docs do
10
+ CliMarkdown::Creator.create_all(cli_class: Lono::CLI, cli_name: "lono")
11
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cli_markdown"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cli_markdown/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cli_markdown"
7
+ spec.version = CliMarkdown::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tongueroo@gmail.com"]
10
+
11
+ spec.summary = "Generate markdown docs from cli docs"
12
+ spec.homepage = "https://github.com/tongueroo/cli_markdown"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "lono"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,8 @@
1
+ require "cli_markdown/version"
2
+
3
+ module CliMarkdown
4
+ autoload :Creator, 'cli_markdown/creator'
5
+ autoload :Index, 'cli_markdown/index'
6
+ autoload :Page, 'cli_markdown/page'
7
+ autoload :Shell, 'cli_markdown/shell'
8
+ end
@@ -0,0 +1,81 @@
1
+ require "active_support/core_ext/object"
2
+
3
+ module CliMarkdown
4
+ class Creator
5
+ cattr_accessor :mute
6
+
7
+ def self.create_all(options={})
8
+ clean unless options[:parent_command_name]
9
+ new(options).create_all
10
+ end
11
+
12
+ def self.clean
13
+ FileUtils.rm_rf("docs/_reference")
14
+ FileUtils.rm_f("docs/reference.md")
15
+ end
16
+
17
+ # cli_class is top-level CLI class.
18
+ def initialize(cli_class:, cli_name:, parent_command_name: nil)
19
+ @cli_class = cli_class
20
+ @cli_name = cli_name
21
+ @parent_command_name = parent_command_name
22
+ end
23
+
24
+ def create_all
25
+ create_index unless @parent_command_name
26
+
27
+ @cli_class.commands.keys.each do |command_name|
28
+ page = Page.new(
29
+ cli_class: @cli_class,
30
+ cli_name: @cli_name,
31
+ command_name: command_name,
32
+ parent_command_name: @parent_command_name,
33
+ )
34
+ create_page(page)
35
+
36
+ if subcommand?(command_name)
37
+ subcommand_class = subcommand_class(command_name)
38
+ parent_command_name = command_name
39
+
40
+ say "Creating subcommands pages for #{parent_command_name}..."
41
+ Creator.create_all(
42
+ cli_class: subcommand_class,
43
+ cli_name: @cli_name,
44
+ parent_command_name: parent_command_name
45
+ )
46
+ end
47
+ end
48
+ end
49
+
50
+ def create_page(page)
51
+ say "Creating #{page.path}..."
52
+ FileUtils.mkdir_p(File.dirname(page.path))
53
+ IO.write(page.path, page.doc)
54
+ end
55
+
56
+ def create_index
57
+ create_include_reference
58
+ page = Index.new(@cli_class, @cli_name)
59
+ FileUtils.mkdir_p(File.dirname(page.path))
60
+ say "Creating #{page.path}"
61
+ IO.write(page.path, page.doc)
62
+ end
63
+
64
+ def create_include_reference
65
+ path = "docs/_includes/reference.md"
66
+ IO.write(path, "Generic tool description. Please edit #{path} with a description.") unless File.exist?(path)
67
+ end
68
+
69
+ def subcommand?(command_name)
70
+ @cli_class.subcommands.include?(command_name)
71
+ end
72
+
73
+ def subcommand_class(command_name)
74
+ @cli_class.subcommand_classes[command_name]
75
+ end
76
+
77
+ def say(text)
78
+ puts text unless self.class.mute
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,36 @@
1
+ module CliMarkdown
2
+ class Index
3
+ def initialize(cli_class, cli_name)
4
+ @cli_class = cli_class
5
+ @cli_name = cli_name
6
+ end
7
+
8
+ def path
9
+ "docs/reference.md"
10
+ end
11
+
12
+ def command_list
13
+ @cli_class.commands.keys.sort.map.each do |command_name|
14
+ page = Page.new(
15
+ cli_class: @cli_class,
16
+ cli_name: @cli_name,
17
+ command_name: command_name,
18
+ )
19
+ link = page.path.sub("docs/", "")
20
+ # Example: [lono cfn]({% link _reference/lono-cfn.md %})
21
+ "* [#{@cli_name} #{command_name}]({% link #{link} %})"
22
+ end.join("\n")
23
+ end
24
+
25
+ def doc
26
+ <<-EOL
27
+ ---
28
+ title: CLI Reference
29
+ ---
30
+ {% include reference.md %}
31
+
32
+ #{command_list}
33
+ EOL
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,167 @@
1
+ module CliMarkdown
2
+ class Page
3
+ attr_reader :cli_name
4
+ def initialize(cli_class:, cli_name:, command_name:, parent_command_name: nil)
5
+ @cli_class = cli_class # IE: Lono::CLI
6
+ @cli_name = cli_name # IE: lono
7
+ @command_name = command_name # IE: generate
8
+ @parent_command_name = parent_command_name # IE: cfn
9
+ @command = @cli_class.commands[@command_name]
10
+ end
11
+
12
+ def usage
13
+ banner = @cli_class.send(:banner, @command) # banner is protected method
14
+ invoking_command = File.basename($0) # could be rspec, etc
15
+ banner.sub(invoking_command, cli_name)
16
+ end
17
+
18
+ def description
19
+ @command.description
20
+ end
21
+
22
+ def options
23
+ shell = Shell.new
24
+ @cli_class.send(:class_options_help, shell, nil => @command.options.values)
25
+ text = shell.stdout.string
26
+ return "" if text.empty? # there are no options
27
+
28
+ lines = text.split("\n")[1..-1] # remove first line wihth "Options: "
29
+ lines.map! do |line|
30
+ # remove 2 leading spaces
31
+ line.sub(/^ /, '')
32
+ end
33
+ lines.join("\n")
34
+ end
35
+
36
+ # Use command's long description as many description
37
+ def long_description
38
+ text = @command.long_description
39
+ return "" if text.nil? # empty description
40
+
41
+ lines = text.split("\n")
42
+ lines.map do |line|
43
+ # In the CLI help, we use 2 spaces to designate commands
44
+ # In Markdown we need 4 spaces.
45
+ line.sub(/^ \b/, ' ')
46
+ end.join("\n")
47
+ end
48
+
49
+ def path
50
+ full_name = if @parent_command_name
51
+ "#{cli_name}-#{@parent_command_name}-#{@command_name}"
52
+ else
53
+ "#{cli_name}-#{@command_name}"
54
+ end
55
+ "docs/_reference/#{full_name}.md"
56
+ end
57
+
58
+ def subcommand?
59
+ @cli_class.subcommands.include?(@command_name)
60
+ end
61
+
62
+ def subcommand_class
63
+ @cli_class.subcommand_classes[@command_name]
64
+ end
65
+
66
+ # Note:
67
+ # printable_commands are in the form:
68
+ # [
69
+ # [command_form,command_comment],
70
+ # [command_form,command_comment],
71
+ # ]
72
+ #
73
+ # It is useful to grab the command form printable_commands as it shows
74
+ # the proper form.
75
+ def subcommand_list
76
+ return '' unless subcommand?
77
+
78
+ invoking_command = File.basename($0) # could be rspec, etc
79
+ command_list = subcommand_class.printable_commands
80
+ .sort_by { |a| a[0] }
81
+ .map { |a| a[0].sub!(invoking_command, cli_name); a } # replace with proper comand
82
+ .reject { |a| a[0].include?("help [COMMAND]") } # filter out help
83
+
84
+ # dress up with markdown
85
+ text = command_list.map do |a|
86
+ command, comment = a[0], a[1].sub(/^# /,'')
87
+ subcommand_name = command.split(' ')[2]
88
+ full_command_path = "#{cli_name}-#{@command_name}-#{subcommand_name}"
89
+ full_command_name = "#{cli_name} #{@command_name} #{subcommand_name}"
90
+ link = "_reference/#{full_command_path}.md"
91
+
92
+ # "* [#{command}]({% link #{link} %})"
93
+ # Example: [lono cfn delete STACK]({% link _reference/lono-cfn-delete.md %})
94
+ "* [#{full_command_name}]({% link #{link} %}) - #{comment}"
95
+ end.join("\n")
96
+
97
+ <<-EOL
98
+ ## Subcommands
99
+
100
+ #{text}
101
+ EOL
102
+ end
103
+
104
+ def doc
105
+ <<-EOL
106
+ #{front_matter}
107
+ #{usage_markdown}
108
+ #{long_desc_markdown}
109
+ #{subcommand_list}
110
+ #{options_markdown}
111
+ EOL
112
+ end
113
+
114
+ def front_matter
115
+ command = [cli_name, @parent_command_name, @command_name].compact.join(' ')
116
+ <<-EOL
117
+ ---
118
+ title: #{command}
119
+ reference: true
120
+ ---
121
+ EOL
122
+ end
123
+
124
+ def usage_markdown
125
+ <<-EOL
126
+ ## Usage
127
+
128
+ #{usage}
129
+ EOL
130
+ end
131
+
132
+ def desc_markdown
133
+ <<-EOL
134
+ ## Description
135
+
136
+ #{description}
137
+ EOL
138
+ end
139
+
140
+ # If the Thor long_description is empty then use the description.
141
+ def long_desc_markdown
142
+ return desc_markdown if long_description.empty?
143
+
144
+ <<-EOL
145
+ ## Description
146
+
147
+ #{description}
148
+
149
+ #{long_description}
150
+ EOL
151
+ end
152
+
153
+ # handles blank options
154
+ def options_markdown
155
+ return '' if options.empty?
156
+
157
+ <<-EOL
158
+ ## Options
159
+
160
+ ```
161
+ #{options}
162
+ ```
163
+ EOL
164
+ end
165
+
166
+ end
167
+ end
@@ -0,0 +1,11 @@
1
+ require 'thor'
2
+
3
+ module CliMarkdown
4
+ # Override stdout as an @io object so we can grab the text written normally
5
+ # outputted to the shell.
6
+ class Shell < Thor::Shell::Basic
7
+ def stdout
8
+ @io ||= StringIO.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module CliMarkdown
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli_markdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lono
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - tongueroo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - cli_markdown.gemspec
86
+ - lib/cli_markdown.rb
87
+ - lib/cli_markdown/creator.rb
88
+ - lib/cli_markdown/index.rb
89
+ - lib/cli_markdown/page.rb
90
+ - lib/cli_markdown/shell.rb
91
+ - lib/cli_markdown/version.rb
92
+ homepage: https://github.com/tongueroo/cli_markdown
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Generate markdown docs from cli docs
115
+ test_files: []