Class: Coradoc::Element::Section

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/element/section.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

children_accessors, #children_accessors, declare_children, #simplify_block_content, visit, #visit

Constructor Details

#initialize(title, options = {}) ⇒ Section

Returns a new instance of Section.



8
9
10
11
12
13
14
15
# File 'lib/coradoc/element/section.rb', line 8

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

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



4
5
6
# File 'lib/coradoc/element/section.rb', line 4

def contents
  @contents
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/coradoc/element/section.rb', line 4

def id
  @id
end

#sectionsObject

Returns the value of attribute sections.



4
5
6
# File 'lib/coradoc/element/section.rb', line 4

def sections
  @sections
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/coradoc/element/section.rb', line 4

def title
  @title
end

Instance Method Details

#contentObject



21
22
23
24
25
# File 'lib/coradoc/element/section.rb', line 21

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

#glossariesObject



17
18
19
# File 'lib/coradoc/element/section.rb', line 17

def glossaries
  @glossaries ||= extract_glossaries
end

#safe_to_collapse?Boolean

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.

Returns:

  • (Boolean)


49
50
51
# File 'lib/coradoc/element/section.rb', line 49

def safe_to_collapse?
  @title.nil? && @id.nil? && @sections.empty?
end

#to_adocObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coradoc/element/section.rb', line 27

def to_adoc
  anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n"
  title = Coradoc::Generator.gen_adoc(@title)
  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.is_a_single?(@contents, Coradoc::Element::TextElement)
    content = Coradoc.strip_unicode(content)
  end

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