Class: Gumdrop::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/gumdrop/content.rb

Direct Known Subclasses

GeneratedContent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, site, params = {}) ⇒ Content

Returns a new instance of Content.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gumdrop/content.rb', line 20

def initialize(path, site, params={})
  @site= site
  @params= HashObject.new params
  @full_path= path
  @ignored= false
  @path= get_source_path
  @level= (@path.split('/').length - 1)
  @source_filename= File.basename path
  @filename= get_target_filename
  @type= File.extname @source_filename
  @ext= File.extname @filename
  @uri= get_uri
  @slug=@uri.gsub('/', '-').gsub(@ext, '')
  @template= unless Tilt[path].nil?
    Tilt.new path
  else
    nil
  end
end

Instance Attribute Details

#extObject

Returns the value of attribute ext.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def ext
  @ext
end

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def filename
  @filename
end

#full_pathObject

Returns the value of attribute full_path.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def full_path
  @full_path
end

#ignoredObject

Returns the value of attribute ignored.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def ignored
  @ignored
end

#levelObject

Returns the value of attribute level.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def level
  @level
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def params
  @params
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def path
  @path
end

#siteObject

Returns the value of attribute site.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def site
  @site
end

#slugObject

Returns the value of attribute slug.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def slug
  @slug
end

#source_filenameObject

Returns the value of attribute source_filename.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def source_filename
  @source_filename
end

#templateObject

Returns the value of attribute template.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def template
  @template
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def type
  @type
end

#uriObject

Returns the value of attribute uri.



6
7
8
# File 'lib/gumdrop/content.rb', line 6

def uri
  @uri
end

Instance Method Details

#copyTo(output, layout = nil, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gumdrop/content.rb', line 68

def copyTo(output, layout=nil, opts={})
  do_copy= if File.exists? output
    !FileUtils.identical? @full_path, output
  else
    true
  end
  if do_copy
    @site.report "   Copying: #{@uri}", :warning
    FileUtils.cp_r @full_path, output, opts
  else
    @site.report "    (same): #{@uri}", :info
  end
end

#mtimeObject



82
83
84
85
86
87
88
# File 'lib/gumdrop/content.rb', line 82

def mtime
  if File.exists? @full_path
    File.new(@full_path).mtime
  else
    Time.now
  end
end

#render(context = nil, ignore_layout = false, reset_context = true, locals = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gumdrop/content.rb', line 40

def render(context=nil, ignore_layout=false, reset_context=true, locals={})
  context= @site.render_context if context.nil?
  if reset_context
    default_layout= (@ext == '.css' or @ext == '.js' or @ext == '.xml') ? nil : 'site'
    context.reset_data 'current_depth'=>@level, 'current_slug'=>@slug, 'page'=>self, 'layout'=>default_layout, 'params'=>self.params
  end
  context.set_content self, locals
  content= @template.render(context) 
  return content if ignore_layout
  layout= context.get_template()
  while !layout.nil?
    content = layout.template.render(context, content:content) { content }
    layout= context.get_template()
  end
  content
end

#renderTo(context, output_path, filters = [], opts = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/gumdrop/content.rb', line 57

def renderTo(context, output_path, filters=[], opts={})
  return copyTo(output_path, opts) unless useLayout?
  @site.report " Rendering: #{@uri}", :warning
  output= render(context)
  filters.each {|f| output= f.call(output, self) }
  File.open output_path, 'w' do |f|
    f.write output
  end
end

#to_sObject



94
95
96
# File 'lib/gumdrop/content.rb', line 94

def to_s
  @uri
end

#useLayout?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/gumdrop/content.rb', line 90

def useLayout?
  !@template.nil?
end