forked from LightTable/docs.lighttable.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredcarpet2_markdown.rb
48 lines (41 loc) · 1.16 KB
/
redcarpet2_markdown.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'fileutils'
require 'digest/md5'
require 'redcarpet'
require 'albino'
PYGMENTS_CACHE_DIR = File.expand_path('../../_cache', __FILE__)
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
class Redcarpet2Markdown < Redcarpet::Render::HTML
def block_code(code, lang)
lang = lang || "text"
path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest code}.html")
cache(path) do
colorized = Albino.colorize(code, lang.downcase)
add_code_tags(colorized, lang)
end
end
def add_code_tags(code, lang)
code.sub(/<pre>/, "<pre><code class=\"#{lang}\">").
sub(/<\/pre>/, "</code></pre>")
end
def cache(path)
if File.exist?(path)
File.read(path)
else
content = yield
File.open(path, 'w') {|f| f.print(content) }
content
end
end
end
class Jekyll::MarkdownConverter
def extensions
Hash[ *@config['redcarpet']['extensions'].map {|e| [e.to_sym, true] }.flatten ]
end
def markdown
@markdown ||= Redcarpet::Markdown.new(Redcarpet2Markdown.new(extensions), extensions)
end
def convert(content)
return super unless @config['markdown'] == 'redcarpet2'
markdown.render(content)
end
end