Module: Octopress::CodeHighlighter
- Defined in:
- lib/octopress-code-highlighter.rb,
lib/octopress-code-highlighter/cache.rb,
lib/octopress-code-highlighter/hooks.rb,
lib/octopress-code-highlighter/version.rb,
lib/octopress-code-highlighter/renderer.rb,
lib/octopress-code-highlighter/options_parser.rb
Defined Under Namespace
Modules: Cache
Classes: OptionsParser, Renderer
Constant Summary
collapse
- DEFAULTS =
{
lang: 'plain',
linenos: true,
marks: [],
start: 1
}
- CODE_CACHE_DIR =
'.code-highlighter-cache'
- VERSION =
"4.3.0"
Class Method Summary
collapse
-
.clean_markup(input) ⇒ Object
-
.highlight(code, options = {}) ⇒ Object
-
.highlight_failed(error, syntax, markup, code, file = nil) ⇒ Object
-
.parse_markup(input, defaults = {}) ⇒ Object
-
.read_cache(code, options = {}) ⇒ Object
-
.select_lines(code, options) ⇒ Object
Return a code partial when start, end, or range options are defined.
Class Method Details
.clean_markup(input) ⇒ Object
.highlight(code, options = {}) ⇒ Object
32
33
34
|
# File 'lib/octopress-code-highlighter.rb', line 32
def self.highlight(code, options={})
Renderer.new(code, options).highlight
end
|
.highlight_failed(error, syntax, markup, code, file = nil) ⇒ Object
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/octopress-code-highlighter.rb', line 64
def self.highlight_failed(error, syntax, markup, code, file = nil)
code_snippet = code.split("\n")[0..9].map{|l| " #{l}" }.join("\n")
fail_message = "\nError while parsing the following markup#{" in #{file}" if file}:\n\n".red
fail_message += " #{markup}\n#{code_snippet}\n"
fail_message += "#{" ..." if code.split("\n").size > 10}\n"
fail_message += "\nValid Syntax:\n\n#{syntax}\n".yellow
fail_message += "\nError:\n\n#{error.message}".red
$stderr.puts fail_message.chomp
raise ArgumentError
end
|
.parse_markup(input, defaults = {}) ⇒ Object
40
41
42
|
# File 'lib/octopress-code-highlighter.rb', line 40
def self.parse_markup(input, defaults={})
OptionsParser.new(input).parse_markup(defaults)
end
|
.read_cache(code, options = {}) ⇒ Object
36
37
38
|
# File 'lib/octopress-code-highlighter.rb', line 36
def self.read_cache(code, options={})
Cache.read_cache(code, options)
end
|
.select_lines(code, options) ⇒ Object
Return a code partial when start, end, or range options are defined
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/octopress-code-highlighter.rb', line 50
def self.select_lines(code, options)
length = code.lines.count
start = options[:start] || 1
endline = options[:end] || length
if start > 1 or endline < length
raise "Code is #{length} lines long, cannot begin at line #{start}." if start > length
raise "Code lines starting line #{start} cannot be after ending line #{endline}." if start > endline
range = Range.new(start - 1, endline - 1)
code = code.lines.to_a[range].join
end
code
end
|