Module: MarkdownRubyDocumentation::TemplateParser::Parsing

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

Constant Summary collapse

CLASS_MACROS =
[:attribute]

Instance Method Summary collapse

Instance Method Details

#extract_dsl_comment(comment_string) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 87

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



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

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



66
67
68
69
70
71
72
73
74
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 66

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



76
77
78
79
80
81
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 76

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



83
84
85
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 83

def ruby_class_meth_source_location(method)
  method.context.public_send(method.type, method.name).source_location
end

#source_location(file_path, name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 55

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} #{Regexp.escape(name.inspect)}"))
      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



102
103
104
105
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 102

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



97
98
99
100
# File 'lib/markdown_ruby_documentation/template_parser/parsing.rb', line 97

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