Class: MagLove::Commands::Fonts

Inherits:
Base
  • Object
show all
Defined in:
lib/maglove/commands/fonts.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Workspace

#gem_dir, #theme_base_dir, #theme_base_file, #theme_config, #theme_dir, #theme_file, #workspace_dir, #workspace_file

Methods included from Helper::LogHelper

#debug, #error, #error!, #info, #logger

Constructor Details

This class inherits a constructor from MagLove::Commands::Base

Instance Method Details

#compileObject



5
6
7
8
9
10
11
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
# File 'lib/maglove/commands/fonts.rb', line 5

def compile
  # clean up
  FileUtils.rm_rf("dist/fonts")
  FileUtils.mkdir_p("dist/fonts")

  # 1: Build font map
  debug("▸ building font map")
  font_map = {}
  font_files = Dir.glob("src/fonts/*/*.ttf")
  font_files.each do |font_file|
    (_root_dir, _font_dir, font_id, font_filename) = font_file.split("/")
    font_variant = font_filename.gsub("#{font_id}-", '').gsub(".ttf", '')
    font_map[font_id] = [] if font_map[font_id].nil?
    font_map[font_id].push(font_variant)
  end

  # 2: Generate stylesheets
  debug("▸ compiling fonts:")
  FileUtils.touch("dist/fonts/fonts.css")
  open("dist/fonts/fonts.css", 'wb') do |master_file|
    font_map.each do |font_id, variants|
      debug("~▸ compiling font '#{font_id}'")
      FileUtils.mkdir_p("dist/fonts/#{font_id}")
      variants.each do |variant|
        FileUtils.copy("src/fonts/#{font_id}/#{font_id}-#{variant}.ttf", "dist/fonts/#{font_id}/#{font_id}-#{variant}.ttf")
      end
      font_style_file = "dist/fonts/#{font_id}/font.css"
      FileUtils.touch(font_style_file)
      open(font_style_file, 'wb') do |file|
        master_file << "/* #{font_id} (#{variants.join(', ')}) */\n"
        variants.each do |variant|
          file << get_font_styles(font_id, variant)
          master_file << get_font_styles(font_id, variant, "#{font_id}/")
        end
        master_file << "\n"
      end
    end
  end
  debug("▸ all font styles created")
  if options.sync
    # @TODO: Submit to cloud server
    debug("▸ all fonts synchronized")
  end
end