Class: Roger::Release::Processors::Mockup

Inherits:
Base
  • Object
show all
Defined in:
lib/roger/release/processors/mockup.rb

Overview

The Mockup processor that will process all templates

Constant Summary collapse

MIME_TYPES_TO_EXTENSION =
{
  "text/html" => "html",
  "text/css"  => "css",
  "application/javascript" => "js",
  "text/xml" => "xml",
  "application/xml" => "xml",
  "text/csv" => "csv",
  "application/json" => "json"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mockup

Returns a new instance of Mockup.



18
19
20
21
22
23
24
25
26
# File 'lib/roger/release/processors/mockup.rb', line 18

def initialize(options = {})
  @options = {
    env: {},
    match: ["**/*.{html,md,html.erb}"],
    skip: [/\Astylesheets/, /\Ajavascripts/]
  }

  @options.update(options) if options
end

Instance Attribute Details

#projectObject

Returns the value of attribute project.



6
7
8
# File 'lib/roger/release/processors/mockup.rb', line 6

def project
  @project
end

Instance Method Details

#call(release, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/roger/release/processors/mockup.rb', line 28

def call(release, options = {})
  self.project = release.project

  options = update_call_options(options)

  log_call(release, options)

  release.get_files(options[:match], options[:skip]).each do |file_path|
    release.log(self, "    Extract: #{file_path}", true)

    # Avoid rendering partials which can also be included
    # in the roger.base_path
    next if File.basename(file_path).start_with? "_"

    self.run_on_file!(file_path, options[:env])
  end
end

#run_on_file(file_path, env = {}) ⇒ Object

Runs the template on a single file and return processed source.



59
60
61
62
63
64
65
66
# File 'lib/roger/release/processors/mockup.rb', line 59

def run_on_file(file_path, env = {})
  renderer = Roger::Renderer.new(
    env.dup,
    partials_path: project.partial_path,
    layouts_path: project.layouts_path
  )
  renderer.render(file_path)
end

#run_on_file!(file_path, env = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/roger/release/processors/mockup.rb', line 46

def run_on_file!(file_path, env = {})
  output = run_on_file(file_path, env)

  # Clean up source file
  FileUtils.rm(file_path)

  # Write out new file
  File.open(target_path(file_path).to_s, "w") do |f|
    f.write(output)
  end
end

#target_path(path) ⇒ Pathname

Determines the output path for a mockup path with a certain template

Returns:

  • (Pathname)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/roger/release/processors/mockup.rb', line 71

def target_path(path)
  parts = File.basename(path.to_s).split(".")
  path = path.to_s

  # Always return .html directly as it will cause too much trouble otherwise
  return Pathname.new(path) if parts.last == "html"

  target_ext = Roger::Renderer.target_extension_for(path)
  source_ext = Roger::Renderer.source_extension_for(path)

  # If there is no target extension
  return Pathname.new(path) if target_ext.empty?

  # If we have at least one extension
  if parts.size > 1
    source_ext_regexp = /#{Regexp.escape(source_ext)}\Z/
    Pathname.new(path.gsub(source_ext_regexp, target_ext))
  else
    Pathname.new(path + "." + target_ext)
  end
end