module MarkdownRubyDocumentation::TemplateParser::CommentMacros

Constants

IGNORE_METHOD_OWNERS
RUBY_TO_MARKDOWN_PROCESSORS
UnimplementedMethod

Attributes

output_object[RW]

Public Instance Methods

array_to_markdown_table(array, key_name:) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 465
def array_to_markdown_table(array, key_name:)
  key_max_length = [array.group_by(&:size).max.first, key_name.size + 1].max
  header         = markdown_table_header([[key_name, key_max_length+3]])
  rows           = array.map { |key| "| #{key.to_s.ljust(key_max_length)} |" }.join("\n")
  [header, rows].join("\n")
end
boolean_blocks(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 162
def boolean_blocks(source_code=print_method_source, proc: false)
  gsub_replacement(source_code, {
    /(.*)\.any\?\s*do\s\|.*\|/ => "Are there any \\1 where\n",
    /(.*)\.all\?\s*do\s\|.*\|/ => "Do all \\1 have\n",
  }, proc: proc)
end
comment_format(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 176
def comment_format(source_code=print_method_source, proc: false)
  gsub_replacement(source_code, { /^#(.*)/ => "</br>*(\\1)*</br>" }, proc: proc)
end
constants_with_name_and_value(ruby_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 403
def constants_with_name_and_value(ruby_source, proc: false)
  ruby_source.gsub(/([A-Z]+[A-Z_0-9]+)/) do |match|
    begin
      value           = ruby_class.const_get(match)
      link            = "##{match.dasherize.downcase}"
      formatted_value = ConstantsPresenter.format(value)
      replacement     = format_link(formatted_value, link)
      proc ? proc.call(replacement, match, { value: value, link: link, formatted_value: formatted_value }) : replacement
    rescue NameError
      match
    end
  end
end
convert_early_return_to_if_else(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 232
def convert_early_return_to_if_else(source_code=print_method_source, proc: false)
  conversions = {
    /(.+) if (.+)/   => "if \\2\n\\1\nend",
    /(.+) unless (.+)/ => "unless \\2\n\\1\nend"
  }
  gsub_replacement(source_code, conversions, proc: proc)
end
default_title(klass_or_path, _ruby_class) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 291
def default_title(klass_or_path, _ruby_class)
  method = MarkdownRubyDocumentation::Method.create(klass_or_path, null_method: true, context: _ruby_class)
  if method.name
    method.name
  else
    method.context_name.to_s.demodulize
  end.to_s.titleize
end
elsif_to_else_if(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 194
def elsif_to_else_if(source_code=print_method_source, proc: false)
  conversions = {
    "elsif" => "else if"
  }
  gsub_replacement(source_code, conversions, proc: proc)
end
eval_method(method_reference=current_method) click to toggle source

@param [String] method_reference @example @return [Object] anything that the evaluated method would return.

# File lib/markdown_ruby_documentation/template_parser.rb, line 84
def eval_method(method_reference=current_method)
  case (method = Method.create(method_reference, context: ruby_class))
  when ClassMethod
    method.context.public_send(method.name)
  when InstanceMethod
    InstanceToClassMethods.new(method: method).eval_instance_method
  end
end
git_hub_file_url(file_path_or_const) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 107
def git_hub_file_url(file_path_or_const)
  if file_path_or_const.include?("/")
    GitHubLink::FileUrl.new(file_path: file_path_or_const)
  else
    const    = Object.const_get(file_path_or_const)
    a_method = const.public_instance_methods.first
    git_hub_method_url("#{file_path_or_const}##{a_method}")
  end
end
git_hub_method_url(method_reference=current_method) click to toggle source

@param [String] method_reference

# File lib/markdown_ruby_documentation/template_parser.rb, line 102
def git_hub_method_url(method_reference=current_method)
  method = Method.create(method_reference.dup, context: ruby_class)
  GitHubLink::MethodUrl.new(subject: method.context, method_object: method)
end
hash_to_markdown_table(hash, key_name:, value_name:) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 457
def hash_to_markdown_table(hash, key_name:, value_name:)
  key_max_length   = [hash.keys.group_by(&:size).max.first, key_name.size + 1].max
  value_max_length = [hash.values.group_by { |v| v.try!(:size) || 1 }.max.first, value_name.size + 1].max
  header           = markdown_table_header([[key_name, key_max_length+2], [value_name, value_max_length+2]])
  rows             = hash.map { |key, value| "| #{key.to_s.ljust(key_max_length)} | #{value.to_s.ljust(value_max_length)}|" }.join("\n")
  [header, rows].join("\n")
end
markdown_table_header(array_headers) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 472
def markdown_table_header(array_headers)
  parts      = array_headers.map { |header, pad_length=0| " #{header.ljust(pad_length-1)}" }
  bar        = parts.map(&:length).map { |length| ("-" * (length)) }.join("|")
  bar[-1]    = "|"
  header     = parts.join("|")
  header[-1] = "|"
  [("|" + header), ("|" + bar)].join("\n")
end
nil_check_readable(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 187
def nil_check_readable(source_code=print_method_source, proc: false)
  conversions = {
    ".nil?" => " is missing?"
  }
  gsub_replacement(source_code, conversions, proc: proc)
end
pretty_early_return(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 240
def pretty_early_return(source_code=print_method_source, proc: false)
  conversions = {
    /return (unless|if)/   => 'return nothing \1'
  }
  gsub_replacement(source_code, conversions, proc: proc)
end
print_mark_doc_from(method_reference) click to toggle source

@param [String] method_reference @example @return [String]

print_method_source(method_reference=current_method) click to toggle source

@param [String] method_reference @return [String] the source of a method block is returned as text.

print_raw_comment(method_reference) click to toggle source

@param [String] method_reference @example @return [String] of any comments proceeding a method def

question_mark_method_format(ruby_source, *) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 417
def question_mark_method_format(ruby_source, *)
  ruby_source.gsub(/(\b(?<!['"])\.[a-z_][a-z_0-9]+\?(?!['"]))/) do |match|
    " is #{match}".sub(".", "")
  end
end
readable_ruby_numbers(source_code=print_method_source, proc: -> (replacement, _) { ActiveSupport::NumberHelper.number_to_delimited(replacement) } click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 226
def readable_ruby_numbers(source_code=print_method_source, proc: -> (replacement, _) { ActiveSupport::NumberHelper.number_to_delimited(replacement) })
  source_code.gsub(/([0-9][0-9_]+[0-9]+)/) do |match|
    proc.call(eval(match), match)
  end
end
remove_colons(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 201
def remove_colons(source_code=print_method_source, proc: false)
  conversions = {
    ":" => ''
  }
  gsub_replacement(source_code, conversions, proc: proc)
end
remove_end_keyword(ruby_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 430
def remove_end_keyword(ruby_source, proc: false)
  conversions = {
    /^[\s]*end\n?/ => ""
  }
  gsub_replacement(ruby_source, conversions, proc: proc)
end
remove_memoized_vars(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 180
def remove_memoized_vars(source_code=print_method_source, proc: false)
  conversions = {
    /@[a-z][a-z0-9_]+ \|\|=?\s/ => "" # @memoized_vars ||=
  }
  gsub_replacement(source_code, conversions, proc: proc)
end
rescue_format(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 169
def rescue_format(source_code=print_method_source, proc: false)
  gsub_replacement(source_code, {
    /=>\s.*/                     => "",
    /rescue ([a-zA-Z0-9::]*)./ => "Given a failure of \\1 __Then__\n",
  }, proc: proc)
end
ruby_case_statement_to_md(ruby_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 448
def ruby_case_statement_to_md(ruby_source, proc: false)
  conversions = {
    /case(.*)/ => "* __Given__\\1",
    /when(.*)/ => "* __When__\\1\n__Then__",
    "else"     => "* __Else__"
  }
  gsub_replacement(ruby_source, conversions, proc: proc)
end
ruby_if_statement_to_md(ruby_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 437
def ruby_if_statement_to_md(ruby_source, proc: false)
  conversions = {
    /elsif(.*)/                    => "* __Else If__\\1\n__Then__",
    /^\s?if(.*)\s([|&]{0,2})\n(.*)/ => "* __If__\\1 \\2 \\3\n__Then__",
    /^\s?if(.*)/                   => "* __If__\\1\n__Then__",
    /unless(.*)/                   => "* __Unless__\\1\n__Then__",
    "else"                         => "* __Else__"
  }
  gsub_replacement(ruby_source, conversions, proc: proc)
end
ruby_operators_to_english(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 208
def ruby_operators_to_english(source_code=print_method_source, proc: false)
  conversions = {
    "&&"   => "and",
    ">="   => "is greater than or equal to",
    "<="   => "is less than or equal to",
    " < "   => " is less than ",
    " > "   => " is greater than ",
    " == "  => " Equal to ",
    "||"    => "or",
    /^!!/  => "",
    /\s!!/ => " ",
    /^!/   => "*is not* ",
    /\s!/  => " *is not* "
  }

  gsub_replacement(source_code, conversions, proc: proc)
end
ruby_to_markdown(*args) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 137
def ruby_to_markdown(*args)
  any_args           = AnyArgs.new(args:                args,
                                   print_method_source: method(:print_method_source),
                                   caller:              caller,
                                   for_method:          __method__,
                                   method_creator:      method(:create_method_with_ruby_class))
  disable_processors = any_args.disable_processors
  ruby_source        = any_args.source_code

  RUBY_TO_MARKDOWN_PROCESSORS.each do |processor|
    options     = disable_processors.fetch(processor, :enabled)
    ruby_source = case options
                  when :enabled
                    send(processor, ruby_source)
                  when Hash
                    send(processor, ruby_source, options)
                  when Proc
                    send(processor, ruby_source, proc: options)
                  else
                    ruby_source
                  end
  end
  ruby_source
end
symbol_to_proc(ruby_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 423
def symbol_to_proc(ruby_source, proc: false)
  conversions = {
    /\(&:([a-z_?!]*)\)/ => " \\1"
  }
  gsub_replacement(ruby_source, conversions, proc: proc)
end
ternary_to_if_else(source_code=print_method_source, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 247
def ternary_to_if_else(source_code=print_method_source, proc: false)
  conversions = {
    /(.*) \? (.*) \: (.*)/   =>  "if \\1\n\\2\nelse\n\\3\nend"
  }
  gsub_replacement(source_code, conversions, proc: proc)
end

Private Instance Methods

compile_comment(method, comment = extract_dsl_comment_from_method(method)) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 529
def compile_comment(method, comment = extract_dsl_comment_from_method(method))
  parse_erb(insert_method_name(strip_comment_hash(comment), method), method)
end
create_method_with_ruby_class(method_reference) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 514
def create_method_with_ruby_class(method_reference)
  Method.create(method_reference, context: ruby_class)
end
fetch_methods(source_code, &block) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 491
def fetch_methods(source_code, &block)
  source_code.gsub(/(\b(?<!['"])[a-z_][a-z_0-9?!]*(?!['"]))/) do |match|
    block.call(match) if is_a_method_on_ruby_class?(match)
  end
end
fetch_strings(source_code, &block) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 483
def fetch_strings(source_code, &block)
  source_code.gsub(/["']?[a-z_A-Z?!0-9]*["']?/, &block)
end
fetch_strings_that_contain_codes(source_code, &block) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 487
def fetch_strings_that_contain_codes(source_code, &block)
  source_code.gsub(/["']?[a-z_A-Z?!0-9]*["']?/, &block)
end
fetch_symbols(source_code, &block) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 497
def fetch_symbols(source_code, &block)
  source_code = source_code.gsub(/:[a-z_?!0-9]+/, &block)
  source_code = source_code.gsub(/[a-z_?!0-9]+:/, &block)
end
gsub_replacement(source_code, conversions, proc: false) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 518
def gsub_replacement(source_code, conversions, proc: false)
  conversions.each do |symbol, replacement|
    source_code = if proc
                    source_code.gsub(symbol, &proc.curry[replacement])
                  else
                    source_code.gsub(symbol, replacement)
                  end
  end
  source_code
end
is_a_method_on_ruby_class?(method, klass=ruby_class) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 502
def is_a_method_on_ruby_class?(method, klass=ruby_class)
  [*klass.public_instance_methods, *klass.private_instance_methods].include?(remove_quotes(method).to_sym)
end
remove_quotes(string) click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 506
def remove_quotes(string)
  string.gsub(/['|"]/, "")
end
ruby_class() click to toggle source
# File lib/markdown_ruby_documentation/template_parser.rb, line 510
def ruby_class
  @ruby_class || self
end