Class: Card::Content::Chunk::Link

Inherits:
Reference show all
Defined in:
card-mod-content/lib/card/content/chunk/link.rb

Overview

extend ActiveSupport::Autoload autoload :Reference , “reference”

Constant Summary collapse

CODE =

L for “Link”

"L".freeze

Instance Attribute Summary collapse

Attributes inherited from Reference

#name, #referee_name

Attributes inherited from Abstract

#text

Instance Method Summary collapse

Methods inherited from Reference

#referee_card, #referee_raw_name

Methods inherited from Abstract

#as_json, #burn_after_reading, #burn_read, #card, context_ok?, #format, full_match, full_re, #initialize, #result, #to_s

Constructor Details

This class inherits a constructor from Card::Content::Chunk::Abstract

Instance Attribute Details

Returns the value of attribute link_text.



14
15
16
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 14

def link_text
  @link_text
end

Instance Method Details

#divider_index(string) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 46

def divider_index string
  # there's probably a better way to do the following.
  # point is to find the first pipe that's not inside an nest
  return unless string.index "|"

  string_copy = string.dup
  string.scan(/\{\{[^}]*\}\}/) do |incl|
    string_copy.gsub! incl, ("x" * incl.length)
  end
  string_copy.index "|"
end

#explicit_link?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 121

def explicit_link?
  @explicit_link
end

#inspectObject



99
100
101
102
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 99

def inspect
  "<##{self.class}:e[#{@explicit_link}]n[#{@name}]l[#{@link_text}]" \
      "p[#{@process_chunk}] txt:#{@text}>"
end

#interpret(match, _content) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 25

def interpret match, _content
  target, @link_text = target_and_link_text match[1]

  @link_text = objectify @link_text
  if target.match? %r{^(/|https?:|mailto:)}
    @explicit_link = objectify target
  else
    @name = target
  end
end


87
88
89
90
91
92
93
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 87

def link_target
  if @explicit_link
    render_obj @explicit_link
  elsif @name
    referee_name
  end
end

#objectify(raw) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 63

def objectify raw
  return unless raw

  raw.strip!
  if raw.match?(/(^|[^\\])\{\{/)
    Card::Content.new raw, format
  else
    raw
  end
end

#optionsObject

view options



59
60
61
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 59

def options
  link_text ? { title: link_text } : {}
end

#process_chunkObject



95
96
97
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 95

def process_chunk
  @process_chunk ||= render_link
end

#reference_codeObject



21
22
23
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 21

def reference_code
  CODE
end


74
75
76
77
78
79
80
81
82
83
84
85
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 74

def render_link view: :link, explicit_link_opts: {}
  @link_text = render_obj @link_text

  if @explicit_link
    @explicit_link = render_obj @explicit_link
    format.link_to_resource @explicit_link, @link_text, explicit_link_opts
  elsif @name
    format.with_nest_mode :normal do
      format.nest referee_name, options.merge(view: view)
    end
  end
end


111
112
113
114
115
116
117
118
119
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 111

def replace_link_text old_name, new_name
  if @link_text.is_a?(Card::Content)
    @link_text.find_chunks(:Reference).each do |chunk|
      chunk.replace_reference old_name, new_name
    end
  elsif @link_text.present?
    @link_text = old_name.to_name.sub_in(@link_text, with: new_name)
  end
end

#replace_reference(old_name, new_name) ⇒ Object



104
105
106
107
108
109
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 104

def replace_reference old_name, new_name
  replace_name_reference old_name, new_name
  replace_link_text old_name, new_name
  @text =
    @link_text.nil? ? "[[#{referee_name}]]" : "[[#{referee_name}|#{@link_text}]]"
end


36
37
38
39
40
41
42
43
44
# File 'card-mod-content/lib/card/content/chunk/link.rb', line 36

def target_and_link_text raw_syntax
  return unless raw_syntax

  if (i = divider_index raw_syntax)                    # [[A | B]]
    [raw_syntax[0..(i - 1)], raw_syntax[(i + 1)..-1]]  # [A, B]
  else                                                 # [[ A ]]
    [raw_syntax, nil]                                  # [A, nil]
  end
end