Class: ClWiki::PageFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cl_wiki/page_formatter.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.



9
10
11
12
13
# File 'lib/cl_wiki/page_formatter.rb', line 9

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.



7
8
9
# File 'lib/cl_wiki/page_formatter.rb', line 7

def content
  @content
end

#full_nameObject

Returns the value of attribute full_name.



6
7
8
# File 'lib/cl_wiki/page_formatter.rb', line 6

def full_name
  @full_name
end

Class Method Details

.only_html(str) ⇒ Object



142
143
144
145
146
147
# File 'lib/cl_wiki/page_formatter.rb', line 142

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



153
154
155
# File 'lib/cl_wiki/page_formatter.rb', line 153

def cgifn
  $wiki_conf&.cgifn
end


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cl_wiki/page_formatter.rb', line 161

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


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cl_wiki/page_formatter.rb', line 69

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


63
64
65
66
67
# File 'lib/cl_wiki/page_formatter.rb', line 63

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


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cl_wiki/page_formatter.rb', line 105

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



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

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

#gsub_wordsObject



101
102
103
# File 'lib/cl_wiki/page_formatter.rb', line 101

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

#header(full_page_name, page = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cl_wiki/page_formatter.rb', line 22

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)


184
185
186
187
188
# File 'lib/cl_wiki/page_formatter.rb', line 184

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



97
98
99
# File 'lib/cl_wiki/page_formatter.rb', line 97

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

#page_update_time(page) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/cl_wiki/page_formatter.rb', line 45

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



55
56
57
58
59
60
61
# File 'lib/cl_wiki/page_formatter.rb', line 55

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



92
93
94
95
# File 'lib/cl_wiki/page_formatter.rb', line 92

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



88
89
90
# File 'lib/cl_wiki/page_formatter.rb', line 88

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

#starts_with_path_char(path) ⇒ Object



149
150
151
# File 'lib/cl_wiki/page_formatter.rb', line 149

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