When you first visit my site, you see a snippet (the first 75 words) of the most recent post. If it exceeds 75 words, a link will be appended to extend the post and read it in full. Now, my problem is when I post a code snippet, and I use "pre" tags to preserve its formatting, I don''t want that to show up on the snippet... only on the full version of the post. The reason is because the site get''s all funky if the 75 work mark happens to fall in between the open "pre" and the closed "pre" and they are no properly matched. I''m using textile for text formatting, so in my view I currently have something like this: <div class="body-text"> <%= to_html(post.body) %> </div> The "to_html" helper simply converts the textile text into html. I would like to write a helper that would accept the converted html text as a parameter, and just completely remove the <pre> snippet </pre> block if I''m not on the full post. So, I''d like something like this: <div class="body-text"> <%= strip_code_snippets(to_html(post.body)) %> </div> Can someone help me with how to do this? What would the "strip_code_snippets" method look like? I think I''d be fine finding the <pre> tag but I don''t know how to completely remove the text from <pre> to </pre>. Any help would be greatly appreciated. Thanks in advance. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
my_string = "<pre>dflakdjflakj</pre> my_string.gsub(/[^<pre>][</pre>$], '''') You basically create a regular expression to find the <pre> and </pre> and nuke all the characters when you find the match. I dont think my regex is correct. Advanced Ruby developers here can help.> Can someone help me with how to do this? What would the > "strip_code_snippets" method look like? I think I''d be fine finding the > <pre> tag but I don''t know how to completely remove the text from <pre> > to </pre>. Any help would be greatly appreciated. Thanks in advance. >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Ok, that''s what I thought it would be. I''m not very good at regular expressions, as I''ve had little experience with them. I''ll try it out, though. Thanks a lot. Anyone else know if this regex will work? Or have a better one? Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On Dec 7, 2006, at 1:45 AM, Bala Paranj wrote:>> Can someone help me with how to do this? What would the >> "strip_code_snippets" method look like? I think I''d be fine >> finding the >> <pre> tag but I don''t know how to completely remove the text from >> <pre> >> to </pre>. Any help would be greatly appreciated. Thanks in >> advance. > > my_string = "<pre>dflakdjflakj</pre> > my_string.gsub(/[^<pre>][</pre>$], '''') > > You basically create a regular expression to find the <pre> and </pre> > and nuke all the characters when you find the match. > > I dont think my regex is correct. Advanced Ruby developers here can > help.>> my_string = "This is my little block of code <pre>puts ''I need to learn about Regexp''\nputs ''Will you help?''</pre>" => "This is my little block of code <pre>puts ''I need to learn about Regexp''\nputs ''Will you help?''</pre>" >> regexp = %r{<pre\b[^>]*>.*?</pre>}m => /<pre\b[^>]*>.*?<\/pre>/m >> my_string.gsub(regexp, '''') => "This is my little block of code " The square brackets [...] enclose a character set and [^...] enclose a negated set (not one of those characters. The previous posting isn''t even well-formed syntactically. Here''s a little explanation to get you started: regexp = %r{<pre\b[^>]*>.*?</pre>}m <pre = literal character matching \b = word boundary [^>] = character set matching anything that''s NOT a > * = zero or more times > = literal > .*? = any character (.) repeated zero or more times, but as few as possible to let the regexp match (*?) Note: this is getting rather advanced, you can look at the pickaxe pp.68-77 </pre> = literal character matching The %r{ } is an alternate way to write a literal regular expression which I used in lieu of escaping the / in the /pre. You can see the equivalent form that irb printed as the value. The ''m'' at the end is a flag to match multi-line input. It turns the ''.'' from matching "any character except newline" to simply "any character". -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Sorry if this gets dup''d, I haven''t seen it hit the list after 8hrs. On Dec 7, 2006, at 1:45 AM, Bala Paranj wrote:>> Can someone help me with how to do this? What would the >> "strip_code_snippets" method look like? I think I''d be fine >> finding the >> <pre> tag but I don''t know how to completely remove the text from >> <pre> >> to </pre>. Any help would be greatly appreciated. Thanks in >> advance. > > my_string = "<pre>dflakdjflakj</pre> > my_string.gsub(/[^<pre>][</pre>$], '''') > > You basically create a regular expression to find the <pre> and </pre> > and nuke all the characters when you find the match. > > I dont think my regex is correct. Advanced Ruby developers here can > help.>> my_string = "This is my little block of code <pre>puts ''I need to learn about Regexp''\nputs ''Will you help?''</pre>" => "This is my little block of code <pre>puts ''I need to learn about Regexp''\nputs ''Will you help?''</pre>" >> regexp = %r{<pre\b[^>]*>.*?</pre>}m => /<pre\b[^>]*>.*?<\/pre>/m >> my_string.gsub(regexp, '''') => "This is my little block of code " The square brackets [...] enclose a character set and [^...] enclose a negated set (not one of those characters. The previous posting isn''t even well-formed syntactically. Here''s a little explanation to get you started: regexp = %r{<pre\b[^>]*>.*?</pre>}m <pre = literal character matching \b = word boundary [^>] = character set matching anything that''s NOT a > * = zero or more times > = literal > .*? = any character (.) repeated zero or more times, but as few as possible to let the regexp match (*?) Note: this is getting rather advanced, you can look at the pickaxe pp.68-77 </pre> = literal character matching The %r{ } is an alternate way to write a literal regular expression which I used in lieu of escaping the / in the /pre. You can see the equivalent form that irb printed as the value. The ''m'' at the end is a flag to match multi-line input. It turns the ''.'' from matching "any character except newline" to simply "any character". -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---