diff --git a/gollum.gemspec b/gollum.gemspec index 5714e44c..8e7f7402 100644 --- a/gollum.gemspec +++ b/gollum.gemspec @@ -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[ diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 839db7ef..56a53276 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -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 diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 1db0b83f..3d3e97b0 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -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. diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 8f042c5b..1cbb2f8b 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -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. #