Module: StaticMatic::Helpers::AssetsHelper

Extended by:
AssetsHelper
Included in:
StaticMatic::Helpers, AssetsHelper
Defined in:
lib/staticmatic/helpers/assets_helper.rb

Instance Method Summary collapse

Instance Method Details

#img(name, options = {}) ⇒ Object

Generates an image tag always relative to the current page unless absolute path or http url specified.

img(‘test_image.gif’) -> <img src=“/images/test_image.gif” alt=“Test image”/> img(‘contact/test_image.gif’) -> <img src=“/images/contact/test_image.gif” alt=“Test image”/> img(‘’) -> <img src=“” alt=“Test image”/>



94
95
96
97
98
# File 'lib/staticmatic/helpers/assets_helper.rb', line 94

def img(name, options = {})
  options[:src] = name.match(%r{^((\.\.?)?/|https?://)}) ? name : "#{current_page_relative_path}images/#{name}"
  options[:alt] ||= name.split('/').last.split('.').first.capitalize.gsub(/_|-/, ' ')
  tag :img, options
end

#javascripts(*files) ⇒ Object

Generate javascript source tags for the specified files

javascripts(‘test’) -> <script language=“javascript” src=“javascripts/test.js”></script>



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/staticmatic/helpers/assets_helper.rb', line 77

def javascripts(*files)
  relative_path = current_page_relative_path

  output = ""
  files.each do |file|
    file_str = file.to_s
    src = file_str.match(%r{^((\.\.?)?/|https?://)}) ? file_str : "#{relative_path}javascripts/#{file_str}.js"
    output << tag(:script, :language => 'javascript', :src => src, :type => "text/javascript") { "" }
  end
  output
end

#stylesheets(*params) ⇒ Object

Generates links to all stylesheets in the source directory

stylesheets

or specific stylesheets in a specific order

stylesheets :reset, :application

Can also pass options hash in at the end so you can specify :media => :print



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/staticmatic/helpers/assets_helper.rb', line 12

def stylesheets(*params)
  options = {}
  if params.last.is_a?(Hash)
    options = params.last
    params.slice!(-1, 1)
  end
  options[:media] = 'all' unless options.has_key?(:media)
  options[:rel] = 'stylesheet'; options[:type] = 'text/css'

  relative_path = current_page_relative_path

  output = ""
  if params.length == 0
    # no specific files requested so include all in no particular order
    stylesheet_dir = File.join(@staticmatic.src_dir, 'stylesheets')
    stylesheet_directories = Dir[File.join(stylesheet_dir, '**','*.{sass,scss}')]
    
    # Bit of a hack here - adds any stylesheets that exist in the site/ dir that haven't been generated from source sass
    Dir[File.join(@staticmatic.site_dir, 'stylesheets', '*.css')].each do |filename|
      search_filename = File.basename(filename).chomp(File.extname(filename))
      puts search_filename
      already_included = false
      stylesheet_directories.each do |path|
        if File.basename(path).include?(search_filename)
          already_included = true
          break
        end
      end
      
      stylesheet_directories << filename unless already_included
    end

    stylesheet_directories.each do |path|
      
      filename_without_extension = File.basename(path).chomp(File.extname(path))
      
      if !filename_without_extension.match(/^\_/)
        
        path = path.gsub(/#{@staticmatic.src_dir}/, "").
                    gsub(/#{@staticmatic.site_dir}/, "").
                    gsub(/#{filename_without_extension}\.(sass|scss|css)/, "")
                    
        options[:href] = File.join(relative_path, path, "#{filename_without_extension}.css")
        output << tag(:link, options)
      end
    end
  else
    #specific files requested and in a specific order
    params.each do |file|
      if File.exist?(File.join(@staticmatic.src_dir, 'stylesheets', "#{file}.sass")) ||
         File.exist?(File.join(@staticmatic.src_dir, 'stylesheets', "#{file}.scss")) || 
         File.exist?(File.join(@staticmatic.site_dir, 'stylesheets', "#{file}.css"))
        options[:href] = File.join(relative_path, "stylesheets", "#{file}.css")
        output << tag(:link, options)
      end
    end
  end
  
  output
end