Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for Redcarpet 2.0 #450

Closed
wants to merge 8 commits into from
@chitsaou

The Redcarpet 2.0.0 has out of beta, and I think it's good to merge it into the trunk.

As I mentioned in #444, the first benefit of Redcarpet 2.0 is that the renderer object can be initialized. There is no need to initialize it each time a Markdown file is going to be converted.

In addition, this patch is compatible with the current config file -- there is no need to change the config file.

Update:

It is also possible to supply renderer options to Redcarpet's HTML renderer. Put the render_options array inside the redcarpet hash. For example, in the _config.yml :

redcarpet:
  extensions:
    # array of ...
  render_options:
    - with_toc_data # add HTML anchors to each header in the output HTML for hyper linking
    - hard_wrap      # translate \n to <br>
@chitsaou

Oops, I found that the smart extension is deprecated, it is implemented as an module, instead. I'll monkey-patch the extension array.

@chitsaou chitsaou referenced this pull request
Closed

Support Redcarpet 2.0 #444

@chitsaou

OK, I've put back the SmartyPants.

@chitsaou

Also added an option to specify render options, which is also a new feature of RedCarpet 2.0. See updated description on the top.

@mixonic

Tests pass for me on 1.9.2 and ree on OS X.

I might rename @converter @redcarpet_converter, but this looks good to me reading through the diff.

@jbrains

+1

@catsby

+1

@scottkf

What they said

@mrcaron

Actually, this is probably a better fix than mine as I retain the old functionality, just pull in the new lib.

@ryanjm

+1

@cboettig

+1

@imathis

+1

@cboettig

Connected to pull request #487. Not sure how the two compare. These requests also connects to issue #200

@5long

+1

@jarib

+1

@mojombo
Owner

Superseded and closed by #619.

@mojombo mojombo closed this
@knuton

Is there official documentation on how to configure RedCarpet?

I found this issue in looking for a way to pass options to RedCarpet, but it now is misleading since all options are passed in the extensions array, including those for the renderer.

That is, the equivalent to the example above would now be:

redcarpet:
  extensions:
    # list of **extensions** could be here
    # below are **renderer options**
    - with_toc_data # add HTML anchors to each header in the output HTML for hyper linking
    - hard_wrap      # translate \n to <br>
@parkr
Owner

Nothing detailed. All we have is this: http://jekyllrb.com/docs/configuration/

Yeah, everything has to go into that array at the moment, as the code support is.

@knuton

Okay, thanks. I guess these docs are actually informative enough, it was rather bad luck that I stumbled upon this issue first.

Hope my comment keeps others from falling into the same trap.

@parkr
Owner

Sorry about that - thank you for your comment! Should clear up some things for others

@robkorv

Been searching all day to get <br /> linebreak with jekyll. At the end of the day I finally stumble on the solution that is written here. This should be in the docs.

markdown: redcarpet
redcarpet:
  extensions:
    - hard_wrap
@parkr
Owner

@robkorv Great idea! Would you mind submitting a PR? The site is in the site/ directory in the master branch.

@robkorv

Yes, I would and I could.
#1254

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 5, 2011
  1. @chitsaou

    specify redcarpet 2.0 in the gem

    chitsaou authored chitsaou committed
  2. @chitsaou

    implement converter with Redcarpet 2.0 API (compatible with 1.x exten…

    chitsaou authored chitsaou committed
    …sions option)
  3. @chitsaou

    test cases for redcarpet 2.0

    chitsaou authored chitsaou committed
  4. @chitsaou
  5. @chitsaou
  6. @chitsaou
  7. @chitsaou
  8. @chitsaou
This page is out of date. Refresh to see the latest.
View
2  jekyll.gemspec
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('cucumber', "= 1.1")
s.add_development_dependency('RedCloth', "~> 4.2")
s.add_development_dependency('rdiscount', "~> 1.6")
- s.add_development_dependency('redcarpet', "~> 1.9")
+ s.add_development_dependency('redcarpet', "~> 2.0")
# = MANIFEST =
s.files = %w[
View
24 lib/jekyll/converters/markdown.rb
@@ -13,7 +13,26 @@ def setup
when 'redcarpet'
begin
require 'redcarpet'
- @redcarpet_extensions = @config['redcarpet']['extensions'].map { |e| e.to_sym }
+ # Redcarpet 2.x, converter can be instantiated,
+ # and the format of renderer options is in hash, not in array
+
+ # convert extensions array to hash form
+ # [ 'ext_a', 'ext_b', 'ext_c' ]
+ # => { :ext_a => true, :ext_b => true, :ext_c => true }
+ redcarpet_extensions = Hash[@config['redcarpet']['extensions'].map { |e| [e.to_sym, true] }]
+ redcarpet_render_options = Hash[@config['redcarpet']['render_options'].map { |e| [e.to_sym, true] }] rescue {}
+
+ custom_redcarpet_class = Class.new(Redcarpet::Render::HTML)
+
+ # add SmartyPants module into the class if 'smart' is specified
+ if redcarpet_extensions[:smart] == true
+ custom_redcarpet_class.class_eval do
+ include Redcarpet::Render::SmartyPants
+ end
+ end
+
+ # TODO: support Renderer options
+ @converter = Redcarpet::Markdown.new(custom_redcarpet_class.new(redcarpet_render_options), redcarpet_extensions)
rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install redcarpet'
@@ -88,7 +107,8 @@ def convert(content)
setup
case @config['markdown']
when 'redcarpet'
- Redcarpet.new(content, *@redcarpet_extensions).to_html
+ # Redcarpet 2.x, converter can be instantiated
+ @converter.render(content)
when 'kramdown'
# Check for use of coderay
if @config['kramdown']['use_coderay']
View
15 test/test_redcarpet.rb
@@ -4,7 +4,10 @@ class TestRedcarpet < Test::Unit::TestCase
context "redcarpet" do
setup do
config = {
- 'redcarpet' => { 'extensions' => ['smart'] },
+ 'redcarpet' => {
+ 'extensions' => ['strikethrough', 'smart'],
+ 'render_options' => ['filter_html']
+ },
'markdown' => 'redcarpet'
}
@markdown = MarkdownConverter.new config
@@ -14,8 +17,16 @@ class TestRedcarpet < Test::Unit::TestCase
assert_equal "<h1>Some Header</h1>", @markdown.convert('# Some Header #').strip
end
- should "pass redcarpet extensions" do
+ should "pass redcarpet SmartyPants option" do
assert_equal "<p>&ldquo;smart&rdquo;</p>", @markdown.convert('"smart"').strip
end
+
+ should "pass redcarpet extensions" do
+ assert_equal "<p><del>deleted</del></p>", @markdown.convert('~~deleted~~').strip
+ end
+
+ should "pass redcarpet render options" do
+ assert_equal "<p><strong>bad code not here</strong>: i am bad</p>", @markdown.convert('**bad code not here**: <script>i am bad</script>').strip
+ end
end
end
Something went wrong with that request. Please try again.