aboutsummaryrefslogtreecommitdiff
path: root/plugins/pygments_code.rb
diff options
context:
space:
mode:
authorZhiming Wang <zmwangx@gmail.com>2015-05-04 14:55:10 -0700
committerZhiming Wang <zmwangx@gmail.com>2015-05-04 14:55:10 -0700
commit301679861a2440a10c9eac746cec86459f445ef9 (patch)
tree5aad22ead01cf0da226623f603f33867896c0fea /plugins/pygments_code.rb
parentd0a07c64afba47bbe8bfb56ba9893296a73fc7db (diff)
downloadmy_new_personal_website-301679861a2440a10c9eac746cec86459f445ef9.tar.xz
my_new_personal_website-301679861a2440a10c9eac746cec86459f445ef9.zip
remove all Octopress stuff
Diffstat (limited to 'plugins/pygments_code.rb')
-rw-r--r--plugins/pygments_code.rb45
1 files changed, 0 insertions, 45 deletions
diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb
deleted file mode 100644
index c0f4de92..00000000
--- a/plugins/pygments_code.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-require 'pygments'
-require 'fileutils'
-require 'digest/md5'
-
-PYGMENTS_CACHE_DIR = File.expand_path('../../.pygments-cache', __FILE__)
-FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
-
-module HighlightCode
- def self.highlight(str, lang)
- lang = 'ruby' if lang == 'ru'
- lang = 'objc' if lang == 'm'
- lang = 'perl' if lang == 'pl'
- lang = 'yaml' if lang == 'yml'
- str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #strip out divs <div class="highlight">
- tableize_code(str, lang)
- end
-
- def self.pygments(code, lang)
- if defined?(PYGMENTS_CACHE_DIR)
- path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html")
- if File.exist?(path)
- highlighted_code = File.read(path)
- else
- begin
- highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8', :startinline => true})
- rescue MentosError
- raise "Pygments can't parse unknown language: #{lang}."
- end
- File.open(path, 'w') {|f| f.print(highlighted_code) }
- end
- else
- highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8', :startinline => true})
- end
- highlighted_code
- end
- def self.tableize_code (str, lang = '')
- table = '<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers">'
- code = ''
- str.lines.each_with_index do |line,index|
- table += "<span class='line-number'>#{index+1}</span>\n"
- code += "<span class='line'>#{line}</span>"
- end
- table += "</pre></td><td class='code'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"
- end
-end