Class: StaticMatic::Base

Inherits:
Object
  • Object
show all
Includes:
BuildMixin, HelpersMixin, RenderMixin, RescueMixin, ServerMixin, SetupMixin
Defined in:
lib/staticmatic/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RescueMixin

#render_rescue_from_error

Methods included from ServerMixin

#preview

Methods included from HelpersMixin

#load_helper, #load_helpers

Methods included from SetupMixin

#setup

Methods included from BuildMixin

#build

Methods included from RenderMixin

#clear_template_variables!, #fetch_layout_path, #generate_partial, #render_template, #render_template_with_layout

Constructor Details

#initialize(base_dir, mode = :preview) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/staticmatic/base.rb', line 31

def initialize(base_dir, mode = :preview)
  @configuration = Configuration.new
  @current_file_stack = []
  @mode = mode

  @base_dir = base_dir
  @src_dir = File.join(@base_dir, "src")
  @site_dir = File.join(@base_dir, "build")

  @default_layout_name = "default"

  @scope = Object.new
  @scope.instance_variable_set("@staticmatic", self)
  @scope.instance_eval do
    extend StaticMatic::Helpers
  end
  
  load_configuration      
  configure_compass

  load_helpers
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



11
12
13
# File 'lib/staticmatic/base.rb', line 11

def configuration
  @configuration
end

#modeObject

Returns the value of attribute mode.



11
12
13
# File 'lib/staticmatic/base.rb', line 11

def mode
  @mode
end

#site_dirObject (readonly)

Returns the value of attribute site_dir.



12
13
14
# File 'lib/staticmatic/base.rb', line 12

def site_dir
  @site_dir
end

#src_dirObject (readonly)

Returns the value of attribute src_dir.



12
13
14
# File 'lib/staticmatic/base.rb', line 12

def src_dir
  @src_dir
end

Class Method Details

.extensionsObject



18
19
20
# File 'lib/staticmatic/base.rb', line 18

def self.extensions
  @extensions ||= (Tilt.mappings.map { |k,v| k } << "slim")
end

Instance Method Details

#base_dirObject



66
67
68
# File 'lib/staticmatic/base.rb', line 66

def base_dir
  @base_dir
end

#configure_compassObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/staticmatic/base.rb', line 122

def configure_compass
  Compass.configuration.project_path = @base_dir 

  compass_config_path = File.join(@base_dir, "config", "compass.rb")
  
  if File.exists?(compass_config_path)
    Compass.add_configuration(compass_config_path)
  end

  options = configuration.engine_options
  compass_options = Compass.configuration.to_sass_engine_options

  options['sass'] = compass_options.merge(options['sass'] || {})
  options['scss'] = compass_options.merge(options['scss'] || {})
end

#current_fileObject



14
15
16
# File 'lib/staticmatic/base.rb', line 14

def current_file
  @current_file_stack[0] || ""
end

#determine_template_path(name, ext, dir = '') ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/staticmatic/base.rb', line 80

def determine_template_path(name, ext, dir = '')
  # remove src path if present
  dir.gsub! /^#{@src_dir}\//, ''

  default_template_name = "#{name}.#{configuration.default_template_engine}"
  default_template = File.join(@src_dir, dir, default_template_name)

  if File.exists? default_template
    default_template
  else
    context = File.join(@src_dir, dir, name)
    possible_templates = Dir[context + '.*'].select do |fname|
      extensions.include? File.extname(fname).sub(/^\./, '')
    end

    if possible_templates.count > 1
      raise StaticMatic::AmbiguousTemplateError.new("#{name}#{ext}", possible_templates)
    end

    possible_templates.first
  end
end

#ensure_extension(filename) ⇒ Object



26
27
28
29
# File 'lib/staticmatic/base.rb', line 26

def ensure_extension(filename)
  ext = File.extname(filename)
  ext.length > 0 ? filename : "#{filename}.#{configuration.default_template_engine}"
end

#expand_path(path_info) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/staticmatic/base.rb', line 103

def expand_path(path_info)
  dirname, basename = File.split(path_info)

  extname = File.extname(path_info).sub(/^\./, '')
  filename = basename.chomp(".#{extname}")

  if extname.empty?
    dir = File.join(dirname, filename)
    is_dir = path_info[-1, 1] == '/'
    if is_dir
      dirname = dir
      filename = 'index'
    end
    extname = 'html'
  end

  [ dirname, filename, extname ]
end

#extensionsObject



22
23
24
# File 'lib/staticmatic/base.rb', line 22

def extensions
  self.class.extensions
end

#load_configurationObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/staticmatic/base.rb', line 54

def load_configuration
  configuration = StaticMatic::Configuration.new
  config_file = File.join(@base_dir, "config", "site.rb")
  
  if File.exists?(config_file)
    config = File.read(config_file)
    eval(config)
  end

  @configuration = configuration
end

#run(command) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/staticmatic/base.rb', line 70

def run(command)
  puts "Site root is: #{@base_dir}"
  
  if %w(build setup preview).include?(command)
    send(command)
  else
    puts "#{command} is not a valid StaticMatic command"
  end
end

#src_file_paths(*exts) ⇒ Object

TODO OPTIMIZE: caching, maybe?



139
140
141
142
143
144
145
# File 'lib/staticmatic/base.rb', line 139

def src_file_paths(*exts)
  Dir["#{@src_dir}/**/*.{#{ exts.join(',') }}"].reject do |path|
    # reject any files with a prefixed underscore, as
    # well as any files in a folder with a prefixed underscore
    path.split('/').map {|x| x.match('^\_')}.any?
  end
end