module Metado::Parser::File

Public Class Methods

comments(file) click to toggle source
# File lib/metado/parser/file.rb, line 7
def comments file
  regex = single_line_comment_regex(file)

  ::File.readlines(file).each_with_index.reduce([[], nil]) { |(comments, current_comment), (line, index)|
    if (line = line[regex, 1])
      (current_comment ||= Type::Comment.new(file, index + 1, []).tap(&comments.method(:push))).body << line
    else
      current_comment = nil
    end

    [comments, current_comment]
  }.first
end

Private Class Methods

single_line_comment_regex(file) click to toggle source
# File lib/metado/parser/file.rb, line 23
def single_line_comment_regex file
  sentinel = case ::File.extname file
  when ".py", ".rb", ".sh"
    "#"
  when ".hs"
    "--"
  else
    "//"
  end

  %r{^\s*#{sentinel}(.*)}
end