summaryrefslogtreecommitdiff
path: root/lib/coderay
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coderay')
-rw-r--r--lib/coderay/encoders/comment_filter.rb30
-rw-r--r--lib/coderay/scanners/python.rb25
-rw-r--r--lib/coderay/styles/alpha.rb2
-rwxr-xr-xlib/coderay/token_kinds.rb1
4 files changed, 47 insertions, 11 deletions
diff --git a/lib/coderay/encoders/comment_filter.rb b/lib/coderay/encoders/comment_filter.rb
index 819c619..0a6a2e8 100644
--- a/lib/coderay/encoders/comment_filter.rb
+++ b/lib/coderay/encoders/comment_filter.rb
@@ -18,7 +18,7 @@ module Encoders
register_for :comment_filter
DEFAULT_OPTIONS = superclass::DEFAULT_OPTIONS.merge \
- :exclude => [:comment]
+ :exclude => [:comment, :docstring]
end
@@ -49,4 +49,32 @@ puts "Hello world!"
RUBY_FILTERED
end
+ def test_filtering_docstrings
+ tokens = CodeRay.scan <<-PYTHON, :python
+'''
+Assuming this is file mymodule.py then this string, being the
+first statement in the file will become the mymodule modules
+docstring when the file is imported
+'''
+
+class Myclass():
+ """The class's docstring"""
+
+ def mymethod(self):
+ "The method's docstring"
+
+def myfunction():
+ "The function's docstring"
+ PYTHON
+ assert_equal <<-PYTHON_FILTERED.chomp, tokens.comment_filter.text
+
+class Myclass():
+
+ def mymethod(self):
+
+def myfunction():
+
+PYTHON_FILTERED
+ end
+
end \ No newline at end of file
diff --git a/lib/coderay/scanners/python.rb b/lib/coderay/scanners/python.rb
index 5d44c29..be5205e 100644
--- a/lib/coderay/scanners/python.rb
+++ b/lib/coderay/scanners/python.rb
@@ -103,6 +103,8 @@ module Scanners
state = :initial
string_delimiter = nil
string_raw = false
+ string_type = nil
+ docstring_coming = false
import_clause = class_name_follows = last_token_dot = false
unicode = string.respond_to?(:encoding) && string.encoding.name == 'UTF-8'
from_import_state = []
@@ -115,7 +117,8 @@ module Scanners
if state == :string
if scan(STRING_DELIMITER_REGEXP[string_delimiter])
tokens << [matched, :delimiter]
- tokens << [:close, :string]
+ tokens << [:close, string_type]
+ string_type = nil
state = :initial
next
elsif string_delimiter.size == 3 && scan(/\n/)
@@ -129,20 +132,20 @@ module Scanners
elsif scan(/ \\ . /x)
kind = :content
elsif scan(/ \\ | $ /x)
- tokens << [:close, :string]
+ tokens << [:close, string_type]
+ string_type = nil
kind = :error
state = :initial
else
raise_inspect "else case \" reached; %p not handled." % peek(1), tokens, state
end
- elsif match = scan(/ [ \t]+ | \\\n /x)
+ elsif match = scan(/ [ \t]+ | \\?\n /x)
tokens << [match, :space]
- next
-
- elsif match = scan(/\n/)
- tokens << [match, :space]
- state = :initial if state == :include_expected
+ if match == "\n"
+ state = :initial if state == :include_expected
+ docstring_coming = true if match?(/[ \t]*u?r?"""/)
+ end
next
elsif match = scan(/ \# [^\n]* /mx)
@@ -155,8 +158,10 @@ module Scanners
kind = :operator
elsif match = scan(/(u?r?|b)?("""|"|'''|')/i)
- tokens << [:open, :string]
string_delimiter = self[2]
+ string_type = docstring_coming ? :docstring : :string
+ docstring_coming = false if docstring_coming
+ tokens << [:open, string_type]
string_raw = false
modifiers = self[1]
unless modifiers.empty?
@@ -277,7 +282,7 @@ module Scanners
end
if state == :string
- tokens << [:close, :string]
+ tokens << [:close, string_type]
end
tokens
diff --git a/lib/coderay/styles/alpha.rb b/lib/coderay/styles/alpha.rb
index 24b364a..5e9ebfa 100644
--- a/lib/coderay/styles/alpha.rb
+++ b/lib/coderay/styles/alpha.rb
@@ -45,6 +45,8 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
.av { color:#700 }
.bi { color:#509; font-weight:bold }
.c { color:#888; }
+.c .dl { color:#444; }
+.c .ch { color:#444; }
.ch { color:#04D }
.ch .k { color:#04D }
diff --git a/lib/coderay/token_kinds.rb b/lib/coderay/token_kinds.rb
index f6341fb..3e63372 100755
--- a/lib/coderay/token_kinds.rb
+++ b/lib/coderay/token_kinds.rb
@@ -82,6 +82,7 @@ module CodeRay
AbbreviationForKind[:open] = AbbreviationForKind[:close] = AbbreviationForKind[:delimiter]
AbbreviationForKind[:nesting_delimiter] = AbbreviationForKind[:delimiter]
AbbreviationForKind[:escape] = AbbreviationForKind[:delimiter]
+ AbbreviationForKind[:docstring] = AbbreviationForKind[:comment]
#AbbreviationForKind.default = AbbreviationForKind[:error] or raise 'no class found for :error!'
end
end