Syntax highlighting to code view

git-svn-id: file:///home/svn/framework3/trunk@5296 4d416f70-5f16-0410-b530-b9f4589650da
unstable
HD Moore 2008-01-20 23:36:20 +00:00
parent cc82a7e854
commit 2ea3ea0c7c
1 changed files with 77 additions and 2 deletions

View File

@ -9,6 +9,8 @@ class MsfWindow
#
class CodeView < Msf::Ui::Gtk2::SkeletonBasic
include Msf::Ui::Gtk2::MyControls
def initialize(m)
@ -41,11 +43,84 @@ class MsfWindow
}
)
buff.insert(buff.end_iter, File.read(m.file_path), "fixr")
font_desc = Pango::FontDescription.new('Courier')
textview.modify_font(font_desc)
# textview.set_pixels_above_lines(2)
# textview.set_pixels_below_lines(2)
buff.create_tag('comment', {'foreground' => 'ForestGreen'})
buff.create_tag('const', {'foreground' => 'DarkGreen'})
buff.create_tag('method', {'foreground' => 'DarkRed'})
buff.create_tag('string', {
'foreground' => 'DarkBlue',
'weight' => Pango::FontDescription::WEIGHT_BOLD
})
buff.create_tag('reserved', {'foreground' => 'purple'})
buff.insert(buff.start_iter, File.read(m.file_path))
start_iter = buff.start_iter
end_iter = buff.end_iter
str = buff.get_text(start_iter, end_iter, true)
tokenizer = RubyTokenizer.new
tokenizer.tokenize(str, start_iter.offset) do |tag, start, last|
buff.apply_tag(
tag.to_s,
buff.get_iter_at_offset(start),
buff.get_iter_at_offset(last)
)
end
show_all
end
#
# Pulled from ruby-gtk2 / gtk-demo (under Ruby license)
#
class RubyTokenizer
RESERVED_WORDS = %w(begin end module class def if then else while unless do case when require yield)
RESERVED_WORDS_PATTERN = Regexp.compile(/(^|\s+)(#{RESERVED_WORDS.collect do |pat| Regexp.quote(pat) end.join('|')})(\s+|$)/)
def tokenize(str, index = 0)
until str.empty?
tag = nil
case str
when /".+?"/, /'.+?'/
tag = :string
when /#.*$/
tag = :comment
when RESERVED_WORDS_PATTERN
tag = :reserved
when /[A-Za-z0-9_]+\(|\)/
tag = :method
when /[A-Z0-9][A-Za-z0-9_]+|false|true/
tag = :const
end
if tag
tokenize($~.pre_match, index) do |*args|
yield(*args)
end
yield(tag, index + $~.begin(0), index + $~.end(0))
index += (str.length - $~.post_match.length)
str = $~.post_match
else
index += str.length
str = ''
end
end
end
end
end
end