Module: Olelo::PageHelper

Includes:
Util
Included in:
ApplicationHelper
Defined in:
lib/olelo/helper.rb

Instance Method Summary collapse

Methods included from Util

#check, #decode64, #deep_copy, #encode64, #escape, #escape_html, #escape_javascript, included, #md5, #no_cache?, #sha256, #titlecase, #truncate, #unescape, #unescape_backslash, #unescape_html, #valid_xml_chars?

Instance Method Details



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/olelo/helper.rb', line 187

def breadcrumbs(page)
  path = page.try(:path) || ''
  li = [%{<li>
<a accesskey="z" href="#{escape_html build_path(nil, version: page)}">#{escape_html :root.t}</a></li>}]
  path.split('/').inject('') do |parent,elem|
    current = parent/elem
    li << %{<li>
<a href="#{escape_html build_path(current, version: page)}">#{escape_html elem}</a></li>}
    current
  end
  ('<ul class="breadcrumbs">' << li.join('<li>/</li>') << '</ul>').html_safe
end

#build_path(page, options = {}) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/olelo/helper.rb', line 200

def build_path(page, options = {})
  options = options.dup
  action = options.delete(:action)
  version = options.delete(:version)
  path = (page.try(:path) || page).to_s

  if action
    raise ArgumentError if version
    path = action.to_s/path
  else
    version ||= page if Page === page
    version = version.tree_version if Page === version
    path = 'version'/version/path if version && (options.delete(:force_version) || !version.head?)
  end

  unless options.empty?
    query = Rack::Utils.build_query(options)
    path += '?' + query unless query.empty?
  end
  '/' + (Config['base_path'] / path)
end

#date(t) ⇒ Object



176
177
178
# File 'lib/olelo/helper.rb', line 176

def date(t)
  %{<span class="date" data-epoch="#{t.to_i}">#{t.strftime('%d %h %Y %H:%M')}</span>}.html_safe
end

#format_diff(diff) ⇒ Object



180
181
182
183
184
185
# File 'lib/olelo/helper.rb', line 180

def format_diff(diff)
  summary   = PatchSummary.new(links: true)
  formatter = PatchFormatter.new(links: true, header: true)
  PatchParser.parse(diff.patch, summary, formatter)
  (summary.html + formatter.html).html_safe
end

#include_page(path) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/olelo/helper.rb', line 123

def include_page(path)
  page = Page.find(path) rescue nil
  if page
    render_page(page)
  else
    %{<a href="#{escape_html build_path(path, action: :new)}">#{escape_html :create_page.t(page: path)}</a>}
  end
end

#pagination(path, page_count, page_nr, options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/olelo/helper.rb', line 136

def pagination(path, page_count, page_nr, options = {})
  return if page_count <= 1
  unlimited = options.delete(:unlimited)
  li = []
  li << if page_nr > 1
          %{<a href="#{escape_html build_path(path, options.merge(page: page_nr - 1))}">&#9666;</a>}
        else
          %{<span class="disabled">&#9666;</span>}
        end
  min = page_nr - 3
  max = page_nr + 3
  if min > 1
    min -= max - page_count if max > page_count
  else
    max -= min if min < 1
  end
  max = max + 2 < page_count ? max : page_count
  min = min > 3 ? min : 1
  if min != 1
    li << %{<a href="#{escape_html build_path(path, options.merge(page: 1))}">1</a>} << %{<span class="ellipsis"/>}
  end
  (min..max).each do |i|
    li << if i == page_nr
            %{<span class="current">#{i}</span>}
          else
            %{<a href="#{escape_html build_path(path, options.merge(page: i))}">#{i}</a>}
          end
  end
  if max != page_count
    li << %{<span class="ellipsis"/>} << %{<a href="#{escape_html build_path(path, options.merge(page: page_count))}">#{page_count}</a>}
  end
  if page_nr < page_count
    li << %{<span class="ellipsis"/>} if unlimited
    li << %{<a href="#{escape_html build_path(path, options.merge(page: page_nr + 1))}">&#9656;</a>}
  else
    li << %{<span class="disabled">&#9656;</span>}
  end
  ('<ul class="pagination">' + li.map {|x| "<li>#{x}</li>"}.join + '</ul>').html_safe
end

#render_page(page) ⇒ Object



132
133
134
# File 'lib/olelo/helper.rb', line 132

def render_page(page)
  page.content
end