mustdown 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bc042b8580cec63abbbd16ce9e0294ba1400988f
4
+ data.tar.gz: 12afa86fffd4bf116c4d653f3114925572a4b11e
5
+ SHA512:
6
+ metadata.gz: b544f888f2840b3c10fd6b8dae15a0c9fb7e308ab995e16c6af7e0cc0d6a0e63e03eb2e1ca21955b275d0acfdbb1581b61972b760c23b07fcd6127cec8505591
7
+ data.tar.gz: 57c76a80031a0fa9d1114aa6729f6df5358e9b368bae832158ab24953efb6a49140a2a5ef072cca4c39958da850ac505bf7c7e3897d30ec5fec52eadd0d499d4
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2012 YOURNAME
1
+ Copyright 2012 Simon Courtois
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- # Mustdown
1
+ # Mustdown [![Build Status](https://secure.travis-ci.org/simonc/mustdown.png?branch=master)](http://travis-ci.org/simonc/mustdown) [![Code Climate](https://codeclimate.com/github/simonc/mustdown.png)](https://codeclimate.com/github/simonc/mustdown) [![Coverage Status](https://coveralls.io/repos/simonc/mustdown/badge.png?branch=master)](https://coveralls.io/r/simonc/mustdown?branch=master)
2
2
 
3
- Mustdown provides helpers to ease the use markdown, mustache and both of them.
3
+ Mustdown provides helpers to ease the use of markdown, mustache and both of
4
+ them.
4
5
 
5
6
  ## Installation
6
7
 
@@ -10,7 +11,7 @@ Add the gem to your `Gemfile` :
10
11
 
11
12
  Then call `bundle install` to install it for your application.
12
13
 
13
- ## Provided helpers
14
+ ## Usage in Rails
14
15
 
15
16
  ### markdown
16
17
 
@@ -54,6 +55,37 @@ Output:
54
55
  <h1>Hello World</h1>
55
56
  ```
56
57
 
58
+ ## Usage outside of Rails
59
+
60
+ If you're not using Rails, it's possible to use the Mustdown module directly.
61
+
62
+ ### markdown
63
+
64
+ This will render the given text through Markdown.
65
+
66
+ ``` ruby
67
+ Mustdown.markdown("# Hello World")
68
+ # => "<h1>Hello World</h1>"
69
+ ```
70
+
71
+ ### mustache
72
+
73
+ The `mustache` method renders a mustache template with the given binding object.
74
+
75
+ ``` ruby
76
+ Mustdown.mustache("Hello {{name}}", name: 'John')
77
+ # => "Hello John"
78
+ ```
79
+
80
+ ### mustdown
81
+
82
+ This method is more complex since it provides the two previous methods in one.
83
+
84
+ ``` ruby
85
+ Mustdown.mustdown("# {{title}}", title: 'Hello World')
86
+ # => "<h1>Hello World</h1>"
87
+ ```
88
+
57
89
  ## Mustdown configuration
58
90
 
59
91
  You can generate a default initializer by calling:
@@ -81,7 +113,6 @@ Let's take an example. Given the following models in your app:
81
113
 
82
114
  ![Company(name) -> Project(title,url)](http://yuml.me/626df1f5)
83
115
 
84
-
85
116
  Define the following in `config/locales/en.yml`:
86
117
 
87
118
  ``` yaml
@@ -1,21 +1,28 @@
1
+ # Public: Wrapper class for Mustdown.
2
+ #
3
+ # This module is a Rails helper that provides an easy access to the Mustdown
4
+ # rendering methods.
1
5
  module Mustdown
2
6
  module MustdownHelper
3
- def markdown(content, markdown_extensions = {}, renderer_options = {})
4
- md_exts = Mustdown.markdown_extensions.merge(markdown_extensions)
5
- renderer_opts = Mustdown.renderer_options.merge(renderer_options)
6
-
7
- renderer = Mustdown.renderer.new(renderer_opts)
8
- markdown = Redcarpet::Markdown.new(renderer, md_exts)
9
-
10
- markdown.render(content).html_safe
7
+ # Public: Calls Mustdown.markdown.
8
+ #
9
+ # See Mustdown.markdown.
10
+ def markdown(*args)
11
+ Mustdown.markdown(*args).html_safe
11
12
  end
