Sha256: ccb4276cc542e94d8887d8e4ff3a29f00c6302aec1a37cff1cc795cb019574bf

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'fileutils'
require 'haml'

module Marksman
  class Converter

    attr_accessor :presentation, :theme

    def initialize(presentation)
      @presentation = presentation
    end

    def write(directory)
      puts 'Generating HTML...'
      FileUtils.mkdir_p(directory)
      Dir.glob(@presentation.theme.path.join('**/*')).each do |file|
        if File.file? file
          copy_file(Pathname.new(file), directory)
        end
      end
    end

    private

    def copy_file(source, target_dir)
      target = target_name(source, target_dir)
      FileUtils.mkdir(target.dirname) unless File.exist? target.dirname
      if (source.extname == '.haml')
        convert_haml(source, target.to_s.gsub(/\.haml$/, '.html'))
      else
        FileUtils.copy(source, target)
      end
    end

    def target_name(file, target_dir)
      base_file = file.to_s[(@presentation.theme.path.to_s.length + 1)..-1]
      target_file = Pathname.new(target_dir).join(base_file)
    end

    def convert_haml(file, output)
      template = File.read(file)
      haml_engine = Haml::Engine.new(template)
      html = haml_engine.render(Object.new, {
        presentation: @presentation
      })
      File.write(output, html)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
marksman-0.1 lib/marksman/converter.rb