Module: Emcee::Processors::Includes
- Included in:
- HtmlProcessor
- Defined in:
- lib/emcee/processors/processor_includes.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"> /^ *<link .*rel=["']import["'].*>$/- STYLESHEET_PATTERN =
Match a stylesheet link tag.
<link rel="stylesheet" href="assets/example.css"> /^ *<link .*rel=["']stylesheet["'].*>$/- SCRIPT_PATTERN =
Match a script tag.
<script src="assets/example.js"></script> /^ *<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\.\/-]+)["']/
Instance Method Summary collapse
-
#process_imports(body, context, directory) ⇒ Object
Scan the body for html imports.
-
#process_scripts(body, directory) ⇒ Object
Scan the body for external script references.
-
#process_stylesheets(body, directory) ⇒ Object
Scan the body for external stylesheet references.
-
#read_file(path) ⇒ Object
Return a file’s contents as text.
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 import.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/emcee/processors/processor_includes.rb', line 50 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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/emcee/processors/processor_includes.rb', line 81 def process_scripts(body, directory) to_inline = [] body.scan(SCRIPT_PATTERN) do |script_tag| if path = script_tag[SRC_PATH_PATTERN, :path] absolute_path = File.absolute_path(path, directory) script_contents = read_file(absolute_path) to_inline << [script_tag, "<script>" + script_contents + "</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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/emcee/processors/processor_includes.rb', line 63 def process_stylesheets(body, directory) to_inline = [] body.scan(STYLESHEET_PATTERN) do |stylesheet_tag| if path = stylesheet_tag[HREF_PATH_PATTERN, :path] absolute_path = File.absolute_path(path, directory) stylesheet_contents = read_file(absolute_path) to_inline << [stylesheet_tag, "<style>" + stylesheet_contents + "</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.
44 45 46 |
# File 'lib/emcee/processors/processor_includes.rb', line 44 def read_file(path) File.read(path) end |