Created
August 23, 2012 10:19
-
-
Save GarPit/3435193 to your computer and use it in GitHub Desktop.
simple xml generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def xml | |
@xml_string << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
end | |
def tag(tag, attrs={}, &block) | |
@xml_string << "<#{tag}" | |
text = attrs.delete(:text) | |
@xml_string << " " if not attrs.empty? | |
attrs.each_pair do |key, value| | |
@xml_string << "#{key.to_s}=\"#{value.to_s}\"" | |
@xml_string << " " if key != attrs.keys.last | |
end | |
@xml_string << ">" | |
if block_given? | |
block.arity < 1 ? self.instance_eval(&block) : block.call(self) | |
end | |
@xml_string << text.to_s.gsub(/[&"'<>]/) {|match| REPLACEMENTS[match]} if text | |
@xml_string << "</#{tag}>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment