Module: Emcee::Processors::Includes

Included in:
HtmlProcessor
Defined in:
lib/emcee/processors/processor_base.rb,
lib/emcee/processors/processor_imports.rb,
lib/emcee/processors/processor_scripts.rb,
lib/emcee/processors/processor_stylesheets.rb

Overview

This module has method definition that we will include into HtmlProcessor.

We seperate these out to make them easier to test. Testing a class that inherits from Sprockets::DirectiveProcessor (which our HtmlProcessor does) is not straightforward.

Constant Summary collapse

IMPORT_PATTERN =

Match an html import tag.

<link rel="import" href="assets/example.html">
/^\s*<link .*rel=["']import["'].*>$/
STYLESHEET_PATTERN =

Match a stylesheet link tag.

<link rel="stylesheet" href="assets/example.css">
/^\s*<link .*rel=["']stylesheet["'].*>$/
SCRIPT_PATTERN =

Match a script tag.

<script src="assets/example.js"></script>
/^\s*<script .*src=["'].+\.js["']><\/script>$/
HREF_PATH_PATTERN =

Match the path from the href attribute of an html import or stylesheet include tag. Captures the actual path.

href="/assets/example.css"
/href=["'](?<path>[\w\.\/-]+)["']/
SRC_PATH_PATTERN =

Match the source path from a script tag. Captures the actual path.

src="/assets/example.js"
/src=["'](?<path>[\w\.\/-]+)["']/
INDENT_PATTERN =

Match the indentation whitespace of a line

/^(?<indent>\s*)/

Instance Method Summary collapse

Instance Method Details

#process_imports(body, context, directory) ⇒ Object

Scan the body for html imports. If any are found, tell sprockets to require their files like we would for a directive. Then remove the imports and return the new body.



7
8
9
10
11
12
13
14
15
16
# File 'lib/emcee/processors/processor_imports.rb', line 7

def process_imports(body, context, directory)
  body.scan(IMPORT_PATTERN) do |import_tag|
    if path = import_tag[HREF_PATH_PATTERN, :path]
      absolute_path = File.absolute_path(path, directory)
      context.require_asset(absolute_path)
    end
  end

  body.gsub(IMPORT_PATTERN, "")
end

#process_scripts(body, directory) ⇒ Object

Scan the body for external script references. If any are found, inline the files in place of the references and return the new body.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/emcee/processors/processor_scripts.rb', line 6

def process_scripts(body, directory)
  to_inline = []

  body.scan(SCRIPT_PATTERN) do |script_tag|
    if path = script_tag[SRC_PATH_PATTERN, :path]

      indent = script_tag[INDENT_PATTERN, :indent] || ""

      absolute_path = File.absolute_path(path, directory)
      script_contents = read_file(absolute_path)

      to_inline << [script_tag, "#{indent}<script>#{script_contents}\n#{indent}</script>"]
    end
  end

  to_inline.reduce(body) do |output, (tag, contents)|
    output.gsub(tag, contents)
  end
end

#process_stylesheets(body, directory) ⇒ Object

Scan the body for external stylesheet references. If any are found, inline the files in place of the references and return the new body.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/emcee/processors/processor_stylesheets.rb', line 6

def process_stylesheets(body, directory)
  to_inline = []

  body.scan(STYLESHEET_PATTERN) do |stylesheet_tag|
    if path = stylesheet_tag[HREF_PATH_PATTERN, :path]

      indent = stylesheet_tag[INDENT_PATTERN, :indent] || ""

      absolute_path = File.absolute_path(path, directory)
      stylesheet_contents = read_file(absolute_path)

      to_inline << [stylesheet_tag, "#{indent}<style>#{stylesheet_contents}\n#{indent}</style>"]
    end
  end

  to_inline.reduce(body) do |output, (tag, contents)|
    output.gsub(tag, contents)
  end
end

#read_file(path) ⇒ Object

Return a file’s contents as text. This is split out as a method so that we can test the other methods in this module without actually reading from the filesystem.



52
53
54
# File 'lib/emcee/processors/processor_base.rb', line 52

def read_file(path)
  File.read(path)
end