Sha256: 3be0c59c8134f5adfdb0322b80e1e0a9b77656e4289951b69a51210caf5e58a5

Contents?: true

Size: 1.1 KB

Versions: 39

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Tocer
  module Elements
    # Represents a table of contents start and finish comment block.
    class CommentBlock
      def self.index lines, id
        (lines.index { |line| line =~ /<!--.*#{Regexp.escape id}.*-->/ }).to_i
      end

      def initialize start_id: "Tocer[start]",
                     finish_id: "Tocer[finish]",
                     message: "Auto-generated, don't remove."

        @start_id = start_id
        @finish_id = finish_id
        @message = message
      end

      def comments = "#{start_tag}\n#{finish_tag}\n"

      def start_index(lines) = self.class.index(lines, start_id)

      def start_tag = comment(start_id, message)

      def finish_index(lines) = self.class.index(lines, finish_id)

      def finish_tag = comment(finish_id, message)

      def empty?(lines) = (finish_index(lines) - start_index(lines)) == 1

      def prependable?(lines) = start_index(lines).zero? && finish_index(lines).zero?

      private

      attr_reader :start_id, :finish_id, :message

      def comment(id, message) = "<!-- #{id}: #{message} -->"
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
tocer-18.7.0 lib/tocer/elements/comment_block.rb
tocer-18.6.0 lib/tocer/elements/comment_block.rb
tocer-18.5.0 lib/tocer/elements/comment_block.rb
tocer-18.4.0 lib/tocer/elements/comment_block.rb
tocer-18.3.0 lib/tocer/elements/comment_block.rb
tocer-18.2.0 lib/tocer/elements/comment_block.rb
tocer-18.1.0 lib/tocer/elements/comment_block.rb
tocer-18.0.0 lib/tocer/elements/comment_block.rb
tocer-17.5.0 lib/tocer/elements/comment_block.rb
tocer-17.3.0 lib/tocer/elements/comment_block.rb
tocer-17.2.0 lib/tocer/elements/comment_block.rb
tocer-17.1.0 lib/tocer/elements/comment_block.rb
tocer-17.0.0 lib/tocer/elements/comment_block.rb
tocer-16.2.1 lib/tocer/elements/comment_block.rb
tocer-16.2.0 lib/tocer/elements/comment_block.rb
tocer-16.1.0 lib/tocer/elements/comment_block.rb
tocer-16.0.3 lib/tocer/elements/comment_block.rb
tocer-16.0.2 lib/tocer/elements/comment_block.rb
tocer-16.0.1 lib/tocer/elements/comment_block.rb
tocer-16.0.0 lib/tocer/elements/comment_block.rb