Module: Octopress::CodeHighlighter::Cache

Extended by:
Cache
Included in:
Cache
Defined in:
lib/octopress-code-highlighter/cache.rb

Instance Method Summary collapse

Instance Method Details

#cleanObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/octopress-code-highlighter/cache.rb', line 40

def clean
  if File.exist?(CODE_CACHE_DIR)
    remove = []

    Find.find(CODE_CACHE_DIR).each do |file|
      remove << file unless @cached_files.include?(file) || File.directory?(file)
    end

    @cached_files = []

    FileUtils.rm remove
  end
end

#get_cache_path(dir, label, str) ⇒ Object



25
26
27
28
# File 'lib/octopress-code-highlighter/cache.rb', line 25

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



11
12
13
14
15
16
# File 'lib/octopress-code-highlighter/cache.rb', line 11

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)
  @cached_files << path
  File.exist?(path) ? File.read(path) : nil unless path.nil?
end

#writeObject



30
31
32
33
34
35
36
37
38
# File 'lib/octopress-code-highlighter/cache.rb', line 30

def write
  @write_cache.each do |path, contents|
    @cached_files << path
    File.open(path, 'w') do |f|
      f.print(contents)
    end
  end
  @write_cache = {}
end

#write_to_cache(contents, code, options) ⇒ Object



18
19
20
21
22
23
# File 'lib/octopress-code-highlighter/cache.rb', line 18

def write_to_cache(contents, code, 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 + code)
  @write_cache[path] = contents
end