Module: Crafty::Tools
- Defined in:
- lib/crafty/tools.rb
Constant Summary collapse
- ESCAPE_SEQUENCE =
{ "&" => "&", ">" => ">", "<" => "<", '"' => """ }
Class Method Summary collapse
-
.create_stream(base) ⇒ Object
Creates a safe string buffer or wraps the given object in an object that acts like a safe string buffer.
-
.escape(content) ⇒ Object
Escape HTML/XML unsafe characters.
-
.format_attributes(attributes) ⇒ Object
Formats the given hash of attributes into a string that can be used directly inside an HTML/XML tag.
-
.format_parameters(parameters) ⇒ Object
Formats the given array of parameters into a string that can be used directly inside an HTML/XML (entity) declaration.
Instance Method Summary collapse
-
#build! ⇒ Object
Collects all elements built inside the given block into a single string.
-
#comment!(content) ⇒ Object
Write a comment with the given content.
-
#declare!(name, *parameters) ⇒ Object
Write a (doctype or entity) declaration with the given name and parameters.
-
#element!(name, content = nil, attributes = nil) ⇒ Object
Write an element with the given name, content and attributes.
-
#instruct!(name = nil, attributes = {}) ⇒ Object
Write a processing instruction with the given name and attributes.
-
#text!(content) ⇒ Object
(also: #write!)
Write the given text, escaping it if necessary.
Class Method Details
.create_stream(base) ⇒ Object
Creates a safe string buffer or wraps the given object in an object that acts like a safe string buffer.
41 42 43 44 45 46 47 |
# File 'lib/crafty/tools.rb', line 41 def create_stream(base) if base.respond_to? :<< SafeWrapper.new(base) else SafeString.new end end |
.escape(content) ⇒ Object
Escape HTML/XML unsafe characters.
9 10 11 12 |
# File 'lib/crafty/tools.rb', line 9 def escape(content) return content if content.html_safe? content.gsub(/[&><"]/) { |char| ESCAPE_SEQUENCE[char] } end |
.format_attributes(attributes) ⇒ Object
Formats the given hash of attributes into a string that can be used directly inside an HTML/XML tag.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/crafty/tools.rb', line 16 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
Formats the given array of parameters into a string that can be used directly inside an HTML/XML (entity) declaration.
31 32 33 34 35 36 37 |
# File 'lib/crafty/tools.rb', line 31 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
Collects all elements built inside the given block into a single string.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/crafty/tools.rb', line 109 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
Write a comment with the given content.
72 73 74 75 76 77 78 |
# File 'lib/crafty/tools.rb', line 72 def comment!(content) build! do @_crafted << "<!-- " @_crafted << Tools.escape(content.to_s) @_crafted << " -->" end end |
#declare!(name, *parameters) ⇒ Object
Write a (doctype or entity) declaration with the given name and parameters.
94 95 96 97 98 |
# File 'lib/crafty/tools.rb', line 94 def declare!(name, *parameters) build! do @_crafted << "<!#{name}#{Tools.format_parameters(parameters)}>" end end |
#element!(name, content = nil, attributes = nil) ⇒ Object
Write an element with the given name, content and attributes. If there is no content and no block is given, a self-closing element is created. Provide an empty string as content to create an empty, non-self-closing element.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/crafty/tools.rb', line 54 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
Write a processing instruction with the given name and attributes. Without arguments, it creates a default xml processing instruction.
82 83 84 85 86 87 88 89 90 |
# File 'lib/crafty/tools.rb', line 82 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 |