class Gumdrop::Renderer

Constants

HTML_MUNGABLE_RE
SPECIAL_OPTS

Attributes

cache[R]
context[R]
ctx_pool[R]

Public Class Methods

new() click to toggle source
# File lib/gumdrop/renderer.rb, line 10
def initialize
  site.active_renderer= self
  @context, @content, @opts= nil, nil, nil
  @ctx_pool= ContextPool.new self
  @cache= {}
end

Private Class Methods

for(ext) click to toggle source

Returns the ‘Tilt::Template` for the given `ext` or nil

# File lib/gumdrop/renderer.rb, line 200
def for(ext)
  Tilt[ext] 
rescue LoadError # stupid tilt and redcarpet, they don't play well together!
  nil
end

Public Instance Methods

draw(content, opts={}) click to toggle source
# File lib/gumdrop/renderer.rb, line 17
def draw(content, opts={})
  if @ctx_pool.size > 0
    _start_rendering(content, opts)
  else
    event_block :render_item do |data|
      _start_rendering(content, opts, data)
    end
  end
end

Private Instance Methods

_default_layout() click to toggle source
# File lib/gumdrop/renderer.rb, line 145
def _default_layout
  if site.config.layout_exts.include? @content.ext
    site.config.default_layout
  else
    nil
  end
end
_hoist_data(to_context, keys=nil) click to toggle source
# File lib/gumdrop/renderer.rb, line 189
def _hoist_data(to_context, keys=nil)
  keys ||= @context.state.keys
  safe_keys= keys.reject {|k| SPECIAL_OPTS.include? k.to_s }
  safe_keys.each do |key|
    to_context.set key, @context.state[key]
  end
end
_in_new_context(content, opts) { || ... } click to toggle source
# File lib/gumdrop/renderer.rb, line 157
def _in_new_context(content, opts)
  @ctx_pool.sub_context do |ctx, prev_ctx|
    ctx._setup content, opts
    safe_opts= opts.reject { |o| SPECIAL_OPTS.include? o.to_s }
    ctx.set safe_opts

    @context= ctx
    @content= content
    @opts= opts

    if @ctx_pool.size == 1
      ctx.set :layout, _default_layout
    end

    output= yield

    case opts[:hoist]
      when :all, true
        _hoist_data(prev_ctx)
      when Array
        _hoist_data(prev_ctx, opts[:hoist])
    end if opts.has_key? :hoist

    @context= prev_ctx
    @content= prev_ctx.content || nil
    @opts= prev_ctx.opts || nil

    ctx._setup nil, nil
    output
  end
end
_layout_for_content() click to toggle source
# File lib/gumdrop/renderer.rb, line 125
def _layout_for_content
  case
    when @opts[:inline_render] then nil
    when (@content.params.has_key?(:layout) and !@content.params.layout) then nil
    # when (@content.partial? and !@opts[:layout] and !@content.params.layout) then nil
    else
      layout= @opts[:layout] || @content.params.layout || @context.get(:layout)
      site.layouts.first layout
  end
end
_layout_pipeline() { |layout| ... } click to toggle source
# File lib/gumdrop/renderer.rb, line 88
def _layout_pipeline
  layout = _layout_for_content
  unless layout.nil?
    yield layout
    # Nested Layouts!
    sub_layout= _sub_layout_for(layout)
    while !sub_layout.nil?
      yield sub_layout
      sub_layout= _sub_layout_for(sub_layout)
    end 
  end 
end
_path_to_root() click to toggle source
# File lib/gumdrop/renderer.rb, line 153
def _path_to_root
  '../' * @content.level
end
_relativize?() click to toggle source
# File lib/gumdrop/renderer.rb, line 117
def _relativize?
  return false if !site.config.relative_paths
  return false if @context.force_absolute
  return false if @content.partial?
  return true if site.config.relative_paths_exts == :all
  site.config.relative_paths_exts.include?(@content.ext)
end
_relativize_uris(text) click to toggle source

CSS_MUNGABLE_RE= Regexp.new(%Q<(href|data|src)(*)=([s]*)(‘|“|”|"|’)?\/(?)>, ‘i’)

# File lib/gumdrop/renderer.rb, line 104
def _relativize_uris(text)
  return text unless _relativize?
  path_to_root= _path_to_root
  text.force_encoding("UTF-8") if text.respond_to? :force_encoding
  text.gsub HTML_MUNGABLE_RE do |match|
    if $5 == '/'
      "#{ $1 }#{ $2 }=#{ $3 }#{ $4 }/"
    else
      "#{ $1 }#{ $2 }=#{ $3 }#{ $4 }#{ path_to_root }"
    end
  end
end
_render_content!() click to toggle source
# File lib/gumdrop/renderer.rb, line 46
def _render_content!
  output= @content.body
  _render_pipeline(@content.source_filename) do |template_class|
    output= _render_text(output, template_class)
  end
  output= _render_layouts output
  _relativize_uris output
end
_render_layout(text, layout) click to toggle source
# File lib/gumdrop/renderer.rb, line 68
def _render_layout(text, layout)
  log.debug "    layout: #{layout.source_filename}"
  _render_pipeline(layout.source_filename) do |layout_class|
    text = _render_text(layout.body, layout_class, text)
  end
  text
end
_render_layouts(text) click to toggle source
# File lib/gumdrop/renderer.rb, line 61
def _render_layouts(text)
  _layout_pipeline do |layout_class|
    text= _render_layout text, layout_class
  end
  text
end
_render_pipeline(path) { |template_class| ... } click to toggle source

NOTE: Currently, the render pipeline ends when Renderer.for returns nil for an ext. Should it continue on until all the possible file ext templates are looked up?

# File lib/gumdrop/renderer.rb, line 79
def _render_pipeline(path)
  filename_parts= path.split('.')
  begin
    ext= filename_parts.pop
    template_class= Renderer.for(ext)
    yield template_class unless template_class.nil?
  end while !template_class.nil? #and filename_parts.size
end
_render_text(text, template_class, sub_content="") click to toggle source
# File lib/gumdrop/renderer.rb, line 55
def _render_text(text, template_class, sub_content="")
  log.debug "            #{ template_class.to_s }"
  template= template_class.new(@content.source_path) { text }
  template.render(@context, content:sub_content) { sub_content }
end
_start_rendering(content, opts, data={}) click to toggle source
# File lib/gumdrop/renderer.rb, line 29
def _start_rendering(content, opts, data={})
  data[:content]= content
  log.debug " rendering: #{ content.source_filename } (#{ content.uri })"
  if content.binary? or content.missing?
    log.warn "Missing content body for: #{ content.uri }"
    nil
  else
    opts[:calling_page]= @context unless opts.has_key? :calling_page
    _in_new_context(content, opts) do
      data[:context]= @context
      data[:output]= _render_content!
      @cache[content.source_path]= data[:output] if @context.cache
      data[:output]
    end
  end
end
_sub_layout_for(layout) click to toggle source
# File lib/gumdrop/renderer.rb, line 136
def _sub_layout_for(layout)
  sub_layout_name= @context.get :layout
  return nil if sub_layout_name.nil?
  sub_layout= site.layouts.first sub_layout_name
  return nil if sub_layout.nil?
  return nil if sub_layout.uri == layout.uri
  sub_layout
end