module MarkdownUsage
Constants
- ALT_HEADING
- ALT_HEADING_LEVELS
- EXTENSIONS
- Error
- HEADING
- STACKTRACE
- VERSION
Public Class Methods
print(options = nil)
click to toggle source
Print the calling program's usage based on options
. See README for a list of options
.
# 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
Private Class Methods
error(message, raise_errors = false)
click to toggle source
# File lib/markdown_usage.rb, line 79 def error(message, raise_errors = false) raise Error, message if raise_errors warn "MarkdownUsage warning: #{message}" true end
extract_usage(lines, sections)
click to toggle source
# File lib/markdown_usage.rb, line 85 def extract_usage(lines, sections) sections = Array(sections).map { |text| Regexp.quote(text) } return lines.join("") unless sections.any? usage = "" while lines.any? sections.each do |pat| next unless lines[0] =~ /#{HEADING}\s*#{pat}/ || lines[0] =~ /\A#{pat}/ && lines[1] =~ ALT_HEADING # TTY::Markdown (Kramdown) chooses the last char to denote heading level cur_level = lines[0][0] == "#" ? $1.size : ALT_HEADING_LEVELS[$1[-1]] usage << lines.shift while lines.any? next_level = if lines[0] =~ /#{HEADING}\s*\S/ $1.size elsif lines[0] =~ /./ && lines[1] =~ ALT_HEADING ALT_HEADING_LEVELS[$1[-1]] end break if next_level && next_level <= cur_level usage << lines.shift end end lines.shift end usage end
find_readme(source, caller_root)
click to toggle source
# File lib/markdown_usage.rb, line 61 def find_readme(source, caller_root) return source if File.exists?(source) roots = [ caller_root ] roots << File.dirname(roots[0]) if %w[bin exe].include?(File.basename(roots[0])) if source != "README" roots.map { |dir| File.join(dir, source) }.find { |path| File.exists?(path) } else roots.each do |dir| readme = EXTENSIONS.map { |ext| File.join(dir, "README.#{ext}") }.find { |path| File.exists?(path) } return readme if readme end nil end end
find_root_directory()
click to toggle source
# File lib/markdown_usage.rb, line 47 def find_root_directory # caller[0] = MarkdownUsage.print # caller[1] = May be caller or MarkdownUsage(), skip the latter # caller[2] = May be caller root = "." return root unless caller[1] =~ STACKTRACE root = File.dirname($1) return root if $2 != "MarkdownUsage" root = File.dirname($1) if caller[2] =~ STACKTRACE root end