Class: ClWiki::PageFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cl_wiki/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil, aFullName = nil) ⇒ PageFormatter

Returns a new instance of PageFormatter.



190
191
192
193
194
# File 'lib/cl_wiki/page.rb', line 190

def initialize(content=nil, aFullName=nil)
  @content = content
  self.fullName = aFullName
  @wikiIndex = nil
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



188
189
190
# File 'lib/cl_wiki/page.rb', line 188

def content
  @content
end

Class Method Details

.only_html(str) ⇒ Object



357
358
359
360
361
362
# File 'lib/cl_wiki/page.rb', line 357

def self.only_html(str)
  onlyOneTag = /\A[^<]*<[^<>]*>[^>]*\z/
  headerTagLine = /\A\s*<h.>.*<\/h.>\s*\z/
  (str =~ onlyOneTag) || (str =~ headerTagLine)
  # str.scan(/<.*>/).to_s == str.chomp
end

Instance Method Details

#cgifnObject



368
369
370
# File 'lib/cl_wiki/page.rb', line 368

def cgifn
  $wiki_conf.cgifn if $wiki_conf
end

#convert_relative_wikinames_to_absoluteObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/cl_wiki/page.rb', line 296

def convert_relative_wikinames_to_absolute
  # do not go ahead without testing here
  #formatLinks do |word|
  #  if isWikiName?(word)
  #end

  # problem here is we should obey the NoWikiLinks and Html tag rules,
  # and those variables aren't being yielded right now. If we change
  # how the yield works, it affects the indexer. And we can't just
  # tack on additional yield params and have existing code that only
  # pays attention to the first keep working:
  #
  # irb(main):001:0> def test
  # irb(main):002:1>   yield 1,2,3
  # irb(main):003:1> end
  # nil
  # irb(main):004:0> test do |a|
  # irb(main):005:1* puts a
  # irb(main):006:1> end
  # 1
  # 2
  # 3
end


376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/cl_wiki/page.rb', line 376

def convertToLink(page_name)
  if ClWiki::Page.page_exists?(page_name)
    "<a href='#{page_name.strip_slash_prefix}'>#{page_name.strip_slash_prefix}</a>"
  else
    if $wiki_conf.useIndex == ClWiki::Configuration::USE_INDEX_NO
      finder = FindInFile.new($wiki_path)
      finder.find(page_name, FindInFile::FILE_NAME_ONLY)
      hits = finder.files.collect { |f| f.sub($wikiPageExt, '') }
    else
      @wikiIndex = ClWiki::IndexClient.new if @wikiIndex.nil?
      titles_only = true
      hits = @wikiIndex.search(page_name, titles_only)
      hits = GlobalHitReducer.reduce_to_exact_if_exists(page_name, hits)
    end

    case hits.length
      when 0
        result = page_name
      when 1
        result = "<a href='#{hits[0]}'>#{page_name}</a>"
      else
        result = "<a href='find?search_text=#{page_name}'>#{page_name}</a>"
    end

    if ($wiki_conf.editable) && ((hits.length == 0) || ($wiki_conf.global_edits))
      result << "<a href='#{page_name}/edit'>?</a>"
    end
    result
  end
end


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/cl_wiki/page.rb', line 250

def footer(page)
  return '' if !page.is_a? ClWiki::Page # blogki does this

  custom_footer = process_custom_footers(page)

  wiki_name = page.full_name

  # refactor string constants
  footer = "<div class='wikiFooter'>"
  footer << "<ul>"
  if (wiki_name != $FIND_PAGE_NAME) and
      (wiki_name != $FIND_RESULTS_NAME) and
      (wiki_name != $wiki_conf.recent_changes_name) and
      (wiki_name != $wiki_conf.stats_name)
    if $wiki_conf.editable
      footer << ("<li><span class='wikiAction'><a href='" + wiki_name.strip_slash_prefix + "/edit'>Edit</a></span></li>")
    end
  end
  footer << "<li><span class='wikiAction'><a href='find'>Find</a></span></li>"
  footer << "<li><span class='wikiAction'><a href='recent'>Recent</a></span></li>"
  # footer << "<li><span class='wikiAction'><a href=#{cgifn}?about=true>About</a></span></li>" if wiki_name == "/FrontPage"
  footer << "</ul></div>"
  return custom_footer << footer
end


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/cl_wiki/page.rb', line 320

def formatLinks
  no_wiki_link_in_effect = false
  inside_html_tags = false

  gsubWords do |word|
    if (word[0, 1] == '<') and (word[-1, 1] == '>')
      # refactor to class,local constant, instead of global
      if word =~ /#{'<NoWikiLinks>'}/i
        no_wiki_link_in_effect = true
        word = ''
        # refactor to class,local constant, instead of global
      elsif word =~ /#{'</NoWikiLinks>'}/i
        no_wiki_link_in_effect = false
        word = ''
      end

      if word =~ /#{'<html>'}/i
        inside_html_tags = true
        word = ''
      elsif word =~ /#{'</html>'}/i
        inside_html_tags = false
        word = ''
      end
    elsif is_wiki_name?(word)
      if !no_wiki_link_in_effect and !inside_html_tags
        # code smell here y'all
        word = convertToLink(word) if !block_given?
      end
    end
    if block_given?
      yield word
    else
      word
    end
  end
