module Olelo::PageHelper

Public Instance Methods

breadcrumbs(page) click to toggle source
build_path(page, options = {}) click to toggle source
# File lib/olelo/helper.rb, line 199
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) click to toggle source
# File lib/olelo/helper.rb, line 175
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) click to toggle source
# File lib/olelo/helper.rb, line 179
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) click to toggle source
# File lib/olelo/helper.rb, line 122
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 = {}) click to toggle source
# File lib/olelo/helper.rb, line 135
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) click to toggle source
# File lib/olelo/helper.rb, line 131
def render_page(page)
  page.content
end