markdownizer 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- markdownizer (0.3.0)
4
+ markdownizer (0.3.1)
5
5
  activerecord (>= 3.0.3)
6
6
  coderay
7
7
  rdiscount
@@ -83,7 +83,12 @@ module Markdownizer
83
83
  # perform the corresponding conversions and parsings.
84
84
 
85
85
  # `Markdownizer.markdown` method converts plain Markdown text to formatted html.
86
- def markdown(text)
86
+ # To parse the markdown in a coherent hierarchical context, you must provide it
87
+ # with the current hierarchical level of the text to be parsed.
88
+ def markdown(text, hierarchy = 0)
89
+ text.gsub! %r[(#+)([\w\s]+)] do
90
+ ('#' * hierarchy) << $1 << $2
91
+ end
87
92
  RDiscount.new(text).to_html
88
93
  end
89
94
 
@@ -170,6 +175,14 @@ module Markdownizer
170
175
  raise "#{self.name} doesn't have required attributes :#{attribute} and :rendered_#{attribute}\nPlease generate a migration to add these attributes -- both should have type :text."
171
176
  end
172
177
 
178
+ # The `:hierarchy` option tells Markdownizer the smallest header tag that
179
+ # precedes the Markdown text. If you have a blogpost with an H1 (title) and
180
+ # an H2 (some kind of tagline), then your hierarchy is 2, and the biggest
181
+ # header found the markdown text will be translated directly to an H3. This
182
+ # allows for semantical coherence within the context where the markdown text
183
+ # is to be introduced.
184
+ hierarchy = options.delete(:hierarchy) || 0
185
+
173
186
  # Create a `before_save` callback which will convert plain text to
174
187
  # Markdownized html every time the model is saved.
175
188
  self.before_save :"render_#{attribute}"
@@ -177,7 +190,7 @@ module Markdownizer
177
190
  # Define the converter method, which will assign the rendered html to the
178
191
  # `:rendered_attribute` field.
179
192
  define_method :"render_#{attribute}" do
180
- self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute), options)))
193
+ self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute), options), hierarchy))
181
194
  end
182
195
  end
183
196
  end
@@ -1,3 +1,3 @@
1
1
  module Markdownizer
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -3,13 +3,31 @@ require 'spec_helper'
3
3
  describe Markdownizer do
4
4
  describe ".markdown(text)" do
5
5
  let(:text) { "#My markdown text"}
6
- it 'calls RDiscount to markdownize the text' do
7
- rdiscount, html_markdown = double(:rdiscount), double(:html_markdown)
8
-
9
- RDiscount.should_receive(:new).with(text).and_return rdiscount
10
- rdiscount.should_receive(:to_html).and_return html_markdown
11
-
12
- subject.markdown(text).should == html_markdown
6
+ context 'when the hierarchy is 0' do
7
+ it 'calls RDiscount to markdownize the text' do
8
+ rdiscount, html_markdown = double(:rdiscount), double(:html_markdown)
9
+
10
+ RDiscount.should_receive(:new).with(text).and_return rdiscount
11
+ rdiscount.should_receive(:to_html).and_return html_markdown
12
+
13
+ subject.markdown(text).should == html_markdown
14
+ end
15
+ end
16
+ context 'when the hierarchy is 2' do
17
+ it "converts all headers into 2 levels deeper" do
18
+ my_text = """
19
+ #This is an H1
20
+ ##This is an H2
21
+ ###This is an H3
22
+ """
23
+ result = double :result, to_html: true
24
+ RDiscount.should_receive(:new).with do |t|
25
+ t.should =~ /###This is an H1/
26
+ t.should =~ /####This is an H2/
27
+ t.should =~ /#####This is an H3/
28
+ end.and_return result
29
+ subject.markdown(my_text, 2)
30
+ end
13
31
  end
14
32
  end
15
33
  describe ".coderay(text)" do
@@ -143,7 +161,7 @@ describe Markdownizer do
143
161
  instance = klass.new
144
162
  instance.should_receive(:send).with(:body).and_return raw_body
145
163
  Markdownizer.should_receive(:coderay).with(raw_body, {}).and_return raw_body_with_code
146
- Markdownizer.should_receive(:markdown).with(raw_body_with_code).and_return final_code
164
+ Markdownizer.should_receive(:markdown).with(raw_body_with_code, 0).and_return final_code
147
165
 
148
166
  instance.should_receive(:send).with(:rendered_body=, final_code)
149
167
  instance.render_body
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Josep M. Bach