module Mustdown

Public: Provides a simple way to render markdown, mustache and mustache+markdown templates.

Examples

# Rendering a markdown template.
tpl = 'This is **bold** and this is *italic*'
Mustdown.markdown(tpl)
# => '<p>This is <strong>bold</strong> and this is <em>italic</em></p>'

# Rendering a mustache template.
tpl = 'My name is {{name}} and I am {{age}}'
Mustdown.mustache(tpl, name: 'Tom', age: 20)
# => 'My name is Tom and I am 20'

# Rendering a mustache+markdown template.
tpl = '# More about {{name}}'
Mustdown.mustdown(tpl, name: 'Tom')
# => '<h1>More about Tom</h1>'

Constants

VERSION

Attributes

markdown_extensions[W]

Public: Sets the Hash of markdown extensions.

markdown_renderer[W]

Public: Sets the markdown renderer (a Redcarpet compatible renderer).

Note: It accepts a class or an instance.

renderer_options[W]

Public: Sets the Hash of markdown renderer options.

Public Instance Methods

configure() { |self| ... } click to toggle source

Public: Provides a way to configure Mustdown.

Yields itself.

Examples

# Configuring Mustdown
Mustdown.configure do |config|
  config.markdown_renderer = MyCustomRenderer
end

Returns nothing.

# File lib/mustdown.rb, line 53
def configure
  yield self
end
markdown(template, markdown_extensions = {}, renderer_options = {}) click to toggle source

Public: Renders a markdown template.

template - The String template containing markdown template. markdown_extensions - A Hash of additional extensions that will be merged

with the default ones.

renderer_options - A Hash of additional markdown renderer options that

will be merged with the default ones.

Examples

tpl = '# Hello world'
Mustdown.markdown(tpl)
# => '<h1>Hello world</h1>'

tpl = 'http://example.com [example](http://example.com)'
Mustdown.markdown(tpl, autolink: false)
# => '<p>http://example.com <a href="http://example.com">example</a></p>'

tpl = 'http://example.com [example](http://example.com)'
Mustdown.markdown(tpl, { autolink: false }, { no_links: true })
# => '<p>http://example.com [example](http://example.com)</p>'

Returns the rendered markdown String.

# File lib/mustdown.rb, line 115
def markdown(template, markdown_extensions = {}, renderer_options = {})
  exts = self.markdown_extensions.merge(markdown_extensions)
  opts = self.renderer_options.merge(renderer_options)

  renderer = markdown_renderer.new(opts)
  markdown = Redcarpet::Markdown.new(renderer, exts)

  markdown.render(template)
end
markdown_extensions() click to toggle source

Public: Returns the default markdown extensions

Returns a Hash.

# File lib/mustdown.rb, line 60
def markdown_extensions
  @markdown_extensions ||= {
    no_intra_emphasis:  true,
    tables:             true,
    fenced_code_blocks: true,
    autolink:           true,
    strikethrough:      true
  }
end
markdown_renderer() click to toggle source

Public: Returns the markdown renderer.

This can be a class or an object. When it's a class, a new instance will be created when rendering.

Defaults to Redcarpet::Render::HTML

Returns an Object.

# File lib/mustdown.rb, line 88
def markdown_renderer
  @markdown_renderer ||= Redcarpet::Render::HTML
end
mustache(template, resource) click to toggle source

Public: Renders a mustache template.

template - The String template containing mustache template. resource - The Hash or Object used as binding for the template.

Examples

tpl = 'Hello {{name}}'
Mustdown.mustache(tpl, name: 'Tom')
# => 'Hello Tom'

tpl = 'Hello {{name}}'
user = User.find(1) # A user named Tom
Mustdown.mustache(tpl, user)
# => 'Hello Tom'

Returns the rendered mustache String.

# File lib/mustdown.rb, line 142
def mustache(template, resource)
  Handlebars::Context.new.compile(template).call(resource)
end
mustdown(template, resource, *markdown_args) click to toggle source

Public: Renders a mustache+markdown template.

template - The String template containing mustache+markdown template. resource - The Hash or Object used as binding for the template. markdown_args - Zero, one or two Hash objects that will be passed to the

markdown method.

Examples

tpl = '# Hello {{name}}'
Mustdown.mustdown(tpl, name: 'Tom')
# => '<h1>Hello Tom</h1>'

tpl = '{{url}}'
website = { url: 'http://example.com' }
Mustdown.markdown(tpl, website, autolink: false)
# => '<p>http://example.com</p>'

tpl = '[{{title}}]({{url}})'
website = { title: 'Example', url: 'http://example.com' }
Mustdown.markdown(tpl, website, { autolink: false }, { no_links: true })
# => '<p>[Example](http://example.com)</p>'

Returns the rendered mustache+arkdown String.

# File lib/mustdown.rb, line 170
def mustdown(template, resource, *markdown_args)
  markdown mustache(template, resource), *markdown_args
end
renderer_options() click to toggle source

Public: Returns the default markdown renderer options

Returns a Hash.

# File lib/mustdown.rb, line 73
def renderer_options
  @renderer_options ||= {
    no_styles:        true,
    safe_links_only:  true
  }
end
warn(message) click to toggle source

Private: Outputs a warning message.

In a Rails app uses Rails.logger, $stderr otherwise.

message - The String message to display as a warning.

Returns nothing.

# File lib/mustdown.rb, line 181
def warn(message)
  if defined?(Rails)
    Rails.logger.warn message
  else
    $stderr.puts "WARNING: #{message}"
  end
end