Sha256: fcbe1601fb00bb68fd70b269521d57d75f592356a0931457992238827a37065b

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# coding: utf-8
# frozen_string_literal: true

############################################
# © Alex Semyonov, 2013—2017, MIT License  #
# Author: Alex Semyonov <alex@semyonov.us> #
############################################

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 = /(?<ruler>##+#\n)(?<copyright>(#\s*[^\s#]+.*\s#\n)+)(#\s+#?\n(?<comment>(#\s*.*#?\n)+))?\k<ruler>\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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
giteaucrat-0.1.0 lib/giteaucrat/formatters/ruby_formatter.rb
giteaucrat-0.0.8 lib/giteaucrat/formatters/ruby_formatter.rb