Sha256: d3b4e6f45a589a12a60920f653c5a88f9c0b027a7c56abc440c53e365989ac11

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

module Coradoc
  module Element
    class Title < Base
      attr_accessor :id, :content, :line_break, :style, :level_int

      declare_children :id, :content

      def initialize(content, level, options = {})
        @level_int = level
        # @level_int = level.length - 1 if level.is_a?(String)
        @content = content
        @id = options.fetch(:id, nil)
        @anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
        @line_break = options.fetch(:line_break, "")
        @style = options.fetch(:style, nil)
      end

      def level
        @level ||= level_from_string
      end

      def to_adoc
        anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n"
        content = Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content))
        <<~HERE

        #{anchor}#{style_str}#{level_str} #{content}
        HERE
      end

      def level_str
        if @level_int <= 5
          "=" * (@level_int + 1)
        else
          "======"
        end
      end

      def style_str
        style = [@style]
        style << "level=#{@level_int}" if @level_int > 5
        style = style.compact.join(",")

        "[#{style}]\n" unless style.empty?
      end

      alias :text :content

      private

      def level_from_string
        case @level_int
        when 2 then :heading_two
        when 3 then :heading_three
        when 4 then :heading_four
        when 5 then :heading_five
        else :unknown
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
coradoc-1.1.6 lib/coradoc/element/title.rb
coradoc-1.1.5 lib/coradoc/element/title.rb
coradoc-1.1.4 lib/coradoc/element/title.rb
coradoc-1.1.3 lib/coradoc/element/title.rb
coradoc-1.1.2 lib/coradoc/element/title.rb
coradoc-1.1.1 lib/coradoc/element/title.rb