diff --git a/bin/madman b/bin/madman index a17232c..6609eb8 100644 --- a/bin/madman +++ b/bin/madman @@ -1,9 +1,8 @@ #!/usr/bin/env ruby -require 'mister_bin' -require 'madman/version' +require 'madman' +require 'madman/cli' -runner = MisterBin::Runner.new 'madman', basedir: __dir__, - version: Madman::VERSION +runner = Madman::CLI.runner begin exit runner.run ARGV diff --git a/bin/madman-nav.rb b/bin/madman-nav.rb deleted file mode 100644 index 4c9f205..0000000 --- a/bin/madman-nav.rb +++ /dev/null @@ -1,62 +0,0 @@ -require 'madman' -require 'io/console' - -summary "Add site-wide navigation links to README files" - -help "This command generates a Table of Contents for a directory, and injects it to a file. In addition, it supports recursive execution, which will add a Table of Contents to all README files (or a filename of your choice) in all the subfolders, creating nagigation pages for an entire Markdown site." - -usage "madman nav DIR [options]" -usage "madman nav (-h|--help)" - -option "-f --force", "Inject TOC to all README files, even if they do not have a marker" -option "-m --marker TEXT", "Look for an HTML comment with [default: nav]" -option "-d --depth N", "The depth of the table of contents [default: 1]" -option "-v --verbose", "Show the updated README content" -option "-t --target NAME", "Set the target filename to look for. [default: README.md]" -option "-r --recursive", "Inject to all target files" -option "-y --dry", "Do not save the updated files, just show what will happen" - -param "DIR", "The directory containing markdown files" - -example "madman nav" -example "madman nav path/to/docs --force --marker toc" -example "madman nav path/to/docs --dry -v -d2" - -action do |args| - @args = args - - if recursive? - Dir["#{dir}/**/#{target}"].each { |file| update_file file } - else - update_file "#{dir}/#{target}" - end - - say dry? ? "Done (dry mode, no changes were made)" : "Done" -end - -def update_file(file) - say "Updating !txtgrn!#{file}" - file_dir = File.dirname file - toc = Madman::Navigation.new file_dir, depth: depth - doc = Madman::Document.from_file file - doc.inject toc.markdown, marker: marker, force: force? - - if verbose? - say word_wrap " !txtblu!#{doc.text}" - say "" - end - - doc.save unless dry? -end - -# CLI Arguments - -def args; @args; end -def dir; args['DIR'] || '.'; end -def depth; args['--depth'].to_i; end -def marker; args['--marker']; end -def target; args['--target']; end -def force?; args['--force']; end -def dry?; args['--dry']; end -def verbose?; args['--verbose']; end -def recursive?; args['--recursive']; end diff --git a/bin/madman-preview.rb b/bin/madman-preview.rb deleted file mode 100644 index c4dbfc1..0000000 --- a/bin/madman-preview.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'madman' - -summary "Serve a markdown file using a local server" - -help "This command will start a local server with two endpoints:\n / will render the markdown with the default renderer\n /github will render with the GitHub API" - -usage "madman preview FILE [--port N --bind ADDRESS]" -usage "madman preview (-h|--help)" - -option "-p --port N", "Set server port [default: 3000]" -option "-b --bind ADDRESS", "Set server listen address [default: 0.0.0.0]" - -param "FILE", "The input markdown file" - -environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nRequired only if you wish to use the '/github' endpoint\nGenerate one here: https://github.com/settings/tokens" - -example "madman preview README.md" -example "madman preview README.md -p4000" - -action do |args| - file = args['FILE'] - port = args['--port'] - bind = args['--bind'] - - say "Starting server at !undblu!localhost:#{port}!txtrst!" - if ENV['GITHUB_ACCESS_TOKEN'] - say "To view the GitHub API version go to !undblu!localhost:#{port}/github!txtrst!" - end - say '' - - Madman::PreviewServer.set bind: bind, port: port, file: file - Madman::PreviewServer.run! -end - diff --git a/bin/madman-readme.rb b/bin/madman-readme.rb deleted file mode 100644 index e6a0c16..0000000 --- a/bin/madman-readme.rb +++ /dev/null @@ -1,37 +0,0 @@ -require 'madman' -require 'lp' - -summary "Create README in all qualified sub directories" - -help "This command generates README.md files in all subdirectories. Each file will receive an H1 caption with the name of the folder it resides in. This command is designed to assist in preparing a folder for table of contents injection." - -usage "madman readme DIR [--dry]" -usage "madman readme (-h|--help)" - -option "-y --dry", "Only show what will be created, don't make any changes" - -param "DIR", "The directory containing markdown files" - -example "madman readme ." -example "madman readme path/to/docs --dry" - -action do |args| - dir = args['DIR'] - basedir = Madman::Directory.new dir, dir - - dirs = basedir.deep_list.select { |i| i.dir? }.map { |i| i.path } - dirs.each do |dir| - file = "#{dir}/README.md" - - if File.exist? file - say "Skipping #{file}" - else - say "Creating !txtgrn!#{file}" - h1 = "# #{File.basename dir}\n\n" - File.write file, h1 unless args['--dry'] - end - end - - say args['--dry'] ? "Done (dry mode, no changes were made)" : "Done" -end - diff --git a/bin/madman-render.rb b/bin/madman-render.rb deleted file mode 100644 index e50d1c4..0000000 --- a/bin/madman-render.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'madman' - -help "Render markdown to HTML" - -usage "madman render FILE [--github --save OUTFILE]" -usage "madman render (-h|--help)" - -option "--github", "Render using the GitHub API\nRequires setting the GITHUB_ACCESS_TOKEN environment variable" -option "--save OUTFILE", "Save the output to a file" - -param "FILE", "The input markdown file" - -environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nGenerate one here: https://github.com/settings/tokens" - -example "madman render README.md" -example "madman render README.md --github" -example "madman render README.md --save out.html" - -action do |args| - infile = args['FILE'] - outfile = args['--save'] - renderer = args['--github'] ? :github : :default - - doc = Madman::Document.from_file infile - - output = doc.render renderer - - if outfile - File.write outfile, output - say "Saved !txtgrn!#{outfile}" - else - puts output - end -end - diff --git a/bin/madman-serve.rb b/bin/madman-serve.rb deleted file mode 100644 index c9ff81c..0000000 --- a/bin/madman-serve.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'madman' - -summary "Serve a markdown directory using a local server" - -usage "madman serve DIR [--port N --bind ADDRESS --github]" -usage "madman serve (-h|--help)" - -option "--github", "Use the GitHub API renderer instead of the default one" -option "-p --port N", "Set server port [default: 3000]" -option "-b --bind ADDRESS", "Set server listen address [default: 0.0.0.0]" - -param "DIR", "The directory containing markdown files" - -environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nRequired only if you wish to use the '/github' endpoint\nGenerate one here: https://github.com/settings/tokens" - -example "madman serve" -example "madman serve path/to/docs -p4000 --github" - -action do |args| - dir = args['DIR'] - port = args['--port'] - bind = args['--bind'] - renderer = args['--github'] ? :github : :default - - say "Starting server at !undblu!localhost:#{port}!txtrst! using the !txtgrn!#{renderer}!txtrst! renderer\n" - - Madman::Server.set bind: bind, port: port, - dir: dir, renderer: renderer - - Madman::Server.run! -end - diff --git a/lib/madman.rb b/lib/madman.rb index 07e8ba7..e6d1fe8 100644 --- a/lib/madman.rb +++ b/lib/madman.rb @@ -5,6 +5,7 @@ require 'slim' require 'string-direction' +require 'madman/cli' require 'madman/injector' require 'madman/directory' @@ -17,4 +18,5 @@ require 'madman/preview_server' require 'madman/server' + require 'byebug' if ENV['BYEBUG'] \ No newline at end of file diff --git a/lib/madman/cli.rb b/lib/madman/cli.rb new file mode 100644 index 0000000..c318e63 --- /dev/null +++ b/lib/madman/cli.rb @@ -0,0 +1,19 @@ +require 'mister_bin' +require 'madman/commands' + +module Madman + class CLI + def self.runner + runner = MisterBin::Runner.new version: Madman::VERSION + + runner.route 'nav', to: Madman::Commands::Nav + runner.route 'preview', to: Madman::Commands::Preview + runner.route 'readme', to: Madman::Commands::Readme + runner.route 'render', to: Madman::Commands::Render + runner.route 'serve', to: Madman::Commands::Serve + + runner + end + end + +end diff --git a/lib/madman/commands.rb b/lib/madman/commands.rb new file mode 100644 index 0000000..32479d2 --- /dev/null +++ b/lib/madman/commands.rb @@ -0,0 +1,5 @@ +require 'madman/commands/nav' +require 'madman/commands/preview' +require 'madman/commands/readme' +require 'madman/commands/render' +require 'madman/commands/serve' \ No newline at end of file diff --git a/lib/madman/commands/nav.rb b/lib/madman/commands/nav.rb new file mode 100644 index 0000000..a674f12 --- /dev/null +++ b/lib/madman/commands/nav.rb @@ -0,0 +1,70 @@ +require 'io/console' + +module Madman + module Commands + class Nav < MisterBin::Command + include Colsole + + summary "Add site-wide navigation links to README files" + + help "This command generates a Table of Contents for a directory, and injects it to a file. In addition, it supports recursive execution, which will add a Table of Contents to all README files (or a filename of your choice) in all the subfolders, creating nagigation pages for an entire Markdown site." + + usage "madman nav DIR [options]" + usage "madman nav (-h|--help)" + + option "-f --force", "Inject TOC to all README files, even if they do not have a marker" + option "-m --marker TEXT", "Look for an HTML comment with [default: nav]" + option "-d --depth N", "The depth of the table of contents [default: 1]" + option "-v --verbose", "Show the updated README content" + option "-t --target NAME", "Set the target filename to look for. [default: README.md]" + option "-r --recursive", "Inject to all target files" + option "-y --dry", "Do not save the updated files, just show what will happen" + + param "DIR", "The directory containing markdown files" + + example "madman nav" + example "madman nav path/to/docs --force --marker toc" + example "madman nav path/to/docs --dry -v -d2" + + def run(args) + @args = args + + if recursive? + Dir["#{dir}/**/#{target}"].each { |file| update_file file } + else + update_file "#{dir}/#{target}" + end + + say dry? ? "Done (dry mode, no changes were made)" : "Done" + end + + def update_file(file) + say "Updating !txtgrn!#{file}" + file_dir = File.dirname file + toc = Madman::Navigation.new file_dir, depth: depth + doc = Madman::Document.from_file file + doc.inject toc.markdown, marker: marker, force: force? + + if verbose? + say word_wrap " !txtblu!#{doc.text}" + say "" + end + + doc.save unless dry? + end + + # CLI Arguments + + def args; @args; end + def dir; args['DIR'] || '.'; end + def depth; args['--depth'].to_i; end + def marker; args['--marker']; end + def target; args['--target']; end + def force?; args['--force']; end + def dry?; args['--dry']; end + def verbose?; args['--verbose']; end + def recursive?; args['--recursive']; end + + end + end +end diff --git a/lib/madman/commands/preview.rb b/lib/madman/commands/preview.rb new file mode 100644 index 0000000..28cab17 --- /dev/null +++ b/lib/madman/commands/preview.rb @@ -0,0 +1,41 @@ +module Madman + module Commands + class Preview < MisterBin::Command + include Colsole + + summary "Serve a markdown file using a local server" + + help "This command will start a local server with two endpoints:\n / will render the markdown with the default renderer\n /github will render with the GitHub API" + + usage "madman preview FILE [--port N --bind ADDRESS]" + usage "madman preview (-h|--help)" + + option "-p --port N", "Set server port [default: 3000]" + option "-b --bind ADDRESS", "Set server listen address [default: 0.0.0.0]" + + param "FILE", "The input markdown file" + + environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nRequired only if you wish to use the '/github' endpoint\nGenerate one here: https://github.com/settings/tokens" + + example "madman preview README.md" + example "madman preview README.md -p4000" + + def run(args) + file = args['FILE'] + port = args['--port'] + bind = args['--bind'] + + say "Starting server at !undblu!localhost:#{port}!txtrst!" + if ENV['GITHUB_ACCESS_TOKEN'] + say "To view the GitHub API version go to !undblu!localhost:#{port}/github!txtrst!" + end + say '' + + Madman::PreviewServer.set bind: bind, port: port, file: file + Madman::PreviewServer.run! + end + + end + end +end + diff --git a/lib/madman/commands/readme.rb b/lib/madman/commands/readme.rb new file mode 100644 index 0000000..c23cf2a --- /dev/null +++ b/lib/madman/commands/readme.rb @@ -0,0 +1,45 @@ +require 'lp' + +module Madman + module Commands + class Readme < MisterBin::Command + include Colsole + + summary "Create README in all qualified sub directories" + + help "This command generates README.md files in all subdirectories. Each file will receive an H1 caption with the name of the folder it resides in. This command is designed to assist in preparing a folder for table of contents injection." + + usage "madman readme DIR [--dry]" + usage "madman readme (-h|--help)" + + option "-y --dry", "Only show what will be created, don't make any changes" + + param "DIR", "The directory containing markdown files" + + example "madman readme ." + example "madman readme path/to/docs --dry" + + def run(args) + dir = args['DIR'] + basedir = Madman::Directory.new dir, dir + + dirs = basedir.deep_list.select { |i| i.dir? }.map { |i| i.path } + dirs.each do |dir| + file = "#{dir}/README.md" + + if File.exist? file + say "Skipping #{file}" + else + say "Creating !txtgrn!#{file}" + h1 = "# #{File.basename dir}\n\n" + File.write file, h1 unless args['--dry'] + end + end + + say args['--dry'] ? "Done (dry mode, no changes were made)" : "Done" + end + + end + end +end + diff --git a/lib/madman/commands/render.rb b/lib/madman/commands/render.rb new file mode 100644 index 0000000..d0044c3 --- /dev/null +++ b/lib/madman/commands/render.rb @@ -0,0 +1,42 @@ +module Madman + module Commands + class Render < MisterBin::Command + include Colsole + + help "Render markdown to HTML" + + usage "madman render FILE [--github --save OUTFILE]" + usage "madman render (-h|--help)" + + option "--github", "Render using the GitHub API\nRequires setting the GITHUB_ACCESS_TOKEN environment variable" + option "--save OUTFILE", "Save the output to a file" + + param "FILE", "The input markdown file" + + environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nGenerate one here: https://github.com/settings/tokens" + + example "madman render README.md" + example "madman render README.md --github" + example "madman render README.md --save out.html" + + def run(args) + infile = args['FILE'] + outfile = args['--save'] + renderer = args['--github'] ? :github : :default + + doc = Madman::Document.from_file infile + + output = doc.render renderer + + if outfile + File.write outfile, output + say "Saved !txtgrn!#{outfile}" + else + puts output + end + end + + end + end +end + diff --git a/lib/madman/commands/serve.rb b/lib/madman/commands/serve.rb new file mode 100644 index 0000000..c125a42 --- /dev/null +++ b/lib/madman/commands/serve.rb @@ -0,0 +1,39 @@ +module Madman + module Commands + class Serve < MisterBin::Command + include Colsole + + summary "Serve a markdown directory using a local server" + + usage "madman serve DIR [--port N --bind ADDRESS --github]" + usage "madman serve (-h|--help)" + + option "--github", "Use the GitHub API renderer instead of the default one" + option "-p --port N", "Set server port [default: 3000]" + option "-b --bind ADDRESS", "Set server listen address [default: 0.0.0.0]" + + param "DIR", "The directory containing markdown files" + + environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nRequired only if you wish to use the '/github' endpoint\nGenerate one here: https://github.com/settings/tokens" + + example "madman serve" + example "madman serve path/to/docs -p4000 --github" + + def run(args) + dir = args['DIR'] + port = args['--port'] + bind = args['--bind'] + renderer = args['--github'] ? :github : :default + + say "Starting server at !undblu!localhost:#{port}!txtrst! using the !txtgrn!#{renderer}!txtrst! renderer\n" + + Madman::Server.set bind: bind, port: port, + dir: dir, renderer: renderer + + Madman::Server.run! + end + + end + end +end + diff --git a/lib/madman/version.rb b/lib/madman/version.rb index a907f10..8c460b7 100644 --- a/lib/madman/version.rb +++ b/lib/madman/version.rb @@ -1,3 +1,3 @@ module Madman - VERSION = "0.1.3" + VERSION = "0.2.0" end \ No newline at end of file diff --git a/madman.gemspec b/madman.gemspec index c843ba9..6bb2d88 100644 --- a/madman.gemspec +++ b/madman.gemspec @@ -12,13 +12,13 @@ Gem::Specification.new do |s| s.authors = ["Danny Ben Shitrit"] s.email = 'db@dannyben.com' s.files = Dir['README.md', 'lib/**/*.*'] - s.executables = Dir['bin/madman*'].map { |f| File.basename f } + s.executables = ['madman'] s.homepage = 'https://github.com/dannyben/madman' s.license = 'MIT' s.required_ruby_version = ">= 2.4.0" s.add_runtime_dependency 'commonmarker', '~> 0.17' - s.add_runtime_dependency 'mister_bin', '~> 0.2' + s.add_runtime_dependency 'mister_bin', '~> 0.3' s.add_runtime_dependency 'puma', '~> 3.11' s.add_runtime_dependency 'sinatra', '~> 2.0' s.add_runtime_dependency 'sinatra-contrib', '~> 2.0' @@ -26,7 +26,6 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'string-direction', '~> 1.2' s.add_development_dependency 'byebug', '~> 10.0' - s.add_development_dependency 'github_changelog_generator', '~> 1.14' s.add_development_dependency 'lp', '~> 0.0' s.add_development_dependency 'octokit', '~> 4.9' s.add_development_dependency 'rack-test', '~> 1.0' diff --git a/spec/madman/bin/bin_spec.rb b/spec/madman/bin_spec.rb similarity index 83% rename from spec/madman/bin/bin_spec.rb rename to spec/madman/bin_spec.rb index 5977f2b..3e2b0c5 100644 --- a/spec/madman/bin/bin_spec.rb +++ b/spec/madman/bin_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'bin/madman' do - subject { MisterBin::Runner.new 'madman', basedir: 'bin' } + subject { Madman::CLI.runner } it "shows list of commands" do expect{ subject.run }.to output_fixture('bin/commands') diff --git a/spec/madman/bin/nav_spec.rb b/spec/madman/commands/nav_spec.rb similarity index 85% rename from spec/madman/bin/nav_spec.rb rename to spec/madman/commands/nav_spec.rb index 15219eb..3e85cbc 100644 --- a/spec/madman/bin/nav_spec.rb +++ b/spec/madman/commands/nav_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'bin/madness-nav' do - subject { MisterBin::Runner.new 'madman', basedir: 'bin' } + subject { Madman::CLI.runner } context "without arguments" do it "shows short usage" do diff --git a/spec/madman/bin/preview_spec.rb b/spec/madman/commands/preview_spec.rb similarity index 91% rename from spec/madman/bin/preview_spec.rb rename to spec/madman/commands/preview_spec.rb index a777095..10d3304 100644 --- a/spec/madman/bin/preview_spec.rb +++ b/spec/madman/commands/preview_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'bin/madness-preview' do - subject { MisterBin::Runner.new 'madman', basedir: 'bin' } + subject { Madman::CLI.runner } context "without arguments" do it "shows short usage" do diff --git a/spec/madman/bin/readme_spec.rb b/spec/madman/commands/readme_spec.rb similarity index 94% rename from spec/madman/bin/readme_spec.rb rename to spec/madman/commands/readme_spec.rb index d2a467c..2b2e457 100644 --- a/spec/madman/bin/readme_spec.rb +++ b/spec/madman/commands/readme_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'bin/madness-readme' do - subject { MisterBin::Runner.new 'madman', basedir: 'bin' } + subject { Madman::CLI.runner } context "without arguments" do it "shows short usage" do diff --git a/spec/madman/bin/render_spec.rb b/spec/madman/commands/render_spec.rb similarity index 94% rename from spec/madman/bin/render_spec.rb rename to spec/madman/commands/render_spec.rb index 709aa1a..a3c8bd3 100644 --- a/spec/madman/bin/render_spec.rb +++ b/spec/madman/commands/render_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'bin/madness-render' do - subject { MisterBin::Runner.new 'madman', basedir: 'bin' } + subject { Madman::CLI.runner } context "without arguments" do it "shows short usage" do diff --git a/spec/madman/bin/serve_spec.rb b/spec/madman/commands/serve_spec.rb similarity index 91% rename from spec/madman/bin/serve_spec.rb rename to spec/madman/commands/serve_spec.rb index 804d95e..b23183f 100644 --- a/spec/madman/bin/serve_spec.rb +++ b/spec/madman/commands/serve_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'bin/madness-serve' do - subject { MisterBin::Runner.new 'madman', basedir: 'bin' } + subject { Madman::CLI.runner } context "without arguments" do it "shows short usage" do