Sha256: afbdb144a549b2a1d917e8d7c444c5df90fec411fbe96fcbc551e4a11e910114

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

module Coradoc
  module Element
    class Section < Base
      attr_accessor :id, :title, :attrs, :contents, :sections, :anchor

      declare_children :id, :title, :contents, :sections

      def initialize(title, options = {})
        @title = title
        @id = options.fetch(:id, nil)
        @id = nil if @id == ""
        @anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
        @attrs = options.fetch(:attribute_list, "")
        @contents = options.fetch(:contents, [])
        @sections = options.fetch(:sections, [])
      end

      def glossaries
        @glossaries ||= extract_glossaries
      end

      def content
        if contents.count == 1 && contents.first.is_a?(Coradoc::Element::Paragraph)
          contents.first
        end
      end

      def to_adoc
        anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n"
        title = Coradoc::Generator.gen_adoc(@title)
        attrs = @attrs.to_s.empty? ? "" : "#{@attrs.to_adoc}\n"
        content = Coradoc::Generator.gen_adoc(@contents)
        sections = Coradoc::Generator.gen_adoc(@sections)

        # A block of " +\n"s isn't parsed correctly. It needs to start
        # with something.
        content = "&nbsp;#{content}" if content.start_with?(" +\n")

        # Only try to postprocess elements that are text,
        # otherwise we could strip markup.
        if Coradoc.a_single?(@contents, Coradoc::Element::TextElement)
          content = Coradoc.strip_unicode(content)
        end

        "\n#{anchor}" << attrs << title << content << sections << "\n"
      end

      # Check for cases when Section is simply an equivalent of an empty <DIV>
      # HTML element and if it happens inside some other block element, can be
      # safely collapsed.
      def safe_to_collapse?
        @title.nil? && @sections.empty?
      end

      private

      def extract_glossaries
        contents.grep(Coradoc::Element::Glossaries).first
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
coradoc-1.1.6 lib/coradoc/element/section.rb
coradoc-1.1.5 lib/coradoc/element/section.rb
coradoc-1.1.4 lib/coradoc/element/section.rb
coradoc-1.1.3 lib/coradoc/element/section.rb