Class: Wiki2Go::LineFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/Wiki2Go/LineFormatter.rb

Direct Known Subclasses

Formatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web, storage, config, generate_html, editable) ⇒ LineFormatter

Returns a new instance of LineFormatter.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/Wiki2Go/LineFormatter.rb', line 14

def initialize(web,storage,config,generate_html,editable)
  @web = web
  @storage = storage
  @config = config
  @in_table = false
  @bullets = Array.new
  @pre_line = ""
  @post_line = ""
  @generate_html = generate_html
  @editable      = editable
  @absolute_urls = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/Wiki2Go/LineFormatter.rb', line 12

def config
  @config
end

Instance Method Details

#absolute_urlObject

Return absolute URL to current page



94
95
96
# File 'lib/Wiki2Go/LineFormatter.rb', line 94

def absolute_url
  absolute_url_of_topic(@web.current_page)
end

#absolute_url_of_topic(topic) ⇒ Object

Return absolute URL of given page



99
100
101
# File 'lib/Wiki2Go/LineFormatter.rb', line 99

def absolute_url_of_topic(topic)
  return @web.base_url.chop + view_page_url(@web.name,topic)
end

Link to admin page



333
334
335
# File 'lib/Wiki2Go/LineFormatter.rb', line 333

def admin_link(page,name)
  return link_to(make_verb_url('admin',page),name)
end

Link to ‘recent changes’ page



230
231
232
# File 'lib/Wiki2Go/LineFormatter.rb', line 230

def changes_link(label)
  return link_to(changes_url,label,false)
end

#changes_urlObject

URL of ‘recent changes’ page



221
222
223
224
225
226
227
# File 'lib/Wiki2Go/LineFormatter.rb', line 221

def changes_url
  if @generate_html then 
    return make_url(@web.name,'recent_changes.html') 
  else
    return make_verb_url('changes',@web.name)
  end
end

Link to page version



307
308
309
310
311
312
313
# File 'lib/Wiki2Go/LineFormatter.rb', line 307

def diff_link(subwiki,page,name,from,to)
  return link_to(diff_url(subwiki,page,from,to),name,false) if from == -1 && to == -1
  return '' if from < 0
  return '' if from >= to
  
  return link_to(diff_url(subwiki,page,from,to),name,false)
end

#diff_url(subwiki, page, from, to) ⇒ Object

URL to diff page versions



299
300
301
302
303
304
# File 'lib/Wiki2Go/LineFormatter.rb', line 299

def diff_url(subwiki,page,from,to)
  params = Hash.new
  params['from'] = from if from >= 0
  params['to'] = to if to >= 0
  return make_verb_url('diff',subwiki,page)+query_string( params )
end

Return a link suitable for creating a new page with the rich editor, if editing is allowed Return the name of the page, otherwise



105
106
107
108
109
110
111
# File 'lib/Wiki2Go/LineFormatter.rb', line 105

def edit_link(subwiki,page,name)
  if @editable then
    return name + edit_this_link(subwiki,page,"?")
  else
    return name 
  end
end

Return a link suitable for editing a page with the rich editor, if editing is allowed Return empty string, otherwise



115
116
117
118
119
120
121
# File 'lib/Wiki2Go/LineFormatter.rb', line 115

def edit_this_link(subwiki,page,name)
  if @editable then
    return link_to(make_verb_url('edit',subwiki,page),name,false)
  else
    return "" 
  end
end

Return a link suitable for creating a new page with the confgured editor, if editing is allowed Return the name of the page, otherwise



145
146
147
148
149
150
151
# File 'lib/Wiki2Go/LineFormatter.rb', line 145

def editor_link(subwiki,page,name)
  if @editable then
    return editor_this_link(subwiki,page,name)
  else
    return name 
  end
end

#editor_link2(subwiki, page, name) ⇒ Object

Return a link suitable for creating a new page with the confgured editor, if editing is allowed Return the name of the page, otherwise



155
156
157
158
159
160
161
# File 'lib/Wiki2Go/LineFormatter.rb', line 155

def editor_link2(subwiki,page,name)
  if @editable then
    return name + editor_this_link(subwiki,page,"?")
  else
    return name 
  end
end

Return a link suitable for editing a page with the configured editor, if editing is allowed Return empty string, otherwise



165
166
167
168
169
170
171
# File 'lib/Wiki2Go/LineFormatter.rb', line 165

def editor_this_link(subwiki,page,name)
  if @editable then
    return link_to(make_verb_url(@config.editor,subwiki,page),name,false)
  else
    return "" 
  end
end

#encode_mail_to(addr) ⇒ Object Also known as: encodeMailTo

Encode a mail address with javascript, so that it isn’t harvestable



365
366
367
368
369
370
# File 'lib/Wiki2Go/LineFormatter.rb', line 365

