Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 98097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:07:51+00:00 2026-05-11T00:07:51+00:00

I have a Rails site, where the content is written in markdown. I wish

  • 0

I have a Rails site, where the content is written in markdown. I wish to display a snippet of each, with a ‘Read more..’ link.

How do I go about this? Simple truncating the raw text will not work, for example..

>> 'This is an [example](http://example.com)'[0..25] => 'This is an [example](http:' 

Ideally I want to allow the author to (optionally) insert a marker to specify what to use as the ‘snippet’, if not it would take 250 words, and append ‘…’ – for example..

This article is an example of something or other.  This segment will be used as the snippet on the index page.  ^^^^^^^^^^^^^^^  This text will be visible once clicking the 'Read more..' link 

The marker could be thought of like an EOF marker (which can be ignored when displaying the full document)

I am using maruku for the Markdown processing (RedCloth is very biased towards Textile, BlueCloth is extremely buggy, and I wanted a native-Ruby parser which ruled out peg-markdown and RDiscount)

Alternatively (since the Markdown is translated to HTML anyway) truncating the HTML correctly would be an option – although it would be preferable to not markdown() the entire document, just to get the first few lines.

So, the options I can think of are (in order of preference)..

  • Add a ‘truncate’ option to the maruku parser, which will only parse the first x words, or till the ‘excerpt’ marker.
  • Write/find a parser-agnostic Markdown truncate’r
  • Write/find an intelligent HTML truncating function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:07:52+00:00Added an answer on May 11, 2026 at 12:07 am
    • Write/find an intelligent HTML truncating function

    The following from http://mikeburnscoder.wordpress.com/2006/11/11/truncating-html-in-ruby/, with some modifications will correctly truncate HTML, and easily allow appending a string before the closing tags.

    >> puts '<p><b><a href=\'hi\'>Something</a></p>'.truncate_html(5, at_end = '...') => <p><b><a href='hi'>Someth...</a></b></p> 

    The modified code:

    require 'rexml/parsers/pullparser'  class String   def truncate_html(len = 30, at_end = nil)     p = REXML::Parsers::PullParser.new(self)     tags = []     new_len = len     results = ''     while p.has_next? && new_len > 0       p_e = p.pull       case p_e.event_type       when :start_element         tags.push p_e[0]         results << '<#{tags.last}#{attrs_to_s(p_e[1])}>'       when :end_element         results << '</#{tags.pop}>'       when :text         results << p_e[0][0..new_len]         new_len -= p_e[0].length       else         results << '<!-- #{p_e.inspect} -->'       end     end     if at_end       results << '...'     end     tags.reverse.each do |tag|       results << '</#{tag}>'     end     results   end    private    def attrs_to_s(attrs)     if attrs.empty?       ''     else       ' ' + attrs.to_a.map { |attr| %{#{attr[0]}='#{attr[1]}'} }.join(' ')     end   end end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a small rails app to serve up content to another site
I'm new on both this site and ruby on rails! I have a common
I have a site where users can submit content based on a link. Is
I have a staging Rails site up that's running on MySQL 5.0.32-Debian. On this
I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so
I have a Rails app hosted in Heroku. If I go the the site
I am going to be deploying a site i have built in rails 3
I have a site built in Ruby On Rails which has many ads in
We have a big site (50 scaffolds) running on Rails 2.3.9 with Heroku and
I've just validated a new site a new site that i've written using Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.