# coding: utf-8 # frozen_string_literal: true ############################################ # © Alex Semyonov, 2013—2017, MIT License # # Author: Alex Semyonov # ############################################ require 'giteaucrat/formatters/formatter' module Giteaucrat module Formatters class RubyFormatter < Formatter COMMENT_PARTS = %w[# # #].freeze # @return [String] CODING_REGEXP = /\A((#\s*.*coding:\s*[^\s]+)\s*\n+)?/ COPYRIGHT_REGEXP = /(?##+#\n)(?(#\s*[^\s#]+.*\s#\n)+)(#\s+#?\n(?(#\s*.*#?\n)+))?\k\n+/ def format_copyright copyright = super 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, '') @encoding = Regexp.last_match(2) if Regexp.last_match(2) 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