def encode_mail_to(addr) 
  addr =~ /(.*)@(.*)/ ;
  name = $1 ;
  site = $2 ;
  return  "<a href=\"#{view_url(@web.name,"MailFormattingRules")}\" rel=\"nofollow\" onmouseover=\"this.href='mai' + 'lto:' + '#{name}' + '&#64;' + '#{site}'\">#{name}</a>" 
end

#format_line(line) ⇒ Object

Format one line with wiki formatting



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/Wiki2Go/LineFormatter.rb', line 30

def format_line(line)
  @added_bullets = false
  line = escape_wiki_markers(line)
  line = format_tags(line)
  line = format_bullets(line)
  line = format_markup(line)
  line = format_table(line)
  line = remove_markers(line)
  if !@added_bullets then
    emit_bullets
  end
  
  return formatted_output(line)
end

#formatting_doneObject

Call when the last line has been formatted. We may need to close lists.



46
47
48
49
50
51
# File 'lib/Wiki2Go/LineFormatter.rb', line 46

def formatting_done
  emit_bullets
  result =@pre_line 
  @pre_line = ''
  result
end

Return an <a> tag filled in.



83
84
85
86
# File 'lib/Wiki2Go/LineFormatter.rb', line 83

def link_to(url,label,follow=true)
  nofollow = (follow ? '' : " rel=\"nofollow\"")
  return "<a href=\"#{url}\"#{nofollow}>#{label}</a>"
end

Return an <a target=“_blank”> tag filled in.



89
90
91
# File 'lib/Wiki2Go/LineFormatter.rb', line 89

def link_to_new_page(url,label)
  return "<a href=\"#{url}\" target=\"_blank\">#{label}</a>"
end

#make_url(*parameters) ⇒ Object

Return the path to the given resource. Nil and empty parameters are ignored The generated url is relative to the root of the site



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/Wiki2Go/LineFormatter.rb', line 69

def make_url(*parameters)
  result = @web.subsite.empty? ? '' : ('/' + @web.subsite)
  parameters.each do |name|
    if !name.nil? and !name.empty? then
      name.split('/').each do |subname|
        result += '/' 
        result += ERB::Util::url_encode(subname)
      end
    end
  end
  result.squeeze('/')
end

#make_verb_url(verb, *parameters) ⇒ Object

Return the path to the script verb, with the right extension and parameters

  • verb = name of the verb (view, edit,…)

  • parameters = further parameters: web.name, web.current_page, …



56
57
58
# File 'lib/Wiki2Go/LineFormatter.rb', line 56

def make_verb_url(verb,*parameters)
  make_url(@web.script_prefix,verb + @web.script_extension,*parameters)
end


346
347
348
349
350
351
352
353
# File 'lib/Wiki2Go/LineFormatter.rb', line 346

def redirect_link(label,url)
  if @config.redirect_url?(@web,url) then
    return "<a href=\"#{redirect_url(url)}\" rel=\"nofollow\" target=\"_blank\">#{label}</a>"
  else
    return link_to_new_page(url,label)
  end
  
end

#redirect_url(url) ⇒ Object



338
339
340
341
342
343
344
# File 'lib/Wiki2Go/LineFormatter.rb', line 338

def redirect_url(url)
  if @config.redirect_url?(@web,url) then
    return make_verb_url('redirect',@web.name) + "?url=" + CGI::escape(url) 
  else
    return url
  end
end

#removespam_url(user) ⇒ Object

URL to remove spam by user



189
190
191
# File 'lib/Wiki2Go/LineFormatter.rb', line 189

def removespam_url(user)
  return make_verb_url('removespam',@web.name,'') 
end

Link to a non-wikipages Images are rendered inline .rbl pages are performed HTML files are linked to Other files are opened in a new browser window



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/Wiki2Go/LineFormatter.rb', line 262

def resource_link(subwiki,page,name)
  if page =~ /^(.+)\.(gif|jpg|jpeg|png)$/i then
    filename = $1
    alt_label = name
    if page == name || url?(name) then
      alt_label = filename
    end
    image_link = "<img src=\"#{resource_url(page)}\" border=0 alt=\"#{alt_label}\" >"
    if url?(name) then
      return redirect_link(image_link,name)
    else
      return image_link
    end
  elsif page =~ /^(.*)\.rbl$/i then
    page_name = $1
    if page == name then
      name = page_name
    end
    return perform_link(subwiki,page,name)
  elsif page =~ /^(.+)\.html?$/i then
    return link_to(resource_url(page),name)
  else
    return link_to_new_page(resource_url(page),name)
  end
end

#resource_url(name) ⇒ Object

URL to a static resource (under the html subdirectory of the site)



356
357
358
359
360
361
362
# File 'lib/Wiki2Go/LineFormatter.rb', line 356

