Class: Octopress::CodeHighlighter::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress-code-highlighter/cache.rb

Constant Summary collapse

CODE_CACHE_DIR =
'.code-style-cache'

Class Method Summary collapse

Class Method Details

.get_cache_path(dir, label, str) ⇒ Object



22
23
24
25
# File 'lib/octopress-code-highlighter/cache.rb', line 22

def get_cache_path(dir, label, str)
  label += '-' unless label === ''
  File.join(dir, "#{label}#{Digest::MD5.hexdigest(str)}.html")
end

.read_cache(code, options) ⇒ Object



7
8
9
10
11
# File 'lib/octopress-code-highlighter/cache.rb', line 7

def read_cache(code, options)
  cache_label = options[:cache_label] || options[:lang] || ''
  path = get_cache_path(CODE_CACHE_DIR, cache_label, options.to_s + code)
  File.exist?(path) ? File.read(path) : nil unless path.nil?
end

.write_to_cache(contents, options) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/octopress-code-highlighter/cache.rb', line 13

def write_to_cache(contents, options)
  FileUtils.mkdir_p(CODE_CACHE_DIR) unless File.directory?(CODE_CACHE_DIR)
  cache_label = options[:cache_label] || options[:lang] || ''
  path = get_cache_path(CODE_CACHE_DIR, cache_label, options.to_s + contents)
  File.open(path, 'w') do |f|
    f.print(contents)
  end
end