Hello, I have come across certain situations where text marked up with textile syntax needs to be displayed where HTML isn''t wanted. For example, in the title element of a an HTML page. In these situations, I would like a way of stripping away the textile markup from a string and displaying it completely "naked" - without HTML tags and, crucially, without the textile markup too. So:>> The _quick_ brown "fox":/fox.html jumps over the *lazy* dog''s tail.becomes>> The quick brown fox jumps over the lazy dog?s tail.(N.B. In the example above, I still want my punctuation made pretty "dog''s tail" should still become "dog?s tail" ) What''s the best way of doing this? If there isn''t an elegant way of doing it, could Redcloth have a to_plaintext method? Thanks, - James -- Posted via http://www.ruby-forum.com/.
You could use Nokogiri: require ''redcloth'' require ''nokogiri'' html = RedCloth.new(str).to_html plaintext = Nokogiri::HTML.fragment(html).text // Magnus Holm On Thu, Aug 5, 2010 at 18:49, James King <lists at ruby-forum.com> wrote:> Hello, > > I have come across certain situations where text marked up with textile > syntax needs to be displayed where HTML isn''t wanted. For example, in > the title element of a an HTML page. > > In these situations, I would like a way of stripping away the textile > markup from a string and displaying it completely "naked" - without HTML > tags and, crucially, without the textile markup too. > > So: > >>> The _quick_ brown "fox":/fox.html jumps over the *lazy* dog''s tail. > > becomes > >>> The quick brown fox jumps over the lazy dog?s tail. > > (N.B. In the example above, I still want my punctuation made pretty > "dog''s tail" should still become "dog?s tail" ) > > What''s the best way of doing this? If there isn''t an elegant way of > doing it, could Redcloth have a to_plaintext method? > > Thanks, > > - James > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Redcloth-upwards mailing list > Redcloth-upwards at rubyforge.org > http://rubyforge.org/mailman/listinfo/redcloth-upwards
Thanks Magnus! Magnus Holm wrote:> You could use Nokogiri: > > require ''redcloth'' > require ''nokogiri'' > > html = RedCloth.new(str).to_html > plaintext = Nokogiri::HTML.fragment(html).text > > // Magnus HolmThis works nicely. Have also found an example using Hpricot http://wiki.github.com/hpricot/hpricot/hpricot-challenge (see under "Strip All HTML Tags"). I''m happy using this, but I''m still left wondering whether it would make more sense for RedCloth to have an elegant "to_plaintext" method. It seems better not to go through an HTML intermediate stage. Best, - James -- Posted via http://www.ruby-forum.com/.
That''s a simple, elegant solution! I was going to suggest writing your own formatter that passed everything through without adding anything, but that''s way too much work! On Aug 5, 2010, at 1:04 PM, Magnus Holm wrote:> You could use Nokogiri: > > require ''redcloth'' > require ''nokogiri'' > > html = RedCloth.new(str).to_html > plaintext = Nokogiri::HTML.fragment(html).text > > // Magnus Holm > > > > On Thu, Aug 5, 2010 at 18:49, James King <lists at ruby-forum.com> wrote: >> Hello, >> >> I have come across certain situations where text marked up with textile >> syntax needs to be displayed where HTML isn''t wanted. For example, in >> the title element of a an HTML page. >> >> In these situations, I would like a way of stripping away the textile >> markup from a string and displaying it completely "naked" - without HTML >> tags and, crucially, without the textile markup too. >> >> So: >> >>>> The _quick_ brown "fox":/fox.html jumps over the *lazy* dog''s tail. >> >> becomes >> >>>> The quick brown fox jumps over the lazy dog?s tail. >> >> (N.B. In the example above, I still want my punctuation made pretty >> "dog''s tail" should still become "dog?s tail" ) >> >> What''s the best way of doing this? If there isn''t an elegant way of >> doing it, could Redcloth have a to_plaintext method? >> >> Thanks, >> >> - James >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Redcloth-upwards mailing list >> Redcloth-upwards at rubyforge.org >> http://rubyforge.org/mailman/listinfo/redcloth-upwards > _______________________________________________ > Redcloth-upwards mailing list > Redcloth-upwards at rubyforge.org > http://rubyforge.org/mailman/listinfo/redcloth-upwards
You should probably add that as a TODO, since I''ve seen plenty of sites who uses the Textile source as a simple plaintext-preview (where a proper formatter would definitely be better). // Magnus Holm On Sun, Aug 8, 2010 at 13:20, Jason Garber <jg at jasongarber.com> wrote:> That''s a simple, elegant solution! ?I was going to suggest writing your own formatter that passed everything through without adding anything, but that''s way too much work! > > On Aug 5, 2010, at 1:04 PM, Magnus Holm wrote: > >> You could use Nokogiri: >> >> ? ?require ''redcloth'' >> ? ?require ''nokogiri'' >> >> ? ?html = RedCloth.new(str).to_html >> ? ?plaintext = Nokogiri::HTML.fragment(html).text >> >> // Magnus Holm >> >> >> >> On Thu, Aug 5, 2010 at 18:49, James King <lists at ruby-forum.com> wrote: >>> Hello, >>> >>> I have come across certain situations where text marked up with textile >>> syntax needs to be displayed where HTML isn''t wanted. For example, in >>> the title element of a an HTML page. >>> >>> In these situations, I would like a way of stripping away the textile >>> markup from a string and displaying it completely "naked" - without HTML >>> tags and, crucially, without the textile markup too. >>> >>> So: >>> >>>>> The _quick_ brown "fox":/fox.html jumps over the *lazy* dog''s tail. >>> >>> becomes >>> >>>>> The quick brown fox jumps over the lazy dog?s tail. >>> >>> (N.B. In the example above, I still want my punctuation made pretty >>> "dog''s tail" should still become "dog?s tail" ) >>> >>> What''s the best way of doing this? If there isn''t an elegant way of >>> doing it, could Redcloth have a to_plaintext method? >>> >>> Thanks, >>> >>> - James >>> -- >>> Posted via http://www.ruby-forum.com/. >>> _______________________________________________ >>> Redcloth-upwards mailing list >>> Redcloth-upwards at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/redcloth-upwards >> _______________________________________________ >> Redcloth-upwards mailing list >> Redcloth-upwards at rubyforge.org >> http://rubyforge.org/mailman/listinfo/redcloth-upwards > > _______________________________________________ > Redcloth-upwards mailing list > Redcloth-upwards at rubyforge.org > http://rubyforge.org/mailman/listinfo/redcloth-upwards >