Class: Repubmark::Elems::ListItem

Inherits:
Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/repubmark/elems/list_item.rb

Instance Attribute Summary collapse

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, index, titled_ref = nil) ⇒ ListItem

Returns a new instance of ListItem.



12
13
14
15
16
17
18
19
20
# File 'lib/repubmark/elems/list_item.rb', line 12

def initialize(parent, index, titled_ref = nil)
  super parent

  self.index = index
  self.titled_ref = titled_ref

  @caption = Caption.new self
  @sublist = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/repubmark/elems/list_item.rb', line 96

def method_missing(method_name, ...)
  if @caption.respond_to? method_name
    raise 'Caption after sublist' if @sublist

    @caption.public_send(method_name, ...)
  else
    super
  end
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



10
11
12
# File 'lib/repubmark/elems/list_item.rb', line 10

def index
  @index
end

#titled_refObject

Returns the value of attribute titled_ref.



10
11
12
# File 'lib/repubmark/elems/list_item.rb', line 10

def titled_ref
  @titled_ref
end

Instance Method Details

#build_ref(format) ⇒ Object (private)



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/repubmark/elems/list_item.rb', line 133

def build_ref(format)
  return if titled_ref.nil?

  case format
  when :html
    "<a href=\"#{titled_ref.url}\">#{titled_ref.title}</a>\n".freeze
  when :gemtext
    "=> #{titled_ref.url} #{unicode_decor_own}#{titled_ref.title} ".freeze
  else
    raise 'Invalid format'
  end
end

#last?Boolean

Returns:

  • (Boolean)


62
# File 'lib/repubmark/elems/list_item.rb', line 62

def last? = index >= parent.items_count - 1

#levelObject

Helper methods #



60
# File 'lib/repubmark/elems/list_item.rb', line 60

def level = parent.level

#parent=(parent) ⇒ Object (private)



108
109
110
111
112
113
114
# File 'lib/repubmark/elems/list_item.rb', line 108

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

  @parent = parent
end

#respond_to_missing?(method_name, _include_private) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/repubmark/elems/list_item.rb', line 92

def respond_to_missing?(method_name, _include_private)
  @caption.respond_to?(method_name) || super
end

#subolist {|@sublist| ... } ⇒ Object

Builder methods #

Yields:

  • (@sublist)


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

def subolist
  raise 'No nested link lists' if titled_ref
  raise 'Sublist already exists' if @sublist

  @sublist = List.new self, links: false, ordered: true
  yield @sublist
  nil
end

#subulist {|@sublist| ... } ⇒ Object

Yields:

  • (@sublist)


83
84
85
86
87
88
89
90
# File 'lib/repubmark/elems/list_item.rb', line 83

def subulist
  raise 'No nested link lists' if titled_ref
  raise 'Sublist already exists' if @sublist

  @sublist = List.new self, links: false, ordered: false
  yield @sublist
  nil
end

#to_gemtextObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/repubmark/elems/list_item.rb', line 45

def to_gemtext
  [
    if titled_ref
      "#{build_ref(:gemtext)}#{@caption.to_gemtext}".strip
    else
      "* #{unicode_decor_own}#{@caption.to_gemtext}".strip
    end,
    @sublist&.to_gemtext,
  ].join("\n").freeze
end

#to_htmlObject



35
36
37
38
39
40
41
42
43
# File 'lib/repubmark/elems/list_item.rb', line 35

def to_html
  [
    "<li>\n",
    build_ref(:html),
    @caption.to_html,
    @sublist&.to_html,
    "</li>\n",
  ].join.freeze
end

#to_summary_plainObject



28
29
30
31
32
33
# File 'lib/repubmark/elems/list_item.rb', line 28

def to_summary_plain
  [
    @caption.to_summary_plain,
    @sublist&.to_summary_plain,
  ].compact.join("\n").freeze
end

#unicode_decor_ownObject



64
65
66
# File 'lib/repubmark/elems/list_item.rb', line 64

def unicode_decor_own
  "#{parent.unicode_decor}#{last? ? '' : ''} " unless level.zero?
end

#unicode_decor_parentObject



68
# File 'lib/repubmark/elems/list_item.rb', line 68

def unicode_decor_parent = parent.unicode_decor

#word_countObject

Basic methods #



26
# File 'lib/repubmark/elems/list_item.rb', line 26

def word_count = @caption.word_count + (@sublist&.word_count || 0)