forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jekyll#4931 from pathawks/converters
Merge pull request 4931
- Loading branch information
Showing
4 changed files
with
119 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,108 @@ | ||
module Jekyll | ||
module Converters | ||
class Markdown | ||
class RedcarpetParser | ||
module CommonMethods | ||
def add_code_tags(code, lang) | ||
code = code.to_s | ||
code = code.sub(/<pre>/, "<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">") | ||
code = code.sub(/<\/pre>/, "</code></pre>") | ||
code | ||
end | ||
end | ||
|
||
module WithPygments | ||
include CommonMethods | ||
def block_code(code, lang) | ||
Jekyll::External.require_with_graceful_fail("pygments") | ||
lang = lang && lang.split.first || "text" | ||
add_code_tags( | ||
Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }), | ||
lang | ||
) | ||
end | ||
end | ||
class Jekyll::Converters::Markdown::RedcarpetParser | ||
module CommonMethods | ||
def add_code_tags(code, lang) | ||
code = code.to_s | ||
code = code.sub( | ||
/<pre>/, | ||
"<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">" | ||
) | ||
code = code.sub(%r!</pre>!, "</code></pre>") | ||
code | ||
end | ||
end | ||
|
||
module WithoutHighlighting | ||
require 'cgi' | ||
module WithPygments | ||
include CommonMethods | ||
def block_code(code, lang) | ||
Jekyll::External.require_with_graceful_fail("pygments") | ||
lang = lang && lang.split.first || "text" | ||
add_code_tags( | ||
Pygments.highlight( | ||
code, | ||
{ | ||
:lexer => lang, | ||
:options => { :encoding => "utf-8" } | ||
} | ||
), | ||
lang | ||
) | ||
end | ||
end | ||
|
||
include CommonMethods | ||
module WithoutHighlighting | ||
require "cgi" | ||
|
||
def code_wrap(code) | ||
"<figure class=\"highlight\"><pre>#{CGI::escapeHTML(code)}</pre></figure>" | ||
end | ||
include CommonMethods | ||
|
||
def block_code(code, lang) | ||
lang = lang && lang.split.first || "text" | ||
add_code_tags(code_wrap(code), lang) | ||
end | ||
end | ||
def code_wrap(code) | ||
"<figure class=\"highlight\"><pre>#{CGI.escapeHTML(code)}</pre></figure>" | ||
end | ||
|
||
module WithRouge | ||
def block_code(code, lang) | ||
code = "<pre>#{super}</pre>" | ||
def block_code(code, lang) | ||
lang = lang && lang.split.first || "text" | ||
add_code_tags(code_wrap(code), lang) | ||
end | ||
end | ||
|
||
output = "<div class=\"highlight\">" | ||
output << add_code_tags(code, lang) | ||
output << "</div>" | ||
end | ||
module WithRouge | ||
def block_code(code, lang) | ||
code = "<pre>#{super}</pre>" | ||
|
||
protected | ||
def rouge_formatter(_lexer) | ||
Rouge::Formatters::HTML.new(:wrap => false) | ||
end | ||
end | ||
output = "<div class=\"highlight\">" | ||
output << add_code_tags(code, lang) | ||
output << "</div>" | ||
end | ||
|
||
def initialize(config) | ||
External.require_with_graceful_fail("redcarpet") | ||
@config = config | ||
@redcarpet_extensions = {} | ||
@config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true } | ||
protected | ||
def rouge_formatter(_lexer) | ||
Rouge::Formatters::HTML.new(:wrap => false) | ||
end | ||
end | ||
|
||
@renderer ||= class_with_proper_highlighter(@config['highlighter']) | ||
end | ||
def initialize(config) | ||
Jekyll::External.require_with_graceful_fail("redcarpet") | ||
@config = config | ||
@redcarpet_extensions = {} | ||
@config["redcarpet"]["extensions"].each do |e| | ||
@redcarpet_extensions[e.to_sym] = true | ||
end | ||
|
||
def class_with_proper_highlighter(highlighter) | ||
case highlighter | ||
when "pygments" | ||
Class.new(Redcarpet::Render::HTML) do | ||
include WithPygments | ||
end | ||
when "rouge" | ||
Class.new(Redcarpet::Render::HTML) do | ||
Jekyll::External.require_with_graceful_fail(%w( | ||
rouge | ||
rouge/plugins/redcarpet | ||
)) | ||
@renderer ||= class_with_proper_highlighter(@config["highlighter"]) | ||
end | ||
|
||
unless Gem::Version.new(Rouge.version) > Gem::Version.new("1.3.0") | ||
abort "Please install Rouge 1.3.0 or greater and try running Jekyll again." | ||
end | ||
def class_with_proper_highlighter(highlighter) | ||
Class.new(Redcarpet::Render::HTML) do | ||
case highlighter | ||
when "pygments" | ||
include WithPygments | ||
when "rouge" | ||
Jekyll::External.require_with_graceful_fail(%w( | ||
rouge rouge/plugins/redcarpet | ||
)) | ||
|
||
include Rouge::Plugins::Redcarpet | ||
include CommonMethods | ||
include WithRouge | ||
end | ||
else | ||
Class.new(Redcarpet::Render::HTML) do | ||
include WithoutHighlighting | ||
end | ||
end | ||
unless Gem::Version.new(Rouge.version) > Gem::Version.new("1.3.0") | ||
abort "Please install Rouge 1.3.0 or greater and try running Jekyll again." | ||
end | ||
|
||
def convert(content) | ||
@redcarpet_extensions[:fenced_code_blocks] = !@redcarpet_extensions[:no_fenced_code_blocks] | ||
@renderer.send :include, Redcarpet::Render::SmartyPants if @redcarpet_extensions[:smart] | ||
markdown = Redcarpet::Markdown.new(@renderer.new(@redcarpet_extensions), @redcarpet_extensions) | ||
markdown.render(content) | ||
end | ||
include Rouge::Plugins::Redcarpet | ||
include CommonMethods | ||
include WithRouge | ||
else | ||
include WithoutHighlighting | ||
end | ||
end | ||
end | ||
|
||
def convert(content) | ||
@redcarpet_extensions[:fenced_code_blocks] = \ | ||
!@redcarpet_extensions[:no_fenced_code_blocks] | ||
if @redcarpet_extensions[:smart] | ||
@renderer.send :include, Redcarpet::Render::SmartyPants | ||
end | ||
markdown = Redcarpet::Markdown.new( | ||
@renderer.new(@redcarpet_extensions), | ||
@redcarpet_extensions | ||
) | ||
markdown.render(content) | ||
end | ||
end |