Permalink
Browse files

Hierarchy level for headers

  • Loading branch information...
1 parent 5670401 commit f03d456e9b98173f0a19e063435d043e1eab19ab @txus txus committed Feb 7, 2011
Showing with 43 additions and 12 deletions.
  1. +1 −1 Gemfile.lock
  2. +15 −2 lib/markdownizer.rb
  3. +1 −1 lib/markdownizer/version.rb
  4. +26 −8 spec/markdownizer_spec.rb
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- markdownizer (0.3.0)
+ markdownizer (0.3.1)
activerecord (>= 3.0.3)
coderay
rdiscount
View
@@ -83,7 +83,12 @@ class << self
# perform the corresponding conversions and parsings.
# `Markdownizer.markdown` method converts plain Markdown text to formatted html.
- def markdown(text)
+ # To parse the markdown in a coherent hierarchical context, you must provide it
+ # with the current hierarchical level of the text to be parsed.
+ def markdown(text, hierarchy = 0)
+ text.gsub! %r[(#+)([\w\s]+)] do
+ ('#' * hierarchy) << $1 << $2
+ end
RDiscount.new(text).to_html
end
@@ -170,14 +175,22 @@ def markdownize! attribute, options = {}
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."
end
+ # The `:hierarchy` option tells Markdownizer the smallest header tag that
+ # precedes the Markdown text. If you have a blogpost with an H1 (title) and
+ # an H2 (some kind of tagline), then your hierarchy is 2, and the biggest
+ # header found the markdown text will be translated directly to an H3. This
+ # allows for semantical coherence within the context where the markdown text
+ # is to be introduced.
+ hierarchy = options.delete(:hierarchy) || 0
+
# Create a `before_save` callback which will convert plain text to
# Markdownized html every time the model is saved.
self.before_save :"render_#{attribute}"
# Define the converter method, which will assign the rendered html to the
# `:rendered_attribute` field.
define_method :"render_#{attribute}" do
- self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute), options)))
+ self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(self.send(attribute), options), hierarchy))
end
end
end
@@ -1,3 +1,3 @@
module Markdownizer
- VERSION = "0.3.1"
+ VERSION = "0.3.2"
end
@@ -3,13 +3,31 @@
describe Markdownizer do
describe ".markdown(text)" do
let(:text) { "#My markdown text"}
- it 'calls RDiscount to markdownize the text' do
- rdiscount, html_markdown = double(:rdiscount), double(:html_markdown)
-
- RDiscount.should_receive(:new).with(text).and_return rdiscount
- rdiscount.should_receive(:to_html).and_return html_markdown
-
- subject.markdown(text).should == html_markdown
+ context 'when the hierarchy is 0' do
+ it 'calls RDiscount to markdownize the text' do
+ rdiscount, html_markdown = double(:rdiscount), double(:html_markdown)
+
+ RDiscount.should_receive(:new).with(text).and_return rdiscount
+ rdiscount.should_receive(:to_html).and_return html_markdown
+
+ subject.markdown(text).should == html_markdown
+ end
+ end
+ context 'when the hierarchy is 2' do
+ it "converts all headers into 2 levels deeper" do
+ my_text = """
+ #This is an H1
+ ##This is an H2
+ ###This is an H3
+ """
+ result = double :result, to_html: true
+ RDiscount.should_receive(:new).with do |t|
+ t.should =~ /###This is an H1/
+ t.should =~ /####This is an H2/
+ t.should =~ /#####This is an H3/
+ end.and_return result
+ subject.markdown(my_text, 2)
+ end
end
end
describe ".coderay(text)" do
@@ -143,7 +161,7 @@ def self.column_names
instance = klass.new
instance.should_receive(:send).with(:body).and_return raw_body
Markdownizer.should_receive(:coderay).with(raw_body, {}).and_return raw_body_with_code
- Markdownizer.should_receive(:markdown).with(raw_body_with_code).and_return final_code
+ Markdownizer.should_receive(:markdown).with(raw_body_with_code, 0).and_return final_code
instance.should_receive(:send).with(:rendered_body=, final_code)
instance.render_body

0 comments on commit f03d456

Please sign in to comment.