add caching hooks for Gollum::Markup

This commit is contained in:
rick
2010-10-12 15:36:36 -07:00
parent 94c3ac767e
commit e7f2da2d4a
3 changed files with 62 additions and 18 deletions
+32 -7
View File
@@ -348,8 +348,11 @@ module Gollum
# Returns the placeholder'd String data. # Returns the placeholder'd String data.
def extract_code(data) def extract_code(data)
data.gsub(/^``` ?(.+?)\r?\n(.+?)\r?\n```\r?$/m) do data.gsub(/^``` ?(.+?)\r?\n(.+?)\r?\n```\r?$/m) do
id = Digest::SHA1.hexdigest($2) id = Digest::SHA1.hexdigest($2)
@codemap[id] = { :lang => $1, :code => $2 } cached = check_cache(id)
@codemap[id] = cached ?
{ :output => cached } :
{ :lang => $1, :code => $2 }
id id
end end
end end
@@ -362,14 +365,36 @@ module Gollum
# Returns the marked up String data. # Returns the marked up String data.
def process_code(data) def process_code(data)
@codemap.each do |id, spec| @codemap.each do |id, spec|
lang = spec[:lang] formatted = spec[:output] || begin
code = spec[:code] lang = spec[:lang]
if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ } code = spec[:code]
code.gsub!(/^( |\t)/m, '') if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ }
code.gsub!(/^( |\t)/m, '')
end
formatted = Gollum::Albino.new(code, lang).colorize
update_cache(id, formatted)
formatted
end end
data.gsub!(id, Gollum::Albino.new(code, lang).colorize) data.gsub!(id, formatted)
end end
data data
end end
# Hook for getting the formatted value of extracted tag data.
#
# id - String SHA1 hash of original extracted tag data.
#
# Returns the String cached formatted data, or nil.
def check_cache(id)
end
# Hook for caching the formatted value of extracted tag data.
#
# id - String SHA1 hash of original extracted tag data.
# data - The String formatted value to be cached.
#
# Returns nothing.
def update_cache(id, data)
end
end end
end end
+1 -1
View File
@@ -120,7 +120,7 @@ module Gollum
# #
# Returns the String data. # Returns the String data.
def formatted_data def formatted_data
@blob && Gollum::Markup.new(self).render(historical?) @blob && @wiki.markup_class.new(self).render(historical?)
end end
# Public: The format of the page. # Public: The format of the page.
+29 -10
View File
@@ -9,6 +9,9 @@ module Gollum
# Sets the file class used by all instances of this Wiki. # Sets the file class used by all instances of this Wiki.
attr_writer :file_class attr_writer :file_class
# Sets the markup class used by all instances of this Wiki.
attr_writer :markup_class
# Sets the default name for commits. # Sets the default name for commits.
attr_accessor :default_committer_name attr_accessor :default_committer_name
@@ -36,6 +39,17 @@ module Gollum
::Gollum::File ::Gollum::File
end end
end end
# Gets the markup class used by all instances of this Wiki.
# Default: Gollum::Markup
def markup_class
@markup_class ||
if superclass.respond_to?(:markup_class)
superclass.markup_class
else
::Gollum::Markup
end
end
end end
self.default_committer_name = 'Anonymous' self.default_committer_name = 'Anonymous'
@@ -51,10 +65,11 @@ module Gollum
# repo - The String path to the Git repository that holds the Gollum # repo - The String path to the Git repository that holds the Gollum
# site. # site.
# options - Optional Hash: # options - Optional Hash:
# :base_path - String base path for all Wiki links. # :base_path - String base path for all Wiki links.
# Default: "/" # Default: "/"
# :page_class - The page Class. Default: Gollum::Page # :page_class - The page Class. Default: Gollum::Page
# :file_class - The file Class. Default: Gollum::File # :file_class - The file Class. Default: Gollum::File
# :markup_class - The markup Class. Default: Gollum::Markup
# #
# Returns a fresh Gollum::Repo. # Returns a fresh Gollum::Repo.
def initialize(path, options = {}) def initialize(path, options = {})
@@ -62,12 +77,13 @@ module Gollum
options[:access] = path options[:access] = path
path = path.path path = path.path
end end
@path = path @path = path
@access = options[:access] || GitAccess.new(path) @access = options[:access] || GitAccess.new(path)
@base_path = options[:base_path] || "/" @base_path = options[:base_path] || "/"
@page_class = options[:page_class] || self.class.page_class @page_class = options[:page_class] || self.class.page_class
@file_class = options[:file_class] || self.class.file_class @file_class = options[:file_class] || self.class.file_class
@repo = @access.repo @markup_class = options[:markup_class] || self.class.markup_class
@repo = @access.repo
end end
# Public: check whether the wiki's git repo exists on the filesystem. # Public: check whether the wiki's git repo exists on the filesystem.
@@ -298,6 +314,9 @@ module Gollum
# Gets the file class used by all instances of this Wiki. # Gets the file class used by all instances of this Wiki.
attr_reader :file_class attr_reader :file_class
# Gets the markup class used by all instances of this Wiki.
attr_reader :markup_class
# Normalize the data. # Normalize the data.
# #
# data - The String data to be normalized. # data - The String data to be normalized.