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
16
# File 'lib/coradoc/element/section.rb', line 8

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

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



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

def anchor
  @anchor
end

#attrsObject

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

#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



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

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

#glossariesObject



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

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)


51
52
53
# File 'lib/coradoc/element/section.rb', line 51

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

#to_adocObject



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

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