Module: Stencil

Defined in:
lib/stencil.rb,
lib/stencil/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.emails(folder) ⇒ Object



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

def self.emails folder
  self.liquid folder, 'emails/', 'build/email-'
end

.inline(folder) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/stencil.rb', line 88

def self.inline folder
  begin
    Liquid::Template.file_system = Liquid::LocalFileSystem.new(folder)
    Dir.glob(folder + '*.html') do |path|
      begin
        next unless path.include?('email-').inspect
        premailer = Premailer.new(path, warn_level: Premailer::Warnings::SAFE)
        File.open(path, 'w') { |file| file.write(premailer.to_inline_css) }

        # Output any CSS warnings
        premailer.warnings.each do |w|
          puts "#{w[:message]} (#{w[:level]}) may not render properly in #{w[:clients]}"
        end
      rescue => e
        puts "Inline error: #{e}"
      end
    end
    "Inline rendered #{folder}"
  rescue => e
    "Inline error: #{e}"
  end
end

.javascript(folder) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stencil.rb', line 14

def self.javascript folder
  begin
    contents = ""
    folders = [folder + 'lib/', folder]
    folders.each do |f|
      Dir.glob(f + '*.js') do |path|
        begin
          contents += File.open(path).read
        rescue => e
          puts "JavaScript error: #{e}"
        end
      end
    end

    uglified = Uglifier.new.compile contents
    output_filename = folder.gsub('assets/javascripts', 'build/') + 'application.js'
    File.open(output_filename, 'w') { |file| file.write(uglified) }

    "JavaScript rendered #{folder}"
  rescue => e
    "JavaScript error: #{e}"
  end
end

.liquid(folder, source, destination) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/stencil.rb', line 66

def self.liquid folder, source, destination
  begin
    Liquid::Template.file_system = Liquid::LocalFileSystem.new(folder)
    Dir.glob(folder + '*.liquid') do |path|
      next if path =~ /\_/ # do not render partials
      begin
        layout_file = File.open(folder + '_layout.liquid').read
        layout = Liquid::Template.parse(layout_file)
        file = File.open(path).read
        liquid = Liquid::Template.parse(file)
        output_filename = path.gsub('liquid', 'html').gsub(source, destination)
        File.open(output_filename, 'w') { |file| file.write(layout.render('content_for_layout' => liquid.render)) }
      rescue => e
        puts "Liquid error: #{e}"
      end
    end
    "Liquid rendered #{folder}"
  rescue => e
    "Liquid error: #{e}"
  end
end

.sass(folder) ⇒ Object



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

def self.sass folder
  begin
    Dir.glob(folder + '*.scss') do |path|
      next if path =~ /\_/ # do not render partials
      begin
        file = File.open(path).read
        engine = SassC::Engine.new(file, { style: :compressed, load_paths: ['assets/stylesheets/'] })
        prefixed = AutoprefixerRails.process(engine.render, browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1'])
        output_filename = path.gsub('scss', 'css').gsub('assets/stylesheets', 'build')
        File.open(output_filename, 'w') { |file| file.write(prefixed.css) }
      rescue => e
        puts "Sass error: #{e}"
      end
    end
    "Sass rendered #{folder}"
  rescue => e
    "Sass error: #{e}"
  end
end

.templates(folder) ⇒ Object



62
63
64
# File 'lib/stencil.rb', line 62

def self.templates folder
  self.liquid folder, 'templates', 'build'
end