Class: ClWiki::PageFormatter

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

Constant Summary collapse

FIND_PAGE_NAME =
'Find'
FIND_RESULTS_NAME =
'Find Results'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PageFormatter.



160
161
162
163
164
# File 'lib/cl_wiki/page.rb', line 160

def initialize(content = nil, full_name = nil)
  @content = content
  self.full_name = full_name
  @wiki_index = nil
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



158
159
160
# File 'lib/cl_wiki/page.rb', line 158

def content
  @content
end

#full_nameObject

Returns the value of attribute full_name.



157
158
159
# File 'lib/cl_wiki/page.rb', line 157

def full_name
  @full_name
end

Class Method Details

.only_html(str) ⇒ Object



293
294
295
296
297
298
# File 'lib/cl_wiki/page.rb', line 293

def self.only_html(str)
  only_one_tag = /\A[^<]*<[^<>]*>[^>]*\z/
  header_tag_line = %r{\A\s*<h.>.*</h.>\s*\z}
  (str =~ only_one_tag) || (str =~ header_tag_line)
  # str.scan(/<.*>/).to_s == str.chomp
end

Instance Method Details

#cgifnObject



304
305
306
# File 'lib/cl_wiki/page.rb', line 304

def cgifn
  $wiki_conf&.cgifn
end


312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/cl_wiki/page.rb', line 312

def convert_to_link(page_name)
  if ClWiki::Page.page_exists?(page_name)
    "<a href='#{page_name}'>#{page_name}</a>"
  else
    @wiki_index ||= ClWiki::MemoryIndexer.instance
    hits = @wiki_index.search(page_name, titles_only: true)

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

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


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/cl_wiki/page.rb', line 220

def core_footer_links(wiki_name, tab_index=0)
  # refactor string constants
  footer = String.new("<div class='wikiFooter'>")
  footer << '<ul>'
  if $wiki_conf.editable
    unless [FIND_PAGE_NAME, FIND_RESULTS_NAME].include?(wiki_name)
      footer << "<li><span class='wikiAction'><a href='#{wiki_name}/edit' tabindex=#{tab_index}>Edit</a></span></li>"
    end
  end
  footer << "<li><span class='wikiAction'><a href='find' tabindex=#{tab_index}>Find</a></span></li>"
  if $wiki_conf.publishTag
    footer << "<li><span class='wikiAction'><a href='recent' tabindex=#{tab_index}>Recent</a></span></li>"
  else
    footer << "<li><span class='wikiAction'><a href='FrontPage' tabindex=#{tab_index}>Home</a></span></li>"
  end
  footer << '</ul></div>'
  footer
end


214
215
216
217
218
# File 'lib/cl_wiki/page.rb', line 214

def footer(page)
  return String.new unless page.is_a? ClWiki::Page
  custom_footer = process_custom_footers(page)
  custom_footer << core_footer_links(page.page_name)
end


256
257
258
259
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
285
286
287
288
289
290
291
# File 'lib/cl_wiki/page.rb', line 256

def format_links
  no_wiki_link_in_effect = false
  inside_html_tags = false

  gsub_words do |word|
    if (word[0, 1] == '<') && (word[-1, 1] == '>')
      # refactor to class,local constant, instead of global
      if /<NoWikiLinks>/i.match?(word)
        no_wiki_link_in_effect = true
        word = ''
        # refactor to class,local constant, instead of global
      elsif /<\/NoWikiLinks>/i.match?(word)
        no_wiki_link_in_effect = false
        word = ''
      end

      if /<html>/i.match?(word)
        inside_html_tags = true
        word = ''
      elsif /<\/html>/i.match?(word)
        inside_html_tags = false
        word = ''
      end
    elsif is_wiki_name?(word)
      if !no_wiki_link_in_effect && !inside_html_tags
        # code smell here y'all
        word = convert_to_link(word) unless block_given?
      end
    end
    if block_given?
      yield word
    else
      word
    end
  end
end

#full_urlObject



308
309
310
# File 'lib/cl_wiki/page.rb', line 308

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

#gsub_wordsObject



252
253
254
# File 'lib/cl_wiki/page.rb', line 252

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

#header(full_page_name, page = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cl_wiki/page.rb', line 173

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 = String.new("<div class='wikiHeader'>")
  head << core_footer_links(full_page_name, -1).sub('wikiFooter', 'wikiFooter wikiFooterFloat')
  if [FIND_PAGE_NAME, FIND_RESULTS_NAME].include?(full_page_name)
    head << "<span class='pageName'>#{full_page_name}</span>"
  else
    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
  end
  head << '</div>'
end

#is_wiki_name?(string) ⇒ Boolean

Returns:

  • (Boolean)


335
336
337
338
339
# File 'lib/cl_wiki/page.rb', line 335

def is_wiki_name?(string)
  return false if string.empty?

  /\A[0-9]*[A-Z][a-z]\w*?[A-Z][a-z]\w*\z/.match?(string)
end

#mailto_urlObject



248
249
250
# File 'lib/cl_wiki/page.rb', line 248

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

#page_update_time(page) ⇒ Object



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

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



206
207
208
209
210
211
212
# File 'lib/cl_wiki/page.rb', line 206

def process_custom_footers(page)
  Dir["#{::File.dirname(__FILE__)}/footer/footer.*"].sort.each do |fn|
    require fn
  end

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

#reload_url(with_global_edit_links = false) ⇒ Object



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

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

#src_urlObject



239
240
241
# File 'lib/cl_wiki/page.rb', line 239

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

#starts_with_path_char(path) ⇒ Object



300
301
302
# File 'lib/cl_wiki/page.rb', line 300

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