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 8339227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:49:43+00:00 2026-06-09T04:49:43+00:00

TL;DR – how can I use something like improved_markdown :some_file to do custom rendering,

  • 0

TL;DR – how can I use something like improved_markdown :some_file to do custom rendering, but still render the layout as usual?


Normally, to render Markdown in Sinatra, you’d just do:

markdown :some_file

But I’d like to add the ability to do “fenced” syntax highlighting, like you can do in Github README files.

```ruby
class Foo
  # etc
end
```

I’ve got this partially working.

First, I installed Redcarpet and added a custom rendering class that uses Pygments.rb for syntax highlighting:

# create a custom renderer that allows highlighting of code blocks
class HTMLwithPygments < Redcarpet::Render::HTML
  def block_code(code, language)
    Pygments.highlight(code, lexer: language)
  end
end

Then I used it in a route, like this:

# Try to load any Markdown file specified in the URL
get '/*' do
  viewname = params[:splat].first

  if File.exist?("views/#{viewname}.md")

    # Uses my custom rendering class
    # The :fenced_code_blocks option means it will take, for example,
    # the word 'ruby' from ```ruby and pass that as the language
    # argument to my block_code method above  
    markdown_renderer = Redcarpet::Markdown.new(HTMLwithPygments, :fenced_code_blocks => true)

    file_contents = File.read("views/#{viewname}.md")
    markdown_renderer.render(file_contents)

  else
    "Nopers, I can't find it."
  end
end

This almost works. The Markdown is rendered as HTML with additional markup for highlighting purposes.

The only problem is that it does not use my layout; after all, I’m just reading a file and returning the rendered string. The normal markdown :foo call would involve Tilt in the process.

Do I have to create a custom Tilt template engine to get that to work, or is there an easier way?

  • 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. Editorial Team
    Editorial Team
    2026-06-09T04:49:45+00:00Added an answer on June 9, 2026 at 4:49 am

    You can pass arbitrary options to the markdown method (or any of the other rendering methods) and they will be passed on to the Tilt template in question. Tilt’s Redcarpet template looks for any provided :renderer option when creating it’s renderer, allowing you to specify a custom one.

    You can also specify options that should be applied to all markdown calls by passing them as the second argument to set :markdown, :option => :value.

    It’s not quite as simply as that though, since the current (released) version of Tilt doesn’t correctly detect if you have Redcarpet 2 installed. You can tell it explicitly though:

    # first ensure we're using the right Redcarpet version
    Tilt.register Tilt::RedcarpetTemplate::Redcarpet2, 'markdown', 'mkd', 'md'
    
    # set the appropriate options for markdown
    set :markdown, :renderer => HTMLwithPygments,
      :fenced_code_blocks => true, :layout_engine => :haml
    

    Now any call to markdown should use your custom code for code blocks, and will use layout.haml as the layout.

    (Disclaimer: I couldn’t get Pygments working (it causes Sinatra to crash every time), but everything else here works (I used a simple custom block_code method that just added a message so I could tell it was working).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading some og tags from sites but I can't seem to decode
I would like to count the length of a string with PHP. The string
Possible Duplicate: replace all occurrences in a string I found this question/answer: Use JavaScript
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I am getting the value from web service like &gt;&lt;&amp;&nbsp; etc. I want to
I need to clean up various Word 'smart' characters in user input, including but
I am generating some text to be shown on a web-site, and use HttpUtility.HtmlEncode
I'm wondering why the below code I'm trying to use to pull in a
I've got the following login script.. <?php $name = $_POST[name]; $password = $_POST[password]; $query
So to start, I have an array of XML files. These files need to

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.