Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for Redcarpet 2.0 #450

Closed
wants to merge 8 commits into from

21 participants

Yu-Cheng Chuang Matthew Beale J. B. Rainsberger Matt Todd Clint Scott Tesoriere Michael Caron Kev Zettler Sindre Sorhus Ryan Mathews Carl Boettiger Brandon Mathis David Briggs Marie Markwell Fernando Mayer Whyme Lyu Jari Bakken Tom Preston-Werner Johannes Emerich Parker Moore Robbert Korving
Yu-Cheng Chuang

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>
Yu-Cheng Chuang

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

Yu-Cheng Chuang chitsaou referenced this pull request
Closed

Support Redcarpet 2.0 #444

Yu-Cheng Chuang

OK, I've put back the SmartyPants.

Yu-Cheng Chuang

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

Matthew Beale

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.

J. B. Rainsberger

+1

Clint

+1

Scott Tesoriere

What they said

Michael Caron

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

Ryan Mathews

+1

Carl Boettiger

+1

Brandon Mathis

+1

Carl Boettiger

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

Whyme Lyu

+1

Jari Bakken

+1

Tom Preston-Werner
Owner

Superseded and closed by #619.

Tom Preston-Werner mojombo closed this
Johannes Emerich

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>
Parker Moore
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.

Johannes Emerich

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.

Parker Moore
Owner

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

Robbert Korving

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
Parker Moore
Owner

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

Robbert Korving

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. Yu-Cheng Chuang

    specify redcarpet 2.0 in the gem

    chitsaou authored chitsaou committed
  2. Yu-Cheng Chuang

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

    chitsaou authored chitsaou committed
    …sions option)
  3. Yu-Cheng Chuang

    test cases for redcarpet 2.0

    chitsaou authored chitsaou committed
  4. Yu-Cheng Chuang
  5. Yu-Cheng Chuang
  6. Yu-Cheng Chuang
  7. Yu-Cheng Chuang
  8. Yu-Cheng Chuang
This page is out of date. Refresh to see the latest.
2  jekyll.gemspec
View
@@ -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[
24 lib/jekyll/converters/markdown.rb
View
@@ -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']
15 test/test_redcarpet.rb
View
@@ -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.