Class: WikiCloth::WikiLinkHandler

Inherits:
WikiNamespaces show all
Defined in:
lib/wikicloth/wiki_link_handler.rb

Direct Known Subclasses

Parser

Constant Summary collapse

FILE_NAMESPACES =
file_namespace_names
MEDIA_NAMESPACES =
media_namespace_names
CATEGORY_NAMESPACES =
category_namespace_names
LANGUAGE_NAMESPACES =
language_namespace_names

Constants inherited from WikiNamespaces

WikiCloth::WikiNamespaces::NAMESPACE_TYPES

Instance Method Summary collapse

Methods inherited from WikiNamespaces

get_namespace_names_for, language_name, language_namespace_names, localise_ns, method_missing, namespace_type

Instance Method Details

#cache(item) ⇒ Object



27
28
29
# File 'lib/wikicloth/wiki_link_handler.rb', line 27

def cache(item)
  nil
end

#categoriesObject



87
88
89
# File 'lib/wikicloth/wiki_link_handler.rb', line 87

def categories
  @categories ||= []
end

#categories=(val) ⇒ Object



101
102
103
# File 'lib/wikicloth/wiki_link_handler.rb', line 101

def categories=(val)
  @categories = val
end


117
118
119
120
# File 'lib/wikicloth/wiki_link_handler.rb', line 117

def external_link(url,text=nil)
  self.external_links << url
  elem.a({ :href => url, :target => "_blank" }) { |x| x << (text.nil? || text.blank? ? url : text) }
end


75
76
77
# File 'lib/wikicloth/wiki_link_handler.rb', line 75

def external_links
  @external_links ||= []
end

#external_links=(val) ⇒ Object



122
123
124
# File 'lib/wikicloth/wiki_link_handler.rb', line 122

def external_links=(val)
  @external_links = val
end

#find_reference_by_name(n) ⇒ Object



91
92
93
94
# File 'lib/wikicloth/wiki_link_handler.rb', line 91

def find_reference_by_name(n)
  references.each { |r| return r if !r[:name].nil? && r[:name].strip == n }
  return nil
end

#function(name, params) ⇒ Object



23
24
25
# File 'lib/wikicloth/wiki_link_handler.rb', line 23

def function(name, params)
  nil
end

#image_url_for(image) ⇒ Object



134
135
136
# File 'lib/wikicloth/wiki_link_handler.rb', line 134

def image_url_for(image)
  "#{image}"
end

#include_resource(resource, options = []) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/wikicloth/wiki_link_handler.rb', line 149

def include_resource(resource, options=[])
  @template_cache ||= {}
  if @template_cache[resource]
    @included_templates[resource] += 1
    @template_cache[resource]
  else
    ret = template(resource)
    unless ret.nil?
      @included_templates ||= {}
      @included_templates[resource] ||= 0
      @included_templates[resource] += 1
    end
    @template_cache[resource] = ret
    ret
  end
end

#included_templatesObject



166
167
168
# File 'lib/wikicloth/wiki_link_handler.rb', line 166

def included_templates
  @included_templates ||= {}
end


79
80
81
# File 'lib/wikicloth/wiki_link_handler.rb', line 79

def internal_links
  @internal_links ||= []
end

#internal_links=(val) ⇒ Object



126
127
128
# File 'lib/wikicloth/wiki_link_handler.rb', line 126

def internal_links=(val)
  @internal_links = val
end

#languagesObject



83
84
85
# File 'lib/wikicloth/wiki_link_handler.rb', line 83

def languages
  @languages ||= {}
end

#languages=(val) ⇒ Object



105
106
107
# File 'lib/wikicloth/wiki_link_handler.rb', line 105

def languages=(val)
  @languages = val
end


138
139
140
# File 'lib/wikicloth/wiki_link_handler.rb', line 138

def link_attributes_for(page)
   { :href => url_for(page) }
end


142
143
144
145
146
147
# File 'lib/wikicloth/wiki_link_handler.rb', line 142

def link_for(page, text)
  self.internal_links << page
  ltitle = !text.nil? && text.blank? ? self.pipe_trick(page) : text
  ltitle = page if text.nil?
  elem.a(link_attributes_for(page)) { |x| x << ltitle.strip }
