Allow indented code blocks in Gollum syntax
This commit is contained in:
+22
-8
@@ -361,17 +361,29 @@ module Gollum
|
|||||||
#
|
#
|
||||||
# Returns the placeholder'd String data.
|
# Returns the placeholder'd String data.
|
||||||
def extract_code(data)
|
def extract_code(data)
|
||||||
data.gsub!(/^``` ?([^\r\n]+)?\r?\n(.+?)\r?\n```\r?$/m) do
|
data.gsub!(/^([ \t]*)``` ?([^\r\n]+)?\r?\n(.+?)\r?\n\1```\r?$/m) do
|
||||||
id = Digest::SHA1.hexdigest("#{$1}.#{$2}")
|
id = Digest::SHA1.hexdigest("#{$2}.#{$3}")
|
||||||
cached = check_cache(:code, id)
|
cached = check_cache(:code, id)
|
||||||
@codemap[id] = cached ?
|
@codemap[id] = cached ?
|
||||||
{ :output => cached } :
|
{ :output => cached } :
|
||||||
{ :lang => $1, :code => $2 }
|
{ :lang => $2, :code => $3, :indent => $1 }
|
||||||
id
|
"#{$1}#{id}" # print the SHA1 ID with the proper indentation
|
||||||
end
|
end
|
||||||
data
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Remove the leading space from a code block. Leading space
|
||||||
|
# is only removed if every single line in the block has leading
|
||||||
|
# whitespace.
|
||||||
|
#
|
||||||
|
# code - The code block to remove spaces from
|
||||||
|
# regex - A regex to match whitespace
|
||||||
|
def remove_leading_space(code, regex)
|
||||||
|
if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ regex }
|
||||||
|
code.gsub!(regex, '')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Process all code from the codemap and replace the placeholders with the
|
# Process all code from the codemap and replace the placeholders with the
|
||||||
# final HTML.
|
# final HTML.
|
||||||
#
|
#
|
||||||
@@ -387,16 +399,18 @@ module Gollum
|
|||||||
next if spec[:output] # cached
|
next if spec[:output] # cached
|
||||||
|
|
||||||
code = spec[:code]
|
code = spec[:code]
|
||||||
if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ }
|
|
||||||
code.gsub!(/^( |\t)/m, '')
|
remove_leading_space(code, /^#{spec[:indent]}/m)
|
||||||
end
|
remove_leading_space(code, /^( |\t)/m)
|
||||||
|
|
||||||
blocks << [spec[:lang], code]
|
blocks << [spec[:lang], code]
|
||||||
end
|
end
|
||||||
|
|
||||||
highlighted = begin
|
highlighted = begin
|
||||||
encoding ||= 'utf-8'
|
encoding ||= 'utf-8'
|
||||||
blocks.map { |lang, code| Pygments.highlight(code, :lexer => lang, :options => {:encoding => encoding.to_s}) }
|
blocks.map { |lang, code|
|
||||||
|
Pygments.highlight(code, :lexer => lang, :options => {:encoding => encoding.to_s})
|
||||||
|
}
|
||||||
rescue ::RubyPython::PythonError
|
rescue ::RubyPython::PythonError
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user