12
13
 
13
- def mustache(template, resource)
14
- Mustache.render(template, resource).html_safe
14
+ # Public: Calls Mustdown.mustache.
15
+ #
16
+ # See Mustdown.mustache.
17
+ def mustache(*args)
18
+ Mustdown.mustache(*args).html_safe
15
19
  end
16
20
 
17
- def mustdown(template, resource, *markdown_args)
18
- markdown mustache(template, resource), *markdown_args
21
+ # Public: Calls Mustdown.mustdown.
22
+ #
23
+ # See Mustdown.mustdown.
24
+ def mustdown(*args)
25
+ Mustdown.mustdown(*args).html_safe
19
26
  end
20
27
  end
21
28
  end
@@ -22,4 +22,6 @@ Mustdown.configure do |config|
22
22
  # safe_links_only: true
23
23
  # }
24
24
 
25
+ # Markdown renderer class
26
+ # config.markdown_renderer = Redcarpet::Render::HTML
25
27
  end
@@ -1,9 +1,11 @@
1
1
  module Mustdown
2
2
  class Engine < ::Rails::Engine
3
+ # Disable specs generation
3
4
  config.generators do |g|
4
5
  g.test_framework :rspec, view_specs: false
5
6
  end
6
7
 
8
+ # Register MustdownHelper as a Rails helper
7
9
  initializer 'mustdown.action_controller' do |app|
8
10
  ActiveSupport.on_load :action_controller do
9
11
  helper Mustdown::MustdownHelper
@@ -1,3 +1,3 @@
1
1
  module Mustdown
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/mustdown.rb CHANGED
@@ -3,37 +3,206 @@ require 'bundler/setup'
3
3
  require 'redcarpet'
4
4
  require 'mustache'
5
5
 
6
- require 'mustdown/engine'
6
+ require 'mustdown/engine' if defined?(Rails)
7
7
 
8
+ # Public: Provides a simple way to render markdown, mustache and
9
+ # mustache+markdown templates.
10
+ #
11
+ # Examples
12
+ #
13
+ # # Rendering a markdown template.
14
+ # tpl = 'This is **bold** and this is *italic*'
15
+ # Mustdown.markdown(tpl)
16
+ # # => '<p>This is <strong>bold</strong> and this is <em>italic</em></p>'
17
+ #
18
+ # # Rendering a mustache template.
19
+ # tpl = 'My name is {{name}} and I am {{age}}'
20
+ # Mustdown.mustache(tpl, name: 'Tom', age: 20)
21
+ # # => 'My name is Tom and I am 20'
22
+ #
23
+ # # Rendering a mustache+markdown template.
24
+ # tpl = '# More about {{name}}'
25
+ # Mustdown.mustdown(tpl, name: 'Tom')
26
+ # # => '<h1>More about Tom</h1>'
8
27
  module Mustdown
9
- class << self
10
- attr_accessor :markdown_extensions
11
- attr_accessor :renderer_options
12
- attr_accessor :renderer
28
+ extend self
13
29
 
14
- def configure
15
- yield self
16
- end
30
+ # Public: Sets the Hash of markdown extensions.
31
+ attr_writer :markdown_extensions
17
32
 
18
- def markdown_extensions
19
- @markdown_extensions ||= {
20
- no_intra_emphasis: true,
21
- tables: true,
22
- fenced_code_blocks: true,
23
- autolink: true,
24
- strikethrough: true
25
- }
26
- end
33
+ # Public: Sets the Hash of markdown renderer options.
34
+ attr_writer :renderer_options
27
35
 
