Skip to content

Commit

Permalink
Hierarchy level for headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Feb 7, 2011
1 parent 5670401 commit f03d456
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
markdownizer (0.3.0)
markdownizer (0.3.1)
activerecord (>= 3.0.3)
coderay
rdiscount
Expand Down
17 changes: 15 additions & 2 deletions lib/markdownizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/markdownizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Markdownizer
VERSION = "0.3.1"
VERSION = "0.3.2"
end
34 changes: 26 additions & 8 deletions spec/markdownizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f03d456

Please sign in to comment.