Bless Gollum with the power of GFM

With these changes, the Markdown rendered in Gollum wikis will have the
same quality and safety as the MD we render everywhere else @ GitHub.

This commit also changes the old `markup_class` accessor to
`markup_classes`, allowing users to specify a custom Markup class for
each markup language.
This commit is contained in:
Vicent Marti
2011-06-21 17:41:08 +02:00
parent 4abc32f1ec
commit fc6149a171
4 changed files with 63 additions and 10 deletions
+2
View File
@@ -37,6 +37,8 @@ Gem::Specification.new do |s|
s.add_development_dependency('shoulda')
s.add_development_dependency('rack-test')
s.add_development_dependency('wikicloth')
s.add_development_dependency('rake')
s.add_development_dependency('redcarpet')
# = MANIFEST =
s.files = %w[
+46
View File
@@ -2,6 +2,7 @@ require 'digest/sha1'
require 'cgi'
module Gollum
class Markup
# Initialize a new Markup object.
#
@@ -437,4 +438,49 @@ module Gollum
def update_cache(type, id, data)
end
end
begin
require 'redcarpet'
class MarkupGFM < Markup
def render(no_follow = false)
sanitize = no_follow ?
@wiki.history_sanitizer :
@wiki.sanitizer
data = extract_tex(@data.dup)
data = extract_tags(data)
flags = [
:autolink,
:fenced_code,
:tables,
:strikethrough,
:lax_htmlblock,
:gh_blockcode,
:no_intraemphasis
]
data = Redcarpet.new(data, *flags).to_html
data = process_tags(data)
doc = Nokogiri::HTML::DocumentFragment.parse(data)
doc.search('pre').each do |node|
next unless lang = node['lang']
text = node.inner_text
html = Gollum::Albino.colorize(text, lang)
node.replace(html)
end
doc = sanitize.clean_node!(doc) if sanitize
yield doc if block_given?
data = doc_to_html(doc)
data = process_tex(data)
data
end
end
rescue LoadError
MarkupGFM = Markup
end
end
+1 -1
View File
@@ -168,7 +168,7 @@ module Gollum
#
# Returns the String data.
def formatted_data(&block)
@blob && @wiki.markup_class.new(self).render(historical?, &block)
@blob && @wiki.markup_classes[format].new(self).render(historical?, &block)
end
# Public: The format of the page.
+14 -9
View File
@@ -10,7 +10,7 @@ module Gollum
attr_writer :file_class
# Sets the markup class used by all instances of this Wiki.
attr_writer :markup_class
attr_writer :markup_classes
# Sets the default ref for the wiki.
attr_accessor :default_ref
@@ -53,12 +53,16 @@ module Gollum
# 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
def markup_classes
@markup_classes ||
if superclass.respond_to?(:markup_classes)
superclass.markup_classes
else
::Gollum::Markup
classes = Hash.new(::Gollum::Markup)
# Add custom markup classes for different languages
classes[:markdown] = ::Gollum::MarkupGFM
classes
end
end
@@ -113,7 +117,8 @@ module Gollum
# 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
# :markup_classes - A hash containing the markup Classes for each
# document type. Default: { Gollum::Markup }
# :sanitization - An instance of Sanitization.
# :page_file_dir - String the directory in which all page files reside
# :ref - String the repository ref to retrieve pages from
@@ -130,7 +135,7 @@ module Gollum
@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
@markup_classes = options[:markup_classes] || self.class.markup_classes
@repo = @access.repo
@ref = options[:ref] || self.class.default_ref
@sanitization = options[:sanitization] || self.class.sanitization
@@ -492,7 +497,7 @@ module Gollum
attr_reader :file_class
# Gets the markup class used by all instances of this Wiki.
attr_reader :markup_class
attr_reader :markup_classes
# Normalize the data.
#