Class: Deface::DSL::Loader
- Inherits:
-
Object
- Object
- Deface::DSL::Loader
- Defined in:
- lib/deface/dsl/loader.rb
Class Method Summary collapse
- .extract_dsl_commands_from_erb(html_file_contents) ⇒ Object
- .extract_dsl_commands_from_haml(file_contents) ⇒ Object (also: extract_dsl_commands_from_slim)
- .load(filename, options = nil, &block) ⇒ Object
- .register ⇒ Object
Class Method Details
.extract_dsl_commands_from_erb(html_file_contents) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/deface/dsl/loader.rb', line 62 def self.extract_dsl_commands_from_erb(html_file_contents) dsl_commands = '' while starts_with_html_comment?(html_file_contents) first_open_comment_index = html_file_contents.lstrip.index('<!--') first_close_comment_index = html_file_contents.index('-->') unless first_close_comment_index.nil? comment = html_file_contents[first_open_comment_index..first_close_comment_index+2] end comment.gsub('<!--', '').gsub('-->', '').strip.scan(/[^\s"']+|"[^"]*"|'[^']*'/).each do |part| dsl_commands =~ /('|")\z/ || part =~ /\A[^\d:='"%]/ ? dsl_commands << "\n" : dsl_commands << ' ' dsl_commands << part end html_file_contents = html_file_contents.gsub(comment, '') end [dsl_commands, html_file_contents] end |
.extract_dsl_commands_from_haml(file_contents) ⇒ Object Also known as: extract_dsl_commands_from_slim
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/deface/dsl/loader.rb', line 85 def self.extract_dsl_commands_from_haml(file_contents) dsl_commands = '' while starts_with_haml_comment?(file_contents) first_open_comment_index = file_contents.lstrip.index('/') first_close_comment_index = file_contents.index("\n") unless first_close_comment_index.nil? comment = file_contents[first_open_comment_index..first_close_comment_index] end dsl_commands << comment.gsub('/', '').strip + "\n" file_contents = file_contents.gsub(comment, '') while file_contents.start_with?(' ') first_newline_index = file_contents.index("\n") comment = file_contents[0..first_newline_index] dsl_commands << comment.gsub('/', '').strip + "\n" file_contents = file_contents.gsub(comment, '') end end [dsl_commands, file_contents] end |
.load(filename, options = nil, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/deface/dsl/loader.rb', line 8 def self.load(filename, = nil, &block) unless File.basename(filename) =~ /^[^\.]+(.html.(erb|haml|slim)){0,1}.deface$/ raise "Deface::DSL does not know how to read '#{filename}'. Override files should end with just .deface, .html.erb.deface, .html.haml.deface or .html.slim.deface" end unless file_in_dir_below_overrides?(filename) raise "Deface::DSL overrides must be in a sub-directory that matches the views virtual path. Move '#{filename}' into a sub-directory." end File.open(filename) do |file| context_name = File.basename(filename).gsub('.deface', '') file_contents = file.read if context_name.end_with?('.html.erb') dsl_commands, the_rest = extract_dsl_commands_from_erb(file_contents) context_name = context_name.gsub('.html.erb', '') context = Context.new(context_name) context.virtual_path(determine_virtual_path(filename)) context.instance_eval(dsl_commands) context.erb(the_rest) context.create_override elsif context_name.end_with?('.html.haml') dsl_commands, the_rest = extract_dsl_commands_from_haml(file_contents) context_name = context_name.gsub('.html.haml', '') context = Context.new(context_name) context.virtual_path(determine_virtual_path(filename)) context.instance_eval(dsl_commands) context.haml(the_rest) context.create_override elsif context_name.end_with?('.html.slim') dsl_commands, the_rest = extract_dsl_commands_from_slim(file_contents) context_name = context_name.gsub('.html.slim', '') context = Context.new(context_name) context.virtual_path(determine_virtual_path(filename)) context.instance_eval(dsl_commands) context.slim(the_rest) context.create_override else context = Context.new(context_name) context.virtual_path(determine_virtual_path(filename)) context.instance_eval(file_contents) context.create_override end end end |