markdownizer 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Readme.md +2 -1
- data/lib/markdownizer.rb +38 -7
- data/lib/markdownizer/version.rb +1 -1
- data/spec/markdownizer_spec.rb +41 -3
- metadata +2 -2
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)
|
data/lib/markdownizer.rb
CHANGED
|
@@ -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
|
|
95
|
-
#
|
|
96
|
-
#
|
|
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[\{%
|
|
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
|
-
"
|
|
101
|
-
|
|
102
|
-
|
|
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
|
data/lib/markdownizer/version.rb
CHANGED
data/spec/markdownizer_spec.rb
CHANGED
|
@@ -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('
|
|
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(
|
|
99
|
+
subject.coderay(text_with_range_highlights)
|
|
62
100
|
end
|
|
63
101
|
end
|
|
64
102
|
|