Module: Mustdown

Extended by:
Mustdown
Included in:
Mustdown
Defined in:
lib/mustdown.rb,
lib/mustdown/engine.rb,
lib/mustdown/version.rb,
app/helpers/mustdown/mustdown_helper.rb,
lib/generators/mustdown/install_generator.rb

Overview

Public: Wrapper class for Mustdown.

This module is a Rails helper that provides an easy access to the Mustdown rendering methods.

Defined Under Namespace

Modules: Generators, MustdownHelper Classes: Engine

Constant Summary collapse

VERSION =
'2.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#markdown_extensionsObject

Public: Returns the default markdown extensions

Returns a Hash.



60
61
62
63
64
65
66
67
68
# 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_rendererObject

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.



88
89
90
# File 'lib/mustdown.rb', line 88

def markdown_renderer
  @markdown_renderer ||= Redcarpet::Render::HTML
end

#renderer_optionsObject

Public: Returns the default markdown renderer options

Returns a Hash.



73
74
75
76
77
78
# File 'lib/mustdown.rb', line 73

def renderer_options
  @renderer_options ||= {
    no_styles:        true,
    safe_links_only:  true
  }
end

Instance Method Details

#configure {|_self| ... } ⇒ Object

Public: Provides a way to configure Mustdown.

Yields itself.

Examples

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

Returns nothing.

Yields:

  • (_self)

Yield Parameters:

  • _self (Mustdown)

    the object that the method was called on



53
54
55
# File 'lib/mustdown.rb', line 53

def configure
  yield self
end

#markdown(template, markdown_extensions = {}, renderer_options = {}) ⇒ Object

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.



115
116
117
118
119
120
121
122
123
# 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

#mustache(template, resource) ⇒ Object

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.



142
143
144
# File 'lib/mustdown.rb', line 142

def mustache(template, resource)
  Handlebars::Context.new.compile(template).call(resource)
end

#mustdown(template, resource, *markdown_args) ⇒ Object

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.



170
171
172
# File 'lib/mustdown.rb', line 170

def mustdown(template, resource, *markdown_args)
  markdown mustache(template, resource), *markdown_args
end

#warn(message) ⇒ Object

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.



181
182
183
184
185
186
187
# File 'lib/mustdown.rb', line 181

def warn(message)
  if defined?(Rails)
    Rails.logger.warn message
  else
    $stderr.puts "WARNING: #{message}"
  end
end