Module: MarkdownRubyDocumentation::TemplateParser::Parsing

Included in:
ClassLevelComment, CommentMacros
Defined in:
lib/markdown_ruby_documentation/template_parser/parsing.rb

Constant Summary collapse

CLASS_MACROS =
[
  ->(name) {"attribute #{Regexp.escape(name.inspect)}"},
  ->(name) {"def_delegator :.*, #{Regexp.escape(name.inspect)}"},
]

Instance Method Summary collapse

Instance Method Details

#extract_dsl_comment(comment_string) ⇒ Object



85
86
87
# 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) ⇒ Object



99
100
101
# 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) ⇒ Object



69
70
71
72
# 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) ⇒ Object



35
36
37
# 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) ⇒ Object



74
75
76
77
78
79
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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)
  ruby_class.module_eval("  def self.get_binding\n    self.send(:binding)\n  end\n  RUBY\n  ruby_class.send(:define_singleton_method, :current_method) do\n    method\n  end\n\n  ruby_class.send(:define_singleton_method, :output_object) do\n    output_object\n  end\n\n  ruby_class.send(:define_singleton_method, :load_path) do\n    load_path\n  end\n\n  ruby_class.extend(CommentMacros)\n\n  erb          = ERB.new(str, nil, \"-\")\n\n  erb.lineno   = adjusted_lineno if erb.respond_to?(:lineno)\n  erb.filename = filename if erb.respond_to?(:filename)\n  erb.result(ruby_class.get_binding)\nrescue => e\n  raise e.class, e.message, [\"\#{filename}:\#{adjusted_lineno}:in `\#{method.name}'\", *e.backtrace]\nend\n", __FILE__, __LINE__+1)

#ruby_class_meth_comment(method) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



81
82
83
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



39
40
41
# 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) ⇒ Object



94
95
96
97
# 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) ⇒ Object



89
90
91
92
# 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