Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to new mister_bin #11

Merged
merged 3 commits into from
Aug 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions bin/madman
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 0 additions & 62 deletions bin/madman-nav.rb

This file was deleted.

34 changes: 0 additions & 34 deletions bin/madman-preview.rb

This file was deleted.

37 changes: 0 additions & 37 deletions bin/madman-readme.rb

This file was deleted.

35 changes: 0 additions & 35 deletions bin/madman-render.rb

This file was deleted.

32 changes: 0 additions & 32 deletions bin/madman-serve.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/madman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'slim'
require 'string-direction'

require 'madman/cli'
require 'madman/injector'

require 'madman/directory'
Expand All @@ -17,4 +18,5 @@
require 'madman/preview_server'
require 'madman/server'


require 'byebug' if ENV['BYEBUG']
19 changes: 19 additions & 0 deletions lib/madman/cli.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lib/madman/commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'madman/commands/nav'
require 'madman/commands/preview'
require 'madman/commands/readme'
require 'madman/commands/render'
require 'madman/commands/serve'
70 changes: 70 additions & 0 deletions lib/madman/commands/nav.rb
Original file line number Diff line number Diff line change
@@ -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 <!-- TEXT --> [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
41 changes: 41 additions & 0 deletions lib/madman/commands/preview.rb
Original file line number Diff line number Diff line change
@@ -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

Loading