Access application configuration

I’m using Redcarpet for HAML markdown:

module Haml::Filters
  remove_filter("Markdown")

  module Markdown
    include Haml::Filters::Base

    def render text
      markdown.render text
    end

    private

    def markdown
      @markdown ||= Redcarpet::Markdown.new MarkdownRenderer, {fenced_code_blocks: true}
    end
  end
end

Is there a way of accessing the Middleman application configuration inside this module? I’d like to be able so save the Redcarpet options using set in order to be able to access them somewhere else, too:

set :markdown_options, {fenced_code_blocks: true}

Try this in your config.rb file:

set :markdown_engine, :redcarpet
set :markdown, :fenced_code_blocks => true, 
               # (more options can be added)
               :strikethrough => true,
               # etc.

HAML does not apply those settings (see http://stackoverflow.com/a/15838956, e.g.). That’s why I have to overwrite the renderer.

The complete code for reference: https://gist.github.com/backflip/7446094. Works fine, but I don’t really like the global $markdown_options

If you have

set :markdown, :fenced_code_blocks => true

in config.rb, you can retrieve it in an extensions initialize by (after super) using

app.config.setting(:markdown)

Unfortunately, app is undefined inside module Haml::Filters. I guess that is a language feature, but I don’t know Ruby well enough.

How about if you set a class variable @@markdown_options in initialize, and use it instead of $markdown_options . Does it work?

Hi, trying to do something similar here. This was helpful…

…and I was able to access app.config.setting in my extension like so.

  def initialize(app, options_hash={}, &block)
    super
    app.config.setting(:markdown)
  end

However, I found that the the config values hadn’t been set yet. I think I remember that the config.rb settings are one of the last things in the execution order.

Does anybody know a way to make those values available to an extension during initialize?

You can always ”cheat” by reading the config.rb file and parsing out the values yourself. Not the most elegant of solutions, but may get the job done.

I just learned that the config values will be set if I wait to lookup configuration until the after_configuration callback. In fact, an example of this situation is given in the documentation:

  def after_configuration
    the_users_setting = app.settings.css_dir
    app.set :my_setting, "#{the_users_setting}_with_my_suffix"
  end

And that’s how you do it.