Class: Alula::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(override = {}) ⇒ Site

Returns a new instance of Site.



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/alula/site.rb', line 24

def initialize(override = {})
  # Load configuration
  @config = YAML.load_file('config.yml').deep_merge(override)
  
  # Register local theme path
  Alula::Theme.register("themes")
  
  # Initialize Jekyll
  options         = Jekyll::DEFAULTS.deep_merge({
    'source'      => '_tmp',
    'destination' => 'public',
    'markdown'    => 'kramdown',
    
    # Site options
    'title'          => @config["title"],
    'tagline'        => @config["tagline"],
    'author'         => @config["author"],
    'root'           => @config["root"],
    'asset_path'     => "#{@config["root"]}assets",
    'permalinks'     => @config["permalinks"],
    'paginate'       => @config["paginate"],
    'pagination_dir' => @config["pagination_dir"],
    'excerpt_link'   => @config["excerpt_link"],
  })
  
  @jekyll = Jekyll::Site.new(options)
  
  @themepath = Alula::Theme.find_theme(@config['theme'])
  unless @themepath
    raise "Cannot find theme #{@config['theme']}"
  end
  
  # Initialize Sprockets
  @sprockets = Sprockets::Environment.new
  # Set our compressor
  if @config['asset_compress']
    @sprockets.css_compressor = Alula::Compressors::CSSCompressor.new
    @sprockets.js_compressor = Alula::Compressors::JSCompressor.new
    
    [Jekyll::Post, Jekyll::Page].each do |klass|
      klass.send(:include, Alula::Compressors::HTMLCompressor)
      klass.send(:alias_method, :output_without_compression, :output)
      klass.send(:alias_method, :output, :output_with_compression)
    end
  end

  # Add theme to asset paths
  @sprockets.append_path File.join(@themepath, @config['theme'], "stylesheets")
  @sprockets.append_path File.join(@themepath, @config['theme'], "javascripts")

  # Generated assets
  @sprockets.append_path File.join("_tmp", "assets")
  
  # Attachments
  @sprockets.append_path File.join("attachments")
  
  # Vendor assets
  vendor_path = File.expand_path(File.join(File.dirname(__FILE__), *%w{.. .. vendor}))
  @sprockets.append_path File.join(vendor_path, "stylesheets")
  @sprockets.append_path File.join(vendor_path, "javascripts")
  
  # Initialize blog plugins
  @config["plugins"].each do |plugin, opts|
    require "alula/plugins/#{plugin}"
    
    plugin_class = Alula::Plugins.const_get(ActiveSupport::Inflector.camelize(plugin, true))
    path = plugin_class.install(opts)
    @sprockets.append_path File.join(path, "stylesheets")
    @sprockets.append_path File.join(path, "javascripts")
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/alula/site.rb', line 22

def config
  @config
end

#jekyllObject (readonly)

Returns the value of attribute jekyll.



22
23
24
# File 'lib/alula/site.rb', line 22

def jekyll
  @jekyll
end

Instance Method Details

#asset_attach(post, assets) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/alula/site.rb', line 139

def asset_attach(post, assets)
  # Find the post
  post = find_post(post)
  post =~ /(?<date>(\d{4}-\d{2}-\d{2}))/
  date = Time.parse(date)
  
  width, height = @config["images"].split("x").collect {|i| i.to_i }
  asset_path = File.join(%w{%Y %m %d}.collect{|f| date.strftime(f) })
  file_path = File.join("attachments", "_originals", asset_path)
  FileUtils.mkdir_p(file_path)
  
  processed = []
  
  post_io = File.open(post, "a")
  
  # Generate assets
  image_types = [".jpg", ".png"]
  assets.each do |asset|
    asset_ext = File.extname(asset).downcase
    asset_base = File.basename(asset).downcase
    asset_plain = File.basename(asset, asset_ext)
    
    if image_types.include?(asset_ext)
      img = Magick::Image.read(asset).first
      img_normal = img.resize_to_fit(width, height)
      img_normal.write(File.join(file_path, asset_base))
      img_normal = nil
      
      img_retina = img.resize_to_fit(width * 2, height * 2)
      retina_fname = File.join(file_path, "#{asset_plain}_2x#{asset_ext}")
      img_retina.write(retina_fname)
      img_retina = nil
      
      processed << File.join(asset_path, asset_base)
      
      if @config["plugins"].keys.include?("lightbox")
        post_io.puts "{% lightbox #{File.join(asset_path, asset_base)} %}"
      else
        post_io.puts "{% image #{File.join("_originals", asset_path, asset_base)} %}"
      end
      
      puts "Asset generated: #{asset_base} (normal, retina)"
    end
  end
  
  post_io.close
end

#generateObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/alula/site.rb', line 96

def generate
  puts "==> Generating blog"
  
  # Prepare Jekyll environment
  prepare
  
  # Generate missing assets
  assetgen
  
  # Generate asset manifest
  compile
  
  # Execute jekyll
  process
  
  # Cleanup
  cleanup
end

#previewObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/alula/site.rb', line 115

def preview
  generate
  
  require 'webrick'
    # include WEBrick

    FileUtils.mkdir_p("public")

    mime_types = WEBrick::HTTPUtils::DefaultMimeTypes
    mime_types.store 'js', 'application/javascript'

    s = WEBrick::HTTPServer.new(
      :Port            => 3000,
      :MimeTypes       => mime_types
    )
    s.mount('/', WEBrick::HTTPServlet::FileHandler, "public")
    t = Thread.new {
      s.start
    }

    trap("INT") { s.shutdown }
    t.join()
end