Class: Repubmark::Elems::List

Inherits:
Base
  • Object
show all
Defined in:
lib/repubmark/elems/list.rb

Instance Attribute Summary

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#absolute_url, #config, #count_words, #html_class, #own_url, parent?, parents, #relative_url

Constructor Details

#initialize(parent, links:, ordered:) ⇒ List

Returns a new instance of List.



8
9
10
11
12
13
14
15
# File 'lib/repubmark/elems/list.rb', line 8

def initialize(parent, links:, ordered:)
  super parent

  @links = !!links
  @ordered = !!ordered

  @items = []
end

Instance Method Details

#itemObject

Builder methods #



53
# File 'lib/repubmark/elems/list.rb', line 53

def item(...) = @links ? item_links(...) : item_nolinks(...)

Yields:

  • (list_item)


75
76
77
78
79
80
81
# File 'lib/repubmark/elems/list.rb', line 75

def item_links(ref_url, ref_title = nil)
  titled_ref = TitledRef.new ref_url, ref_title
  list_item = ListItem.new self, @items.count, titled_ref
  @items << list_item
  yield list_item if block_given?
  nil
end

Yields:

  • (list_item)


68
69
70
71
72
73
# File 'lib/repubmark/elems/list.rb', line 68

def item_nolinks
  list_item = ListItem.new self, @items.count
  @items << list_item
  yield list_item
  nil
end

#items_countObject



41
# File 'lib/repubmark/elems/list.rb', line 41

def items_count = @items.count

#levelObject

Helper methods #



39
# File 'lib/repubmark/elems/list.rb', line 39

def level = parent.instance_of?(ListItem) ? parent.level + 1 : 0

#parent=(parent) ⇒ Object (private)



57
58
59
60
61
62
63
64
# File 'lib/repubmark/elems/list.rb', line 57

def parent=(parent)
  unless parent.instance_of?(Canvas) || parent.instance_of?(ListItem)
    raise TypeError,
          "Expected #{Canvas} or #{ListItem}, got #{parent.class}"
  end

  @parent = parent
end

#tag_nameObject (private)



66
# File 'lib/repubmark/elems/list.rb', line 66

def tag_name = @ordered ? 'ol' : 'ul'

#to_gemtextObject



33
# File 'lib/repubmark/elems/list.rb', line 33

def to_gemtext = @items.map(&:to_gemtext).join.freeze

#to_htmlObject



25
26
27
28
29
30
31
# File 'lib/repubmark/elems/list.rb', line 25

def to_html
  [
    "<#{tag_name}>\n",
    *@items.map(&:to_html),
    "</#{tag_name}>\n",
  ].join.freeze
end

#to_summary_plainObject



23
# File 'lib/repubmark/elems/list.rb', line 23

def to_summary_plain = @items.map(&:to_summary_plain).join(' ').freeze

#unicode_decorObject



43
44
45
46
47
# File 'lib/repubmark/elems/list.rb', line 43

def unicode_decor
  return if level <= 1

  "#{parent.unicode_decor_parent}#{parent.last? ? '' : ''}"
end

#word_countObject

Basic methods #



21
# File 'lib/repubmark/elems/list.rb', line 21

def word_count = @items.sum(&:word_count)