4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/minisyntax/highlighter/ruby.rb', line 4
def self.highlight(code)
keywords = %w(BEGIN begin case class else elsif END end ensure for if in module rescue then unless until when while)
keywords += %w(def do and not or require)
keywords += %w(alias alias_method break next redo retry return super undef yield)
keywords += %w(initialize new loop include extend raise attr_reader attr_writer attr_accessor attr catch throw private module_function public protected)
code.gsub! /\b(#{keywords.join('|')})\b(?!\?)/, "<em>\\1</em>"
code.gsub! /\b([A-Z_][a-zA-Z0-9_]+)\b/, "<b>\\1</b>"
code.gsub! /(\#([^\{].*?)?)\n/, "<i>\\1</i>\n"
code.gsub! /("(.*?)"|'.*?'|%[qrw]\(.*?\)|%([QRW])\((.*?)\))/ do |text|
if $2 or type = $3
text.gsub! /#\{(.*?)\}/ do
%Q(<code>\#{#{highlight($1)}}</code>)
end
end
%Q(<q>#{text}</q>)
end
code.gsub! %r(<<-<b>HALLO</b>.*<b>HALLO</b>) do |text|
if $2 or type = $3
text.gsub! /#\{(.*?)\}/ do
%Q(<code>\#{#{highlight($1)}}</code>)
end
end
%Q(<q>#{text}</q>)
end
code.gsub! %r((\#.*?)$) do ||
if .gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
else
.gsub! %r(</?(b|i|em|var|code)>), ""
%Q(<i>#{comment}</i>)
end
end
code.gsub!('<i><i>', '<i>')
code
end
|