I''ve got a web service which outputs XML. I''m generating it very conveniently by creating a file in the view named index.xml.builder. That all works great, almost. I can write that file very simply and cleanly like this: xml.instruct! xml.search do xml.total @results[:total] end That''s really maintainable, but the output contains lots of extra spaces and newlines that muck it up. I can get rid of all the spaces by adding this line to the top of each xml.builder file: xml = Builder::XmlMarkup.new But that takes out ALL the spaces and newlines, so the entire output is on one line. What I want is something in the middle. Through experimentation, I''ve explicitly added newlines and spaces where I want them, but suddenly the file is no longer as easily maintainable: xml = Builder::XmlMarkup.new xml.instruct! xml.text!("\n") xml.search do xml.text!("\n ") xml.total @results[:total] end Does anyone know of a cleaner approach where I get the maintainability of the top example with the output of the bottom example? Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 15, 2011, at 11:38 AM, Paul wrote:> I''ve got a web service which outputs XML. I''m generating it very > conveniently by creating a file in the view named index.xml.builder. > That all works great, almost. > > I can write that file very simply and cleanly like this: > > xml.instruct! > xml.search do > xml.total @results[:total] > end > > That''s really maintainable, but the output contains lots of extra > spaces and newlines that muck it up. I can get rid of all the spaces > by adding this line to the top of each xml.builder file: > > xml = Builder::XmlMarkup.new > > But that takes out ALL the spaces and newlines, so the entire output > is on one line. What I want is something in the middle. Through > experimentation, I''ve explicitly added newlines and spaces where I > want them, but suddenly the file is no longer as easily maintainable: > > xml = Builder::XmlMarkup.new > xml.instruct! > xml.text!("\n") > xml.search do > xml.text!("\n ") > xml.total @results[:total] > end > > Does anyone know of a cleaner approach where I get the maintainability > of the top example with the output of the bottom example?Couldn''t you run the output through pp? Walter> > Thanks! > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Jun 15, 2011 at 12:02 PM, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> > On Jun 15, 2011, at 11:38 AM, Paul wrote: > >> I''ve got a web service which outputs XML. I''m generating it very >> conveniently by creating a file in the view named index.xml.builder. >> That all works great, almost. >> >> I can write that file very simply and cleanly like this: >> >> xml.instruct! >> xml.search do >> xml.total @results[:total] >> end >> >> That''s really maintainable, but the output contains lots of extra >> spaces and newlines that muck it up. I can get rid of all the spaces >> by adding this line to the top of each xml.builder file: >> >> xml = Builder::XmlMarkup.new >> >> But that takes out ALL the spaces and newlines, so the entire output >> is on one line. What I want is something in the middle. Through >> experimentation, I''ve explicitly added newlines and spaces where I >> want them, but suddenly the file is no longer as easily maintainable: >> >> xml = Builder::XmlMarkup.new >> xml.instruct! >> xml.text!("\n") >> xml.search do >> xml.text!("\n ") >> xml.total @results[:total] >> end >> >> Does anyone know of a cleaner approach where I get the maintainability >> of the top example with the output of the bottom example? > > Couldn''t you run the output through pp? > > WalterMaybe if I knew how. I''m not sure how to insert pp after the view template. My controllers look like this: respond_to do |format| format.html format.xml end And I don''t understand the magic enough to know how to put pp in there. Also, I was looking at the documentation for pp and it''s not clear what it will do with a string in xml format. If I could solve the first problem I''ll experiment with it. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Try xml = Builder::XmlMarkup.new(:indent=>2) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/J3ZCOsSfXhYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks. That''s where I started. That does two things: it indents the markup just like I want, but it does a second thing, too. It leaves all the white space that exists in the xml.builder file also, so that the resultant XML document is really ugly and contains extra newlines and extra spaces. On Thu, Jun 16, 2011 at 4:53 PM, Allan <allan.cochrane-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try xml = Builder::XmlMarkup.new(:indent=>2) >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.