markdownizer 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/Readme.md +9 -3
- data/lib/markdownizer.rb +17 -5
- data/lib/markdownizer/version.rb +1 -1
- data/spec/markdownizer_spec.rb +26 -3
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/Readme.md
CHANGED
@@ -26,7 +26,12 @@ whatever.
|
|
26
26
|
In your model, let's say, Post:
|
27
27
|
|
28
28
|
class Post < ActiveRecord::Base
|
29
|
-
markdownize! :body
|
29
|
+
markdownize! :body
|
30
|
+
# In this case we want to treat :body as markdown.
|
31
|
+
# You can pass an options hash to the code renderer, such as:
|
32
|
+
#
|
33
|
+
# markdownize! :body, :line_numbers => :table
|
34
|
+
#
|
30
35
|
end
|
31
36
|
|
32
37
|
Markdownizer needs an additional field (`:rendered_body`), which you should
|
@@ -40,7 +45,8 @@ You save your posts with markdown text like this:
|
|
40
45
|
Markdown is awesome!
|
41
46
|
## Some H2 title...
|
42
47
|
|
43
|
-
{%
|
48
|
+
{% caption 'This caption will become an h5 and also a property of the enclosing div' %}
|
49
|
+
{% code ruby %}
|
44
50
|
|
45
51
|
# All this code will be highlighted properly! :)
|
46
52
|
def my_method(*my_args)
|
@@ -49,7 +55,7 @@ You save your posts with markdown text like this:
|
|
49
55
|
end
|
50
56
|
end
|
51
57
|
|
52
|
-
{%
|
58
|
+
{% endcode %}
|
53
59
|
"""
|
54
60
|
|
55
61
|
And then, in your view you just have to call `@post.rendered_body` :)
|
data/lib/markdownizer.rb
CHANGED
@@ -90,9 +90,16 @@ module Markdownizer
|
|
90
90
|
# `Markdownizer.coderay` method parses a code block delimited from `{% code
|
91
91
|
# ruby %}` until `{% endcode %}` and replaces it with appropriate classes for
|
92
92
|
# code highlighting. It can take many languages aside from Ruby.
|
93
|
-
|
94
|
-
|
95
|
-
|
93
|
+
#
|
94
|
+
# It also parses the special idiom {% caption 'my caption' %}, which both
|
95
|
+
# introduces an h5 before the code and passes the caption to the enclosing div
|
96
|
+
# as a parameter.
|
97
|
+
def coderay(text, options = {})
|
98
|
+
text.gsub(%r[\{% caption '([\w\s]+)' %\}]) do
|
99
|
+
options.merge!({:caption => $1}) if $1
|
100
|
+
"#####" << $1
|
101
|
+
end.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
|
102
|
+
CodeRay.scan($2, $1).div({:css => :class}.merge(options))
|
96
103
|
end
|
97
104
|
end
|
98
105
|
end
|
@@ -105,7 +112,12 @@ module Markdownizer
|
|
105
112
|
|
106
113
|
# Calling `markdownize! :attribute` (where `:attribute` can be any database
|
107
114
|
# attribute with type `text`) will treat this field as Markdown.
|
108
|
-
|
115
|
+
# You can pass an `options` hash for CodeRay. An example option would be:
|
116
|
+
#
|
117
|
+
# * `:line_numbers => :table` (or `:inline`)
|
118
|
+
#
|
119
|
+
# You can check other available options in CodeRay's documentation.
|
120
|
+
def markdownize! attribute, options = {}
|
109
121
|
# Check that both `:attribute` and `:rendered_attribute` columns exist.
|
110
122
|
# If they don't, it raises an error indicating that the user should generate
|
111
123
|
# a migration.
|
@@ -121,7 +133,7 @@ module Markdownizer
|
|
121
133
|
# Define the converter method, which will assign the rendered html to the
|
122
134
|
# `:rendered_attribute` field.
|
123
135
|
define_method :"render_#{attribute}" do
|
124
|
-
self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute))))
|
136
|
+
self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute), options)))
|
125
137
|
end
|
126
138
|
end
|
127
139
|
end
|
data/lib/markdownizer/version.rb
CHANGED
data/spec/markdownizer_spec.rb
CHANGED
@@ -24,6 +24,19 @@ describe Markdownizer do
|
|
24
24
|
|
25
25
|
"""
|
26
26
|
}
|
27
|
+
let(:text_with_caption) { """
|
28
|
+
#My markdown text
|
29
|
+
|
30
|
+
{% caption 'This will become an h5' %}
|
31
|
+
{% code ruby %}
|
32
|
+
def function(*args)
|
33
|
+
puts 'result'
|
34
|
+
end
|
35
|
+
{% endcode %}
|
36
|
+
|
37
|
+
"""
|
38
|
+
}
|
39
|
+
|
27
40
|
it 'calls CodeRay to parse the code inside {% highlight ruby %} blocks' do
|
28
41
|
scanned_code, html_code = double(:scanned_code), double(:html_code)
|
29
42
|
|
@@ -33,9 +46,19 @@ describe Markdownizer do
|
|
33
46
|
end
|
34
47
|
""", 'ruby').and_return scanned_code
|
35
48
|
|
36
|
-
scanned_code.should_receive(:div).with(:css => :class).and_return 'parsed code'
|
49
|
+
scanned_code.should_receive(:div).with(:css => :class, :my => :option).and_return 'parsed code'
|
50
|
+
|
51
|
+
subject.coderay(text, :my => :option).should match('parsed code')
|
52
|
+
end
|
53
|
+
it 'accepts a caption option inside the code' do
|
54
|
+
subject.coderay(text_with_caption).should match('#####This will become an h5')
|
55
|
+
end
|
56
|
+
it 'passes the caption to the div' do
|
57
|
+
parsed = double :parsed
|
58
|
+
CodeRay.should_receive(:scan).and_return parsed
|
59
|
+
parsed.should_receive(:div).with(:css => :class, :caption => 'This will become an h5')
|
37
60
|
|
38
|
-
subject.coderay(
|
61
|
+
subject.coderay(text_with_caption).should match('#####This will become an h5')
|
39
62
|
end
|
40
63
|
end
|
41
64
|
|
@@ -80,7 +103,7 @@ describe Markdownizer do
|
|
80
103
|
|
81
104
|
instance = klass.new
|
82
105
|
instance.should_receive(:send).with(:body).and_return raw_body
|
83
|
-
Markdownizer.should_receive(:coderay).with(raw_body).and_return raw_body_with_code
|
106
|
+
Markdownizer.should_receive(:coderay).with(raw_body, {}).and_return raw_body_with_code
|
84
107
|
Markdownizer.should_receive(:markdown).with(raw_body_with_code).and_return final_code
|
85
108
|
|
86
109
|
instance.should_receive(:send).with(:rendered_body=, final_code)
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Josep M. Bach
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-02-
|
19
|
+
date: 2011-02-07 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|