class ClientPages::MarkdownContent::Renderer

Attributes

file[R]
options[R]

Public Class Methods

new(file, options = {}) click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 19
def initialize(file, options = {})
  @file, @options = file, options
end
render_content(file, options = {}, &block) click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 5
def render_content(file, options = {}, &block)
  html = new(file, options).html
  block_given? ? ContentBuilder.new(html).instance_eval(&block) : html.strip
rescue OpenURI::HTTPError
  "404: could not find '#{file}'"
rescue Errno::ENOENT
  "No such file or directory - #{file}"
rescue SocketError
  'Error: content_path not available'
end

Public Instance Methods

html() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 23
def html
  html = html_from_markdown
  html = template(html) if options.key?(:template)
  html
end

Private Instance Methods

cache() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 79
def cache
  ClientPages.cache
end
caching?() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 71
def caching?
  config.caching
end
config() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 75
def config
  ClientPages.config
end
content_path() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 63
def content_path
  config.remote ? config.remote_content_path : config.content_path
end
extensions() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 35
def extensions
  config.markdown_extensions.merge(options.key?(:extensions) ? options[:extensions] : {})
end
html_from_markdown() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 39
def html_from_markdown
  Redcarpet::Markdown.new(html_renderer, extensions).render(markdown)
end
html_renderer() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 31
def html_renderer
  Redcarpet::Render::HTML.new(config.render_options)
end
markdown() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 47
def markdown
  raw? ? file : read_file
end
raw?() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 67
def raw?
  config.raw_markdown || options[:raw]
end
read_file() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 55
def read_file
  if caching?
    cache.fetch(url) { cache[url] = open(url).read }
  else
    open(url).read
  end
end
template(html) click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 43
def template(html)
  ContentBuilder.new(html).instance_eval(&Template[options[:template]])
end
url() click to toggle source
# File lib/client_pages/markdown_content/renderer.rb, line 51
def url
  "#{content_path}/#{file}.md"
end