Class: BlastWolf

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

Instance Method Summary collapse

Constructor Details

#initialize(user_config = {}) ⇒ BlastWolf

Returns a new instance of BlastWolf.



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

def initialize( user_config={} )

  @config = {
    :template_dir => 'blastwolf/templates',
    :default_scss => ['html.syntax', '_colours', '_layout'],
    :build_folder => File.join(PROJECT_ROOT, '_build'),
    :patterns_folder => File.join(PROJECT_ROOT, 'examples'),
    :basepath => ''
  }
  
  @config.merge!( user_config )

  setup_build_folders( @config[:build_folder] )
end

Instance Method Details

#build_artifact(type) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/blastwolf.rb', line 85

def build_artifact( type )
  contents = ''

  Dir.glob(examples_path type) do |file|
    contents << File.read( file )
    contents << "\n\t/* -------------------------- */\n"
  end

  contents
end

#build_global_scssObject



114
115
116
117
118
119
120
121
122
# File 'lib/blastwolf.rb', line 114

def build_global_scss
  includes = @config[:default_scss]
  includes << "patterns"
  file_contents = includes.map { |inc|
    "@import '#{inc}';\n"
    }.join()

  write_file( :global_scss, file_contents )
end

#build_image_dirObject



69
70
71
72
73
74
75
# File 'lib/blastwolf.rb', line 69

def build_image_dir
  img_dir = File.join(@config[:build_folder], 'img')
  Dir.glob(File.join(examples_path, "*.{jpg,png,gif}")) do |file|
    puts file
    FileUtils.cp file, img_dir
  end
end

#build_jsObject



96
97
98
99
100
101
102
# File 'lib/blastwolf.rb', line 96

def build_js
  js = "$(document).ready(function() {\n\n"
  js << build_artifact(:js)
  js << "});"

  write_file( :js, js )
end

#build_patterns_artifactsObject



63
64
65
66
67
# File 'lib/blastwolf.rb', line 63

def build_patterns_artifacts
  write_file( :scss, build_artifact( :scss ) )
  build_js
  build_image_dir
end

#compile_cssObject



104
105
106
107
108
109
110
111
112
# File 'lib/blastwolf.rb', line 104

def compile_css
  scss_path = File.join(@config[:build_folder], 'scss')
  file = File.join(scss_path, 'global.scss')
  template = File.read(file)
  sass_engine = Sass::Engine.new(template, { :load_paths => [scss_path], :syntax => :scss,})
  output = sass_engine.render

  write_file( :css, output )
end

#copy_folders(folders, src, dest) ⇒ Object



40
41
42
43
44
# File 'lib/blastwolf.rb', line 40

def copy_folders( folders, src, dest )
  folders.each do |f|
    FileUtils.cp_r( File.join(src, f), dest )
  end
end

#copy_markup(src, dest) ⇒ Object



34
35
36
37
38
# File 'lib/blastwolf.rb', line 34

def copy_markup(src, dest)
  Dir.glob(File.join(src, '*.html')).each do |file|
    FileUtils.cp file, dest
  end
end

#do_it_allObject



27
28
29
30
31
32
# File 'lib/blastwolf.rb', line 27

def do_it_all
  generate_homepage
  generate_doc_pages
  build_patterns_artifacts
  build_global_scss
end

#examples_path(filetype = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/blastwolf.rb', line 191

def examples_path( filetype=nil )
  path = ''
  if [:html, :scss, :js].include? filetype
    path = File.join(@config[:patterns_folder], '*', '*.' + filetype.to_s)
  else
    path = File.join(@config[:patterns_folder], '*')
  end
  path
end

#generate_doc_pagesObject



77
78
79
80
81
82
83
# File 'lib/blastwolf.rb', line 77

def generate_doc_pages
  get_patterns.each do |pattern|
    doc = render( 'doc_page', pattern['title'], pattern )
    filename = File.join(@config[:build_folder], pattern['filename'])
    File.open(filename, "w") { |file| file.write(doc) }
  end
end

#generate_homepageObject



56
57
58
59
60
61
# File 'lib/blastwolf.rb', line 56

def generate_homepage
  title = "Index of examples"
  patterns = get_patterns
  html = render( 'index', title, patterns )
  write_file( :home, html )
end

#get_filename(absolute_file_path) ⇒ Object



186
187
188
# File 'lib/blastwolf.rb', line 186

def get_filename( absolute_file_path )
  Pathname.new( absolute_file_path ).basename.to_s
end

#get_patternsObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/blastwolf.rb', line 125

def get_patterns
  patterns = []
  Dir.glob(examples_path :html ).each do |file|
    patterns << {
      'title' => get_title( file ),
      'filename' => get_filename( file ),
      'contents' => File.read( file )
    }
  end
  patterns
end

#get_template(filename) ⇒ Object



152
153
154
155
156
# File 'lib/blastwolf.rb', line 152

def get_template( filename )
  template_filename = filename + '.liquid'
  template_dir = File.join(File.dirname(__FILE__), 'blastwolf', 'templates')
  File.read(File.join(template_dir, template_filename))
end

#get_title(file) ⇒ Object



181
182
183
# File 'lib/blastwolf.rb', line 181

def get_title( file )
  get_filename( file ).gsub(/\..*$/, '').gsub(/_(.)/) {|s| " #{s.upcase}" }
end

#render(template_file, page_title, vars) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/blastwolf.rb', line 137

def render( template_file, page_title, vars )

  contents = Liquid::Template.parse( get_template( template_file ) )
  layout = Liquid::Template.parse( get_template('layout') )


  vars = { 'patterns' => vars } if vars.kind_of?(Array)
  vars[:basepath] = @config[:basepath]

  rendered_page = layout.render(
    'title' => page_title, 
    'content' => contents.render( vars )
    )
end

#setup_build_folders(target_folder) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/blastwolf.rb', line 46

def setup_build_folders( target_folder )
  # FileUtils.rm_r target_folder unless File.directory?( target_folder )
  FileUtils.mkdir(target_folder) unless File.directory?( target_folder )

  ['css', 'js', 'scss', 'img'].each do |dir|
    subdir = File.join(target_folder, dir)
    FileUtils.mkdir( subdir ) unless File.directory?( subdir )
  end
end

#write_file(file_descriptor, contents) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/blastwolf.rb', line 158

def write_file( file_descriptor, contents )
  case file_descriptor
  when :scss
    filename = 'scss/patterns.scss'
  when :js
    filename = 'js/patterns.js'
  when :home
    filename = 'index.html'
  when :global_scss
    filename = 'scss/global.scss'
  when :css
    filename = 'css/global.css'
  else
    raise "I've no idea what file you want me to write"
  end

  filename = File.join(@config[:build_folder], filename)
  puts filename

  File.open(filename, 'w') { |file| file.write(contents) }
end