lib/giteaucrat/formatters/ruby_formatter.rb in giteaucrat-0.0.4 vs lib/giteaucrat/formatters/ruby_formatter.rb in giteaucrat-0.0.5
- old
+ new
@@ -11,48 +11,43 @@
module Formatters
class RubyFormatter < Formatter
COMMENT_PARTS = %w(# # #)
# @return [String]
- CODING_REGEXP = /\A(#\s*.*coding:\s*utf-8\s*\n+)?/
+ CODING_REGEXP = /\A((#\s*.*coding:\s*[^\s]+)\s*\n+)?/
COPYRIGHT_REGEXP = /(?<ruler>##+#\n)(?<copyright>(#\s*[^\s#]+.*\s#\n)+)(#\s+#?\n(?<comment>(#\s*.*#?\n)+))?\k<ruler>\n+/
def format_copyright
copyright = super
- copyright = "# coding: utf-8\n\n#{copyright}" if include_encoding?
+ copyright = [encoding, copyright].compact.join("\n\n")
copyright
end
def format_line(line)
first, _, last = comment_parts
"#{first} #{line} #{last}"
end
def remove_copyright!
super
- contents.sub!(CODING_REGEXP, '') if include_encoding?
+ contents.sub!(CODING_REGEXP, '')
+ @encoding = $2 if $2
end
- def add_copyright!
- if !include_encoding? && !!(contents =~ CODING_REGEXP)
- lines = contents.split(/\n/).to_a
- lines.insert(1, format_copyright)
- @contents = lines.join("\n")
- else
- super
- end
- end
-
def parse_comment(comment)
middle = Regexp.escape(comment_parts[1])
comment_lines = comment.split("\n").map do |line|
line.sub(/\A#{middle}\s?/, '').sub(/\s*#{middle}\s*\Z/, '')
end
@comment_lines = comment_lines
end
def include_encoding?
repo.include_encoding?
+ end
+
+ def encoding
+ @encoding || (include_encoding? && '# coding: utf-8' || nil)
end
end
end
end