Class: Coradoc::Element::List::Core

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

Direct Known Subclasses

Ordered, Unordered

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(items, options = {}) ⇒ Core

Returns a new instance of Core.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coradoc/element/list/core.rb', line 12

def initialize(items, options = {})
  @items = items
  @items = [@items] unless @items.is_a?(Array)
  @id = options.fetch(:id, nil)
  @anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
  @ol_count = options.fetch(:ol_count, nil)
  if @ol_count.nil?
    m = @items.find do |i|
      i.is_a?(Coradoc::Element::ListItem) &&
        !i.marker.nil?
    end&.marker.to_s
    @ol_count = m.size
  end
  @ol_count = 1 if @ol_count.nil?
  @attrs = options.fetch(:attrs, AttributeList.new)
end

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



8
9
10
# File 'lib/coradoc/element/list/core.rb', line 8

def anchor
  @anchor
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/coradoc/element/list/core.rb', line 8

def id
  @id
end

#itemsObject

Returns the value of attribute items.



8
9
10
# File 'lib/coradoc/element/list/core.rb', line 8

def items
  @items
end

#ol_countObject

Returns the value of attribute ol_count.



8
9
10
# File 'lib/coradoc/element/list/core.rb', line 8

def ol_count
  @ol_count
end

#prefixObject

Returns the value of attribute prefix.



8
9
10
# File 'lib/coradoc/element/list/core.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#to_adocObject



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

def to_adoc
  anchor = @anchor.nil? ? "" : @anchor.to_adoc.to_s
  attrs = @attrs.to_adoc(false).to_s
  content = "\n"
  @items.each do |item|
    c = Coradoc::Generator.gen_adoc(item)
    if !c.empty?
      # If there's a list inside a list directly, we want to
      # skip adding an empty list item.
      # See: https://github.com/metanorma/coradoc/issues/96
      unless item.is_a? List::Core
        content << prefix.to_s
        content << " " if c[0] != " "
      end
      content << c
    end
  end
  "\n#{anchor}#{attrs}" + content
end