28
- def renderer_options
29
- @renderer_options ||= {
30
- no_styles: true,
31
- safe_links_only: true
32
- }
33
- end
36
+ # Public: Sets the markdown renderer (a Redcarpet compatible renderer).
37
+ #
38
+ # Note: It accepts a class or an instance.
39
+ attr_writer :markdown_renderer
40
+
41
+ # Public: Provides a way to configure Mustdown.
42
+ #
43
+ # Yields itself.
44
+ #
45
+ # Examples
46
+ #
47
+ # # Configuring Mustdown
48
+ # Mustdown.configure do |config|
49
+ # config.markdown_renderer = MyCustomRenderer
50
+ # end
51
+ #
52
+ # Returns nothing.
53
+ def configure
54
+ yield self
55
+ end
56
+
57
+ # Public: Returns the default markdown extensions
58
+ #
59
+ # Returns a Hash.
60
+ def markdown_extensions
61
+ @markdown_extensions ||= {
62
+ no_intra_emphasis: true,
63
+ tables: true,
64
+ fenced_code_blocks: true,
65
+ autolink: true,
66
+ strikethrough: true
67
+ }
68
+ end
69
+
70
+ # Public: Returns the default markdown renderer options
71
+ #
72
+ # Returns a Hash.
73
+ def renderer_options
74
+ @renderer_options ||= {
75
+ no_styles: true,
76
+ safe_links_only: true
77
+ }
78
+ end
79
+
80
+ # Public: Returns the markdown renderer.
81
+ #
82
+ # This can be a class or an object. When it's a class, a new instance will be
83
+ # created when rendering.
84
+ #
85
+ # Defaults to Redcarpet::Render::HTML
86
+ #
87
+ # Returns an Object.
88
+ def markdown_renderer
89
+ @markdown_renderer ||= Redcarpet::Render::HTML
90
+ end
91
+
92
+ # Deprecated: Returns the markdown renderer.
93
+ #
94
+ # Use markdown_renderer instead.
95
+ #
96
+ # Returns an Object.
97
+ def renderer
98
+ warn "Mustdown.renderer is deprecated. Use Mustdown.markdown_renderer instead."
99
+ self.markdown_renderer
100
+ end
101
+
102
+ # Deprecated: Sets the markdown renderer
103
+ #
104
+ # Use markdown_renderer= instead.
105
+ #
106
+ # Returns nothing.
107
+ def renderer=(value)
108
+ warn "Mustdown.renderer= is deprecated. Use Mustdown.markdown_renderer= instead."
109
+ self.markdown_renderer = value
110
+ end
111
+
112
+ # Public: Renders a markdown template.
113
+ #
114
+ # template - The String template containing markdown template.
115
+ # markdown_extensions - A Hash of additional extensions that will be merged
116
+ # with the default ones.
117
+ # renderer_options - A Hash of additional markdown renderer options that
118
+ # will be merged with the default ones.
119
+ #
120
+ # Examples
121
+ #
122
+ # tpl = '# Hello world'
123
+ # Mustdown.markdown(tpl)
124
+ # # => '<h1>Hello world</h1>'
125
+ #
126
+ # tpl = 'http://example.com [example](http://example.com)'
127
+ # Mustdown.markdown(tpl, autolink: false)
128
+ # # => '<p>http://example.com <a href="http://example.com">example</a></p>'
129
+ #
130
+ # tpl = 'http://example.com [example](http://example.com)'
131
+ # Mustdown.markdown(tpl, { autolink: false }, { no_links: true })
132
+ # # => '<p>http://example.com [example](http://example.com)</p>'
133
+ #
134
+ # Returns the rendered markdown String.
135
+ def markdown(template, markdown_extensions = {}, renderer_options = {})
136
+ exts = markdown_extensions.merge(markdown_extensions)
137
+ opts = renderer_options.merge(renderer_options)
138
+
139
+ renderer = markdown_renderer.new(opts)
140
+ markdown = Redcarpet::Markdown.new(renderer, exts)
141
+
142
+ markdown.render(template)
143
+ end
144
+
145
+ # Public: Renders a mustache template.
146
+ #
147
+ # template - The String template containing mustache template.
148
+ # resource - The Hash or Object used as binding for the template.
149
+ #
150
+ # Examples
151
+ #
152
+ # tpl = 'Hello {{name}}'
153
+ # Mustdown.mustache(tpl, name: 'Tom')
154
+ # # => 'Hello Tom'
155
+ #
156
+ # tpl = 'Hello {{name}}'
157
+ # user = User.find(1) # A user named Tom
158
+ # Mustdown.mustache(tpl, user)
159
+ # # => 'Hello Tom'
160
+ #
161
+ # Returns the rendered mustache String.
162
+ def mustache(template, resource)
163
+ Mustache.render(template, resource)
164
+ end
165
+
166
+ # Public: Renders a mustache+markdown template.
167
+ #
168
+ # template - The String template containing mustache+markdown template.
169
+ # resource - The Hash or Object used as binding for the template.
170
+ # markdown_args - Zero, one or two Hash objects that will be passed to the
171
+ # markdown method.
172
+ #
173
+ # Examples
174
+ #
175
+ # tpl = '# Hello {{name}}'
176
+ # Mustdown.mustdown(tpl, name: 'Tom')
177
+ # # => '<h1>Hello Tom</h1>'
178
+ #
179
+ # tpl = '{{url}}'
180
+ # website = { url: 'http://example.com' }
181
+ # Mustdown.markdown(tpl, website, autolink: false)
182
+ # # => '<p>http://example.com</p>'
183
+ #
184
+ # tpl = '[{{title}}]({{url}})'
185
+ # website = { title: 'Example', url: 'http://example.com' }
186
+ # Mustdown.markdown(tpl, website, { autolink: false }, { no_links: true })
187
+ # # => '<p>[Example](http://example.com)</p>'
188
+ #
189
+ # Returns the rendered mustache+arkdown String.
190
+ def mustdown(template, resource, *markdown_args)
191
+ markdown mustache(template, resource), *markdown_args
192
+ end
34
193
 
