Class: Coradoc::Element::ListItem

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

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(content, options = {}) ⇒ ListItem



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

def initialize(content, options = {})
  @marker = options.fetch(:marker, nil)
  @id = options.fetch(:id, nil)
  @anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
  @content = content
  # @content = [@content] unless @content.class == Array
  @attached = options.fetch(:attached, [])
  @nested = options.fetch(:nested, nil)
  @line_break = options.fetch(:line_break, "\n")
end

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



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

def anchor
  @anchor
end

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#line_breakObject

Returns the value of attribute line_break.



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

def line_break
  @line_break
end

#markerObject

Returns the value of attribute marker.



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

def marker
  @marker
end

#subitemObject

Returns the value of attribute subitem.



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

def subitem
  @subitem
end

Instance Method Details

#inline?(elem) ⇒ Boolean



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/coradoc/element/list_item.rb', line 19

def inline?(elem)
  case elem
  when Inline::HardLineBreak
    :hardbreak
  when ->(i) { i.class.name.to_s.include? "::Inline::" }
    true
  when String, TextElement, Image::InlineImage
    true
  else
    false
  end
end

#to_adocObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/coradoc/element/list_item.rb', line 32

def to_adoc
  anchor = @anchor.nil? ? "" : " #{@anchor.to_adoc} "

  content = Array(@content).flatten.compact
  out = ""
  prev_inline = :init

  # Collapse meaningless <DIV>s
  while content.map(&:class) == [Section] && content.first.safe_to_collapse?
    content = Array(content.first.contents)
  end

  content.each_with_index do |subitem, idx|
    subcontent = Coradoc::Generator.gen_adoc(subitem)
    inline = inline?(subitem)
    next_inline = idx + 1 == content.length ? :end : inline?(content[idx + 1])

    # Only try to postprocess elements that are text,
    # otherwise we could strip markup.
    if subitem.is_a? Coradoc::Element::TextElement
      if [:hardbreak, :init, false].include?(prev_inline)
        subcontent = Coradoc.strip_unicode(subcontent, only: :begin)
      end
      if [:hardbreak, :end, false].include?(next_inline)
        subcontent = Coradoc.strip_unicode(subcontent, only: :end)
      end
    end

    case inline
    when true
      out += if prev_inline == false
               "\n+\n#{subcontent}"
             else
               subcontent
             end
    when false
      out += case prev_inline
             when :hardbreak
               subcontent.strip
             when :init
               "{empty}\n+\n#{subcontent.to_s.strip}"
             else
               "\n+\n#{subcontent.to_s.strip}"
             end
    when :hardbreak
      if i[hardbreak init].include? prev_inline
        # can't have two hard breaks in a row; can't start with a hard break
      else
        out += "\n+\n"
      end
    end

    prev_inline = inline
  end
  out += "{empty}" if prev_inline == :hardbreak
  out = "{empty}" if out.empty?

  attach = @attached.map do |elem|
    "+\n#{Coradoc::Generator.gen_adoc(elem)}"
  end.join
  nest = Coradoc::Generator.gen_adoc(@nested)
  out = " #{anchor}#{out}#{@line_break}"
  out + attach + nest
end