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

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
71
72
73
# 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.display_name
    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
    unless obj.nil?
      to_relative path_to obj
    else
      nil
    end  
  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

Note:

this method can be overwritten in every included Helper to fit your custom API-Layout

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.html
Dom['Foo.bar'].file_path :format => :json #=> Foo/bar.json

Parameters:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/helper/linker.rb', line 90

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


75
76
77
# File 'lib/helper/linker.rb', line 75

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


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/helper/linker.rb', line 109

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