Module: Helper::Linker

Included in:
Helper
Defined in:
lib/helper/linker.rb

Overview

This Helper contains all needed functionality to link to an object, on-page-element or some other urls

Constant Summary collapse

FILE =
/^file\:(\S+)/
EXTERNAL =
/^((?:http|ftp|https|ssh):\/\/\S+)/
MAIL =
/^(mailto\:\S+)/
HASH =
/^#\S*/
DOCUMENTATION =
/^doc\:([^\s#]+)(#\S+)?/

Instance Method Summary collapse

Instance Method Details



121
122
123
124
125
126
127
# File 'lib/helper/linker.rb', line 121

def link_on_page(object)
  if object.is_a? CodeObject::Function
     link_to "#method-#{object.name}", ".#{object.name}()"
  else
    link_to "#object-#{object.name}", ".#{object.name}"
  end
end
Note:

link_to - first argument can be “file:some/path/to_a.file” “Code.object.path” “.relative.code_object.path” “external.address.com” instance_of_code_object



20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/helper/linker.rb', line 20

def link_to(target, text = nil, args = {})
       
  Logger.debug "Trying to link #{target}"
  
  link = if target.is_a? Document::Document
    
    text = target.name if text.nil?     
    to_relative path_to target        
  
  elsif target.is_a? CodeObject::Base
  
    if text.nil? and target.parent == context and context != Dom.root
      text = ".#{target.name}"
      text += "()" if target.is_a? CodeObject::Function
    elsif text.nil?
      text = target.qualified_name
    end
  
    to_relative path_to target
    
  elsif target.match EXTERNAL or target.match MAIL or target.match HASH
    target
    
  elsif target.match FILE
    to_relative target.match(FILE).captures.first
    
  elsif target.match DOCUMENTATION
    Logger.debug target + " matched DOCUMENTATION"
  
    doc_name, hash = target.match(DOCUMENTATION).captures
    obj = Dom.docs.find doc_name        
    text ||= obj.name

    # find relative path to our object and reattach hash to path
    to_relative(path_to obj) + (hash || "") unless obj.nil?        
    
  else        
    # use context dependent resolving functionality as specified in {Tasks::RenderTask}
    obj = resolve target
    to_relative path_to obj unless obj.nil?  
  end
    
  text ||= target 
  
  if link.nil?
    Logger.warn "Could not resolve link to '#{target}'"
    return text 
  end
 
  tag :a, text, :href => link      
end

#path_to(object, args = {}) ⇒ Object

Returns the relative path (from dom) to this node The Node can be either a CodeObject or a Document.

Examples:

Dom[:Foo][:bar].file_path #=> Foo/bar

Parameters:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/helper/linker.rb', line 84

def path_to(object, args = {})

  return "" if object.nil? 
  format = args[:format] || :html      
  path = object.parents.push(object).map{|p| p.name}.join('/') + ".#{format.to_s}"

  # object can be either a CodeObject or a Document
  # maybe source this one out later on in Configs.some_path
  if object.is_a? CodeObject::Base
    "api/" + path
  elsif object.is_a? Document::Document
    "docs/" + path
  else
    Logger.warn "Could not resolve link to '#{object}'"
    object.to_s
  end   
end


72
73
74
# File 'lib/helper/linker.rb', line 72

def relative_link(path, text)
  tag :a, text, :href => to_relative(path)
end


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/helper/linker.rb', line 103

def replace_links(text)
  code_tags = 0
  text.gsub(/<(\/)?(pre|code|tt)|(\\)?\{(?!\})(\S+?)(?:\s([^\}]*?\S))?\}(?=[\W<]|.+<\/|$)/m) do |str|
    closed, tag, escape, name, title, match = $1, $2, $3, $4, $5, $&
    if tag
      code_tags += (closed ? -1 : 1)
      next str
    end
    next str unless code_tags == 0

    next(match[1..-1]) if escape

    next(match) if name[0,1] == '|'
    
    link_to(name, title)
  end
end