Module: Crafty::Tools
- Defined in:
- lib/crafty/tools.rb
Constant Summary collapse
- ESCAPE_SEQUENCE =
{ "&" => "&", ">" => ">", "<" => "<", '"' => """ }
Class Method Summary collapse
- .create_stream(base) ⇒ Object
- .escape(content) ⇒ Object
- .format_attributes(attributes) ⇒ Object
- .format_parameters(parameters) ⇒ Object
Instance Method Summary collapse
- #build! ⇒ Object
- #comment!(content) ⇒ Object
- #declare!(name, *parameters) ⇒ Object
- #element!(name, content = nil, attributes = nil) ⇒ Object
- #instruct!(name = nil, attributes = {}) ⇒ Object
- #text!(content) ⇒ Object (also: #write!)
Class Method Details
.create_stream(base) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/crafty/tools.rb', line 34 def create_stream(base) if base.respond_to? :<< SafeWrapper.new(base) else SafeString.new end end |
.escape(content) ⇒ Object
8 9 10 11 |
# File 'lib/crafty/tools.rb', line 8 def escape(content) return content if content.html_safe? content.gsub(/[&><"]/) { |char| ESCAPE_SEQUENCE[char] } end |
.format_attributes(attributes) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/crafty/tools.rb', line 13 def format_attributes(attributes) return if attributes.nil? attributes.collect do |name, value| value = if value.kind_of? Array value.flatten.compact * " " else value.to_s end next if value == "" %Q{ #{name}="#{escape(value)}"} end.join end |
.format_parameters(parameters) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/crafty/tools.rb', line 26 def format_parameters(parameters) return if parameters.nil? parameters.collect do |name| name = name.inspect if name.kind_of? String %Q{ #{name}} end.join end |
Instance Method Details
#build! ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/crafty/tools.rb', line 91 def build! @_appended = false if @_crafted yield @_appended = true nil else begin @_crafted = Tools.create_stream(self) yield @_crafted.render ensure @_crafted = nil end end end |
#comment!(content) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/crafty/tools.rb', line 60 def comment!(content) build! do @_crafted << "<!-- " @_crafted << Tools.escape(content.to_s) @_crafted << " -->" end end |
#declare!(name, *parameters) ⇒ Object
78 79 80 81 82 |
# File 'lib/crafty/tools.rb', line 78 def declare!(name, *parameters) build! do @_crafted << "<!#{name}#{Tools.format_parameters(parameters)}>" end end |
#element!(name, content = nil, attributes = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/crafty/tools.rb', line 43 def element!(name, content = nil, attributes = nil) build! do if content or block_given? @_crafted << "<#{name}#{Tools.format_attributes(attributes)}>" if block_given? value = yield content = value if !@_appended or value.kind_of? String end content = content.to_s @_crafted << Tools.escape(content) if content != "" @_crafted << "</#{name}>" else @_crafted << "<#{name}#{Tools.format_attributes(attributes)}/>" end end end |
#instruct!(name = nil, attributes = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/crafty/tools.rb', line 68 def instruct!(name = nil, attributes = {}) unless name name = "xml" attributes = { :version => "1.0", :encoding => "UTF-8" } end build! do @_crafted << "<?#{name}#{Tools.format_attributes(attributes)}?>" end end |