Skip to content

Commit

Permalink
Markdownizer 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Feb 7, 2011
1 parent 0abbf48 commit 5670401
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 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.2.1)
markdownizer (0.3.0)
activerecord (>= 3.0.3)
coderay
rdiscount
Expand Down
14 changes: 12 additions & 2 deletions lib/markdownizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def markdown(text)
# ruby %}` until `{% endcode %}` and replaces it with appropriate classes for
# code highlighting. It can take many languages aside from Ruby.
#
# With a hash of options you can specify `:line_numbers` (`:table` or `:inline`),
# and the class of the enclosing div with `:enclosing_class`.
#
# It also parses a couple of special idioms:
#
# * {% caption 'my caption' %} introduces an h5 before the code and passes
Expand All @@ -104,12 +107,19 @@ def coderay(text, options = {})
options.delete(:highlight_lines)
options.delete(:caption)

enclosing_class = options[:enclosing_class] || 'markdownizer_code'

code, language = $2.strip, $1.strip

code, options, caption = extract_caption_from(code, options)
code, options = extract_highlights_from(code, options)

(caption || '') << CodeRay.scan(code, language).div({:css => :class}.merge(options))
html_caption = caption ? '<h5>' << caption << '</h5>' : nil

"<div class=\"#{enclosing_class}#{caption ? "\" caption=\"#{caption}" : ''}\">" <<
(html_caption || '') <<
CodeRay.scan(code, language).div({:css => :class}.merge(options)) <<
"</div>"
end
end

Expand All @@ -119,7 +129,7 @@ def extract_caption_from(code, options)
caption = nil
code.gsub!(%r[\{% caption '([\w\s]+)' %\}]) do
options.merge!({:caption => $1.strip}) if $1
caption = "<h5>" << $1.strip << "</h5>"
caption = $1.strip
''
end
[code.strip, options, caption]
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.0"
VERSION = "0.3.1"
end
3 changes: 3 additions & 0 deletions spec/markdownizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def function(*args)

subject.coderay(text_with_range_highlights)
end
it 'encloses everything in a nice class' do
subject.coderay(text_with_caption).should match(/div class=\"markdownizer_code\" caption=\"This will become an h5\"/)
end
end

describe Markdownizer::DSL do
Expand Down

0 comments on commit 5670401

Please sign in to comment.