From f5f50a14c523327a28b39eeb0223261434d6d9f7 Mon Sep 17 00:00:00 2001 From: murphy Date: Wed, 19 Apr 2006 00:13:41 +0000 Subject: New Version: 0.7.0! Adjusted gem.rake. HTML scanner fixed. Enhanced filetype.rb: .rake files, xml, yaml (preparing for YAML scanner.) Enhanced test/ruby/1.in.rb. Added XML scanner with example. plugin.rb: made all_plugin_names public. --- lib/coderay/helpers/filetype.rb | 44 +++++++++++++++++++++++++++++++++++++---- lib/coderay/helpers/plugin.rb | 22 ++++++++++----------- lib/coderay/scanners/html.rb | 4 ++-- lib/coderay/scanners/xml.rb | 18 +++++++++++++++++ 4 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 lib/coderay/scanners/xml.rb (limited to 'lib/coderay') diff --git a/lib/coderay/helpers/filetype.rb b/lib/coderay/helpers/filetype.rb index 8e6e16b..bc8ccf2 100644 --- a/lib/coderay/helpers/filetype.rb +++ b/lib/coderay/helpers/filetype.rb @@ -6,16 +6,29 @@ # # Version: 0.1 (2005.september.1) # -# ==Documentation -# -# TODO +# == Documentation # +# # determine the type of the given +# lang = FileType[ARGV.first] +# +# # return :plaintext if the file type is unknown +# lang = FileType.fetch ARGV.first, :plaintext +# +# # try the shebang line, too +# lang = FileType.fetch ARGV.first, :plaintext, true +# module FileType UnknownFileType = Class.new Exception class << self + # Try to determine the file type of the file. + # + # +filename+ is a relative or absolute path to a file. + # + # The file itself is only accessed when +read_shebang+ is set to true. + # That means you can get filetypes from files that don't exist. def [] filename, read_shebang = false name = File.basename filename ext = File.extname name @@ -43,6 +56,9 @@ module FileType end # This works like Hash#fetch. + # + # If the filetype cannot be found, the +default+ value + # is returned. def fetch filename, default = nil, read_shebang = false if default and block_given? warn 'block supersedes default value argument' @@ -61,12 +77,17 @@ module FileType TypeFromExt = { 'rb' => :ruby, 'rbw' => :ruby, - 'cpp' => :cpp, + 'rake' => :ruby, + 'cpp' => :c, 'c' => :c, 'h' => :c, 'xml' => :xml, 'htm' => :html, 'html' => :html, + 'xhtml' => :xhtml, + 'rhtml' => :rhtml, + 'yaml' => :yaml, + 'yml' => :yaml, } TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/ @@ -118,6 +139,7 @@ class TC_FileType < Test::Unit::TestCase assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw'] assert_equal :ruby, FileType['/usr/bin/something/Rakefile'] assert_equal :ruby, FileType['~/myapp/gem/Rantfile'] + assert_equal :ruby, FileType['./lib/tasks\repository.rake'] assert_not_equal :ruby, FileType['test_rb'] assert_not_equal :ruby, FileType['Makefile'] assert_not_equal :ruby, FileType['set.rb/set'] @@ -133,6 +155,20 @@ class TC_FileType < Test::Unit::TestCase assert_not_equal :c, FileType['~/projects/blabla/c'] end + def test_html + assert_equal :html, FileType['test.htm'] + assert_equal :xhtml, FileType['test.xhtml'] + assert_equal :xhtml, FileType['test.html.xhtml'] + assert_equal :rhtml, FileType['_form.rhtml'] + end + + def test_yaml + assert_equal :yaml, FileType['test.yml'] + assert_equal :yaml, FileType['test.yaml'] + assert_equal :yaml, FileType['my.html.yaml'] + assert_not_equal :yaml, FileType['YAML'] + end + def test_shebang dir = './test' if File.directory? dir diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb index c55e565..2b47d8e 100644 --- a/lib/coderay/helpers/plugin.rb +++ b/lib/coderay/helpers/plugin.rb @@ -157,6 +157,17 @@ module PluginHost @plugin_hash ||= create_plugin_hash end + # Returns an array of all .rb files in the plugin path. + # + # The extension .rb is not included. + def all_plugin_names + Dir[path_to('*')].select do |file| + File.basename(file)[/^(?!_)\w+\.rb$/] + end.map do |file| + File.basename file, '.rb' + end + end + protected # Created a new plugin list and stores it to @plugin_hash. def create_plugin_hash @@ -204,17 +215,6 @@ protected end end - # Returns an array of all .rb files in the plugin path. - # - # The extension .rb is not included. - def all_plugin_names - Dir[path_to('*')].select do |file| - File.basename(file)[/^(?!_)\w+\.rb$/] - end.map do |file| - File.basename file, '.rb' - end - end - # Returns the Plugin for +id+. # Use it like Hash#fetch. # diff --git a/lib/coderay/scanners/html.rb b/lib/coderay/scanners/html.rb index 186a255..f37a8dd 100644 --- a/lib/coderay/scanners/html.rb +++ b/lib/coderay/scanners/html.rb @@ -130,9 +130,9 @@ module Scanners next elsif scan(/#{ENTITY}/ox) kind = :entity - elsif match(/[\n>]/) + elsif scan(/[\n>]/) tokens << [:close, :string] - kind = error + kind = :error state = :initial end diff --git a/lib/coderay/scanners/xml.rb b/lib/coderay/scanners/xml.rb new file mode 100644 index 0000000..d1c6bad --- /dev/null +++ b/lib/coderay/scanners/xml.rb @@ -0,0 +1,18 @@ +module CodeRay +module Scanners + + load :html + + # XML Scanner + # + # $Id$ + # + # Currently this is the same scanner as Scanners::HTML. + class XML < HTML + + register_for :xml + + end + +end +end -- cgit v1.2.1