Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allow retrieving the options used for a renderer #205

Closed
aprescott opened this Issue Mar 3, 2013 · 6 comments

Comments

Projects
None yet
5 participants

Sometimes within custom renderer methods like header(text, level) for custom renderers, you need to call Redcarpet::Markdown.new(SomeRenderer).render because there is no super (see #51), but there's no way of getting the options passed to the original instance that has had render called on it, meaning there's potential duplication.

A somewhat contrived example to show what I mean:

Redcarpet::Markdown.new(CustomRenderer, no_intra_emphasis: true)

Where CustomRenderer might look like:

class CustomRenderer < Redcarpet::Render::SmartyHTML
  def header(text, level)
    if level > 3
      # no super method, so this, with duplicated no_intra_emphasis flag
      original = Redcarpet::Markdown.new(Redcarpet::Render::SmartyHTML, no_intra_emphasis: true)
      return original.render("#"*3 + " " + text)
    end

    %Q{<h#{level} class="demonstrating">#{text}</h#{level}>}
  end
end

Having a way to grab the options { no_intra_emphasis: true } in the custom renderer would make this a little easier, although fixing #51 by allowing super / returning the original value would probably make this mostly unneeded.

Collaborator

mattr- commented Jun 2, 2013

I like this idea. Perhaps you could create a pull request for it?

I'd love to! Unfortunately I don't know enough C for that part of the codebase: is it something that can be implemented in Ruby? Any pointers, if so?

Collaborator

mattr- commented Jun 3, 2013

I'm not sure if this is something that can be implemented in Ruby or not. I'll take a closer look and update this issue when I have more information.

+1 problems like this make the library hard to extend. :(

A workaround is to define your own initializer (which can call super). Then store a reference to the options in an instance variable (beware in this sample it's a reference not a copy).

class CustomRenderer < Redcarpet::Render::SmartyHTML

  def initialize(options = {})
    @options = options
    super(options)
  end

  def header(text, level)
    # You can access @options here
  end

end

robin850 added a commit that referenced this issue Jun 6, 2016

Give access to Markdown and rendering options
Expose the Markdown and rendering options through the `@options`
instance variable which is just a Hash which mixes both options.
Thus, custom callbacks can return an output based upon these settings.

Closes #205.

robin850 added a commit that referenced this issue Jun 6, 2016

Give access to Markdown and rendering options
Expose the Markdown and rendering options through the `@options`
instance variable which is just a Hash which mixes both options.
Thus, custom callbacks can return an output based upon these settings.

Closes #205.

@robin850 robin850 closed this in #560 Jun 6, 2016

Collaborator

robin850 commented Jun 6, 2016

This has been opened for a loooong time but this is now implemented, this will be in the next Redcarpet release, thanks for the proposal ! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment