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



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

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



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

def cgifn
  $wiki_conf&.cgifn
end


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/cl_wiki/page.rb', line 307

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


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cl_wiki/page.rb', line 213

def footer(page)
  return String.new unless page.is_a? ClWiki::Page

  custom_footer = process_custom_footers(page)

  wiki_name = page.page_name

  # 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'>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 << '</ul></div>'
  custom_footer << footer
end


251
252
253
254
255
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
# File 'lib/cl_wiki/page.rb', line 251

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



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

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

#gsub_wordsObject



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

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
# 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\'>')
  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)


330
331
332
333
334
# File 'lib/cl_wiki/page.rb', line 330

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

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

#mailto_urlObject



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

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

#page_update_time(page) ⇒ Object



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

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



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

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



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

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



234
235
236
# File 'lib/cl_wiki/page.rb', line 234

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

#starts_with_path_char(path) ⇒ Object



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

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