Class: MiniYard::Generate

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generate

Returns a new instance of Generate.



9
10
11
12
13
# File 'lib/miniyard.rb', line 9

def initialize(options)
  raise 'Unable to generate MiniYard at path %s: not a directory' % path unless File.directory?(options[:root])
  @options = options
  @root = File.expand_path(@options[:root])
end

Instance Method Details

#bootstrap_fileObject



71
72
73
# File 'lib/miniyard.rb', line 71

def bootstrap_file
  File.join(@root, "bootstrap.#{bootstrap_version}.min.css")
end

#bootstrap_versionObject



67
68
69
# File 'lib/miniyard.rb', line 67

def bootstrap_version
  "1.4.0"
end

#ci_pathObject



63
64
65
# File 'lib/miniyard.rb', line 63

def ci_path
  File.join(@root, @options[:name], 'ci.url')
end

#ensure_bootstrap_existsObject



75
76
77
78
79
80
# File 'lib/miniyard.rb', line 75

def ensure_bootstrap_exists
  unless File.exist?(bootstrap_file)
    local_bootstrap = File.expand_path("../templates/bootstrap.#{bootstrap_version}.min.css", File.expand_path(__FILE__))
    FileUtils.cp(local_bootstrap, bootstrap_file)
  end
end

#foldersObject



24
25
26
# File 'lib/miniyard.rb', line 24

def folders
  @folders ||= Dir[File.join(@root, '*')].sort.select{|n| File.directory?(n) }.map{|n| MiniYard::Folder.new(n) }
end

#haml_render(file) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/miniyard.rb', line 44

def haml_render(file)
  contents = File.read(file)
  Haml::Engine.new(contents, {
    filename: file,
    format: :html5,
    attr_wrapper: '"'
  }).render(self)
end

#haml_template(tpl_name) ⇒ Object



40
41
42
# File 'lib/miniyard.rb', line 40

def haml_template(tpl_name)
  File.expand_path("../templates/#{tpl_name}.haml", File.expand_path(__FILE__))
end

#index_exists?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/miniyard.rb', line 36

def index_exists?
  File.exist?(index_file)
end

#index_fileObject



32
33
34
# File 'lib/miniyard.rb', line 32

def index_file
  File.join(@root, 'index.html')
end

#index_templateObject



28
29
30
# File 'lib/miniyard.rb', line 28

def index_template
  haml_template("index")
end

#miniyard_versionObject



82
83
84
# File 'lib/miniyard.rb', line 82

def miniyard_version
  "v#{MiniYard::VERSION}"
end

#runObject



15
16
17
18
19
20
21
22
# File 'lib/miniyard.rb', line 15

def run
  $stdout.puts('Generating miniyard at '+@root)
  write_ci_url if @options[:ci]
  FileUtils.rm(index_file) if index_exists?
  output = haml_render(index_template)
  write_file(index_file, output)
  ensure_bootstrap_exists
end

#write_ci_urlObject



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

def write_ci_url
  File.open(ci_path, 'w'){|f| f.write(@options[:ci]) }
end

#write_file(file, contents) ⇒ Object



53
54
55
56
57
# File 'lib/miniyard.rb', line 53

def write_file(file, contents)
  File.open(file, 'w+') do |f|
    f.write(contents)
  end
end