markdownizer 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -45,8 +45,9 @@ You save your posts with markdown text like this:
45
45
  Markdown is awesome!
46
46
  ## Some H2 title...
47
47
 
48
- {% caption 'This caption will become an h5 and also a property of the enclosing div' %}
49
48
  {% code ruby %}
49
+ {% caption 'This caption will become an h5 and also a property of the enclosing div' %}
50
+ {% highlight [1,2,3] %} <- this will highlight lines 1, 2 and 3 (it accepts a Range as well)
50
51
 
51
52
  # All this code will be highlighted properly! :)
52
53
  def my_method(*my_args)
@@ -91,17 +91,48 @@ module Markdownizer
91
91
  # ruby %}` until `{% endcode %}` and replaces it with appropriate classes for
92
92
  # code highlighting. It can take many languages aside from Ruby.
93
93
  #
94
- # It also parses the special idiom {% caption 'my caption' %}, which both
95
- # introduces an h5 before the code and passes the caption to the enclosing div
96
- # as a parameter.
94
+ # It also parses a couple of special idioms:
95
+ #
96
+ # * {% caption 'my caption' %} introduces an h5 before the code and passes
97
+ # the caption to the enclosing div as well.
98
+ #
99
+ # * {% highlight [1,2,3] %} highlights lines 1, 2 and 3. It accepts any
100
+ # Enumerable, so you can also give a Range (1..3).
101
+ #
97
102
  def coderay(text, options = {})
98
- text.gsub(%r[\{% caption '([\w\s]+)' %\}]) do
103
+ text.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
104
+ code, language = $2, $1
105
+
106
+ code, options, caption = extract_caption_from(code, options)
107
+ code, options = extract_highlights_from(code, options)
108
+
109
+ (caption || '') << CodeRay.scan(code, language).div({:css => :class}.merge(options))
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ def extract_caption_from(code, options)
116
+ caption = nil
117
+ code.gsub!(%r[\{% caption '([\w\s]+)' %\}]) do
99
118
  options.merge!({:caption => $1}) if $1
100
- "#####" << $1
101
- end.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
102
- CodeRay.scan($2, $1).div({:css => :class}.merge(options))
119
+ caption = "<h5>" << $1 << "</h5>"
120
+ ''
121
+ end
122
+ [code, options, caption]
123
+ end
124
+
125
+ # FIXME: Find a safer way to eval code, MY LORD
126
+ def extract_highlights_from(code, options)
127
+ code.gsub!(%r[\{% highlight (.+) %\}]) do
128
+ enumerable = eval($1)
129
+ enumerable = (Enumerable === enumerable)? enumerable : nil
130
+ options.merge!({:highlight_lines => enumerable}) if enumerable
131
+ ''
103
132
  end
133
+ [code, options]
104
134
  end
135
+
105
136
  end
106
137
 
107
138
  #### Public interface
@@ -1,3 +1,3 @@
1
1
  module Markdownizer
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -27,8 +27,32 @@ describe Markdownizer do
27
27
  let(:text_with_caption) { """
28
28
  #My markdown text
29
29
 
30
+ {% code ruby %}
30
31
  {% caption 'This will become an h5' %}
32
+ def function(*args)
33
+ puts 'result'
34
+ end
35
+ {% endcode %}
36
+
37
+ """
38
+ }
39
+ let(:text_with_array_highlights) { """
40
+ #My markdown text
41
+
42
+ {% code ruby %}
43
+ {% highlight [1,2,3] %}
44
+ def function(*args)
45
+ puts 'result'
46
+ end
47
+ {% endcode %}
48
+
49
+ """
50
+ }
51
+ let(:text_with_range_highlights) { """
52
+ #My markdown text
53
+
31
54
  {% code ruby %}
55
+ {% highlight (1..3) %}
32
56
  def function(*args)
33
57
  puts 'result'
34
58
  end
@@ -51,14 +75,28 @@ describe Markdownizer do
51
75
  subject.coderay(text, :my => :option).should match('parsed code')
52
76
  end
53
77
  it 'accepts a caption option inside the code' do
54
- subject.coderay(text_with_caption).should match('#####This will become an h5')
78
+ subject.coderay(text_with_caption).should match('<h5>This will become an h5</h5>')
55
79
  end
56
80
  it 'passes the caption to the div' do
57
81
  parsed = double :parsed
58
82
  CodeRay.should_receive(:scan).and_return parsed
59
- parsed.should_receive(:div).with(:css => :class, :caption => 'This will become an h5')
83
+ parsed.should_receive(:div).with(:css => :class, :caption => 'This will become an h5').and_return 'result'
84
+
85
+ subject.coderay(text_with_caption)
86
+ end
87
+ it 'accepts highlighted lines with an array' do
88
+ parsed = double :parsed
89
+ CodeRay.should_receive(:scan).and_return parsed
90
+ parsed.should_receive(:div).with(:css => :class, :highlight_lines => [1,2,3]).and_return 'result'
91
+
92
+ subject.coderay(text_with_array_highlights)
93
+ end
94
+ it 'accepts highlighted lines with a range' do
95
+ parsed = double :parsed
96
+ CodeRay.should_receive(:scan).and_return parsed
97
+ parsed.should_receive(:div).with(:css => :class, :highlight_lines => (1..3)).and_return 'result'
60
98
 
61
- subject.coderay(text_with_caption).should match('#####This will become an h5')
99
+ subject.coderay(text_with_range_highlights)
62
100
  end
63
101
  end
64
102
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Josep M. Bach