octopress-pygments 1.0.0 → 1.1.0
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/.rspec +1 -0
- data/.travis.yml +2 -0
- data/Rakefile +5 -0
- data/lib/octopress-pygments.rb +8 -166
- data/lib/octopress-pygments/cache.rb +30 -0
- data/lib/octopress-pygments/options_parser.rb +109 -0
- data/lib/octopress-pygments/renderer.rb +111 -0
- data/lib/octopress-pygments/version.rb +1 -1
- data/octopress-pygments.gemspec +5 -1
- data/spec/octopress-pygments_spec.rb +0 -0
- data/spec/pygments_spec.rb +146 -0
- data/spec/spec_helper.rb +3 -0
- metadata +59 -3
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Rakefile
CHANGED
data/lib/octopress-pygments.rb
CHANGED
@@ -9,178 +9,20 @@ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
|
|
9
9
|
|
10
10
|
module Octopress
|
11
11
|
module Pygments
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
|
16
|
-
highlighted_code.to_s
|
17
|
-
end
|
12
|
+
autoload :Cache, 'octopress-pygments/cache'
|
13
|
+
autoload :OptionsParser, 'octopress-pygments/options_parser'
|
14
|
+
autoload :Renderer, 'octopress-pygments/renderer'
|
18
15
|
|
19
16
|
def self.highlight(code, options = {})
|
20
|
-
|
21
|
-
lang = 'ruby' if lang == 'ru'
|
22
|
-
lang = 'objc' if lang == 'm'
|
23
|
-
lang = 'perl' if lang == 'pl'
|
24
|
-
lang = 'yaml' if lang == 'yml'
|
25
|
-
lang = 'coffeescript' if lang == 'coffee'
|
26
|
-
lang = 'csharp' if lang == 'cs'
|
27
|
-
lang = 'plain' if lang == '' or lang.nil? or !lang
|
28
|
-
|
29
|
-
options[:lang] = lang
|
30
|
-
options[:title] ||= ' ' if options[:url]
|
31
|
-
|
32
|
-
# Attempt to retrieve cached code
|
33
|
-
cache = nil
|
34
|
-
unless options[:no_cache]
|
35
|
-
path = options[:cache_path] || get_cache_path(PYGMENTS_CACHE_DIR, options[:lang], options.to_s + code)
|
36
|
-
cache = read_cache(path)
|
37
|
-
end
|
38
|
-
|
39
|
-
unless cache
|
40
|
-
if options[:lang] == 'plain'
|
41
|
-
# Escape html tags
|
42
|
-
code = code.gsub('<','<')
|
43
|
-
else
|
44
|
-
code = render_pygments(code, options[:lang]).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
|
45
|
-
end
|
46
|
-
code = tableize_code(code, options[:lang], {linenos: options[:linenos], start: options[:start], marks: options[:marks]})
|
47
|
-
title = captionize(options[:title], options[:url], options[:link_text]) if options[:title]
|
48
|
-
code = "<figure class='code'>#{title}#{code}</figure>"
|
49
|
-
File.open(path, 'w') {|f| f.print(code) } unless options[:no_cache]
|
50
|
-
end
|
51
|
-
cache || code
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.read_cache (path)
|
55
|
-
File.exist?(path) ? File.read(path) : nil unless path.nil?
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.get_cache_path (dir, name, str)
|
59
|
-
File.join(dir, "#{name}-#{Digest::MD5.hexdigest(str)}.html")
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.captionize (caption, url, link_text)
|
63
|
-
figcaption = "<figcaption>#{caption}"
|
64
|
-
figcaption += "<a href='#{url}'>#{(link_text || 'link').strip}</a>" if url
|
65
|
-
figcaption += "</figcaption>"
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.tableize_code (code, lang, options = {})
|
69
|
-
start = options[:start] || 1
|
70
|
-
lines = options[:linenos] || true
|
71
|
-
marks = options[:marks] || []
|
72
|
-
table = "<div class='highlight'><table><tr>"
|
73
|
-
table += number_lines(start, code.lines.count, marks) if lines
|
74
|
-
table += "<td class='main #{'unnumbered' unless lines} #{lang}'><pre>"
|
75
|
-
code.lines.each_with_index do |line,index|
|
76
|
-
classes = 'line'
|
77
|
-
if marks.include? index + start
|
78
|
-
classes += ' marked'
|
79
|
-
classes += ' start' unless marks.include? index - 1 + start
|
80
|
-
classes += ' end' unless marks.include? index + 1 + start
|
81
|
-
end
|
82
|
-
line = line.strip.empty? ? ' ' : line
|
83
|
-
table += "<div class='#{classes}'>#{line}</div>"
|
84
|
-
end
|
85
|
-
table +="</pre></td></tr></table></div>"
|
86
|
-
end
|
87
|
-
|
88
|
-
def self.number_lines (start, count, marks)
|
89
|
-
start ||= 1
|
90
|
-
lines = "<td class='line-numbers' aria-hidden='true'><pre>"
|
91
|
-
count.times do |index|
|
92
|
-
classes = 'line-number'
|
93
|
-
if marks.include? index + start
|
94
|
-
classes += ' marked'
|
95
|
-
classes += ' start' unless marks.include? index - 1 + start
|
96
|
-
classes += ' end' unless marks.include? index + 1 + start
|
97
|
-
end
|
98
|
-
lines += "<div data-line='#{index + start}' class='#{classes}'></div>"
|
99
|
-
end
|
100
|
-
lines += "</pre></td>"
|
101
|
-
end
|
102
|
-
|
103
|
-
def self.parse_markup (input, defaults={})
|
104
|
-
lang = input.match(/\s*lang:\s*(\S+)/i)
|
105
|
-
lang = (lang.nil? ? nil : lang[1])
|
106
|
-
|
107
|
-
url = input.match(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
|
108
|
-
url = (url.nil? ? nil : url[3] || url[5] || url[6])
|
109
|
-
|
110
|
-
title = input.match(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
|
111
|
-
title = (title.nil? ? nil : title[3] || title[5] || title[6])
|
112
|
-
title ||= ' ' if url
|
113
|
-
|
114
|
-
linenos = input.match(/\s*linenos:\s*(\w+)/i)
|
115
|
-
linenos = (linenos.nil? ? nil : linenos[1])
|
116
|
-
|
117
|
-
marks = get_marks(input)
|
118
|
-
|
119
|
-
link_text = input.match(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
|
120
|
-
link_text = (link_text.nil? ? 'link' : link_text[3] || link_text[5] || link_text[6])
|
121
|
-
|
122
|
-
start = input.match(/\s*start:\s*(\d+)/i)
|
123
|
-
start = (start.nil? ? nil : start[1].to_i)
|
124
|
-
|
125
|
-
endline = input.match(/\s*end:\s*(\d+)/i)
|
126
|
-
endline = (endline.nil? ? nil : endline[1].to_i)
|
127
|
-
|
128
|
-
if input =~ / *range:(\d+)-(\d+)/i
|
129
|
-
start = $1.to_i
|
130
|
-
endline = $2.to_i
|
131
|
-
end
|
132
|
-
|
133
|
-
options = {
|
134
|
-
lang: lang,
|
135
|
-
url: url,
|
136
|
-
title: title,
|
137
|
-
linenos: linenos,
|
138
|
-
marks: marks,
|
139
|
-
link_text: link_text,
|
140
|
-
start: start,
|
141
|
-
end: endline
|
142
|
-
}
|
143
|
-
|
144
|
-
defaults.each { |k,v| options[k] ||= defaults[k] }
|
145
|
-
options
|
146
|
-
end
|
147
|
-
|
148
|
-
def self.clean_markup (input)
|
149
|
-
input.sub(/\s*lang:\s*\S+/i,''
|
150
|
-
).sub(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i,''
|
151
|
-
).sub(/\s*url:\s*(\S+)/i,''
|
152
|
-
).sub(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i,''
|
153
|
-
).sub(/\s*mark:\s*\d\S*/i,''
|
154
|
-
).sub(/\s*linenos:\s*\w+/i,''
|
155
|
-
).sub(/\s*start:\s*\d+/i,''
|
156
|
-
).sub(/\s*end:\s*\d+/i,''
|
157
|
-
).sub(/\s*range:\s*\d+-\d+/i,'')
|
17
|
+
Renderer.new(code, options).highlight
|
158
18
|
end
|
159
19
|
|
160
|
-
def self.
|
161
|
-
|
162
|
-
# Example input mark:1,5-10,2
|
163
|
-
# Outputs: [1,2,5,6,7,8,9,10]
|
164
|
-
marks = []
|
165
|
-
if input =~ / *mark:(\d\S*)/i
|
166
|
-
marks = $1.gsub /(\d+)-(\d+)/ do
|
167
|
-
($1.to_i..$2.to_i).to_a.join(',')
|
168
|
-
end
|
169
|
-
marks = marks.split(',').collect {|s| s.to_i}.sort
|
170
|
-
end
|
171
|
-
marks
|
20
|
+
def self.parse_markup(input, defaults={})
|
21
|
+
OptionsParser.new(input).parse_markup(defaults)
|
172
22
|
end
|
173
23
|
|
174
|
-
def self.
|
175
|
-
|
176
|
-
start ||= 1
|
177
|
-
endline ||= length
|
178
|
-
if start > 1 or endline < length
|
179
|
-
raise "#{filepath} is #{length} lines long, cannot begin at line #{start}" if start > length
|
180
|
-
raise "#{filepath} is #{length} lines long, cannot read beyond line #{endline}" if endline > length
|
181
|
-
code = code.split(/\n/).slice(start - 1, endline + 1 - start).join("\n")
|
182
|
-
end
|
183
|
-
code
|
24
|
+
def self.clean_markup(input)
|
25
|
+
OptionsParser.new(input).clean_markup
|
184
26
|
end
|
185
27
|
|
186
28
|
def self.highlight_failed(error, syntax, markup, code, file = nil)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Octopress
|
2
|
+
module Pygments
|
3
|
+
class Cache
|
4
|
+
PYGMENTS_CACHE_DIR = '.pygments-cache'
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def fetch_from_cache(code, options)
|
8
|
+
path = options[:cache_path] || get_cache_path(PYGMENTS_CACHE_DIR, options[:lang], options.to_s + code)
|
9
|
+
cache = read_cache(path)
|
10
|
+
end
|
11
|
+
|
12
|
+
def write_to_cache(contents, options)
|
13
|
+
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR) unless File.directory?(PYGMENTS_CACHE_DIR)
|
14
|
+
path = options[:cache_path] || get_cache_path(PYGMENTS_CACHE_DIR, options[:lang], options.to_s + contents)
|
15
|
+
File.open(path, 'w') do |f|
|
16
|
+
f.print(contents)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_cache(path)
|
21
|
+
File.exist?(path) ? File.read(path) : nil unless path.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_cache_path(dir, name, str)
|
25
|
+
File.join(dir, "#{name}-#{Digest::MD5.hexdigest(str)}.html")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module Octopress
|
2
|
+
module Pygments
|
3
|
+
class OptionsParser
|
4
|
+
attr_accessor :input
|
5
|
+
|
6
|
+
def initialize(markup)
|
7
|
+
@input = markup.strip
|
8
|
+
end
|
9
|
+
|
10
|
+
def clean_markup
|
11
|
+
input.sub(/\s*lang:\s*\S+/i,'')
|
12
|
+
.sub(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i,'')
|
13
|
+
.sub(/\s*url:\s*(\S+)/i,'')
|
14
|
+
.sub(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i,'')
|
15
|
+
.sub(/\s*mark:\s*\d\S*/i,'')
|
16
|
+
.sub(/\s*linenos:\s*\w+/i,'')
|
17
|
+
.sub(/\s*start:\s*\d+/i,'')
|
18
|
+
.sub(/\s*end:\s*\d+/i,'')
|
19
|
+
.sub(/\s*range:\s*\d+-\d+/i,'')
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_markup(defaults = {})
|
23
|
+
defaults.merge({
|
24
|
+
lang: lang,
|
25
|
+
url: url,
|
26
|
+
title: title,
|
27
|
+
linenos: linenos,
|
28
|
+
marks: marks,
|
29
|
+
link_text: link_text,
|
30
|
+
start: start,
|
31
|
+
end: endline
|
32
|
+
})
|
33
|
+
end
|
34
|
+
|
35
|
+
def lang
|
36
|
+
extract(/\s*lang:\s*(\S+)/i)
|
37
|
+
end
|
38
|
+
|
39
|
+
def url
|
40
|
+
extract(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6])
|
41
|
+
end
|
42
|
+
|
43
|
+
def title
|
44
|
+
extract(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6])
|
45
|
+
end
|
46
|
+
|
47
|
+
def linenos
|
48
|
+
extract(/\s*linenos:\s*(\w+)/i)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Public: Matches pattern for line marks and returns array of line
|
52
|
+
# numbers to mark
|
53
|
+
#
|
54
|
+
# Example input
|
55
|
+
# Input: "mark:1,5-10,2"
|
56
|
+
# Output: [1,2,5,6,7,8,9,10]
|
57
|
+
#
|
58
|
+
# Returns an array of integers corresponding to the lines which are
|
59
|
+
# indicated as marked
|
60
|
+
def marks
|
61
|
+
marks = []
|
62
|
+
if input =~ / *mark:(\d\S*)/i
|
63
|
+
marks = $1.gsub /(\d+)-(\d+)/ do
|
64
|
+
($1.to_i..$2.to_i).to_a.join(',')
|
65
|
+
end
|
66
|
+
marks = marks.split(',').collect {|s| s.to_i}.sort
|
67
|
+
end
|
68
|
+
marks
|
69
|
+
end
|
70
|
+
|
71
|
+
def link_text
|
72
|
+
extract(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6], 'link')
|
73
|
+
end
|
74
|
+
|
75
|
+
def start
|
76
|
+
if range
|
77
|
+
range.first
|
78
|
+
else
|
79
|
+
extract(/\s*start:\s*(\d+)/i).to_i
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def endline
|
84
|
+
if range
|
85
|
+
range.last
|
86
|
+
else
|
87
|
+
extract(/\s*end:\s*(\d+)/i).to_i
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def range
|
92
|
+
if input.match(/ *range:(\d+)-(\d+)/i)
|
93
|
+
[$1.to_i, $2.to_i]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def extract(regexp, indices_to_try = [1], default = nil)
|
98
|
+
thing = input.match(regexp)
|
99
|
+
if thing.nil?
|
100
|
+
default
|
101
|
+
else
|
102
|
+
indices_to_try.each do |index|
|
103
|
+
return thing[index] if thing[index]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module Octopress
|
2
|
+
module Pygments
|
3
|
+
class Renderer
|
4
|
+
attr_accessor :code, :options, :lang
|
5
|
+
|
6
|
+
def initialize(code, options = {})
|
7
|
+
@code = code
|
8
|
+
@options = options
|
9
|
+
@lang = determine_lang
|
10
|
+
end
|
11
|
+
|
12
|
+
def highlight
|
13
|
+
@options[:title] ||= ' ' if @options[:url]
|
14
|
+
cache = Cache.fetch_from_cache(code, options)
|
15
|
+
unless cache
|
16
|
+
if @lang == 'plain'
|
17
|
+
rendered_code = code.to_s.gsub('<','<')
|
18
|
+
else
|
19
|
+
rendered_code = render_pygments(code, options[:lang]).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
|
20
|
+
end
|
21
|
+
rendered_code = tableize_code(rendered_code, options[:lang], {linenos: options[:linenos], start: options[:start], marks: options[:marks]})
|
22
|
+
title = captionize(options[:title], options[:url], options[:link_text]) if options[:title]
|
23
|
+
rendered_code = "<figure class='code'>#{title}#{rendered_code}</figure>"
|
24
|
+
Cache.write_to_cache(rendered_code, options) unless options[:no_cache]
|
25
|
+
end
|
26
|
+
cache || rendered_code
|
27
|
+
end
|
28
|
+
|
29
|
+
def determine_lang
|
30
|
+
lang = options[:lang]
|
31
|
+
lang = 'ruby' if lang == 'ru'
|
32
|
+
lang = 'objc' if lang == 'm'
|
33
|
+
lang = 'perl' if lang == 'pl'
|
34
|
+
lang = 'yaml' if lang == 'yml'
|
35
|
+
lang = 'coffeescript' if lang == 'coffee'
|
36
|
+
lang = 'csharp' if lang == 'cs'
|
37
|
+
lang = 'plain' if lang == '' or lang.nil? or !lang
|
38
|
+
options[:lang] = lang
|
39
|
+
end
|
40
|
+
|
41
|
+
def render_pygments(code, lang)
|
42
|
+
highlighted_code = ::Pygments.highlight(
|
43
|
+
code,
|
44
|
+
{
|
45
|
+
lexer: lang,
|
46
|
+
formatter: 'html',
|
47
|
+
options: {
|
48
|
+
encoding: 'utf-8'
|
49
|
+
}
|
50
|
+
}
|
51
|
+
)
|
52
|
+
encode_liquid(highlighted_code).to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
def tableize_code (code, lang, options = {})
|
56
|
+
start = options[:start] || 1
|
57
|
+
lines = options[:linenos] || true
|
58
|
+
marks = options[:marks] || []
|
59
|
+
table = "<div class='highlight'><table><tr>"
|
60
|
+
table += number_lines(start, code.lines.count, marks) if lines
|
61
|
+
table += "<td class='main #{'unnumbered' unless lines} #{lang}'><pre>"
|
62
|
+
code.lines.each_with_index do |line,index|
|
63
|
+
classes = 'line'
|
64
|
+
if marks.include? index + start
|
65
|
+
classes += ' marked'
|
66
|
+
classes += ' start' unless marks.include? index - 1 + start
|
67
|
+
classes += ' end' unless marks.include? index + 1 + start
|
68
|
+
end
|
69
|
+
line = line.strip.empty? ? ' ' : line
|
70
|
+
table += "<div class='#{classes}'>#{line}</div>"
|
71
|
+
end
|
72
|
+
table +="</pre></td></tr></table></div>"
|
73
|
+
end
|
74
|
+
|
75
|
+
def number_lines (start, count, marks)
|
76
|
+
start ||= 1
|
77
|
+
lines = "<td class='line-numbers' aria-hidden='true'><pre>"
|
78
|
+
count.times do |index|
|
79
|
+
classes = 'line-number'
|
80
|
+
if marks.include? index + start
|
81
|
+
classes += ' marked'
|
82
|
+
classes += ' start' unless marks.include? index - 1 + start
|
83
|
+
classes += ' end' unless marks.include? index + 1 + start
|
84
|
+
end
|
85
|
+
lines += "<div data-line='#{index + start}' class='#{classes}'></div>"
|
86
|
+
end
|
87
|
+
lines += "</pre></td>"
|
88
|
+
end
|
89
|
+
|
90
|
+
# Public:
|
91
|
+
#
|
92
|
+
#
|
93
|
+
def get_range(code, start, endline)
|
94
|
+
length = code.lines.count
|
95
|
+
start ||= 1
|
96
|
+
endline ||= length
|
97
|
+
if start > 1 or endline < length
|
98
|
+
raise "#{filepath} is #{length} lines long, cannot begin at line #{start}" if start > length
|
99
|
+
raise "#{filepath} is #{length} lines long, cannot read beyond line #{endline}" if endline > length
|
100
|
+
code = code.split(/\n/).slice(start - 1, endline + 1 - start).join("\n")
|
101
|
+
end
|
102
|
+
code
|
103
|
+
end
|
104
|
+
|
105
|
+
def encode_liquid(code)
|
106
|
+
code.gsub(/{{/, '{{')
|
107
|
+
.gsub(/{%/, '{%')
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
data/octopress-pygments.gemspec
CHANGED
@@ -13,7 +13,11 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.homepage = "https://github.com/octopress/octopress-pygments"
|
14
14
|
gem.license = "MIT"
|
15
15
|
|
16
|
-
gem.add_runtime_dependency 'pygments.rb >= 0.5'
|
16
|
+
gem.add_runtime_dependency 'pygments.rb', '>= 0.5'
|
17
|
+
gem.add_runtime_dependency 'colorator', '~> 0.1.0'
|
18
|
+
|
19
|
+
gem.add_development_dependency 'rake'
|
20
|
+
gem.add_development_dependency 'rspec'
|
17
21
|
|
18
22
|
gem.files = `git ls-files`.split($/)
|
19
23
|
gem.require_paths = ["lib"]
|
File without changes
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Octopress::Pygments do
|
4
|
+
let(:wrapper) do
|
5
|
+
Proc.new do |stuff, numbers|
|
6
|
+
[
|
7
|
+
"<figure class='code'>",
|
8
|
+
"<div class='highlight'>",
|
9
|
+
"<table><tr>",
|
10
|
+
"<td class='line-numbers' aria-hidden='true'>",
|
11
|
+
"<pre>#{numbers}</pre>",
|
12
|
+
"</td>",
|
13
|
+
"<td class='main plain'>",
|
14
|
+
"<pre>#{stuff}</pre>",
|
15
|
+
"</td></tr>",
|
16
|
+
"</table></div></figure>"
|
17
|
+
].join
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:expected_output_no_options) do
|
22
|
+
stuff = <<-EOF
|
23
|
+
<figure class='code'><div class='highlight'><table><tr><td class='line-numbers' aria-hidden='true'><pre><div data-line='1' class='line-number'></div><div data-line='2' class='line-number'></div><div data-line='3' class='line-number'></div><div data-line='4' class='line-number'></div><div data-line='5' class='line-number'></div><div data-line='6' class='line-number'></div><div data-line='7' class='line-number'></div><div data-line='8' class='line-number'></div></pre></td><td class='main plain'><pre><div class='line'> require "hi-there-honey"
|
24
|
+
</div><div class='line'> </div><div class='line'> def hi-there-honey
|
25
|
+
</div><div class='line'> HiThereHoney.new("your name")
|
26
|
+
</div><div class='line'> end
|
27
|
+
</div><div class='line'> </div><div class='line'> hi-there-honey
|
28
|
+
</div><div class='line'> # => "Hi, your name"
|
29
|
+
</div></pre></td></tr></table></div></figure>
|
30
|
+
EOF
|
31
|
+
stuff.strip
|
32
|
+
end
|
33
|
+
|
34
|
+
let(:expected_output_lang_ruby) do
|
35
|
+
stuff = <<-EOF
|
36
|
+
<figure class='code'><div class='highlight'><table><tr><td class='line-numbers' aria-hidden='true'><pre><div data-line='1' class='line-number'></div><div data-line='2' class='line-number'></div><div data-line='3' class='line-number'></div><div data-line='4' class='line-number'></div><div data-line='5' class='line-number'></div><div data-line='6' class='line-number'></div><div data-line='7' class='line-number'></div><div data-line='8' class='line-number'></div></pre></td><td class='main ruby'><pre><div class='line'> <span class="nb">require</span> <span class="s2">"hi-there-honey"</span>
|
37
|
+
</div><div class='line'> </div><div class='line'> <span class="k">def</span> <span class="nf">hi</span><span class="o">-</span><span class="n">there</span><span class="o">-</span><span class="n">honey</span>
|
38
|
+
</div><div class='line'> <span class="no">HiThereHoney</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">"your name"</span><span class="p">)</span>
|
39
|
+
</div><div class='line'> <span class="k">end</span>
|
40
|
+
</div><div class='line'> </div><div class='line'> <span class="n">hi</span><span class="o">-</span><span class="n">there</span><span class="o">-</span><span class="n">honey</span>
|
41
|
+
</div><div class='line'> <span class="c1"># => "Hi, your name"</span>
|
42
|
+
</div></pre></td></tr></table></div></figure>
|
43
|
+
EOF
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:code) do
|
47
|
+
<<-EOF
|
48
|
+
require "hi-there-honey"
|
49
|
+
|
50
|
+
def hi-there-honey
|
51
|
+
HiThereHoney.new("your name")
|
52
|
+
end
|
53
|
+
|
54
|
+
hi-there-honey
|
55
|
+
# => "Hi, your name"
|
56
|
+
EOF
|
57
|
+
end
|
58
|
+
|
59
|
+
let(:markup) do
|
60
|
+
[
|
61
|
+
"lang:ruby",
|
62
|
+
'title:"Hello"',
|
63
|
+
"url:http://something.com/hi/fuaiofnioaf.html",
|
64
|
+
"link_text:'get it here'",
|
65
|
+
"mark:5,8-10,15",
|
66
|
+
"linenos: yes",
|
67
|
+
"start: 5",
|
68
|
+
"end: 15",
|
69
|
+
"range: 5-15"
|
70
|
+
].join(" ")
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:bad_markup) do
|
74
|
+
[
|
75
|
+
"lang:ruby",
|
76
|
+
'title:"Hello"',
|
77
|
+
"url:http://something.com/hi/fuaiofnioaf.html",
|
78
|
+
"link_text: get it here",
|
79
|
+
"mark:5,8-10,15",
|
80
|
+
"linenos: yes",
|
81
|
+
"start: 5",
|
82
|
+
"end: 15",
|
83
|
+
"range: 5-15"
|
84
|
+
].join(" ")
|
85
|
+
end
|
86
|
+
|
87
|
+
let(:options) do
|
88
|
+
{
|
89
|
+
lang: "ruby",
|
90
|
+
url: "http://something.com/hi/fuaiofnioaf.html",
|
91
|
+
title: "Hello",
|
92
|
+
linenos: "yes",
|
93
|
+
marks: [5, 8, 9, 10, 15],
|
94
|
+
link_text: "get it here",
|
95
|
+
start: 5,
|
96
|
+
end: 15
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
describe ".highlight" do
|
101
|
+
it "returns HTML for an empty code block" do
|
102
|
+
expect(described_class.highlight("", {})).to eql(wrapper.call("", ""))
|
103
|
+
end
|
104
|
+
|
105
|
+
context "with no options" do
|
106
|
+
it "returns the right HTML for a given set of code" do
|
107
|
+
expect(described_class.highlight(code, {})).to eql(expected_output_no_options)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "with a language" do
|
112
|
+
it "returns the right HTML for a given set of code" do
|
113
|
+
expect(described_class.highlight(code, { lang: 'ruby' })).to eql(expected_output_lang_ruby.chop)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe ".highlight_failed" do
|
119
|
+
#described_class.highlight_failed(error, syntax, markup, code, file = nil)
|
120
|
+
end
|
121
|
+
|
122
|
+
describe ".parse_markup" do
|
123
|
+
context "with no defaults" do
|
124
|
+
it "parses the defaults correctly" do
|
125
|
+
expect(described_class.parse_markup(markup, {})).to eql(options)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "with defaults with a nil value" do
|
130
|
+
it "overrides the nil values" do
|
131
|
+
expect(described_class.parse_markup(markup, { lang: nil })).to eql(options)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe ".clean_markup" do
|
137
|
+
|
138
|
+
it "returns an empty string with good markup" do
|
139
|
+
expect(described_class.clean_markup(markup)).to eql("")
|
140
|
+
end
|
141
|
+
|
142
|
+
it "returns erroneous text that isn't part of the markup" do
|
143
|
+
expect(described_class.clean_markup(bad_markup)).to eql(" it here")
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: octopress-pygments
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,15 +12,63 @@ cert_chain: []
|
|
12
12
|
date: 2013-08-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name: pygments.rb
|
15
|
+
name: pygments.rb
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
21
|
+
version: '0.5'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.5'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: colorator
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.1.0
|
22
38
|
type: :runtime
|
23
39
|
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.1.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
24
72
|
version_requirements: !ruby/object:Gem::Requirement
|
25
73
|
none: false
|
26
74
|
requirements:
|
@@ -35,13 +83,21 @@ extensions: []
|
|
35
83
|
extra_rdoc_files: []
|
36
84
|
files:
|
37
85
|
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- .travis.yml
|
38
88
|
- Gemfile
|
39
89
|
- LICENSE.txt
|
40
90
|
- README.md
|
41
91
|
- Rakefile
|
42
92
|
- lib/octopress-pygments.rb
|
93
|
+
- lib/octopress-pygments/cache.rb
|
94
|
+
- lib/octopress-pygments/options_parser.rb
|
95
|
+
- lib/octopress-pygments/renderer.rb
|
43
96
|
- lib/octopress-pygments/version.rb
|
44
97
|
- octopress-pygments.gemspec
|
98
|
+
- spec/octopress-pygments_spec.rb
|
99
|
+
- spec/pygments_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
45
101
|
homepage: https://github.com/octopress/octopress-pygments
|
46
102
|
licenses:
|
47
103
|
- MIT
|