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.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/compiler.rb', line 13

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, 如果存在, 则删除



31
32
33
34
35
36
37
38
# File 'lib/compiler.rb', line 31

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



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

def get_theme_dir()
    #获取用户配置的theme
    theme_dir = @setup.get_theme
    #如果这个目录存在,则使用用户设置的主题
    return theme_dir if theme_dir and File::exists?(theme_dir)

    #没有找到配置,则考虑默认的theme目录
    theme_dir = File::join(@workbench, @util.local_theme_dir)
    return theme_dir if(File::exists?(theme_dir))

    #还是没有找到,则使用系统自带主题
    File::join(@util.themes_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')
    @util.read_file file
end

#theme_dirObject



26
27
28
# File 'lib/compiler.rb', line 26

def theme_dir
    @theme_dir
end