class Startback::Web::MagicAssets::NgHtmlTransformer
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] }
Constants
- DEFAULT_OPTIONS
- JS_ESCAPE_MAP
inspired by Rails' action_view/helpers/javascript_helper.rb
- TPL
Attributes
options[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/startback/web/magic_assets/ng_html_transformer.rb, line 26 def initialize(options = {}) @options = DEFAULT_OPTIONS.merge(options) end
Public Instance Methods
call(input)
click to toggle source
# File lib/startback/web/magic_assets/ng_html_transformer.rb, line 68 def call(input) file_path = input[:filename] angular_template_name = "#{options[:path]}/#{input[:name]}.html" source_file = file_path ng_module = options[:ng_module] html = escape_javascript(input[:data].chomp) ERB.new(TPL).result(binding) end
escape_javascript(raw)
click to toggle source
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 ”
# 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)
click to toggle source
# File lib/startback/web/magic_assets/ng_html_transformer.rb, line 31 def install(sprockets) sprockets.register_mime_type options[:mime_type], extensions: options[:extensions] sprockets.register_transformer options[:mime_type], 'application/javascript', self end