android.moparisthebest.org/plugins/backtick_code_block.rb
Brandon Mathis 8eac9cf471 Further simplified code plugins
- The highlight function now accepts only to variables: code, options.
- Extracting part of a code snippet is now a method of pygments_code.rb.
- Options assignment has been simplified for all code plugins.
2012-12-24 15:59:48 -06:00

43 lines
1.2 KiB
Ruby

require './plugins/pygments_code'
module BacktickCodeBlock
include HighlightCode
AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+|\/\S+)\s*(.+)?/i
LangCaption = /([^\s]+)\s*(.+)?/i
def render_code_block(input)
input.encode!("UTF-8")
input.gsub /^`{3}(.+?)`{3}/m do
str = $1.to_s
str.gsub /([^\n]+)?\n(.+?)\Z/m do
markup = $1 || ''
code = $2.to_s
opts = parse_markup(markup)
@options = {
lang: opts[:lang],
title: opts[:title],
lineos: opts[:lineos],
marks: opts[:marks],
url: opts[:url],
link_text: opts[:link_text] || 'link',
start: opts[:start] || 1,
}
markup = clean_markup(markup)
if markup =~ AllOptions
@options[:lang] ||= $1
@options[:title] ||= $2
@options[:url] ||= $3
@options[:link_text] ||= $4
elsif markup =~ LangCaption
@options[:lang] ||= $1
@options[:title] ||= $2
else
@options[:lang] ||= 'plain'
end
highlight(code, @options)
end
end
end
end