module MarkdownRubyDocumentation::TemplateParser::Parsing

Constants

CLASS_MACROS

Public Instance Methods

extract_dsl_comment(comment_string) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 85
def extract_dsl_comment(comment_string)
  [when_start_and_end(comment_string), when_only_start(comment_string), ""].compact.first
end
extract_dsl_comment_from_method(method) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 99
def extract_dsl_comment_from_method(method)
  extract_dsl_comment strip_comment_hash(ruby_class_meth_comment(method))
end
get_line_number(file, word) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 69
def get_line_number(file, word)
  return unless file && word
  `grep -nrH -m 1 "^[\s]*#{word}" #{file}`.match(/#{file}:(\d*)/).to_a[1].try!(:to_i)
end
insert_method_name(string, method) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 35
def insert_method_name(string, method)
  string.gsub("__method__", "'#{method.to_s}'")
end
look_for_class_macro_comment(method) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 74
def look_for_class_macro_comment(method)
  return "" unless (sl = source_location(method.file_path, method.name))
  MethodSource.comment_helper(sl, method.name).tap do |comment|
    method.line_no = sl[1] unless comment.blank?
  end
end
parse_erb(str, method) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 4
      def parse_erb(str, method)
        filename, lineno = ruby_class_meth_source_location(method)
        adjusted_lineno  = (lineno - ruby_class_meth_comment(method).split("\n").count-1)
        method.context.module_eval(<<-RUBY, __FILE__, __LINE__+1)
        def self.get_binding
          self.send(:binding)
        end
        RUBY
        method.context.send(:define_singleton_method, :current_method) do
          method
        end

        method.context.send(:define_singleton_method, :output_object) do
          output_object
        end

        method.context.send(:define_singleton_method, :load_path) do
          load_path
        end

        method.context.extend(CommentMacros)

        erb          = ERB.new(str, nil, "-")

        erb.lineno   = adjusted_lineno if erb.respond_to?(:lineno)
        erb.filename = filename if erb.respond_to?(:filename)
        erb.result(method.context.get_binding)
      rescue => e
        raise e.class, e.message, ["#{filename}:#{adjusted_lineno}:in `#{method.name}'", *e.backtrace]
      end
ruby_class_meth_comment(method) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 43
def ruby_class_meth_comment(method)
  comment = method.context.public_send(method.type, method.name).comment
  if comment.blank?
    look_for_class_macro_comment(method)
  else
    comment
  end
rescue MethodSource::SourceNotFoundError => e
  raise e.class, "#{ method.context}#{method.type_symbol}#{method.name}, \n#{e.message}"
end
ruby_class_meth_source_location(method) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 81
def ruby_class_meth_source_location(method)
  method.context.public_send(method.type, method.name).source_location
end
source_location(file_path, name) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 58
def source_location(file_path, name)
  return unless file_path && name
  found_match = nil
  CLASS_MACROS.each do |macro|
    if (ln = get_line_number(file_path.split(":").first, macro.call(name)))
      found_match = ln
    end
  end
  [file_path, found_match] if found_match
end
strip_comment_hash(str) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 39
def strip_comment_hash(str)
  str.gsub(/^#[\s]?/, "")
end
when_only_start(comment_string) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 94
def when_only_start(comment_string)
  v = /#{START_TOKEN}\n((.|\n)*)/.match(comment_string)
  v.try!(:captures).try!(:first)
end
when_start_and_end(comment_string) click to toggle source
# File lib/markdown_ruby_documentation/template_parser/parsing.rb, line 89
def when_start_and_end(comment_string)
  v = /#{START_TOKEN}\n((.|\n)*)#{END_TOKEN}/.match(comment_string)
  v.try!(:captures).try!(:first)
end