def resource_url(name)
  if @absolute_urls then
    @web.base_url.chop + make_url('html',@web.name,name)
  else
    make_url('html',@web.name,name)
  end
end

#save_urlObject

URL to save current page in current subwiki



174
175
176
# File 'lib/Wiki2Go/LineFormatter.rb', line 174

def save_url
  make_verb_url('save',@web.name,@web.current_page)
end

Link to search for given text, with (optional) label to display within link



213
214
215
216
# File 'lib/Wiki2Go/LineFormatter.rb', line 213

def search_link(for_text,label=nil)
  label ||= for_text
  return link_to(search_url(for_text),label,false)
end

#search_url(for_text = nil) ⇒ Object

URL to search for (optional) text



207
208
209
210
# File 'lib/Wiki2Go/LineFormatter.rb', line 207

def search_url(for_text=nil)
  parameter = for_text.nil? ? '' :  '?text=' + CGI::escape(for_text)
  return make_verb_url('search',@web.name) + parameter
end

Link to side by side view of two page versions



324
325
326
327
328
329
330
# File 'lib/Wiki2Go/LineFormatter.rb', line 324

def sidebyside_link(subwiki,page,name,from,to)
  return link_to(sidebyside_url(subwiki,page,from,to),name,false) if from == -1 && to == -1
  return '' if from < 0
  return '' if from >= to
  
  return link_to(sidebyside_url(subwiki,page,from,to),name,false)
end

#sidebyside_url(subwiki, page, from, to) ⇒ Object

URL to side by side view of two page versions



316
317
318
319
320
321
# File 'lib/Wiki2Go/LineFormatter.rb', line 316

def sidebyside_url(subwiki,page,from,to)
  params = Hash.new
  params['from'] = from if from >= 0
  params['to'] = to if to >= 0
  return make_verb_url('sidebyside',subwiki,page)+query_string( params )
end

#verb_url(verb) ⇒ Object

Return the path to the script verb, with the right extension and parameters

  • verb = name of the verb (view, edit,…)

Adds current subwiki and page name



63
64
65
# File 'lib/Wiki2Go/LineFormatter.rb', line 63

def verb_url(verb)
  return make_verb_url(verb,@web.name,@web.current_page)
end

Link to display the different versions of a page



199
200
201
# File 'lib/Wiki2Go/LineFormatter.rb', line 199

def versions_link(page,label)
  return link_to(versions_url(page),label,false)
end

#versions_url(page) ⇒ Object Also known as: version_url

URL to display the different versions of a page



194
195
196
# File 'lib/Wiki2Go/LineFormatter.rb', line 194

def versions_url(page)
  return make_verb_url('versions',@web.name,page)
end

Link to view a page



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/Wiki2Go/LineFormatter.rb', line 245

def view_link(subwiki,page,name)
  if subwiki == @web.name then
    return link_to(view_url(subwiki,page),name)
  else
    if page == name then
      return link_to(view_url(subwiki,page),"#{subwiki}:#{name}")
    else 
      return link_to(view_url(subwiki,page),name)
    end
  end
end

#view_url(subwiki, page) ⇒ Object

URL to the given page in the given subwiki URL is relative or absolute depending on @absolute_urls setting



236
237
238
239
240
241
242
# File 'lib/Wiki2Go/LineFormatter.rb', line 236

def view_url(subwiki,page)
  if @absolute_urls then
    return @web.base_url.chop + view_page_url(subwiki,page)
  else
    return view_page_url(subwiki,page)
  end
end

Link to a specific version of a page



294
295
296
# File 'lib/Wiki2Go/LineFormatter.rb', line 294

def view_version_link(subwiki,page,name,version)
  return link_to(view_version_url(subwiki,page,version),name)
end

#view_version_url(subwiki, page, version) ⇒ Object

URL to a specific version of a page



289
290
291
# File 'lib/Wiki2Go/LineFormatter.rb', line 289

def view_version_url(subwiki,page,version)
  return make_verb_url('view',subwiki,page)+query_string( 'version' => version )
end

Return a link suitable for creating a new page with the wiki editor, if editing is allowed Return the name of the page, otherwise



125
126
127
128
129
130
131
# File 'lib/Wiki2Go/LineFormatter.rb', line 125

def wikiedit_link(subwiki,page,name)
  if @editable then
    return name + wikiedit_this_link(subwiki,page,"?")
  else
    return name 
  end
end

Return a link suitable for editing a page with the wiki editor, if editing is allowed Return empty string, otherwise



135
136
137
138
139
140
141
# File 'lib/Wiki2Go/LineFormatter.rb', line 135

def wikiedit_this_link(subwiki,page,name)
  if @editable then
    return link_to(make_verb_url('wikiedit',subwiki,page),name,false)
  else
    return "" 
  end
end