end


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/wikicloth/wiki_link_handler.rb', line 174

def link_for_resource(prefix, resource, options=[])
  ret = ""
  prefix.downcase!
  case
  when (MEDIA_NAMESPACES+FILE_NAMESPACES).include?(prefix)
    ret += wiki_image(resource,options,prefix)
  when CATEGORY_NAMESPACES.include?(prefix)
    self.categories << resource
  when LANGUAGE_NAMESPACES.include?(prefix)
    self.languages[prefix] = resource
  else
    title = options[0] ? options[0] : "#{prefix}:#{resource}"
    ret += link_for("#{prefix}:#{resource}",title)
  end
  ret
end

#paramsObject



19
20
21
# File 'lib/wikicloth/wiki_link_handler.rb', line 19

def params
  @params ||= {}
end

#params=(val) ⇒ Object



113
114
115
# File 'lib/wikicloth/wiki_link_handler.rb', line 113

def params=(val)
  @params = val
end

#reference_index(h) ⇒ Object



96
97
98
99
# File 'lib/wikicloth/wiki_link_handler.rb', line 96

def reference_index(h)
  references.each_index { |r| return r+1 if references[r] == h }
  return nil
end

#referencesObject



11
12
13
# File 'lib/wikicloth/wiki_link_handler.rb', line 11

def references
  @references ||= []
end

#references=(val) ⇒ Object



109
110
111
# File 'lib/wikicloth/wiki_link_handler.rb', line 109

def references=(val)
  @references = val
end


15
16
17
# File 'lib/wikicloth/wiki_link_handler.rb', line 15

def section_link(section)
  "?section=#{section}"
end

#section_list(root = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wikicloth/wiki_link_handler.rb', line 31

def section_list(root=nil)
  ret = []
  root = sections[0].children if root.nil?
  root.each do |child|
    ret << child
    unless child.children.empty?
      ret << [section_list(child.children)]
    end
  end
  ret.flatten
end

#template(template) ⇒ Object



170
171
172
# File 'lib/wikicloth/wiki_link_handler.rb', line 170

def template(template)
  nil
end

#toc(sections, toc_numbered = false) ⇒ Object



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/wikicloth/wiki_link_handler.rb', line 43

def toc(sections, toc_numbered=false)
  ret = "<table id=\"toc\" class=\"toc\" summary=\"Contents\"><tr><td><div id=\"toctitle\"><h2>#{I18n.t('table of contents')}</h2></div><ul>"
  previous_depth = 1
  indices = []
  section_list(sections).each do |section|
    next if section.title.nil?
    if section.depth > previous_depth
      indices[section.depth] = 0 if indices[section.depth].nil?
      indices[section.depth] += 1
      c = section.depth - previous_depth
      c.times { ret += "<ul>" }
      ret += "<li><a href=\"##{section.id}\">#{indices[0..section.depth].compact.join('.') + " " if toc_numbered}#{section.title}</a>"
    elsif section.depth == previous_depth
      indices[section.depth] = 0 if indices[section.depth].nil?
      indices[section.depth] += 1
      ret += "</li><li><a href=\"##{section.id}\">#{indices[0..section.depth].compact.join('.') + " " if toc_numbered}#{section.title}</a>"
    else 
      indices[section.depth] = 0 if indices[section.depth].nil?
      indices[section.depth] += 1
      indices = indices[0..section.depth]
      ret += "</li>" unless previous_depth == 1
      c = previous_depth - section.depth
      c.times { ret += "</ul>" }
      ret += "<li><a href=\"##{section.id}\">#{indices[0..section.depth].compact.join('.') + " " if toc_numbered}#{section.title}</a>"
    end
    previous_depth = section.depth
  end
  ret += "</li>"
  (previous_depth-1).times { ret += "</ul>" }
  "#{ret}</ul></td></tr></table>"
end

#url_for(page) ⇒ Object



130
131
132
# File 'lib/wikicloth/wiki_link_handler.rb', line 130

def url_for(page)
  "#{page}"
end