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 =
'0.1.1'
Instance Attribute Summary collapse
-
#markdown_extensions ⇒ Object
Public: Returns the default markdown extensions.
-
#markdown_renderer ⇒ Object
Public: Returns the markdown renderer.
-
#renderer_options ⇒ Object
Public: Returns the default markdown renderer options.
Instance Method Summary collapse
-
#configure {|_self| ... } ⇒ Object
Public: Provides a way to configure Mustdown.
-
#markdown(template, markdown_extensions = {}, renderer_options = {}) ⇒ Object
Public: Renders a markdown template.
-
#mustache(template, resource) ⇒ Object
Public: Renders a mustache template.
-
#mustdown(template, resource, *markdown_args) ⇒ Object
Public: Renders a mustache+markdown template.
-
#renderer ⇒ Object
Deprecated: Returns the markdown renderer.
-
#renderer=(value) ⇒ Object
Deprecated: Sets the markdown renderer.
-
#warn(message) ⇒ Object
Private: Outputs a warning message.
Instance Attribute Details
#markdown_extensions ⇒ Object
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_renderer ⇒ Object
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_options ⇒ Object
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 ||= { 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.
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.
135 136 137 138 139 140 141 142 143 |
# File 'lib/mustdown.rb', line 135 def markdown(template, markdown_extensions = {}, = {}) exts = self.markdown_extensions.merge(markdown_extensions) opts = self..merge() 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.
162 163 164 |
# File 'lib/mustdown.rb', line 162 def mustache(template, resource) Mustache.render(template, 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.
190 191 192 |
# File 'lib/mustdown.rb', line 190 def mustdown(template, resource, *markdown_args) markdown mustache(template, resource), *markdown_args end |
#renderer ⇒ Object
Deprecated: Returns the markdown renderer.
Use markdown_renderer instead.
Returns an Object.
97 98 99 100 |
# File 'lib/mustdown.rb', line 97 def renderer warn "Mustdown.renderer is deprecated. Use Mustdown.markdown_renderer instead." self.markdown_renderer end |
#renderer=(value) ⇒ Object
Deprecated: Sets the markdown renderer
Use markdown_renderer= instead.
Returns nothing.
107 108 109 110 |
# File 'lib/mustdown.rb', line 107 def renderer=(value) warn "Mustdown.renderer= is deprecated. Use Mustdown.markdown_renderer= instead." self.markdown_renderer = value 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.
201 202 203 204 205 206 207 |
# File 'lib/mustdown.rb', line 201 def warn() if defined?(Rails) Rails.logger.warn else $stderr.puts "WARNING: #{}" end end |