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



90
91
92
93
94
95
96
97
98
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 90

def extract_dsl_comment(comment_string)
  if (v = when_start_and_end(comment_string))
    v
  elsif (x = when_only_start(comment_string))
    x
  else
    ""
  end
end

#extract_dsl_comment_from_method(method) ⇒ Object



110
111
112
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 110

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
73
74
75
76
77
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 69

def get_line_number(file, word)
  return unless file && word
  count = 0
  file_or_result = File.open(file, "r") { |file| file.each_line { |line|
    count += 1
    return count if line =~ /^[\s]*#{word}/
  }}
  file_or_result.is_a?(File) ? nil : file_or_result
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



79
80
81
82
83
84
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 79

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(<<-RUBY, __FILE__, __LINE__+1)
  def self.get_binding
    self.send(:binding)
  end
  RUBY
  ruby_class.send(:define_singleton_method, :current_method) do
    method
  end

  ruby_class.send(:define_singleton_method, :output_object) do
    output_object
  end

  ruby_class.send(:define_singleton_method, :load_path) do
    load_path
  end

  ruby_class.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(ruby_class.get_binding)
rescue => e
  raise e.class, e.message, ["#{filename}:#{adjusted_lineno}:in `#{method.name}'", *e.backtrace]
end

#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



86
87
88
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 86

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



105
106
107
108
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 105

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



100
101
102
103
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 100

def when_start_and_end(comment_string)
  v = /#{START_TOKEN}\n((.|\n)*)#{END_TOKEN}/.match(comment_string)
  v.try!(:captures).try!(:first)
end