Module: MarkdownUsage

Defined in:
lib/markdown_usage.rb,
lib/markdown_usage/version.rb

Constant Summary collapse

Error =
Class.new(StandardError)
HEADING =
/\A(\#{1,6})/
ALT_HEADING =
/\A([-=]+)\s*\Z/
ALT_HEADING_LEVELS =
{ "=" => 1, "-" => 2 }.freeze
STACKTRACE =
/\A(.+):\d+:in\s+`(.+)'/
EXTENSIONS =
%w[md markdown].freeze
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

Print the calling program’s usage based on options. See README for a list of options.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/markdown_usage.rb', line 20

def print(options = nil)
  options ||= {}

  output = options[:output] || $stdout
  exitcode = options.include?(:exit) ? options[:exit] : 0
  raise_errors = options.include?(:raise_errors) ? options[:raise_errors] : true

  error("Usage document cannot be found", raise_errors) and return unless defined?(DATA) || options[:source]

  if !options[:source]
    lines = DATA.readlines
  else

    source = find_readme(options[:source], find_root_directory)
    error("Cannot find usage source: #{options[:source]}", raise_errors) and return unless source

    lines = File.readlines(source)
  end

  output.puts TTY::Markdown.parse(extract_usage(lines, options[:sections]))
  exit exitcode unless exitcode == false
rescue => e
  error(e, raise_errors)
end