Class: Compiler

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

Instance Method Summary collapse

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/compiler.rb', line 11

def initialize()
    @util = Util.instance
    @setup = Setup.instance

    #当前的工作目录
    @workbench = @util.workbench
    @theme_dir = self.get_theme_dir
    @template_dir = File::join(@theme_dir, 'template')
    Mustache.template_path = File::join(@template_dir, 'partials')

end

Instance Method Details

#ensure_targetObject

获取target, 如果存在, 则删除



28
29
30
31
32
33
34
35
# File 'lib/compiler.rb', line 28

def ensure_target()
    dir = @util.target_dir
    #存在则先删除
    FileUtils.rm_rf(dir) if File::exists?(dir)
    #创建目录
    Dir::mkdir(dir)
    dir
end

#execute(type, data, auto_save = true, filename = '') ⇒ Object

执行生成, filename: 相对文件路径



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/compiler.rb', line 65

def execute(type, data, auto_save = true, filename = '')
    data['site'] = @setup.site_config
    data['m2m'] = @util.get_product

    template = self.read_template type
    html = Mustache.render(template, data)

    return html if not auto_save

    file = File::join @setup.target_dir, filename
    @util.write_file file, html
end

#get_theme_dirObject

根据配置获取theme, 如果没有, 则使用默认的theme



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/compiler.rb', line 38

def get_theme_dir()
    #先从当前工作目录下查找theme目录
    dir = File::join(@workbench, @util.local_theme_dir)
    #当前有theme目录
    return dir if(File::exists?(dir))

    base_dir = File::join(Pathname.new(File.dirname(__FILE__)), 'themes')

    
    #根据用户配置获取theme
    theme_name = @setup.get_merged_config['theme'] || 'hyde'
    theme_dir = File::join(base_dir, theme_name)

    #如果没有找到对应的theme, 则
    return theme_dir if(File.exists?(theme_dir))
    File::join(base_dir, 'hyde')
end

#read_template(name) ⇒ Object

读取模板



58
59
60
61
# File 'lib/compiler.rb', line 58

def read_template(name)
    file = File::join(@template_dir, name + '.mustache')
    IO.read(file)
end

#theme_dirObject



23
24
25
# File 'lib/compiler.rb', line 23

def theme_dir
    @theme_dir
end