Class: Roger::Extractor Deprecated

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

Overview

Deprecated.

Don’t use the extractor anymore, use release.use(:mockup, options) processor and

release.use(:url_relativizer, options) processor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, target_path, options = {}) ⇒ Extractor

Returns a new instance of Extractor.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :url_attributes (Array)

    The element attributes to parse and relativize

  • :url_relativize (Array)

    Wether or not we should relativize

  • :env (Array)

    ENV variable to pass to template renderer.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roger/extractor.rb', line 17

def initialize(project, target_path, options = {})
  @project = project
  @target_path = Pathname.new(target_path)
  @resolver = Resolver.new(self.target_path)

  @options = {
    url_attributes: %w(src href action),
    url_relativize: true,
    env: {}
  }

  @options.update(options) if options

  @options[:env].update("MOCKUP_PROJECT" => project)
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



8
9
10
# File 'lib/roger/extractor.rb', line 8

def project
  @project
end

#target_pathObject (readonly)

Returns the value of attribute target_path.



8
9
10
# File 'lib/roger/extractor.rb', line 8

def target_path
  @target_path
end

Instance Method Details

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

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



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/roger/extractor.rb', line 62

def extract_source_from_file(file_path, env = {})
  source = Roger::Template.open(
    file_path,
    partials_path: project.partial_path,
    layouts_path: project.layouts_path
  ).render(env.dup)

  source = relativize_urls(source, file_path) if @options[:url_relativize]

  source
end

#run!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/roger/extractor.rb', line 33

def run!
  target_path = self.target_path
  source_path = project.html_path

  filter = "**/*.html"
  fail(
    ArgumentError,
    "Target #{target_path} already exists, please choose a new directory to extract into"
  ) if target_path.exist?

  mkdir_p(target_path)
  target_path = target_path.realpath

  # Copy source to target first, we'll overwrite the templates later on.
  cp_r(source_path.children, target_path)

  Dir.chdir(source_path) do
    Dir.glob(filter).each do |file_path|
      self.run_on_file!(file_path, @options[:env])
    end
  end
end

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



56
57
58
59
# File 'lib/roger/extractor.rb', line 56

def run_on_file!(file_path, env = {})
  source = extract_source_from_file(file_path, env)
  File.open(target_path + file_path, "w") { |f| f.write(source) }
end