Class: Dryml::Template
- Inherits:
-
Object
- Object
- Dryml::Template
- Defined in:
- lib/dryml/template.rb
Constant Summary collapse
- DRYML_NAME =
"[a-zA-Z\-][a-zA-Z0-9\-]*"
- DRYML_NAME_RX =
/^#{DRYML_NAME}$/
- RUBY_NAME =
"[a-zA-Z_][a-zA-Z0-9_]*"
- RUBY_NAME_RX =
/^#{RUBY_NAME}$/
- CODE_ATTRIBUTE_CHAR =
"&"
- NO_METADATA_TAGS =
%w(doctype if else unless repeat do with name type-name)
- SPECIAL_ATTRIBUTES =
%w(param merge merge-params merge-attrs for-type if unless repeat part part-locals restore)
- VALID_PARAMETER_TAG_ATTRIBUTES =
%w(param replace)
Class Attribute Summary collapse
-
.build_cache ⇒ Object
readonly
Returns the value of attribute build_cache.
Instance Attribute Summary collapse
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#template_path ⇒ Object
readonly
Returns the value of attribute template_path.
Class Method Summary collapse
- .clear_build_cache ⇒ Object
-
.descendent_select(el) ⇒ Object
Using REXML::XPath is slow.
- .descendents(el, &block) ⇒ Object
Instance Method Summary collapse
- #after_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
- #append_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
- #apply_control_attributes(expression, el) ⇒ Object
- #attribute_to_ruby(*args) ⇒ Object
- #before_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
- #call_name(el) ⇒ Object
- #call_to_self_from_type_specific_def?(el) ⇒ Boolean
- #children_to_erb(nodes) ⇒ Object
- #compile(local_names = [], auto_taglibs = []) ⇒ Object
- #compile_merge_attrs(el) ⇒ Object
- #contains_param?(el) ⇒ Boolean
- #create_render_page_method ⇒ Object
- #current_def_name ⇒ Object
- #declared_attributes(def_element) ⇒ Object
- #def_element(el, extend_tag = false) ⇒ Object
- #default_param_proc(el, containing_param_name = nil) ⇒ Object
- #define_polymorphic_dispatcher(el, name) ⇒ Object
- #delegated_part_element(el, content) ⇒ Object
- #dryml_exception(message, el = nil) ⇒ Object
- #element_line_num(el) ⇒ Object
- #element_to_erb(el) ⇒ Object
- #extend_element(el) ⇒ Object
- #field_shorthand_element?(el) ⇒ Boolean
- #find_ancestor(el) ⇒ Object
- #gensym(name = "__tmp") ⇒ Object
- #get_param_name(el) ⇒ Object
- #import_module(mod, as = nil) ⇒ Object
- #include_element(el) ⇒ Object
-
#initialize(src, environment, template_path) ⇒ Template
constructor
A new instance of Template.
- #inside_def_for_type? ⇒ Boolean
- #is_code_attribute?(attr_value) ⇒ Boolean
- #is_taglib? ⇒ Boolean
- #logger ⇒ Object
- #maybe_make_part_call(el, call) ⇒ Object
- #merge_attribute(el) ⇒ Object
- #node_to_erb(node) ⇒ Object
- #old_tag_call?(el) ⇒ Boolean
- #param_content_element(name_or_el) ⇒ Object
- #param_content_local_name(name) ⇒ Object
- #param_names_in_definition(el) ⇒ Object
- #param_proc(el, metadata_name_suffix) ⇒ Object
- #param_restore_local_name(name) ⇒ Object
- #parameter_tag_hash_item(el, metadata_name) ⇒ Object
- #parameter_tags_hash(el, containing_tag_name = nil) ⇒ Object
- #part_delegate_tag_name(el) ⇒ Object
- #part_element(el, content) ⇒ Object
- #polymorphic_call_type(el) ⇒ Object
- #prepend_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
- #process_src ⇒ Object
- #promote_static_tag_to_method_call?(el) ⇒ Boolean
- #replace_parameter_proc(el, metadata_name, content = nil) ⇒ Object
- #require_attribute(el, name, rx = nil, optional = false) ⇒ Object
- #require_toplevel(el, message = nil) ⇒ Object
- #restore_erb_scriptlets(src) ⇒ Object
- #ruby_name(dryml_name) ⇒ Object
- #set_element(el) ⇒ Object
- #set_scoped_element(el) ⇒ Object
- #simple_part_element(el, content) ⇒ Object
- #static_element_to_erb(el) ⇒ Object
- #static_tag_to_method_call(el) ⇒ Object
- #tag_attributes(el) ⇒ Object
- #tag_call(el) ⇒ Object
- #tag_method(name, el, extend_tag = false) ⇒ Object
- #tag_method_body(el) ⇒ Object
- #tag_newlines(el) ⇒ Object
- #text_with_scriplets_to_erb(s) ⇒ Object
- #type_specific_suffix ⇒ Object
- #with_containing_tag_name(el) ⇒ Object
- #without_parameters(el) ⇒ Object
- #wrap_replace_parameter(el, name) ⇒ Object
-
#wrap_source_with_metadata(content, kind, name, *args) ⇒ Object
this function is now basically just a hook for DrymlFireMarker.
- #wrap_tag_call_with_metadata(el, content) ⇒ Object
- #wrap_tag_method_body_with_metadata(content) ⇒ Object
Constructor Details
#initialize(src, environment, template_path) ⇒ Template
Returns a new instance of Template.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dryml/template.rb', line 37 def initialize(src, environment, template_path) @src = src @environment = environment # a class or a module @template_path = template_path @template_path = @template_path.sub(%r(^#{Regexp.escape(Rails.root.to_s)}/), "") if Object.const_defined?(:Rails) && Rails.root @builder = Template.build_cache[@template_path] || DRYMLBuilder.new(self) @builder.set_environment(environment) @last_element = nil end |
Class Attribute Details
.build_cache ⇒ Object (readonly)
Returns the value of attribute build_cache.
30 31 32 |
# File 'lib/dryml/template.rb', line 30 def build_cache @build_cache end |
Instance Attribute Details
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
49 50 51 |
# File 'lib/dryml/template.rb', line 49 def @tags end |
#template_path ⇒ Object (readonly)
Returns the value of attribute template_path.
49 50 51 |
# File 'lib/dryml/template.rb', line 49 def template_path @template_path end |
Class Method Details
.clear_build_cache ⇒ Object
32 33 34 |
# File 'lib/dryml/template.rb', line 32 def clear_build_cache @build_cache.clear() end |
.descendent_select(el) ⇒ Object
Using REXML::XPath is slow
352 353 354 355 356 357 358 |
# File 'lib/dryml/template.rb', line 352 def self.descendent_select(el) result = [] descendents(el) { |desc| result << desc if yield(desc) } result end |
.descendents(el, &block) ⇒ Object
343 344 345 346 347 348 349 |
# File 'lib/dryml/template.rb', line 343 def self.descendents(el,&block) return if el.elements.empty? el.elements.each do |child| block.call(child) descendents(child,&block) end end |
Instance Method Details
#after_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
715 716 717 718 719 720 |
# File 'lib/dryml/template.rb', line 715 def after_parameter_tag_hash_item(name, el, ) param_name = get_param_name(el) dryml_exception("param declaration not allowed on 'after' parameters", el) if param_name content = "<% concat(#{param_restore_local_name(name)}.call({}, {})) %>" + children_to_erb(el) ":#{ruby_name name}_replacement => #{replace_parameter_proc(el, , content)}" end |
#append_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
723 724 725 726 727 |
# File 'lib/dryml/template.rb', line 723 def append_parameter_tag_hash_item(name, el, ) ":#{ruby_name name} => proc { [{}, { :default => proc { |#{param_content_local_name(name)}| new_context { %>" + param_content_element(name) + children_to_erb(el) + "<% ; output_buffer } } } ] }" end |
#apply_control_attributes(expression, el) ⇒ Object
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 |
# File 'lib/dryml/template.rb', line 910 def apply_control_attributes(expression, el) controls = %w(if unless repeat).map_hash { |x| el.attributes[x] }.compact dryml_exception("You can't have multiple control attributes on the same element", el) if controls.length > 1 attr = controls.keys.first val = controls.values.first if val.nil? expression else control = if !el.attribute(attr).has_rhs? "this" elsif is_code_attribute?(val) "#{val[1..-1]}" else val.gsub!('-', '_') attr == "repeat" ? %("#{val}") : "this.#{val}" end x = gensym case attr when "if" "(if !(#{control}).blank?; (#{x} = #{expression}; Dryml.last_if = true; #{x}) " + "else (Dryml.last_if = false; ''); end)" when "unless" "(if (#{control}).blank?; (#{x} = #{expression}; Dryml.last_if = true; #{x}) " + "else (Dryml.last_if = false; ''); end)" when "repeat" "repeat_attribute(#{control}) { #{expression} }" end end end |
#attribute_to_ruby(*args) ⇒ Object
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 |
# File 'lib/dryml/template.rb', line 945 def attribute_to_ruby(*args) = args. attr, el = args dryml_exception('erb scriptlet not allowed in this attribute (use #{ ... } instead)', el) if attr.is_a?(String) && attr.index("[![HOBO-ERB") if [:symbolize] && attr =~ /^[a-zA-Z_][^a-zA-Z0-9_]*[\?!]?/ ":#{attr}" else res = if attr.nil? "nil" elsif is_code_attribute?(attr) "(#{attr[1..-1]})" else if attr !~ /"/ if attr =~ /\#\{/ '"' + attr + '"' else '"' + attr + '"' end elsif attr !~ /'/ "'#{attr}'" else dryml_exception("invalid quote(s) in attribute value") end end [:symbolize] ? (res + ".to_sym") : res end end |
#before_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
707 708 709 710 711 712 |
# File 'lib/dryml/template.rb', line 707 def before_parameter_tag_hash_item(name, el, ) param_name = get_param_name(el) dryml_exception("param declaration not allowed on 'before' parameters", el) if param_name content = children_to_erb(el) + "<% concat(#{param_restore_local_name(name)}.call({}, {})) %>" ":#{ruby_name name}_replacement => #{replace_parameter_proc(el, , content)}" end |
#call_name(el) ⇒ Object
521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/dryml/template.rb', line 521 def call_name(el) dryml_exception("invalid tag name (remember to use '-' rather than '_')", el) unless el.dryml_name =~ /^#{DRYML_NAME}(\.#{DRYML_NAME})*$/ name = Dryml.unreserve(ruby_name(el.dryml_name)) if call_to_self_from_type_specific_def?(el) "#{name}__base" elsif old_tag_call?(el) name = name[4..-1] # remove 'old-' prefix name += type_specific_suffix if inside_def_for_type? "#{name}_without_#{@extend_key}" else name end end |
#call_to_self_from_type_specific_def?(el) ⇒ Boolean
542 543 544 |
# File 'lib/dryml/template.rb', line 542 def call_to_self_from_type_specific_def?(el) inside_def_for_type? && el.dryml_name == current_def_name &&!el.attributes['for-type'] end |
#children_to_erb(nodes) ⇒ Object
105 106 107 |
# File 'lib/dryml/template.rb', line 105 def children_to_erb(nodes) nodes.map { |x| node_to_erb(x) }.join end |
#compile(local_names = [], auto_taglibs = []) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dryml/template.rb', line 51 def compile(local_names=[], auto_taglibs=[]) now = Time.now unless @template_path.blank? p = Pathname.new template_path p = Pathname.new(Rails.root) + p unless p.absolute? || !Object.const_defined?(:Rails) || Rails.root.nil? mtime = p.mtime rescue Time.now if !@builder.ready?(mtime) @builder.start parsed = true # parse the DRYML file creating a list of build instructions if is_taglib? process_src else create_render_page_method end # store build instructions in the cache Template.build_cache[@template_path] = @builder end end # compile the build instructions @builder.build(local_names, auto_taglibs, mtime) logger.try.info(" DRYML: Compiled #{template_path} in #{'%.2fs' % (Time.now - now)}") if parsed end |
#compile_merge_attrs(el) ⇒ Object
839 840 841 842 843 844 845 846 847 848 849 |
# File 'lib/dryml/template.rb', line 839 def compile_merge_attrs(el) merge_attrs = el.attributes['merge-attrs'] if merge_attrs == "&true" "attributes" elsif is_code_attribute?(merge_attrs) "(#{merge_attrs[1..-1]})" else merge_attr_names = merge_attrs.split(/\s*,\s*/).*.gsub("-", "_").*.to_sym "(all_attributes & #{merge_attr_names.inspect})" end end |
#contains_param?(el) ⇒ Boolean
486 487 488 489 |
# File 'lib/dryml/template.rb', line 486 def contains_param?(el) # TODO false end |
#create_render_page_method ⇒ Object
81 82 83 84 85 |
# File 'lib/dryml/template.rb', line 81 def create_render_page_method erb_src = process_src @builder.add_build_instruction(:render_page, :src => erb_src, :line_num => 1) end |
#current_def_name ⇒ Object
497 498 499 |
# File 'lib/dryml/template.rb', line 497 def current_def_name @def_element && @def_element.attributes['tag'] end |
#declared_attributes(def_element) ⇒ Object
227 228 229 230 231 232 233 |
# File 'lib/dryml/template.rb', line 227 def declared_attributes(def_element) attrspec = def_element.attributes["attrs"] attr_names = attrspec ? attrspec.split(/\s*,\s*/).map{ |n| n.underscore.to_sym } : [] invalids = attr_names & ([:with, :field, :this] + SPECIAL_ATTRIBUTES.*.to_sym) dryml_exception("invalid attrs in def: #{invalids * ', '}", def_element) unless invalids.empty? attr_names end |
#def_element(el, extend_tag = false) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/dryml/template.rb', line 292 def def_element(el, extend_tag=false) require_toplevel(el) require_attribute(el, "tag", DRYML_NAME_RX) require_attribute(el, "attrs", /^\s*#{DRYML_NAME}(\s*,\s*#{DRYML_NAME})*\s*$/, true) require_attribute(el, "alias-of", DRYML_NAME_RX, true) @def_element = el unsafe_name = el.attributes["tag"] name = Dryml.unreserve(unsafe_name) suffix = type_specific_suffix if suffix name += suffix unsafe_name += suffix end if el.attributes['polymorphic'] %w(for alias-of).each do |attr| dryml_exception("def cannot have both 'polymorphic' and '#{attr}' attributes") if el.attributes[attr] end define_polymorphic_dispatcher(el, ruby_name(name)) name += "__base" unsafe_name += "__base" end alias_of = el.attributes['alias-of'] dryml_exception("def with alias-of must be empty", el) if alias_of and el.size > 0 alias_of and @builder.add_build_instruction(:alias_method, :new => ruby_name(name).to_sym, :old => ruby_name(Dryml.unreserve(alias_of)).to_sym) res = if alias_of "<% #{tag_newlines(el)} %>" else src = tag_method(name, el, extend_tag) + "<% _register_tag_attrs(:#{ruby_name name}, #{declared_attributes(el).inspect.underscore}) %>" logger.debug(restore_erb_scriptlets(src)) if el.attributes["debug-source"] @builder.add_build_instruction(:def, :src => restore_erb_scriptlets(src), :line_num => element_line_num(el)) # keep line numbers matching up "<% #{"\n" * src.count("\n")} %>" end @def_element = nil res end |
#default_param_proc(el, containing_param_name = nil) ⇒ Object
737 738 739 740 741 742 |
# File 'lib/dryml/template.rb', line 737 def default_param_proc(el, containing_param_name=nil) content = children_to_erb(el) content = (content, "param", containing_param_name, element_line_num(el)) if containing_param_name "proc { |#{param_content_local_name(el.dryml_name)}| new_context { %>#{content}<% ; output_buffer } #{tag_newlines(el)}}" end |
#define_polymorphic_dispatcher(el, name) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/dryml/template.rb', line 249 def define_polymorphic_dispatcher(el, name) # FIXME: The new erb context ends up being set-up twice src = %( def #{name}(attributes={}, parameters={}) _tag_context(attributes) do attributes.delete :with attributes.delete :field if for_klass = parse_for_type(attributes) call_polymorphic_tag('#{name}', for_klass, attributes, parameters) { #{name}__base(attributes.except, parameters) } else call_polymorphic_tag('#{name}', attributes, parameters) { #{name}__base(attributes.except, parameters) } end end end ) @builder.add_build_instruction(:eval, :src => src, :line_num => element_line_num(el)) end |
#delegated_part_element(el, content) ⇒ Object
481 482 483 |
# File 'lib/dryml/template.rb', line 481 def delegated_part_element(el, content) # TODO end |
#dryml_exception(message, el = nil) ⇒ Object
999 1000 1001 1002 |
# File 'lib/dryml/template.rb', line 999 def dryml_exception(, el=nil) el ||= @last_element raise DrymlException.new(, template_path, element_line_num(el)) end |
#element_line_num(el) ⇒ Object
1004 1005 1006 |
# File 'lib/dryml/template.rb', line 1004 def element_line_num(el) @doc.element_line_num(el) end |
#element_to_erb(el) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/dryml/template.rb', line 145 def element_to_erb(el) dryml_exception("old-style parameter tag (<#{el.name}>)", el) if el.name.starts_with?(":") @last_element = el case el.dryml_name when "include" include_element(el) # return just the newlines to keep line-number matching - the # include has no presence in the erb source tag_newlines(el) when "set-theme" require_toplevel(el) require_attribute(el, "name", /^#{DRYML_NAME}$/) Rails.logger.debug "set-theme has been deprecated. Please use <include gem='hobo_#{el.attributes['name']}'/> instead." @builder.add_build_instruction(:include, :gem => "hobo_#{el.attributes['name']}") # return nothing - set_theme has no presence in the erb source tag_newlines(el) when "def" def_element(el) when "extend" extend_element(el) when "set" set_element(el) when "set-scoped" set_scoped_element(el) when "param-content" param_content_element(el) else if el.dryml_name.not_in?(Dryml.) || el.attributes['param'] || el.attributes['restore'] tag_call(el) else static_element_to_erb(el) end end end |
#extend_element(el) ⇒ Object
268 269 270 |
# File 'lib/dryml/template.rb', line 268 def extend_element(el) def_element(el, true) end |
#field_shorthand_element?(el) ⇒ Boolean
807 808 809 |
# File 'lib/dryml/template.rb', line 807 def field_shorthand_element?(el) el. =~ /:./ end |
#find_ancestor(el) ⇒ Object
976 977 978 979 980 981 982 983 |
# File 'lib/dryml/template.rb', line 976 def find_ancestor(el) e = el.parent until e.is_a? REXML::Document return e if yield(e) e = e.parent end return nil end |
#gensym(name = "__tmp") ⇒ Object
1021 1022 1023 1024 1025 |
# File 'lib/dryml/template.rb', line 1021 def gensym(name="__tmp") @gensym_counter ||= 0 @gensym_counter += 1 "#{name}_#{@gensym_counter}" end |
#get_param_name(el) ⇒ Object
502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/dryml/template.rb', line 502 def get_param_name(el) param_name = el.attributes["param"] if param_name def_tag = find_ancestor(el) {|e| e.name == "def" || e.name == "extend" } dryml_exception("param is not allowed outside of tag definitions", el) if def_tag.nil? ruby_name(param_name == "&true" ? el.dryml_name : param_name) else nil end end |
#import_module(mod, as = nil) ⇒ Object
202 203 204 |
# File 'lib/dryml/template.rb', line 202 def import_module(mod, as=nil) @builder.import_module(mod, as) end |
#include_element(el) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/dryml/template.rb', line 191 def include_element(el) require_toplevel(el) require_attribute(el, "as", /^#{DRYML_NAME}$/, true) = {} %w(src module plugin gem as).each do |attr| [attr.to_sym] = el.attributes[attr] if el.attributes[attr] end @builder.add_build_instruction(:include, ) end |
#inside_def_for_type? ⇒ Boolean
516 517 518 |
# File 'lib/dryml/template.rb', line 516 def inside_def_for_type? @def_element && @def_element.attributes['for'] end |
#is_code_attribute?(attr_value) ⇒ Boolean
1013 1014 1015 |
# File 'lib/dryml/template.rb', line 1013 def is_code_attribute?(attr_value) attr_value =~ /^\&/ && attr_value !~ /^\&\S+;/ end |
#is_taglib? ⇒ Boolean
88 89 90 |
# File 'lib/dryml/template.rb', line 88 def is_taglib? @environment.class == Module end |
#logger ⇒ Object
1017 1018 1019 |
# File 'lib/dryml/template.rb', line 1017 def logger ActionController::Base.logger rescue nil end |
#maybe_make_part_call(el, call) ⇒ Object
793 794 795 796 797 798 799 800 801 802 803 804 |
# File 'lib/dryml/template.rb', line 793 def maybe_make_part_call(el, call) part_name = el.attributes['part'] if part_name part_id=(el.attributes['id'] ||= "\#{create_part_id('#{part_name}', '#{el.attributes['part-locals']}', binding)}") "<% safe_concat(\"<div class='part-wrapper' id='#{attribute_to_ruby(part_id)[1..-2]}'>\") %>" + part_element(el, call) + "<% safe_concat(%(</div>)) %>" else call end end |
#merge_attribute(el) ⇒ Object
613 614 615 616 617 |
# File 'lib/dryml/template.rb', line 613 def merge_attribute(el) merge = el.attributes['merge'] dryml_exception("merge cannot have a RHS", el) if merge && merge != "&true" merge end |
#node_to_erb(node) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/dryml/template.rb', line 110 def node_to_erb(node) case node # v important this comes before REXML::Text, as REXML::CData < REXML::Text when REXML::CData "<% safe_concat %(#{REXML::CData::START + node.to_s + REXML::CData::STOP}) %>" when REXML::Comment "<% safe_concat %(#{REXML::Comment::START + node.to_s + REXML::Comment::STOP}) %>" when REXML::Text text_with_scriplets_to_erb(node.to_s) when REXML::Element element_to_erb(node) end end |
#old_tag_call?(el) ⇒ Boolean
537 538 539 |
# File 'lib/dryml/template.rb', line 537 def old_tag_call?(el) @def_element && el.dryml_name == "old-#{current_def_name}" end |
#param_content_element(name_or_el) ⇒ Object
439 440 441 442 443 444 445 446 447 448 |
# File 'lib/dryml/template.rb', line 439 def param_content_element(name_or_el) name = if name_or_el.is_a?(String) name_or_el else el = name_or_el el.attributes['for'] || @containing_tag_name end local_name = param_content_local_name(name) "<%= #{local_name}.call if #{local_name} %>" end |
#param_content_local_name(name) ⇒ Object
434 435 436 |
# File 'lib/dryml/template.rb', line 434 def param_content_local_name(name) "_#{ruby_name name}__default_content" end |
#param_names_in_definition(el) ⇒ Object
360 361 362 363 364 365 366 367 |
# File 'lib/dryml/template.rb', line 360 def param_names_in_definition(el) self.class.descendent_select(el) { |el| el.attribute 'param' }.map do |e| name = get_param_name(e) dryml_exception("invalid param name: #{name.inspect}", e) unless is_code_attribute?(name) || name =~ RUBY_NAME_RX || name =~ /#\{/ name.to_sym unless is_code_attribute?(name) end.compact end |
#param_proc(el, metadata_name_suffix) ⇒ Object
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 |
# File 'lib/dryml/template.rb', line 755 def param_proc(el, ) = "#{el.name} < #{}" nl = tag_newlines(el) if (repl = el.attribute("replace")) dryml_exception("replace attribute must not have a value", el) if repl.has_rhs? dryml_exception("replace parameters must not have attributes", el) if el.attributes.length > 1 replace_parameter_proc(el, , children_to_erb(el)) else attributes = el.attributes.dup # Providing one of 'with' or 'field' but not the other should cancel out the other attributes[:with] = "&nil" if attributes.key?(:field) && !attributes.key?(:with) attributes[:field] = "&nil" if !attributes.key?(:field) && attributes.key?(:with) attribute_items = attributes.map do |name, value| if name.in?(VALID_PARAMETER_TAG_ATTRIBUTES) # just ignore elsif name.in?(SPECIAL_ATTRIBUTES) dryml_exception("attribute '#{name}' is not allowed on parameter tags (<#{el.name}:>)", el) else ":#{ruby_name name} => #{attribute_to_ruby(value, el)}" end end.compact nested_parameters_hash = (el, ) "proc { [{#{attribute_items * ', '}}, #{nested_parameters_hash}] #{nl}}" end end |
#param_restore_local_name(name) ⇒ Object
745 746 747 |
# File 'lib/dryml/template.rb', line 745 def param_restore_local_name(name) "_#{ruby_name name}_restore" end |
#parameter_tag_hash_item(el, metadata_name) ⇒ Object
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
# File 'lib/dryml/template.rb', line 685 def parameter_tag_hash_item(el, ) name = el.name.dup if name.sub!(/^before-/, "") before_parameter_tag_hash_item(name, el, ) elsif name.sub!(/^after-/, "") after_parameter_tag_hash_item(name, el, ) elsif name.sub!(/^prepend-/, "") prepend_parameter_tag_hash_item(name, el, ) elsif name.sub!(/^append-/, "") append_parameter_tag_hash_item(name, el, ) else hash_key = ruby_name name hash_key += "_replacement" if el.attribute("replace") if (param_name = get_param_name(el)) ":#{hash_key} => merge_tag_parameter(#{param_proc(el, )}, all_parameters[:#{param_name}])" else ":#{hash_key} => #{param_proc(el, )}" end end end |
#parameter_tags_hash(el, containing_tag_name = nil) ⇒ Object
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
# File 'lib/dryml/template.rb', line 620 def (el, containing_tag_name=nil) call_type = nil = containing_tag_name || el. param_items = el.map do |node| case node when REXML::Text text = node.to_s unless text.blank? dryml_exception("mixed content and parameter tags", el) if call_type == :named_params call_type = :default_param_only end text when REXML::Element e = node is_parameter_tag = e.parameter_tag? # Make sure there isn't a mix of parameter tags and normal content case call_type when nil call_type = is_parameter_tag ? :named_params : :default_param_only when :named_params dryml_exception("mixed parameter tags and non-parameter tags (did you forget a ':'?)", el) unless is_parameter_tag when :default_param_only dryml_exception("mixed parameter tags and non-parameter tags (did you forget a ':'?)", el) if is_parameter_tag end if is_parameter_tag parameter_tag_hash_item(e, ) + ", " end end end.join if call_type == :default_param_only || (call_type.nil? && param_items.length > 0) || (el.children.empty? && el.has_end_tag?) with_containing_tag_name(el) do param_items = " :default => #{default_param_proc(el, containing_tag_name)}, " end end param_items.concat without_parameters(el) merge_params = el.attributes['merge-params'] || merge_attribute(el) if merge_params extra_params = if merge_params == "&true" "parameters" elsif is_code_attribute?(merge_params) merge_params[1..-1] else merge_param_names = merge_params.split(/\s*,\s*/).*.gsub("-", "_").*.to_sym "all_parameters & #{merge_param_names.inspect}" end "merge_parameter_hashes({#{param_items}}, (#{extra_params}) || {})" else "{#{param_items}}" end end |
#part_delegate_tag_name(el) ⇒ Object
492 493 494 |
# File 'lib/dryml/template.rb', line 492 def part_delegate_tag_name(el) "#{@def_name}_#{el.attributes['part']}__part_delegate" end |
#part_element(el, content) ⇒ Object
451 452 453 454 455 456 457 458 459 |
# File 'lib/dryml/template.rb', line 451 def part_element(el, content) require_attribute(el, "part", DRYML_NAME_RX) if contains_param?(el) delegated_part_element(el, content) else simple_part_element(el, content) end end |
#polymorphic_call_type(el) ⇒ Object
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/dryml/template.rb', line 547 def polymorphic_call_type(el) t = el.attributes['for-type'] if t.nil? nil elsif t == "&true" 'this_type' elsif t =~ /^[A-Z]/ t elsif t =~ /^[a-z]/ && defined?(HoboFields.to_class) klass = HoboFields.to_class(t) klass.name elsif is_code_attribute?(t) t[1..-1] else dryml_exception("invalid for-type attribute", el) end end |
#prepend_parameter_tag_hash_item(name, el, metadata_name) ⇒ Object
730 731 732 733 734 |
# File 'lib/dryml/template.rb', line 730 def prepend_parameter_tag_hash_item(name, el, ) ":#{ruby_name name} => proc { [{}, { :default => proc { |#{param_content_local_name(name)}| new_context { %>" + children_to_erb(el) + param_content_element(name) + "<% ; output_buffer } } } ] }" end |
#process_src ⇒ Object
93 94 95 96 97 |
# File 'lib/dryml/template.rb', line 93 def process_src @doc = Dryml::Parser::Document.new(@src, @template_path) result = children_to_erb(@doc.root) restore_erb_scriptlets(result) end |
#promote_static_tag_to_method_call?(el) ⇒ Boolean
905 906 907 |
# File 'lib/dryml/template.rb', line 905 def promote_static_tag_to_method_call?(el) %w(part merge-attrs if unless repeat).any? {|x| el.attributes[x]} end |
#replace_parameter_proc(el, metadata_name, content = nil) ⇒ Object
786 787 788 789 790 |
# File 'lib/dryml/template.rb', line 786 def replace_parameter_proc(el, , content=nil) content ||= wrap_replace_parameter(el, ) param_name = el.dryml_name.sub(/^(before|after|append|prepend)-/, "") "proc { |#{param_restore_local_name(param_name)}| new_context { %>#{content}<% ; output_buffer } #{tag_newlines(el)}}" end |
#require_attribute(el, name, rx = nil, optional = false) ⇒ Object
990 991 992 993 994 995 996 997 |
# File 'lib/dryml/template.rb', line 990 def require_attribute(el, name, rx=nil, optional=false) val = el.attributes[name] if val dryml_exception("invalid #{name}=\"#{val}\" attribute on <#{el.dryml_name}>", el) unless rx && val =~ rx else dryml_exception("missing #{name} attribute on <#{el.dryml_name}>", el) unless optional end end |
#require_toplevel(el, message = nil) ⇒ Object
985 986 987 988 |
# File 'lib/dryml/template.rb', line 985 def require_toplevel(el, =nil) ||= "can only be at the top level" dryml_exception("<#{el.dryml_name}> #{}", el) if el.parent != @doc.root end |
#restore_erb_scriptlets(src) ⇒ Object
100 101 102 |
# File 'lib/dryml/template.rb', line 100 def restore_erb_scriptlets(src) @doc.restore_erb_scriptlets(src) end |
#ruby_name(dryml_name) ⇒ Object
236 237 238 |
# File 'lib/dryml/template.rb', line 236 def ruby_name(dryml_name) dryml_name.gsub('-', '_') end |
#set_element(el) ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/dryml/template.rb', line 207 def set_element(el) assigns = el.attributes.map do |name, value| next if name.in?(SPECIAL_ATTRIBUTES) dryml_exception("invalid name in <set> (remember to use '-' rather than '_')", el) unless name =~ /^#{DRYML_NAME}(\.#{DRYML_NAME})*$/ "#{ruby_name name} = #{attribute_to_ruby(value)}; " end.join code = apply_control_attributes("begin; #{assigns}; end", el) "<% #{code}#{tag_newlines(el)} %>" end |
#set_scoped_element(el) ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/dryml/template.rb', line 218 def set_scoped_element(el) variables = el.attributes.map do |name, value| dryml_exception("invalid name in <set-scoped> (remember to use '-' rather than '_')", el) unless name =~ DRYML_NAME_RX ":#{ruby_name name} => #{attribute_to_ruby(value)} " end "<% scope.new_scope(#{variables * ', '}) { #{tag_newlines(el)} %>#{children_to_erb(el)}<% } %>" end |
#simple_part_element(el, content) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/dryml/template.rb', line 462 def simple_part_element(el, content) part_name = el.attributes['part'] part_name = ruby_name(part_name) part_locals = el.attributes["part-locals"] dom_id = el.attributes['id'] raise 'id should have been added elsewhere' if dom_id.nil? part_src = "<% def #{part_name}_part(#{part_locals._?.gsub('@', '')}) #{tag_newlines(el)}; new_context do %>" + content + "<% end; end %>" @builder.add_part(part_name, restore_erb_scriptlets(part_src), element_line_num(el)) newlines = "\n" * part_src.count("\n") args = [attribute_to_ruby(dom_id), ":#{part_name}", part_locals].compact "<%=raw call_part(#{args * ', '}) #{newlines} %>" end |
#static_element_to_erb(el) ⇒ Object
889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
# File 'lib/dryml/template.rb', line 889 def static_element_to_erb(el) if promote_static_tag_to_method_call?(el) static_tag_to_method_call(el) else start_tag_src = el.start_tag_source.gsub(REXML::CData::START, "").gsub(REXML::CData::STOP, "") sts = "<% safe_concat(%(#{start_tag_src}))%>" if el.has_end_tag? sts + children_to_erb(el) + "<% safe_concat(%(</#{el.name}>)) %>" else sts end end end |
#static_tag_to_method_call(el) ⇒ Object
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 |
# File 'lib/dryml/template.rb', line 852 def static_tag_to_method_call(el) part = el.attributes["part"] if part && !el.attributes["id"] el.attributes["id"] = "\#{create_part_id('#{part}', '#{el.attributes['part-locals']}', binding)}" end attrs = el.attributes.map do |n, v| next if n.in? SPECIAL_ATTRIBUTES val = restore_erb_scriptlets(v).gsub('"', '\"').gsub(/<%=(.*?)%>/, '#{\1}') %('#{n}' => "#{val}") end.compact # Convert the attributes hash to a call to merge_attrs if # there's a merge-attrs attribute attrs = if el.attributes['merge-attrs'] merge_attrs = compile_merge_attrs(el) "merge_attrs({#{attrs * ', '}}, #{merge_attrs} || {})" else "{" + attrs.join(', ') + "}" end if el.children.empty? dryml_exception("part attribute on empty static tag", el) if part "<%= " + apply_control_attributes("element(:#{el.name}, #{attrs}, nil, true, #{!el.has_end_tag?} #{tag_newlines(el)})", el) + " %>" else if part body = part_element(el, children_to_erb(el)) else body = children_to_erb(el) end output_tag = "element(:#{el.name}, #{attrs}, new_context { %>#{body}<% })" "<% concat(" + apply_control_attributes(output_tag, el) + ") %>" end end |
#tag_attributes(el) ⇒ Object
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 |
# File 'lib/dryml/template.rb', line 812 def tag_attributes(el) attributes = el.attributes items = attributes.map do |n,v| dryml_exception("invalid attribute name '#{n}' (remember to use '-' rather than '_')", el) unless n =~ DRYML_NAME_RX next if n.in?(SPECIAL_ATTRIBUTES-['for-type']) || n =~ /^without-/ next if el.attributes['part'] && n == 'id' # The id is rendered on the <div class="part-wrapper"> instead ":#{ruby_name n} => #{attribute_to_ruby(v)}" end.compact # if there's a ':' el.name is just the part after the ':' items << ":field => \"#{ruby_name el.name}\"" if field_shorthand_element?(el) hash = "{#{items.join(", ")}}" if merge_attribute(el) "merge_attrs(#{hash}, attributes)" elsif el.attributes['merge-attrs'] merge_attrs = compile_merge_attrs(el) "merge_attrs(#{hash}, #{merge_attrs} || {})" else hash end end |
#tag_call(el) ⇒ Object
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/dryml/template.rb', line 565 def tag_call(el) name = call_name(el) param_name = get_param_name(el) attributes = tag_attributes(el) newlines = tag_newlines(el) parameters = tag_newlines(el) + (el) is_param_restore = el.attributes['restore'] call = if param_name param_name = attribute_to_ruby(param_name, :symbolize => true) args = "#{attributes}, #{parameters}, all_parameters, #{param_name}" to_call = if is_param_restore # The tag is available in a local variable # holding a proc param_restore_local_name(name) elsif (call_type = polymorphic_call_type(el)) "find_polymorphic_tag(:#{ruby_name name}, #{call_type})" else ":#{ruby_name name}" end "call_tag_parameter(#{to_call}, #{args})" else if is_param_restore # The tag is a proc available in a local variable "#{param_restore_local_name(name)}.call(#{attributes}, #{parameters})" elsif (call_type = polymorphic_call_type(el)) "send(find_polymorphic_tag(:#{ruby_name name}, #{call_type}), #{attributes}, #{parameters})" elsif attributes == "{}" && parameters == "{}" if name =~ /^[A-Z]/ # it's a tag with a cap name - not a local "#{ruby_name name}()" else # could be a tag or a local variable "#{ruby_name name}.to_s" end else "#{ruby_name name}(#{attributes}, #{parameters})" end end call = apply_control_attributes(call, el) call = maybe_make_part_call(el, "<% concat(#{call}) %>") (el, call) end |
#tag_method(name, el, extend_tag = false) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/dryml/template.rb', line 370 def tag_method(name, el, extend_tag=false) name = ruby_name name param_names = param_names_in_definition(el) if extend_tag @extend_key = 'a' + Digest::SHA1.hexdigest(el.to_s)[0..10] alias_statement = "; alias_method_chain_on_include :#{name}, :#{@extend_key}" name = "#{name}_with_#{@extend_key}" end src = "<% def #{name}(all_attributes={}, all_parameters={}); " + "parameters = Dryml::TagParameters.new(all_parameters, #{param_names.inspect.underscore}); " + "all_parameters = Dryml::TagParameters.new(all_parameters); " + tag_method_body(el) + "; end#{alias_statement} %>" @extend_key = nil src end |
#tag_method_body(el) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/dryml/template.rb', line 390 def tag_method_body(el) attrs = declared_attributes(el) # A statement to assign values to local variables named after the tag's attrs # The trailing comma on `attributes` is supposed to be there! setup_locals = attrs.map{|a| "#{Dryml.unreserve(a).underscore}, "}.join + "attributes, = " + "_tag_locals(all_attributes, #{attrs.inspect})" start = "_tag_context(all_attributes) do #{setup_locals}" "#{start} " + # reproduce any line breaks in the start-tag so that line numbers are preserved tag_newlines(el) + "%>" + (children_to_erb(el)) + "<% output_buffer; end" end |
#tag_newlines(el) ⇒ Object
1008 1009 1010 1011 |
# File 'lib/dryml/template.rb', line 1008 def tag_newlines(el) src = el.start_tag_source "\n" * src.count("\n") end |
#text_with_scriplets_to_erb(s) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/dryml/template.rb', line 129 def text_with_scriplets_to_erb(s) scriplet_rex = /(\[!\[DRYML-ERB\d+\s*\]!\])/m s.split(scriplet_rex).map do |t| case t when scriplet_rex t when /\S+/ t.gsub!(/(\(|\))/){"\\#{$1}"} "<% safe_concat %(#{t}) %>" else t end end.join end |
#type_specific_suffix ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/dryml/template.rb', line 273 def type_specific_suffix el = @def_element for_type = el.attributes['for'] if for_type type_name = if defined?(HoboFields) && for_type =~ /^[a-z]/ # It's a symbolic type name - look up the Ruby type name klass = HoboFields.to_class(for_type) or dryml_exception("No such type in polymorphic tag definition: '#{for_type}'", el) # ActiveSupport::TimeWithZone.name would return 'Time' # so we add an exception to pick the right datetime type klass == ActiveSupport::TimeWithZone ? 'datetime' : klass.name else for_type end.underscore.gsub('/', '__') "__for_#{type_name}" end end |
#with_containing_tag_name(el) ⇒ Object
241 242 243 244 245 246 |
# File 'lib/dryml/template.rb', line 241 def with_containing_tag_name(el) old = @containing_tag_name @containing_tag_name = el.dryml_name yield @containing_tag_name = old end |
#without_parameters(el) ⇒ Object
679 680 681 682 |
# File 'lib/dryml/template.rb', line 679 def without_parameters(el) without_names = el.attributes.keys.map { |name| name =~ /^without-(.*)/ and $1 }.compact without_names.map { |name| ":#{ruby_name name}_replacement => proc {|__discard__| '' }, " }.join end |
#wrap_replace_parameter(el, name) ⇒ Object
750 751 752 |
# File 'lib/dryml/template.rb', line 750 def wrap_replace_parameter(el, name) (children_to_erb(el), "replace", name, element_line_num(el)) end |
#wrap_source_with_metadata(content, kind, name, *args) ⇒ Object
this function is now basically just a hook for DrymlFireMarker
408 409 410 |
# File 'lib/dryml/template.rb', line 408 def (content, kind, name, *args) content end |
#wrap_tag_call_with_metadata(el, content) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/dryml/template.rb', line 420 def (el, content) name = el. param = el.attributes['param'] if param == "&true" name += " param" elsif param name += " param='#{param}'" end (content, "call", name, element_line_num(el)) end |
#wrap_tag_method_body_with_metadata(content) ⇒ Object
412 413 414 415 416 417 |
# File 'lib/dryml/template.rb', line 412 def (content) name = @def_element.attributes['tag'] for_ = @def_element.attributes['for'] name += " for #{for_}" if for_ (content, "def", name, element_line_num(@def_element)) end |