Hi All! I have a small problem with a data conversion that I am doing from WordPress to a custom app that I am building in rails. In WordPress, line breaks were handled in the PHP code and converted to <p></p>. What I am wondering is, how can I add in the paragraph tags around lines of text (e.g. <p>some text that is line 1</p><p>some text that is line 2</p> etc...)? I''ve seen gems that might be able to do this, but I''d prefer to stay away from that route. Also, I''d prefer to stick away from markdown formatting since we already have many entries that utilize raw HTML. Thanks in advance! -- Ryan Prins rprins@gmail.com lazyi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: wrath.rubyonrails.org/pipermail/rails/attachments/20060313/3880f2c1/attachment.html
The first regex will do pretty much what you said: Wrap newline separated strings in paragraph tags. The second will wrap groups of lines in a paragraph tag set. You can get way more elegant with the regexes. => "this is a line of text\nand here is another" irb(main):009:0> a.gsub(/^(.*)$/, ''<p>\1</p>'') => "<p>this is a line of text</p>\n<p>and here is another</p>" irb(main):010:0> a.gsub(/^(.*)$/m, ''<p>\1</p>'') => "<p>this is a line of text\nand here is another</p>" Ryan Prins wrote:> Hi All! > > I have a small problem with a data conversion that I am doing from > WordPress > to a custom app that I am building in rails. In WordPress, line breaks > were > handled in the PHP code and converted to <p></p>. What I am wondering > is, > how can I add in the paragraph tags around lines of text (e.g. <p>some > text > that is line 1</p><p>some text that is line 2</p> etc...)? I''ve seen > gems > that might be able to do this, but I''d prefer to stay away from that > route. > Also, I''d prefer to stick away from markdown formatting since we already > have many entries that utilize raw HTML. > > Thanks in advance!-- Posted via ruby-forum.com.
Thanks Steve! That''s excellent. Now, I wish I only knew regular expressions better, because I have one last question. How and I get it do put a <br /> after lines that don''t have a space in between them. So, for example, if I have the following text: This is a first line with an empty line after it. This is a 2nd line with one following it. This is the 3rd line. I would like the HTML to be like this: <p>This is a first line with an empty line after it.</p> <p>This is a 2nd line with one following it.<br />This is the 3rd line<br /></p> Thanks for any help you can offer! Best, Ryan On 3/12/06, Steve Ross <cwdinfo@gmail.com> wrote:> > The first regex will do pretty much what you said: Wrap newline > separated strings in paragraph tags. The second will wrap groups of > lines in a paragraph tag set. You can get way more elegant with the > regexes. > > => "this is a line of text\nand here is another" > irb(main):009:0> a.gsub(/^(.*)$/, ''<p>\1</p>'') > => "<p>this is a line of text</p>\n<p>and here is another</p>" > irb(main):010:0> a.gsub(/^(.*)$/m, ''<p>\1</p>'') > => "<p>this is a line of text\nand here is another</p>" > > Ryan Prins wrote: > > Hi All! > > > > I have a small problem with a data conversion that I am doing from > > WordPress > > to a custom app that I am building in rails. In WordPress, line breaks > > were > > handled in the PHP code and converted to <p></p>. What I am wondering > > is, > > how can I add in the paragraph tags around lines of text (e.g. <p>some > > text > > that is line 1</p><p>some text that is line 2</p> etc...)? I''ve seen > > gems > > that might be able to do this, but I''d prefer to stay away from that > > route. > > Also, I''d prefer to stick away from markdown formatting since we already > > have many entries that utilize raw HTML. > > > > Thanks in advance! > > > -- > Posted via ruby-forum.com. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > lists.rubyonrails.org/mailman/listinfo/rails >-- Ryan Prins rprins@gmail.com lazyi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: wrath.rubyonrails.org/pipermail/rails/attachments/20060313/c8cf4f88/attachment.html
I typed this ugly stuff into irb and it appears to capture the case you describe. Geez, this is ugly. Do any of you regex gurus know a better way to do this? irb(main):006:0> a.gsub(/\n\n/, ''%%%'').gsub(/\n/, ''<br />'').gsub(/%%%/, ''</p><p>'').gsub(/^(.*)$/, ''<p>\1</p>'') => "<p>this is the first line with an empty line after it</p><p>this is a 2nd line with one following it<br />this is the 3rd line</p>" Alternatively, you could do something with splitting the lines: e.g.: a = line.split("\n") then based on the array, piece it back together how you want it. Ryan Prins wrote:> Thanks Steve! That''s excellent. Now, I wish I only knew regular > expressions > better, because I have one last question. How and I get it do put a <br > /> > after lines that don''t have a space in between them. So, for example, if > I > have the following text: > > This is a first line with an empty line after it. > > This is a 2nd line with one following it. > This is the 3rd line. > > I would like the HTML to be like this: > > <p>This is a first line with an empty line after it.</p> > <p>This is a 2nd line with one following it.<br />This is the 3rd > line<br > /></p> > > Thanks for any help you can offer! > > Best, > Ryan-- Posted via ruby-forum.com.