end

#full_urlObject



372
373
374
# File 'lib/cl_wiki/page.rb', line 372

def full_url
  ($wiki_conf.url_prefix + cgifn) if $wiki_conf
end

#fullNameObject



203
204
205
# File 'lib/cl_wiki/page.rb', line 203

def fullName
  @full_name
end

#fullName=(value) ⇒ Object



196
197
198
199
200
201
# File 'lib/cl_wiki/page.rb', line 196

def fullName=(value)
  @full_name = value
  if @full_name
    @full_name = @full_name[1..-1] if @full_name[0..1] == '//'
  end
end

#gsubWordsObject



292
293
294
# File 'lib/cl_wiki/page.rb', line 292

def gsubWords
  @content.gsub(/<.+?>|<\/.+?>|\w+/) { |word| yield word }
end

#header(full_page_name, page = nil) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cl_wiki/page.rb', line 207

def header(full_page_name, page = nil)
  search_text = ::File.basename(full_page_name)
  page_path, page_name = ::File.split(full_page_name)
  page_path = '/' if page_path == '.'
  dirs = page_path.split('/')
  dirs = dirs[1..-1] if !dirs.empty? && dirs[0].empty?
  full_dirs = (0..dirs.length-1).each { |i| full_dirs[i] = ('/' + dirs[0..i].join('/')) }
  head = '<div class=\'wikiHeader\'>'
  if (full_page_name != $FIND_PAGE_NAME) and
      (full_page_name != $FIND_RESULTS_NAME) and
      (full_page_name != $wiki_conf.recent_changes_name) and
      (full_page_name != $wiki_conf.stats_name)
    head << "<span class='pageName'><a href='find?search_text=#{search_text}'>#{page_name}</a></span><br/>"
    full_dirs.each do |dir|
      head << '<span class=\'pageTag\'>'
      head << "<a href=#{cgifn}?page=#{dir}>#{File.split(dir)[-1]}</a></span>"
    end
    head << '<br/>'
    head << "<span class='wikiPageData'>#{page_update_time(page)}</span><br/>" if page
  else
    head << '<span class=\'pageName\'>' + full_page_name + '</span>'
  end
  head << '</div>'
end

#is_wiki_name?(string) ⇒ Boolean

Returns:

  • (Boolean)


407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/cl_wiki/page.rb', line 407

def is_wiki_name?(string)
  all_wiki_names = true
  names = string.split(/[\\\/]/)

  # if first character is a slash, then split puts an empty string into names[0]
  names.delete_if { |name| name.empty? }
  all_wiki_names = false if names.empty?
  names.each do |name|
    all_wiki_names =
        (
        all_wiki_names and

            # the number of all capitals followed by a lowercase is greater than 1
            (name.scan(/[A-Z][a-z]/).length > 1) and

            # the first letter is capitalized or slash
            (
            (name[0, 1] == name[0, 1].capitalize) or (name[0, 1] == '/') or (name[0, 1] == "\\")
            ) and

            # there are no non-word characters in the string (count is 0)
            # ^[\w|\\|\/] is read:
            # _____________[_____  _^_  ____\w_________  _|  __\\______  _|  ___\/________]
            # characters that are  not  word characters  or  back-slash  or  forward-slash
            # (the not negates the *whole* character set (stuff in brackets))
            (name.scan(/[^\w\\\/]/).length == 0)
        )
  end
  return all_wiki_names
end

#mailto_urlObject



288
289
290
# File 'lib/cl_wiki/page.rb', line 288

def mailto_url
  "mailto:?Subject=wikifyi:%20#{@full_name}&Body=#{reload_url}"
end

#page_update_time(page) ⇒ Object



232
233
234
235
236
237
238
239
240
# File 'lib/cl_wiki/page.rb', line 232

def page_update_time(page)
  mod_time = page.mtime
  if mod_time
    update_format = $wiki_conf.page_update_format.gsub(/ /, '&nbsp;')
    mod_time.strftime(update_format)
  else
    ''
  end
end

#process_custom_footers(page) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/cl_wiki/page.rb', line 242

def process_custom_footers(page)
  Dir[::File.dirname(__FILE__) + '/footer/footer.*'].each do |fn|
    require fn
  end

  ClWiki::CustomFooters.instance.process_footers(page)
end

#reload_url(with_global_edit_links = false) ⇒ Object



279
280
281
282
283
284
285
286
# File 'lib/cl_wiki/page.rb', line 279

def reload_url(with_global_edit_links=false)
  result = "#{full_url}?page=#{@full_name}"
  if with_global_edit_links
    result << "&globaledits=true"
  else
    result << "&globaledits=false"
  end
end

#src_urlObject



275
276
277
# File 'lib/cl_wiki/page.rb', line 275

def src_url
  "file://#{ClWiki::Page.read_file_full_path_and_name(@full_name)}"
end

#starts_with_path_char(path) ⇒ Object



364
365
366
# File 'lib/cl_wiki/page.rb', line 364

def starts_with_path_char(path)
  (path[0..0] == '/') || (path[0..1] == '//')
end