module MarkdownRubyDocumentation::TemplateParser::CommentMacros
Constants
- IGNORE_METHOD_OWNERS
- RUBY_TO_MARKDOWN_PROCESSORS
- UnimplementedMethod
Attributes
Public Instance Methods
# 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
# 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
# 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
# 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
# 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
# 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
# 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
@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
@param [String] title the name of the link @param [String] link_ref the url with method anchor @example format_link
(“MyLink”, “path/to/it#method_name?”)
#=> "[MyLink](#path/to/it#method-name)"
# File lib/markdown_ruby_documentation/template_parser.rb, line 258 def format_link(title, link_ref) path, anchor = *link_ref.to_s.split("#") formatted_path = [path, anchor.try!(:dasherize).try!(:delete, "?").try(:delete, "!")].compact.join("#") "[#{title}](#{formatted_path})" end
# 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
@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
# 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
@param [Class, String, Pathname] klass_or_path
1. String or Class representing a method reference 2. Pathname representing the full path of the file location a method is defined
@param [String] title is the link display value @return [String, Symbol] Creates link to a given generated markdown file or returns :non_project_location message.
1. "[title](path/to/markdown/file.md#method-name)" 2. :non_project_location
# File lib/markdown_ruby_documentation/template_parser.rb, line 279 def link_to_markdown(klass_or_path, _ruby_class: ruby_class, title: default_title(klass_or_path, _ruby_class)) if klass_or_path.is_a?(String) || klass_or_path.is_a?(Class) || klass_or_path.is_a?(Module) link_to_markdown_method_reference(method_reference: klass_or_path, title: title, ruby_class: _ruby_class) elsif klass_or_path.is_a?(Pathname) link_to_markdown_full_path(path: klass_or_path, title: title, ruby_class: _ruby_class) else raise ArgumentError, "invalid first arg given: #{klass_or_path} for #{__method__}" end end
# File lib/markdown_ruby_documentation/template_parser.rb, line 313 def link_to_markdown_full_path(path:, title:, ruby_class:) if path.to_s.include?(MarkdownRubyDocumentation::Generate.load_path) relative_path = path.to_s.gsub(MarkdownRubyDocumentation::Generate.load_path, "") const_nest, meth = relative_path.split("#") const = const_nest.split("/").map(&:camelize).join("::") link_to_markdown_method_reference(method_reference: "#{const.gsub(".rb", "")}##{meth}", title: title, ruby_class: ruby_class) else :non_project_location end end
# File lib/markdown_ruby_documentation/template_parser.rb, line 304 def link_to_markdown_method_reference(method_reference:, title:, ruby_class:) return title if IGNORE_METHOD_OWNERS.include?(ruby_class) method = MarkdownRubyDocumentation::Method.create(method_reference, null_method: true, context: ruby_class) parts = method.context_name.to_s.split("::").reject(&:blank?) path = parts.map { |p| p.underscore }.join("/") path = "#{path}.md#{method.type_symbol}#{method.name}" format_link title, MarkdownRubyDocumentation::GitHubLink::FileUrl.new(file_path: File.join(MarkdownRubyDocumentation::Generate.output_object.relative_dir, path)).to_s end
# 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
# File lib/markdown_ruby_documentation/template_parser.rb, line 385 def methods_as_local_links(ruby_source, call_on_title: :titleize, method_to_class: {}, proc: false) ruby_source.gsub(MethodLink::RUBY_METHOD_REGEX) do |match| if is_a_method_on_ruby_class?(match, method_to_class[match.to_sym] || ruby_class) replacement = MethodLink.new(match: match, ruby_class: ruby_class, call_on_title: call_on_title, method_to_class: method_to_class, link_to_markdown: method(:link_to_markdown)).link proc ? proc.call(replacement, match) : replacement else match end end end
# 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
# 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
@param [String] method_reference @example @return [String]
# File lib/markdown_ruby_documentation/template_parser.rb, line 75 def print_mark_doc_from(method_reference) method = Method.create(method_reference, context: ruby_class) comment = extract_dsl_comment(print_raw_comment(method_reference)) compile_comment(method, comment) end
@param [String] method_reference @return [String] the source of a method block is returned as text.
# File lib/markdown_ruby_documentation/template_parser.rb, line 95 def print_method_source(method_reference=current_method) method = Method.create(method_reference.dup, context: ruby_class) PrintMethodSource.new(method: method).print end
@param [String] method_reference @example @return [String] of any comments proceeding a method def
# File lib/markdown_ruby_documentation/template_parser.rb, line 68 def print_raw_comment(method_reference) strip_comment_hash(ruby_class_meth_comment(Method.create(method_reference, context: ruby_class))) end
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
@param [String] link_ref the url with method anchor @example title_from_link
“path/to/it#method_name?”)
#=> "[Method Name](#path/to/it#method-name)"
# File lib/markdown_ruby_documentation/template_parser.rb, line 267 def title_from_link(link_ref) [link_ref.split("/").last.split("#").last.to_s.humanize, link_ref] end
Private Instance Methods
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# File lib/markdown_ruby_documentation/template_parser.rb, line 506 def remove_quotes(string) string.gsub(/['|"]/, "") end
# File lib/markdown_ruby_documentation/template_parser.rb, line 510 def ruby_class @ruby_class || self end