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.
def extract_code(data)
data.gsub(/^``` ?(.+?)\r?\n(.+?)\r?\n```\r?$/m) do
id = Digest::SHA1.hexdigest($2)
@codemap[id] = { :lang => $1, :code => $2 }
id = Digest::SHA1.hexdigest($2)
cached = check_cache(id)
@codemap[id] = cached ?
{ :output => cached } :
{ :lang => $1, :code => $2 }
id
end
end
@@ -362,14 +365,36 @@ module Gollum
# Returns the marked up String data.
def process_code(data)
@codemap.each do |id, spec|
lang = spec[:lang]
code = spec[:code]
if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ }
code.gsub!(/^( |\t)/m, '')
formatted = spec[:output] || begin
lang = spec[:lang]
code = spec[:code]
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
data.gsub!(id, Gollum::Albino.new(code, lang).colorize)
data.gsub!(id, formatted)
end
data
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
+1 -1
View File
@@ -120,7 +120,7 @@ module Gollum
#
# Returns the String data.
def formatted_data
@blob && Gollum::Markup.new(self).render(historical?)
@blob && @wiki.markup_class.new(self).render(historical?)
end
# 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.
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.
attr_accessor :default_committer_name
@@ -36,6 +39,17 @@ module Gollum
::Gollum::File
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
self.default_committer_name = 'Anonymous'
@@ -51,10 +65,11 @@ module Gollum
# repo - The String path to the Git repository that holds the Gollum
# site.
# options - Optional Hash:
# :base_path - String base path for all Wiki links.
# Default: "/"
# :page_class - The page Class. Default: Gollum::Page
# :file_class - The file Class. Default: Gollum::File
# :base_path - String base path for all Wiki links.
# Default: "/"
# :page_class - The page Class. Default: Gollum::Page
# :file_class - The file Class. Default: Gollum::File
# :markup_class - The markup Class. Default: Gollum::Markup
#
# Returns a fresh Gollum::Repo.
def initialize(path, options = {})
@@ -62,12 +77,13 @@ module Gollum
options[:access] = path
path = path.path
end
@path = path
@access = options[:access] || GitAccess.new(path)
@base_path = options[:base_path] || "/"
@page_class = options[:page_class] || self.class.page_class
@file_class = options[:file_class] || self.class.file_class
@repo = @access.repo
@path = path
@access = options[:access] || GitAccess.new(path)
@base_path = options[:base_path] || "/"
@page_class = options[:page_class] || self.class.page_class
@file_class = options[:file_class] || self.class.file_class
@markup_class = options[:markup_class] || self.class.markup_class
@repo = @access.repo
end
# 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.
attr_reader :file_class
# Gets the markup class used by all instances of this Wiki.
attr_reader :markup_class
# Normalize the data.
#
# data - The String data to be normalized.