Module: Precious::Helpers

Included in:
App, MapGollum
Defined in:
lib/gollum/helpers.rb

Constant Summary collapse

EMOJI_PATHNAME =
Pathname.new(Gemojione.images_path).freeze

Instance Method Summary collapse

Instance Method Details

#clean_url(url) ⇒ Object

Remove all slashes from the start of string. Remove all double slashes



44
45
46
47
# File 'lib/gollum/helpers.rb', line 44

def clean_url(url)
  return url if url.nil?
  url.gsub('%2F', '/').gsub(%r{/{2,}}, '/').gsub(%r{^/}, '')
end

#emoji(name) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/gollum/helpers.rb', line 66

def emoji(name)
  if emoji = Gemojione.index.find_by_name(name)
    IO.read(EMOJI_PATHNAME.join("#{emoji['unicode'].downcase}.png"))
  else
    fail ArgumentError, "emoji `#{name}' not found"
  end
end

#encodeURI(uri) ⇒ Object



38
39
40
# File 'lib/gollum/helpers.rb', line 38

def encodeURI(uri)
  encodeURIComponent(uri).gsub('%2F', '/')
end

#find_per_page_upload_subdir(referer, host_with_port, base_path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gollum/helpers.rb', line 9

def find_per_page_upload_subdir(referer, host_with_port, base_path)
  base = base_path ? remove_leading_and_trailing_slashes(base_path) : ''
  dir = referer.match(/^https?:\/\/#{host_with_port}\/#{base}\/?(.*)/)[1]
  
  # remove gollum/* subpath if necessary
  dir.sub!(/^gollum\/[-\w]+\//, '')
  # remove file extension
  dir.sub!(/#{::File.extname(dir)}$/, '')
  # revert escaped whitespaces
  dir.gsub!(/%20/, ' ')
  
  return ::File.join('uploads', dir)
end

#forbid(msg = "Forbidden. This wiki is set to no-edit mode.") ⇒ Object



49
50
51
52
53
# File 'lib/gollum/helpers.rb', line 49

def forbid(msg = "Forbidden. This wiki is set to no-edit mode.")
  @message = msg
  status 403
  halt mustache :error
end

#not_found(msg = nil) ⇒ Object



55
56
57
58
59
# File 'lib/gollum/helpers.rb', line 55

def not_found(msg = nil)
  @message = msg || "The requested page does not exist."
  status 404
  return mustache :error
end

#not_found_procObject



61
62
63
64
# File 'lib/gollum/helpers.rb', line 61

def not_found_proc
  not_found_msg = 'Not found.'
  Proc.new {[404, {'Content-Type' => 'text/html', 'Content-Length' => not_found_msg.length.to_s}, [not_found_msg]]}
end

#remove_leading_and_trailing_slashes(str) ⇒ Object



32
33
34
# File 'lib/gollum/helpers.rb', line 32

def remove_leading_and_trailing_slashes(str)
  str.sub(%r{^(/+)}, '').sub(%r{/+$}, '')
end

#sanitize_empty_params(param) ⇒ Object



23
24
25
# File 'lib/gollum/helpers.rb', line 23

def sanitize_empty_params(param)
  [nil, ''].include?(param) ? nil : CGI.unescape(param)
end

#strip_page_name(name) ⇒ Object



27
28
29
30
# File 'lib/gollum/helpers.rb', line 27

def strip_page_name(name)
  # Check if name already has a format extension, and if so, strip it.
  Gollum::Page.valid_extension?(name) ? Gollum::Page.strip_filename(name) : name
end