forked from gettalong/kramdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Syntax highlighters are configurable now
* Syntax highlighters can now be set via the syntax_highlighter options which defaults to coderay for backwards-compatibility * Options for the syntax highlighter can be set with the syntax_highlighter_opts option * The old coderay_* options are deprecated as of now and will be removed in 2.0.0 * The coderay implementation has been extracted and is now just another syntax highlighter implementation.
- Loading branch information
Showing
12 changed files
with
245 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Coderay | ||
--- | ||
|
||
## Syntax Highlighting With Coderay | ||
|
||
[Coderay](http://coderay.rubychan.de) can be used as syntax highlighter for code blocks and code | ||
spans when converting to HTML. | ||
|
||
To use Coderay, set the [option 'syntax_highlighter'](../options.html#option-syntax-highlighter) to | ||
'coderay' and make sure that Coderay is available. The Coderay library can be installed, e.g., via | ||
Rubygems by running `gem install coderay`. | ||
|
||
> Note that the 'coderay_*' options are deprecated and should not be used anymore! | ||
{:.information} | ||
|
||
The [option 'syntax_highlighter_opts'](../options.html#option-syntax-highlighter-opts) supports the | ||
following special keys: | ||
|
||
span | ||
: A key-value map of options that are only used when syntax highlighting code spans. | ||
|
||
block | ||
: A key-value map of options that are only used when syntax highlighting code blocks. | ||
|
||
default_lang | ||
: The default language that should be used when no language is set for a **code block**. | ||
|
||
Furthermore all Coderay options (e.g. 'css', 'line_numbers', 'line_numbers_start', 'bold_every', | ||
'tab_width', 'wrap') can be set directly on the 'syntax_highlighter_opts' option (where they apply | ||
to code spans *and* code blocks) and/or on the 'span'/'block' keys. | ||
|
||
Here is an example that shows how Ruby code is highlighted: | ||
|
||
require 'kramdown' | ||
|
||
Kramdown::Document.new('* something').to_html | ||
puts 1 + 1 | ||
{: .language-ruby} |
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,2 +1,8 @@ | ||
rdoc/index.html: | ||
title: API Documentation | ||
|
||
/options.en.html#option-syntax-highlighter: | ||
dest_path: options.html#option-syntax-highlighter | ||
|
||
/options.en.html#option-syntax-highlighter-opts: | ||
dest_path: options.html#option-syntax-highlighter-opts |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
#-- | ||
# Copyright (C) 2009-2014 Thomas Leitner <[email protected]> | ||
# | ||
# This file is part of kramdown which is licensed under the MIT. | ||
#++ | ||
# | ||
|
||
module Kramdown::Converter::SyntaxHighlighter | ||
|
||
# Uses Coderay to highlight code blocks and code spans. | ||
module Coderay | ||
|
||
begin | ||
require 'coderay' | ||
|
||
# Highlighting via coderay is available if this constant is +true+. | ||
AVAILABLE = true | ||
rescue LoadError | ||
AVAILABLE = false # :nodoc: | ||
end | ||
|
||
def self.call(converter, text, lang, type) | ||
return nil unless converter.options[:enable_coderay] | ||
|
||
if type == :span && lang | ||
::CodeRay.scan(text, lang.to_sym).html(options(converter, :span)).chomp | ||
elsif type == :block && (lang || options(converter, :default_lang)) | ||
lang = (lang || options(converter, :default_lang)).to_sym | ||
::CodeRay.scan(text, lang).html(options(converter, :block)).chomp << "\n" | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def self.options(converter, type) | ||
prepare_options(converter) | ||
converter.data[:syntax_highlighter_coderay][type] | ||
end | ||
|
||
def self.prepare_options(converter) | ||
return if converter.data.key?(:syntax_highlighter_coderay) | ||
|
||
cache = converter.data[:syntax_highlighter_coderay] = {} | ||
|
||
opts = converter.options[:syntax_highlighter_opts].dup | ||
span_opts = (opts.delete(:span) || {}).dup | ||
block_opts = (opts.delete(:block) || {}).dup | ||
[span_opts, block_opts].each do |hash| | ||
hash.keys.each do |k| | ||
hash[k.kind_of?(String) ? Kramdown::Options.str_to_sym(k) : k] = hash.delete(k) | ||
end | ||
end | ||
|
||
cache[:default_lang] = converter.options[:coderay_default_lang] || opts.delete(:default_lang) | ||
cache[:span] = { | ||
:css => converter.options[:coderay_css] | ||
}.update(opts).update(span_opts).update(:wrap => :span) | ||
cache[:block] = { | ||
:wrap => converter.options[:coderay_wrap], | ||
:line_numbers => converter.options[:coderay_line_numbers], | ||
:line_number_start => converter.options[:coderay_line_number_start], | ||
:tab_width => converter.options[:coderay_tab_width], | ||
:bold_every => converter.options[:coderay_bold_every], | ||
:css => converter.options[:coderay_css] | ||
}.update(opts).update(block_opts) | ||
|
||
[:css, :wrap, :line_numbers].each do |key| | ||
[:block, :span].each do |type| | ||
cache[type][key] = cache[type][key].to_sym if cache[type][key].kind_of?(String) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div><span class="CodeRay">x = <span class="constant">Class</span>.new | ||
</span> | ||
</div> | ||
<div><span class="CodeRay"><span class="tag"><a></span>href<span class="tag"></a></span> | ||
</span> | ||
</div> |
Oops, something went wrong.