35
- def renderer
36
- @renderer ||= Redcarpet::Render::HTML
194
+ # Private: Outputs a warning message.
195
+ #
196
+ # In a Rails app uses Rails.logger, $stderr otherwise.
197
+ #
198
+ # message - The String message to display as a warning.
199
+ #
200
+ # Returns nothing.
201
+ def warn(message)
202
+ if defined?(Rails)
203
+ Rails.logger.warn message
204
+ else
205
+ $stderr.puts "WARNING: #{message}"
37
206
  end
38
207
  end
39
208
  end
metadata CHANGED
@@ -1,94 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Simon COURTOIS
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-03 00:00:00.000000000 Z
11
+ date: 2013-07-31 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.1.1
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.1.1
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: redcarpet
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
- version: '2'
33
+ version: 2.0.0
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
- version: '2'
40
+ version: 2.0.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: mustache
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: 0.99.4
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: 0.99.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.6'
62
69
  - !ruby/object:Gem::Dependency
63
70
  name: sqlite3
64
71
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
72
  requirements:
67
- - - ! '>='
73
+ - - '>='
68
74
  - !ruby/object:Gem::Version
69
75
  version: '0'
70
76
  type: :development
71
77
  prerelease: false
72
78
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
79
  requirements:
75
- - - ! '>='
80
+ - - '>='
76
81
  - !ruby/object:Gem::Version
77
82
  version: '0'
78
83
  - !ruby/object:Gem::Dependency
79
84
  name: rspec-rails
80
85
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
86
  requirements:
83
- - - ! '>='
87
+ - - '>='
84
88
  - !ruby/object:Gem::Version
85
89
  version: '0'
86
90
  type: :development
87
91
  prerelease: false
88
92
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
93
  requirements:
91
- - - ! '>='
94
+ - - '>='
92
95
  - !ruby/object:Gem::Version
93
96
  version: '0'
94
97
  description: Rails helpers to use Markdown and Mustache all together
@@ -108,33 +111,27 @@ files:
108
111
  - Rakefile
109
112
  - README.md
110
113
  homepage: http://github.com/simonc/mustdown
111
- licenses: []
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
112
117
  post_install_message:
113
118
  rdoc_options: []
114
119
  require_paths:
115
120
  - lib
116
121
  required_ruby_version: !ruby/object:Gem::Requirement
117
- none: false
118
122
  requirements:
119
- - - ! '>='
123
+ - - '>='
120
124
  - !ruby/object:Gem::Version
121
125
  version: '0'
122
- segments:
123
- - 0
124
- hash: -3051764860609270774
125
126
  required_rubygems_version: !ruby/object:Gem::Requirement
126
- none: false
127
127
  requirements:
128
- - - ! '>='
128
+ - - '>='
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
- segments:
132
- - 0
133
- hash: -3051764860609270774
134
131
  requirements: []
135
132
  rubyforge_project:
136
- rubygems_version: 1.8.23
133
+ rubygems_version: 2.0.3
137
134
  signing_key:
138
- specification_version: 3
135
+ specification_version: 4
139
136
  summary: Rails helpers to use Markdown and Mustache all together
140
137
  test_files: []