Refactor: lib/jekyll/convertor/markdown.rb - tests: no additions/breaks.
Reason: #3770
This commit is contained in:
		
							parent
							
								
									80f63949cd
								
							
						
					
					
						commit
						34438ed325
					
				|  | @ -1,54 +1,63 @@ | ||||||
| module Jekyll | module Jekyll | ||||||
|   module Converters |   module Converters | ||||||
|     class Markdown < Converter |     class Markdown < Converter | ||||||
|       safe true |  | ||||||
| 
 |  | ||||||
|       highlighter_prefix "\n" |       highlighter_prefix "\n" | ||||||
|       highlighter_suffix "\n" |       highlighter_suffix "\n" | ||||||
|  |       safe true | ||||||
| 
 | 
 | ||||||
|       def setup |       def setup | ||||||
|         return if @setup |         return if @setup | ||||||
|         @parser = |         if (!@parser = get_processor) | ||||||
|           case @config['markdown'].downcase |           Jekyll.logger.error "Invalid Markdown processor given:", @config["markdown"] | ||||||
|             when 'redcarpet' then RedcarpetParser.new(@config) |           Jekyll.logger.info  "", "Custom processors are not loaded in safe mode" if @config["safe"] | ||||||
|             when 'kramdown'  then KramdownParser.new(@config) |           Jekyll.logger.error "", "Available processors are: #{valid_processors.join(", ")}" | ||||||
|             when 'rdiscount' then RDiscountParser.new(@config) |           raise Errors::FatalException, "Bailing out; invalid Markdown processor." | ||||||
|           else |         end | ||||||
|             # So they can't try some tricky bullshit or go down the ancestor chain, I hope. | 
 | ||||||
|             if allowed_custom_class?(@config['markdown']) |         @setup = \ | ||||||
|               self.class.const_get(@config['markdown']).new(@config) |           true | ||||||
|             else |  | ||||||
|               Jekyll.logger.error "Invalid Markdown Processor:", "#{@config['markdown']}" |  | ||||||
|               Jekyll.logger.error "", "Valid options are [ #{valid_processors.join(" | ")} ]" |  | ||||||
|               raise Errors::FatalException, "Invalid Markdown Processor: #{@config['markdown']}" |  | ||||||
|             end |  | ||||||
|           end |  | ||||||
|         @setup = true |  | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|  |       def get_processor | ||||||
|  |         case @config["markdown"].downcase | ||||||
|  |           when "redcarpet" then return RedcarpetParser.new(@config) | ||||||
|  |           when "kramdown"  then return KramdownParser.new(@config) | ||||||
|  |           when "rdiscount" then return RDiscountParser.new(@config) | ||||||
|  |         else | ||||||
|  |           get_custom_processor | ||||||
|  |         end | ||||||
|  |       end | ||||||
|  | 
 | ||||||
|  |       # Public: Provides you with a list of processors, the ones we | ||||||
|  |       # support internally and the ones that you have provided to us (if you | ||||||
|  |       # are not in safe mode.) | ||||||
|  | 
 | ||||||
|       def valid_processors |       def valid_processors | ||||||
|         %w[ |         %W(rdiscount kramdown redcarpet) + \ | ||||||
|           rdiscount |           third_party_processors | ||||||
|           kramdown |  | ||||||
|           redcarpet |  | ||||||
|         ] + third_party_processors |  | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|  |       # Public: A list of processors that you provide via plugins. | ||||||
|  |       # This is really only available if you are not in safe mode, if you are | ||||||
|  |       # in safe mode (re: Github) then there will be none. | ||||||
|  | 
 | ||||||
|       def third_party_processors |       def third_party_processors | ||||||
|         self.class.constants - %w[ |         self.class.constants - \ | ||||||
|           KramdownParser |         %w[KramdownParser RDiscountParser RedcarpetParser PRIORITIES].map( | ||||||
|           RDiscountParser |           &:to_sym | ||||||
|           RedcarpetParser |         ) | ||||||
|           PRIORITIES |  | ||||||
|         ].map(&:to_sym) |  | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|       def extname_list |       def extname_list | ||||||
|         @extname_list ||= @config['markdown_ext'].split(',').map { |e| ".#{e.downcase}" } |         @extname_list ||= @config['markdown_ext'].split(',').map do |e| | ||||||
|  |           ".#{e.downcase}" | ||||||
|  |         end | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|       def matches(ext) |       def matches(ext) | ||||||
|         extname_list.include? ext.downcase |         extname_list.include?( | ||||||
|  |           ext.downcase | ||||||
|  |         ) | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|       def output_ext(ext) |       def output_ext(ext) | ||||||
|  | @ -56,21 +65,35 @@ module Jekyll | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|       def convert(content) |       def convert(content) | ||||||
|         setup |       setup | ||||||
|         @parser.convert(content) |         @parser.convert( | ||||||
|  |           content | ||||||
|  |         ) | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|       private |       private | ||||||
|  |       def get_custom_processor | ||||||
|  |         md = @config["markdown"] | ||||||
|  |         if custom_class_allowed?(md) | ||||||
|  |           self.class.const_get(md).new( | ||||||
|  |             @config | ||||||
|  |           ) | ||||||
|  |         end | ||||||
|  |       end | ||||||
| 
 | 
 | ||||||
|       # Private: Determine whether a class name is an allowed custom markdown |       # Private: Determine whether a class name is an allowed custom | ||||||
|       # class name |       #   markdown class name. | ||||||
|       # |       # | ||||||
|       # parser_name - the name of the parser class |       # parser_name - the name of the parser class | ||||||
|       # |       # | ||||||
|       # Returns true if the parser name contains only alphanumeric characters |       # Returns true if the parser name contains only alphanumeric | ||||||
|       # and is defined within Jekyll::Converters::Markdown |       # characters and is defined within Jekyll::Converters::Markdown | ||||||
|       def allowed_custom_class?(parser_name) | 
 | ||||||
|         parser_name !~ /[^A-Za-z0-9]/ && self.class.constants.include?(parser_name.to_sym) |       private | ||||||
|  |       def custom_class_allowed?(parser_name) | ||||||
|  |         parser_name !~ /[^A-Za-z0-9]/ && self.class.constants.include?( | ||||||
|  |           parser_name.to_sym | ||||||
|  |         ) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue