Class: Startback::Web::MagicAssets::NgHtmlTransformer
- Inherits:
-
Object
- Object
- Startback::Web::MagicAssets::NgHtmlTransformer
- Defined in:
- lib/startback/web/magic_assets/ng_html_transformer.rb
Overview
Plugin for MagicAssets that compiles .html angular templates in the assets structure to javascript files filling angular’s template cache.
Heavily inspired, yet over-simplified version, of angular-rails-templates See github.com/pitr/angular-rails-templates, licensed under MIT
Example:
use Startback::Web::MagicAssets, {
plugins: [Startback::Web::MagicAssets::NgHtmlTransfomer.new]
}
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :path => '/assets', :ng_module => 'templates', :mime_type => 'text/ng-html', :extensions => [".html"] }
- TPL =
<<-EOF angular.module("<%= ng_module %>").run(["$templateCache", function($templateCache) { $templateCache.put("<%= angular_template_name %>", <%= html %>) }]); EOF
- JS_ESCAPE_MAP =
inspired by Rails’ action_view/helpers/javascript_helper.rb
{ '\\' => '\\\\', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(input) ⇒ Object
-
#escape_javascript(raw) ⇒ Object
We want to deliver the shortist valid javascript escaped string Count the number of “ vs ‘ If more ’, escape ” If more “, escape ‘ If equal, prefer to escape ”.
-
#initialize(options = {}) ⇒ NgHtmlTransformer
constructor
A new instance of NgHtmlTransformer.
- #install(sprockets) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ NgHtmlTransformer
Returns a new instance of NgHtmlTransformer.
26 27 28 |
# File 'lib/startback/web/magic_assets/ng_html_transformer.rb', line 26 def initialize( = {}) @options = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/startback/web/magic_assets/ng_html_transformer.rb', line 29 def @options end |
Instance Method Details
#call(input) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/startback/web/magic_assets/ng_html_transformer.rb', line 68 def call(input) file_path = input[:filename] angular_template_name = "#{[:path]}/#{input[:name]}.html" source_file = file_path ng_module = [:ng_module] html = escape_javascript(input[:data].chomp) ERB.new(TPL).result(binding) end |
#escape_javascript(raw) ⇒ Object
We want to deliver the shortist valid javascript escaped string Count the number of “ vs ‘ If more ’, escape ” If more “, escape ‘ If equal, prefer to escape ”
58 59 60 61 62 63 64 65 66 |
# File 'lib/startback/web/magic_assets/ng_html_transformer.rb', line 58 def escape_javascript(raw) if raw quote = raw.count(%{'}) >= raw.count(%{"}) ? %{"} : %{'} escaped = raw.gsub(/(\\|\r\n|[\n\r#{quote}])/u) {|match| JS_ESCAPE_MAP[match] } "#{quote}#{escaped}#{quote}" else '""' end end |
#install(sprockets) ⇒ Object
31 32 33 34 |
# File 'lib/startback/web/magic_assets/ng_html_transformer.rb', line 31 def install(sprockets) sprockets.register_mime_type [:mime_type], extensions: [:extensions] sprockets.register_transformer [:mime_type], 'application/javascript', self end |