Class: Plate::Asset

Inherits:
Page
  • Object
show all
Defined in:
lib/plate/asset.rb

Overview

An asset is a dynamic stylesheet or javascript file that needs some processing before it is ready to be placed into the site’s destination directory.

Some examples of supported asset formats are [CoffeeScript](coffeescript.org) and [Sass](sass-lang.com).

# Asset Engines

Assets are compiled based on the file extensions of the source file, and the so-called “engines” that are available and registered to Plate. By default, Plate does not require any of these asset engines to run, so any external dependencies will need to be installed before attempting to use an asset engine.

For example, in order to use the CoffeeScript engine (and thus a file with the extension of ‘.coffee`) the CoffeeScript gem will need to be installed. (`gem install coffee-script`).

# File Naming

For best results, name your source files exactly how you would like them to be displayed once compiled.

To create a file in the destination named site.js that is compiled from a CoffeeScript source, name the file in the source directory site.js.coffee. The asset compilation engine will stop trying to render the file with an asset engine when it finds the first non-registered file extension. In this case, “js” is not recognized as an asset engine extension that needs compilation, so compilation stops and the file is written to the destination.

Instance Attribute Summary

Attributes inherited from Page

#body, #content, #file, #layout, #meta, #partials, #raw, #site

Instance Method Summary collapse

Methods inherited from Page

#<=>, #==, #basename, #directory, #downgrade?, #extension, #file?, #file_name, #id, #initialize, #inspect, #keywords, #load!, #loaded?, #path, #relative_file, #reload!, #rendered_body, #slug, #title_for_url, #to_s, #url

Methods included from Callbacks

included

Constructor Details

This class inherits a constructor from Plate::Page

Instance Method Details

#base_pathObject



29
30
31
32
33
34
35
36
37
# File 'lib/plate/asset.rb', line 29

def base_path
  assets_dir = File.join(self.site.source, 'assets')

  if self.file.start_with?(assets_dir)
    Pathname.new(File.join(self.site.source, 'assets'))
  else
    Pathname.new(File.join(self.site.source, 'content'))
  end
end

#enginesArray

The engines in use for this asset. Engines are determined by looping through the extensions of the base file and associating those with a registered engine.

Returns:

  • (Array)

    List of engines in use for this file.



43
44
45
# File 'lib/plate/asset.rb', line 43

def engines
  @engines ||= self.extensions.reverse.collect { |e| self.site.registered_asset_engines[e.gsub(/\./, '').to_sym] }.reject { |e| !e }
end

#extensionsArray

The file extensions for this asset’s source file.

Returns:

  • (Array)

    List of extensions



50
51
52
# File 'lib/plate/asset.rb', line 50

def extensions
  @extensions ||= self.basename.scan(/\.[^.]+/)
end

#file_pathString

The destination file path for this asset.

Returns:

  • (String)


67
68
69
# File 'lib/plate/asset.rb', line 67

def file_path
  [ directory, file_name ].join('/')
end

#format_extensionString

The end result format for this file. This is the first file extension in the asset’s source file name that is not a registered engine extension.

For example, a file named ‘desktop.css.css` will have a format extension of “css”.

Returns:

  • (String)


60
61
62
# File 'lib/plate/asset.rb', line 60

def format_extension
  self.extensions.reverse.detect { |e| !self.site.asset_engine_extensions.include?(e) }
end

#pathnameString

Directory name for the source asset file

Returns:

  • (String)


74
75
76
# File 'lib/plate/asset.rb', line 74

def pathname
  File.dirname(self.file)
end

#rendered_contentString

Generates the resulting content for this asset using the ‘engines` determined by the source file’s name. Unlike a page, assets do not render content using a layout.

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/plate/asset.rb', line 83

def rendered_content
  return @rendered_content if @rendered_content

  around_callback :render do
    result = File.read(file)

    self.engines.each do |engine|
      template = engine.new() { result }
      result = template.render(self, :site => self.site)
    end

    @rendered_content = result
  end

  @rendered_content
end

#write!String

Write this asset file to the destination. The content is written to disk using the path designated in #file_path and the content from #rendered_content.

The callbacks ‘before_write` and `after_write` are included here. To perform custom actions before or after an asset file is written to disk, use these callback methods.

See Callbacks for more information on setting up callbacks.

Returns:

  • (String)

    The file path that was written to.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/plate/asset.rb', line 110

def write!
  path = File.join(site.build_destination, file_path)
  FileUtils.mkdir_p(File.dirname(path))

  around_callback :write do
    File.open(path, 'w') do |f|
      f.write(self.rendered_content)
    end
  end

  path
end