Module: Cheatr::Server::Helpers

Included in:
App
Defined in:
lib/cheatr/server/helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_content_typeObject



11
12
13
# File 'lib/cheatr/server/helpers.rb', line 11

def default_content_type
  html? ? 'text/html' : 'text/markdown'
end

#html?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/cheatr/server/helpers.rb', line 7

def html?
  request.accept? 'text/html'
end

#md(text) ⇒ Object

Processes the given text as markdown, additionally processing cheatr links as well.



18
19
20
21
22
23
# File 'lib/cheatr/server/helpers.rb', line 18

def md(text)
  markdown text.
    gsub(/{{([a-z]+([\.\-\_][a-z]+)*)}}/, '[\1](/\1)').             # {{name}}           -> [name](/name)
    gsub(/{{([^\|}]+)\|([a-z]+([\.\-\_][a-z]+)*)}}/, '[\1](/\2)').  # {{link text|name}} -> [link text](/name)
    to_s
end

#query?(name) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/cheatr/server/helpers.rb', line 3

def query?(name)
  name.include? '*'
end

#template(name, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cheatr/server/helpers.rb', line 35

def template(name, opts = {})
  status opts[:status] if opts[:status]
  content_type opts[:content_type] || default_content_type
  logger.info "Rendering template #{name}"
  output = erb :"#{name}.md", layout: false
  if html?
    erb md(output), layout: :"layout.html"
  else
    output
  end
end

#text(output, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/cheatr/server/helpers.rb', line 25

def text(output, opts = {})
  content_type 'text/plain'
  status opts[:status] if opts[:status]
  if output.is_a?(Array)
    output = output.map { |s| "#{s}\n" }
  end
  logger.info output
  output
end