Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 0.2.1

  • Loading branch information...
commit 22558521e3014bf8d089f34a12a8d3c66db7e9dd 1 parent 35bbf68
@txus txus authored
View
3  Readme.md
@@ -45,8 +45,9 @@ You save your posts with markdown text like this:
Markdown is awesome!
## Some H2 title...
- {% caption 'This caption will become an h5 and also a property of the enclosing div' %}
{% code ruby %}
+ {% caption 'This caption will become an h5 and also a property of the enclosing div' %}
+ {% highlight [1,2,3] %} <- this will highlight lines 1, 2 and 3 (it accepts a Range as well)
# All this code will be highlighted properly! :)
def my_method(*my_args)
View
45 lib/markdownizer.rb
@@ -91,17 +91,48 @@ def markdown(text)
# ruby %}` until `{% endcode %}` and replaces it with appropriate classes for
# code highlighting. It can take many languages aside from Ruby.
#
- # It also parses the special idiom {% caption 'my caption' %}, which both
- # introduces an h5 before the code and passes the caption to the enclosing div
- # as a parameter.
+ # It also parses a couple of special idioms:
+ #
+ # * {% caption 'my caption' %} introduces an h5 before the code and passes
+ # the caption to the enclosing div as well.
+ #
+ # * {% highlight [1,2,3] %} highlights lines 1, 2 and 3. It accepts any
+ # Enumerable, so you can also give a Range (1..3).
+ #
def coderay(text, options = {})
- text.gsub(%r[\{% caption '([\w\s]+)' %\}]) do
+ text.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
+ code, language = $2, $1
+
+ 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))
+ end
+ end
+
+ private
+
+ def extract_caption_from(code, options)
+ caption = nil
+ code.gsub!(%r[\{% caption '([\w\s]+)' %\}]) do
options.merge!({:caption => $1}) if $1
- "#####" << $1
- end.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
- CodeRay.scan($2, $1).div({:css => :class}.merge(options))
+ caption = "<h5>" << $1 << "</h5>"
+ ''
+ end
+ [code, options, caption]
+ end
+
+ # FIXME: Find a safer way to eval code, MY LORD
+ def extract_highlights_from(code, options)
+ code.gsub!(%r[\{% highlight (.+) %\}]) do
+ enumerable = eval($1)
+ enumerable = (Enumerable === enumerable)? enumerable : nil
+ options.merge!({:highlight_lines => enumerable}) if enumerable
+ ''
end
+ [code, options]
end
+
end
#### Public interface
View
2  lib/markdownizer/version.rb
@@ -1,3 +1,3 @@
module Markdownizer
- VERSION = "0.2.0"
+ VERSION = "0.2.1"
end
View
44 spec/markdownizer_spec.rb
@@ -27,8 +27,32 @@ def function(*args)
let(:text_with_caption) { """
#My markdown text
+ {% code ruby %}
{% caption 'This will become an h5' %}
+ def function(*args)
+ puts 'result'
+ end
+ {% endcode %}
+
+ """
+ }
+ let(:text_with_array_highlights) { """
+ #My markdown text
+
+ {% code ruby %}
+ {% highlight [1,2,3] %}
+ def function(*args)
+ puts 'result'
+ end
+ {% endcode %}
+
+ """
+ }
+ let(:text_with_range_highlights) { """
+ #My markdown text
+
{% code ruby %}
+ {% highlight (1..3) %}
def function(*args)
puts 'result'
end
@@ -51,14 +75,28 @@ def function(*args)
subject.coderay(text, :my => :option).should match('parsed code')
end
it 'accepts a caption option inside the code' do
- subject.coderay(text_with_caption).should match('#####This will become an h5')
+ subject.coderay(text_with_caption).should match('<h5>This will become an h5</h5>')
end
it 'passes the caption to the div' do
parsed = double :parsed
CodeRay.should_receive(:scan).and_return parsed
- parsed.should_receive(:div).with(:css => :class, :caption => 'This will become an h5')
+ parsed.should_receive(:div).with(:css => :class, :caption => 'This will become an h5').and_return 'result'
+
+ subject.coderay(text_with_caption)
+ end
+ it 'accepts highlighted lines with an array' do
+ parsed = double :parsed
+ CodeRay.should_receive(:scan).and_return parsed
+ parsed.should_receive(:div).with(:css => :class, :highlight_lines => [1,2,3]).and_return 'result'
+
+ subject.coderay(text_with_array_highlights)
+ end
+ it 'accepts highlighted lines with a range' do
+ parsed = double :parsed
+ CodeRay.should_receive(:scan).and_return parsed
+ parsed.should_receive(:div).with(:css => :class, :highlight_lines => (1..3)).and_return 'result'
- subject.coderay(text_with_caption).should match('#####This will become an h5')
+ subject.coderay(text_with_range_highlights)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.