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



92
93
94
# File 'lib/Wiki2Go/LineFormatter.rb', line 92

def absolute_url
  absolute_url_of_topic(@web.current_page)
end

#absolute_url_of_topic(topic) ⇒ Object

Return absolute URL of given page



97
98
99
# File 'lib/Wiki2Go/LineFormatter.rb', line 97

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

Link to admin page



331
332
333
# File 'lib/Wiki2Go/LineFormatter.rb', line 331

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

Link to ‘recent changes’ page



228
229
230
# File 'lib/Wiki2Go/LineFormatter.rb', line 228

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

#changes_urlObject

URL of ‘recent changes’ page



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

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



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

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



297
298
299
300
301
302
# File 'lib/Wiki2Go/LineFormatter.rb', line 297

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



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

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



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

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



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

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 the name of the page, otherwise



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

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



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

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



367
368
369
370
371
372
# File 'lib/Wiki2Go/LineFormatter.rb', line 367

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

#escape_spaces(name) ⇒ Object



353
354
355
# File 'lib/Wiki2Go/LineFormatter.rb', line 353

def escape_spaces(name)
  name.gsub(/ /,'%20')
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.



81
82
83
84
# File 'lib/Wiki2Go/LineFormatter.rb', line 81

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.



87
88
89
# File 'lib/Wiki2Go/LineFormatter.rb', line 87

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
# 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
      result += '/' 
      result += name
    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


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

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



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

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



187
188
189
# File 'lib/Wiki2Go/LineFormatter.rb', line 187

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



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

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)



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

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

#save_urlObject

URL to save current page in current subwiki



172
173
174
# File 'lib/Wiki2Go/LineFormatter.rb', line 172

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



211
212
213
214
# File 'lib/Wiki2Go/LineFormatter.rb', line 211

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



205
206
207
208
# File 'lib/Wiki2Go/LineFormatter.rb', line 205

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



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

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



314
315
316
317
318
319
# File 'lib/Wiki2Go/LineFormatter.rb', line 314

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



197
198
199
# File 'lib/Wiki2Go/LineFormatter.rb', line 197

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



192
193
194
# File 'lib/Wiki2Go/LineFormatter.rb', line 192

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

Link to view a page



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

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



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

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



292
293
294
# File 'lib/Wiki2Go/LineFormatter.rb', line 292

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



287
288
289
# File 'lib/Wiki2Go/LineFormatter.rb', line 287

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



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

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



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

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