Module: Emcee::Processors::Includes
- Included in:
- HtmlProcessor
- Defined in:
- lib/emcee/processors/processor_includes.rb
Overview
This module defines methods that we will include into HtmlProcessor.
Testing a class that inherits from Sprockets::DirectiveProcessor, which HtmlProcessor does, is difficult. Seperating out these methods makes them easy to unit test.
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
-
#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 imports and return the new body.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/emcee/processors/processor_includes.rb', line 55 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/emcee/processors/processor_includes.rb', line 68 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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/emcee/processors/processor_includes.rb', line 90 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.
48 49 50 |
# File 'lib/emcee/processors/processor_includes.rb', line 48 def read_file(path) File.read(path) end |