summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changes-1.0.textile329
-rw-r--r--Changes.textile296
-rw-r--r--Gemfile1
-rwxr-xr-xbin/coderay40
-rw-r--r--etc/CodeRay.tmproj50
-rw-r--r--etc/language_report.textile12
-rw-r--r--lib/coderay.rb3
-rw-r--r--lib/coderay/encoder.rb12
-rw-r--r--lib/coderay/encoders/_map.rb1
-rw-r--r--lib/coderay/encoders/count.rb12
-rw-r--r--lib/coderay/encoders/debug.rb2
-rw-r--r--lib/coderay/encoders/filter.rb18
-rw-r--r--lib/coderay/encoders/html.rb12
-rw-r--r--lib/coderay/encoders/html/css.rb2
-rw-r--r--lib/coderay/encoders/html/output.rb1
-rw-r--r--lib/coderay/encoders/json.rb27
-rw-r--r--lib/coderay/encoders/lines_of_code.rb3
-rw-r--r--lib/coderay/encoders/statistic.rb11
-rw-r--r--lib/coderay/encoders/text.rb13
-rw-r--r--lib/coderay/encoders/token_kind_filter.rb1
-rw-r--r--lib/coderay/encoders/xml.rb8
-rw-r--r--lib/coderay/encoders/yaml.rb21
-rw-r--r--lib/coderay/helpers/file_type.rb4
-rw-r--r--lib/coderay/helpers/plugin.rb12
-rw-r--r--lib/coderay/scanner.rb48
-rw-r--r--lib/coderay/scanners/_map.rb4
-rw-r--r--lib/coderay/scanners/clojure.rb2
-rw-r--r--lib/coderay/scanners/css.rb63
-rw-r--r--lib/coderay/scanners/erb.rb (renamed from lib/coderay/scanners/rhtml.rb)6
-rw-r--r--lib/coderay/scanners/groovy.rb2
-rw-r--r--lib/coderay/scanners/java_script.rb2
-rw-r--r--lib/coderay/scanners/nitro_xhtml.rb136
-rw-r--r--lib/coderay/scanners/php.rb4
-rw-r--r--lib/coderay/scanners/ruby.rb17
-rw-r--r--lib/coderay/tokens.rb4
-rw-r--r--rake_tasks/diff.rake2
-rw-r--r--test/executable/source_with_comments.rb3
-rw-r--r--test/executable/suite.rb76
-rwxr-xr-xtest/functional/basic.rb41
-rwxr-xr-xtest/functional/examples.rb2
-rw-r--r--test/functional/for_redcloth.rb18
-rwxr-xr-xtest/functional/suite.rb3
-rw-r--r--test/lib/README1
-rw-r--r--test/lib/assert_warning.rb15
-rw-r--r--test/unit/file_type.rb4
-rwxr-xr-xtest/unit/plugin.rb22
-rw-r--r--test/unit/plugins/example.rb2
-rw-r--r--test/unit/plugins/user_defined/user_plugin.rb2
-rw-r--r--test/unit/plugins_with_default/default.rb5
-rw-r--r--test/unit/plugins_with_default/default_plugin.rb5
-rw-r--r--test/unit/plugins_with_default/example_without_register_for.rb4
51 files changed, 713 insertions, 671 deletions
diff --git a/Changes-1.0.textile b/Changes-1.0.textile
new file mode 100644
index 0000000..8c74145
--- /dev/null
+++ b/Changes-1.0.textile
@@ -0,0 +1,329 @@
+h1=. CodeRay Version History
+
+p=. _This files lists all changes in the CodeRay library since the 0.9.8 release._
+
+{{toc}}
+
+h2. Changes in 1.0
+
+CodeRay 1.0 is a major rewrite of the library, and incompatible to earlier versions.
+
+The command line and programmer interfaces are similar to 0.9, but the internals have completely changed.
+
+h3. General changes
+
+* *NEW*: The new Diff scanner colorizes code inside of the diff, and highlights inline changes.
+* *NEW*: Extended support and usage of HTML5 and CSS 3 features.
+* *NEW*: Direct Streaming
+* *IMPROVED* documentation
+* *IMPROVED* speed: faster startup (using @autoload@), scanning, and encoding
+* *IMPROVED* Ruby 1.9 encodings support
+* *IMPROVED* Tests: There are more of them now!
+
+h3. Direct Streaming
+
+CodeRay 1.0 introduces _Direct Streaming_ as a faster and simpler alternative to Tokens. It means that all Scanners,
+Encoders and Filters had to be rewritten, and that older scanners using the Tokens API are no longer compatible with
+this version.
+
+The main benefits of this change are:
+
+* more speed (benchmarks show 10% to 50% more tokens per second compared to CodeRay 0.9)
+* the ability to stream output into a pipe on the command line
+* a simpler API
+* less code
+
+Changes related to the new tokens handling include:
+* *CHANGED*: The Scanners now call Encoders directly; tokens are not added to a Tokens array, but are send to the
+ Encoder as a method call. The Tokens representation (which can be seen as a cache now) is still present, but as a
+ special case; Tokens just encodes the given tokens into an Array for later use.
+* *CHANGED*: The token actions (@text_token@, @begin_group@ etc.) are now public methods of @Encoder@ and @Tokens@.
+* *REWRITE* of all Scanners, Encoders, Filters, and Tokens.
+* *RENAMED* @:open@ and @:close@ actions to @:begin_group@ and @:end_group@.
+* *RENAMED* @open_token@ and @close_token@ methods to @begin_group@ and @end_group@.
+* *NEW* method @#tokens@ allows to add several tokens to the stream. @Tokens@ and @Encoders::Encoder@ define this
+ method.
+* *CHANGED* The above name changes also affect the JSON, XML, and YAML encoders. CodeRay 1.0 output will be incompatible
+ with earlier versions.
+* *REMOVED* @TokenStream@ and the @Streamable@ API and all related features like @NotStreamableError@ are now obsolete
+ and have been removed.
+
+h3. Command Line
+
+The @coderay@ executable was rewritten and has a few new features:
+
+* *NEW* Ability to stream into a pipe; try @coderay file | more -r@
+* *NEW* help
+* *IMPROVED*: more consistent parameter handling
+* *REMOVED* @coderay_stylesheet@ executable; use @coderay stylesheet [name]@.
+
+h3. @Tokens@
+
+* *NEW* methods @encode_with@, @count@, @begin_group@, @end_group@, @begin_line@, and @end_line@.
+* *REMOVED* methods @#stream?@, @#each_text_token@.
+* *REMOVED* @#text@ and @#text_size@ methods. Use the @Text@ encoder instead.
+* *REMOVED* special implementation of @#each@ taking a filter parameter. Use @TokenKindFilter@ instead.
+
+h3. *RENAMED*: @TokenKinds@
+
+Renamed from @Tokens::ClassOfKind@ (was also @Tokens::AbbreviationForKind@ for a while).
+The term "token class" is no longer used in CodeRay. Instead, tokens have _kinds_.
+See "#122":http://redmine.rubychan.de/issues/122.
+
+* *ADDED* token kinds @:filename@, @:namespace@, and @:eyecatcher@.
+* *RENAMED* @:pre_constant@ and @:pre_type@ to @:predefined_constant@ and @predefined_type@.
+* *REMOVED* token kinds @:attribute_name_fat@, @:attribute_value_fat@, @:operator_fat@,
+ @:tag_fat@, @:xml_text@, @:nesting_delimiter@, @:open@, and @:close@.
+* *CHANGED*: Don't raise error for unknown token kinds unless in @$CODERAY_DEBUG@ mode.
+* *CHANGED* the value for a token kind that is not highlighted from
+ @:NO_HIGHLIGHT@ to @false@.
+
+h3. @Duo@
+
+* *NEW* method @call@ for allowing code like @CodeRay::Duo[:python => :yaml].(code)@ in Ruby 1.9.
+
+h3. @Encoders::CommentFilter@
+
+* *NEW* alias @:remove_comments@
+
+h3. @Encoders::Filter@
+
+* *NEW* option @tokens@.
+* *CHANGED*: Now it simply delegates to the output.
+* *REMOVED* @include_text_token?@ and @include_block_token?@ methods.
+
+h3. @Encoders::HTML@
+
+The HTML encoder was cleaned up and simplified.
+
+* *NEW*: HTML5 and CSS 3 compatible.
+ See "#215":http://redmine.rubychan.de/issues/215.
+* *ADDED* support for @:line_number_anchors@.
+ See "#208":http://redmine.rubychan.de/issues/208.
+* *CHANGED* the default style to @:alpha@.
+* *CHANGED*: Use double click to toggle line numbers in table mode (as single
+ click jumps to an anchor.)
+* *REMOVED* support for @:line_numbers => :list@.
+* *FIXED* splitting of lines for @:line_numbers => :inline@, so that the line
+ numbers don't get colored, too.
+* *RENAMED* @Output#numerize@ to @#number@, which is an actual English word.
+
+h3. @Encoders::LinesOfCode@
+
+* *CHANGED*: @compile@ and @finish@ methods are now protected.
+
+h3. *Renamed*: @Encoders::Terminal@ (was @Encoders::Term@)
+
+* *RENAMED* from @Encoders::Term@, added @:term@ alias.
+* *CLEANUP*: Use @#setup@'s @super@, don't use @:procedure@ token class.
+* *CHANGED*: @#token@'s second parameter is no longer optional.
+* *REMOVED* colors for obsolete token kinds.
+* *FIXED* handling of line tokens.
+
+h3. @Encoders::Text@
+
+* *FIXED* default behavior of stripping the trailing newline.
+
+h3. *RENAMED*: @Encoders::TokenKindFilter@ (was @Encoders::TokenClassFilter@)
+
+* *NEW*: Handles token groups.
+ See "#223":http://redmine.rubychan.de/issues/223.
+* *RENAMED* @include_block_token?@ to @include_group?@.
+
+h3. @Encoders::Statistic@
+
+* *CHANGED*: Tokens actions are counted separately.
+
+h3. @Scanners::Scanner@
+
+* *NEW* methods @#file_extension@ and @#encoding@.
+* *NEW*: The @#tokenize@ method also takes an Array of Strings as source. The
+ code is highlighted as one and split into parts of the input lengths
+ after that using @Tokens#split_into_parts@.
+* *NEW* method @#binary_string@
+* *REMOVED* helper method @String#to_unix@.
+* *REMOVED* method @#streamable?@.
+* *REMOVED* @#marshal_load@ and @#marshal_dump@.
+* *RENAMED* class method @normify@ to @normalize@; it also deals with encoding now.
+* *CHANGED*: @#column@ starts counting with 1 instead of 0
+
+h3. *NEW*: @Scanners::Clojure@
+
+Thanks to Licenser, CodeRay now supports the Clojure language.
+
+h3. @Scanners::CSS@
+
+* *NEW*: Rudimentary support for the @attr@, @counter@, and @counters@ functions.
+ See "#224":http://redmine.rubychan.de/issues/224.
+* *NEW*: Rudimentary support for CSS 3 colors.
+* *CHANGED*: Attribute selectors are highlighted as @:attribute_name@ instead of @:string@.
+* *CHANGED*: Comments are scanned as one token instead of three.
+
+h3. @Scanners::Debug@
+
+* *NEW*: Support for line tokens (@begin_line@ and @end_line@ represented by @[@ and @]@.)
+* *FIXED*: Don't send @:error@ and @nil@ tokens for buggy input any more.
+* *FIXED*: Closes unclosed tokens at the end of @scan_tokens@.
+* *IMPROVED*: Highlight unknown tokens as @:error@.
+* *CHANGED*: Raises an error when trying to end an invalid token group.
+
+h3. @Scanners::Delphi@
+
+* *FIXED*: Closes open string groups.
+
+h3. @Scanners::Diff@
+
+* *NEW*: Highlighting of code based on file names.
+ See ticket "#52":http://redmine.rubychan.de/issues/52.
+
+ Use the @:highlight_code@ option to turn this feature off. It's enabled
+ by default.
+
+ This is a very original feature. It enables multi-language highlighting for
+ diff files, which is especially helpful for CodeRay development itself. The
+ updated version of the scanner test suite generated .debug.diff.html files
+ using this.
+
+ Note: This is still experimental. Tokens spanning more than one line
+ may get highlighted incorrectly. CodeRay tries to keep scanner states
+ between the lines and changes, but the quality of the results depend on
+ the scanner.
+* *NEW*: Inline change highlighting, as suggested by Eric Thomas.
+ See ticket "#227":http://redmine.rubychan.de/issues/227 for details.
+
+ Use the @:inline_diff@ option to turn this feature off. It's enabled by
+ default.
+
+ For single-line changes (that is, a single deleted line followed by a single
+ inserted line), this feature surrounds the changed parts with an
+ @:eyecatcher@ group which appears in a more saturated background color.
+ The implementation is quite complex, and highly experimental. The problem
+ with multi-layer tokenizing is that the tokens have to be split into parts.
+ If the inline change starts, say, in the middle of a string, then additional
+ @:end_group@ and @:begin_group@ tokens must be inserted to keep the group
+ nesting intact. The extended @Scanner#tokenize@ method and the new
+ @Tokens#split_into_parts@ method take care of this.
+* *NEW*: Highlight the file name in the change headers as @:filename@.
+* *CHANGED*: Highlight unknown lines as @:comment@ instead of @:head@.
+* *IMPROVED*: Background colors for Diff output have been optimized.
+
+h3. *RENAMED*: @Scanners::ERB@ (was @Scanners::RHTML@)
+
+
+h3. @Scanners::HTML@
+
+* *FIXED*: Closes open string groups.
+
+h3. @Scanners::JavaScript@
+
+* *IMPROVED*: Added @NaN@ and @Infinity@ to list of predefined constants.
+* *IMPROVED* recognition of RegExp literals with leading spaces.
+
+h3. @Scanners::Java@
+
+* *NEW*: Package names are highlighted as @:namespace@.
+ See "#210":http://redmine.rubychan.de/issues/210.
+
+h3. *RENAMED*: @Scanners::Text@ (was @Scanners::Plaintext@)
+
+* *IMPROVED*: Just returns the string without scanning (faster).
+
+ This is much faster than scanning until @/\z/@ in Ruby 1.8.
+
+h3. @Scanners::Python@
+
+* *CHANGED*: Docstrings are highlighted as @:comment@.
+ See "#190":http://redmine.rubychan.de/issues/190.
+
+h3. *NEW*: @Scanners::Raydebug@
+
+Copied from @Scanners::Debug@, highlights the token dump instead of importing it. It also reacts to the @.raydebug@ file
+name suffix now.
+
+h3. @Scanners::Ruby@
+
+* *ADDED* more predefined keywords (see http://murfy.de/ruby-constants).
+* *IMPROVED* support for singleton method definitions.
+ See "#147":http://redmine.rubychan.de/issues/147.
+* *FIXED*: Don't highlight methods with a capital letter as constants
+ (eg. GL.PushMatrix).
+* *NEW*: Highlight buggy floats (like .5) as @:error@.
+* *CLEANUP* of documentation, names of constants and variables, state handling.
+
+ Moved @StringState@ class from @patterns.rb@ into a separate file.
+* *NEW*: Complicated rule for recognition of @foo=@ style method names.
+* *NEW*: Handles @:keep_state@ option (a bit; experimental).
+
+ Actually, Ruby checks if there is @[~>=]@, but not @=>@ following the name.
+
+* *REMOVED* @EncodingError@
+
+h3. *REMOVED* @Scanners::Scheme@
+
+* It is too buggy, and nobody was using it. To be added again when it's fixed.
+ See "#59":http://redmine.rubychan.de/issues/59.
+
+h3. @Scanners::SQL@
+
+* *IMPROVED*: Extended list of keywords and functions (thanks to
+ Joshua Galvez, Etienne Massip, and others).
+
+ See "#221":http://redmine.rubychan.de/issues/221.
+* *FIXED*: Closes open string groups.
+* *FIXED*: Words after @.@ are always recognized as @:ident@.
+
+h3. @Scanners::YAML@
+
+* *FIXED*: Allow spaces before colon in mappings.
+
+ See "#231":http://redmine.rubychan.de/issues/231.
+
+h3. *NEW*: @Styles::Alpha@
+
+A style that uses transparent HSLA colors as defined in CSS 3. See "#199":http://redmine.rubychan.de/issues/199.
+
+It also uses the CSS 3 property @user-select: none@ to keep the user from selecting the line numbers. This is especially
+nice for @:inline@ line numbers. See "#226":http://redmine.rubychan.de/issues/226.
+
+h3. @WordList@
+
+Stripped down to 19 LOC.
+
+* *REMOVED* caching option because it creates memory leaks.
+* *REMOVED* block option.
+
+h3. @FileType@
+
+* *NEW*: Recognizes @.gemspec@, @.rjs@, @.rpdf@ extensions, @Gemfile@, and @Capfile@ as Ruby.
+
+ Thanks to the authors of the TextMate Ruby bundle!
+* *REMOVED* @FileType#shebang@ is a protected method now.
+
+h3. @Plugin@
+
+* *IMPROVED*: @register_for@ sets the @plugin_id@; it can now be a @Symbol@.
+* *ADDED* @PluginHost#const_missing@ method: Plugins are loaded automatically.
+ Using @Scanners::JavaScript@ in your code loads @scanners/java_script.rb@.
+* *ADDED* @#all_plugins@ method to simplify getting
+ information about all available plugins (suggested by bnhymn).
+* *CHANGED* the default plugin key from @nil@ to @:default@.
+
+h3. @GZip@
+
+* *MOVED* into @CodeRay@ namespace.
+* *MOVED* file from @gzip_simple.rb@ to @gzip.rb@.
+* *REMOVED* @String@ extensions.
+
+h3. More API changes
+
+* *FIXED* @Encoders::HTML#token@'s second parameter is no longer optional.
+* *CHANGED* @Encoders::HTML::Output@'s API.
+* *REMOVED* lots of unused methods.
+
+The helper classes were cleaned up; see above for details.
+
+* *CHANGED* @Plugin@ API was simplified and stripped of all unnecessary features.
+* *CHANGED* Moved @GZip@ and @FileType@ libraries into @CodeRay@; cleaned them up.
+
+
+
diff --git a/Changes.textile b/Changes.textile
index 318d0a8..cdbfdd9 100644
--- a/Changes.textile
+++ b/Changes.textile
@@ -4,302 +4,6 @@ p=. _This files lists all changes in the CodeRay library since the 0.8.4 release
{{toc}}
-h2. Changes in 1.0
-
-h3. Direct Streaming
-
-CodeRay 1.0 introduces _Direct Streaming_ as a faster and simpler alternative to Tokens. It means that all Scanners, Encoders and Filters had to be rewritten, and that older scanners using the Tokens API are no longer compatible with this version.
-
-The benefit of this change is more speed (benchmarks show 10% to 50% more tokens per second compared to CodeRay 0.9), a simpler API, and less code.
-
-Changes related to the new tokens handling include:
-* *CHANGED*: The Scanners now call Encoders directly; tokens are not added to a Tokens array, but are send to the Encoder as a method call. The Tokens representation (which can be seen as a cache now) is still present, but as a special case; Tokens just encodes the given tokens into an Array for later use.
-* *CHANGED*: The token actions (@text_token@, @begin_group@ etc.) are now public methods of @Encoder@ and @Tokens@.
-* *REWRITE* of all Scanners, Encoders, Filters, and Tokens.
-* *RENAMED* @:open@ and @:close@ actions to @:begin_group@ and @:end_group@.
-* *RENAMED* @open_token@ and @close_token@ methods to @begin_group@ and @end_group@.
-* *NEW* method @#tokens@ allows to add several tokens to the stream. @Tokens@ and @Encoders::Encoder@ define this method.
-* *CHANGED* The above name changes also affect the JSON, XML, and YAML encoders. CodeRay 1.0 output will be incompatible with earlier versions.
-* *REMOVED* @TokenStream@ and the @Streamable@ API and all related features like @NotStreamableError@ are now obsolete and have been removed.
-
-h3. General changes
-
-* *IMPROVED* documentation in general; additions, corrections and cleanups.
-* *NEW*: Extended support and usage of HTML5 and CSS 3 features.
-* *IMPROVED* Ruby 1.9 support (_._ not in @$LOAD_PATH@)
-* *IMPROVED* speed of HTML encoding when using CSS classes.
-* *IMPROVED*: Faster startup by replacing @require@ with @autoload@. CodeRay
- features will now only be loaded when they're needed.
-* *FIXED* some image links in the documentation.
-* *ADDED* a lot of tests.
-
-h3. Helpers
-
-The helper classes were cleaned up; see below for details.
-
-* *CHANGED* @Plugin@ API was simplified and stripped of all unnecessary features.
-* *CHANGED* Moved @GZip@ and @FileType@ libraries into @CodeRay@; cleaned them up.
-
-h3. @Tokens@
-
-* *REMOVED* methods @#stream?@, @#each_text_token@.
-* *REMOVED* @#text@ and @#text_size@ methods. Use the @Text@ encoder instead.
-* *REMOVED* special implementation of @#each@ taking a filter parameter. Use @TokenKindFilter@ instead.
-* *NEW* methods @encode_with@, @count@, @begin_group@, @end_group@, @begin_line@, and @end_line@.
-
-h3. *RENAMED*: @TokenKinds@
-
-Renamed from @Tokens::ClassOfKind@ (was also @Tokens::AbbreviationForKind@ for a while).
-The term "token class" is no longer used in CodeRay. Instead, tokens have _kinds_.
-See "#122":http://redmine.rubychan.de/issues/122.
-
-* *RENAMED* @:pre_constant@ and @:pre_type@ to @:predefined_constant@ and @predefined_type@.
-* *REMOVED* token kinds @:attribute_name_fat@, @:attribute_value_fat@, @:operator_fat@,
- @:tag_fat@, @:xml_text@, @:nesting_delimiter@, @:open@, and @:close@.
-* *ADDED* token kinds @:filename@, @:namespace@, and @:eyecatcher@.
-* *CHANGED*: Don't raise error for unknown token kinds unless in @$CODERAY_DEBUG@ mode.
-* *CHANGED* the value for a token kind that is not highlighted from
- @:NO_HIGHLIGHT@ to @false@.
-
-h3. @Duo@
-
-* *NEW* method @call@ for allowing code like @CodeRay::Duo[:python => :yaml].(code)@ in Ruby 1.9.
-
-h3. @Encoders::CommentFilter@
-
-* *NEW* alias @:remove_comments@
-
-h3. @Encoders::Filter@
-
-* *NEW* option @tokens@.
-* *CHANGED*: Now it simply delegates to the output.
-* *REMOVED* @include_text_token?@ and @include_block_token?@ methods.
-
-h3. @Encoders::HTML@
-
-The HTML encoder was cleaned up and simplified.
-
-* *CHANGED* the default style to @:alpha@.
-* *NEW*: HTML5 and CSS 3 compatible.
- See "#215":http://redmine.rubychan.de/issues/215.
-* *ADDED* support for @:line_number_anchors@.
- See "#208":http://redmine.rubychan.de/issues/208.
-* *CHANGED*: Use double click to toggle line numbers in table mode (as single
- click jumps to an anchor.)
-* *REMOVED* support for @:line_numbers => :list@.
-* *FIXED* splitting of lines for @:line_numbers => :inline@, so that the line
- numbers don't get colored, too.
-* *RENAMED* @Output#numerize@ to @#number@, which is an actual English word.
-
-h3. @Encoders::LinesOfCode@
-
-* *CHANGED*: @compile@ and @finish@ methods are now protected.
-
-h3. @Encoders::Terminal@
-
-* *RENAMED* from @Encoders::Term@, added @:term@ alias.
-* *CLEANUP*: Use @#setup@'s @super@, don't use @:procedure@ token class.
-* *CHANGED*: @#token@'s second parameter is no longer optional.
-* *REMOVED* colors for obsolete token kinds.
-* *FIXED* handling of line tokens.
-
-h3. @Encoders::Text@
-
-* *FIXED* default behavior of stripping the trailing newline.
-
-h3. *RENAMED*: @Encoders::TokenKindFilter@
-
-Renamed from @TokenClassFilter@.
-
-* *NEW*: Handles token groups.
- See "#223":http://redmine.rubychan.de/issues/223.
-* *RENAMED* @include_block_token?@ to @include_group?@.
-
-h3. @Encoders::Statistic@
-
-* *CHANGED*: Tokens actions are counted separately.
-
-h3. @Scanners::Scanner@
-
-* *REMOVED* helper method @String#to_unix@.
-* *REMOVED* method @#streamable?@.
-* *REMOVED* @#marshal_load@ and @#marshal_dump@.
-* *RENAMED* class method @normify@ to @normalize@; it also deals with encoding now.
-* *NEW* methods @#file_extension@ and @#encoding@.
-* *NEW*: The @#tokenize@ method also takes an Array of Strings as source. The
- code is highlighted as one and split into parts of the input lengths
- after that using @Tokens#split_into_parts@.
-
-h3. *NEW*: @Scanners::Clojure@
-
-Thanks to Licenser, CodeRay now supports the Clojure language.
-
-h3. @Scanners::CSS@
-
-* *NEW*: Rudimentary support for the @attr@, @counter@, and @counters@ functions.
- See "#224":http://redmine.rubychan.de/issues/224.
-* *NEW*: Rudimentary support for CSS 3 colors.
-* *CHANGED*: Attribute selectors are highlighted as @:attribute_name@ instead of @:string@.
-* *CHANGED*: Comments are scanned as one token instead of three.
-
-h3. @Scanners::Debug@
-
-* *NEW*: Support for line tokens (@begin_line@ and @end_line@ represented by @[@ and @]@.)
-* *FIXED*: Don't send @:error@ and @nil@ tokens for buggy input any more.
-* *FIXED*: Closes unclosed tokens at the end of @scan_tokens@.
-* *IMPROVED*: Highlight unknown tokens as @:error@.
-* *CHANGED*: Raises an error when trying to end an invalid token group.
-
-h3. @Scanners::Delphi@
-
-* *FIXED*: Closes open string groups.
-
-h3. @Scanners::Diff@
-
-* *NEW*: Highlighting of code based on file names.
- See ticket "#52":http://redmine.rubychan.de/issues/52.
-
- Use the @:highlight_code@ option to turn this feature off. It's enabled
- by default.
-
- This is a very original feature. It enables multi-language highlighting for
- diff files, which is especially helpful for CodeRay development itself. The
- updated version of the scanner test suite generated .debug.diff.html files
- using this.
-
- Note: This is still experimental. Tokens spanning more than one line
- may get highlighted incorrectly. CodeRay tries to keep scanner states
- between the lines and changes, but the quality of the results depend on
- the scanner.
-* *NEW*: Inline change highlighting, as suggested by Eric Thomas.
- See ticket "#227":http://redmine.rubychan.de/issues/227 for details.
-
- Use the @:inline_diff@ option to turn this feature off. It's enabled by
- default.
-
- For single-line changes (that is, a single deleted line followed by a single
- inserted line), this feature surrounds the changed parts with an
- @:eyecatcher@ group which appears in a more saturated background color.
- The implementation is quite complex, and highly experimental. The problem
- with multi-layer tokenizing is that the tokens have to be split into parts.
- If the inline change starts, say, in the middle of a string, then additional
- @:end_group@ and @:begin_group@ tokens must be inserted to keep the group
- nesting intact. The extended @Scanner#tokenize@ method and the new
- @Tokens#split_into_parts@ method take care of this.
-* *NEW*: Highlight the file name in the change headers as @:filename@.
-* *CHANGED*: Highlight unknown lines as @:comment@ instead of @:head@.
-* *IMPROVED*: Background colors for Diff output have been optimized.
-
-h3. @Scanners::HTML@
-
-* *FIXED*: Closes open string groups.
-
-h3. @Scanners::JavaScript@
-
-* *IMPROVED*: Added @NaN@ and @Infinity@ to list of predefined constants.
-* *IMPROVED* recognition of RegExp literals with leading spaces.
-
-h3. @Scanners::Java@
-
-* *NEW*: Package names are highlighted as @:namespace@.
- See "#210":http://redmine.rubychan.de/issues/210.
-
-h3. *RENAMED*: @Scanners::Text@ (was @Scanners::Plaintext@)
-
-* *IMPROVED*: Just returns the string without scanning (faster).
-
- This is much faster than scanning until @/\z/@ in Ruby 1.8.
-
-h3. @Scanners::Python@
-
-* *CHANGED*: Docstrings are highlighted as @:comment@.
- See "#190":http://redmine.rubychan.de/issues/190.
-
-h3. *NEW*: @Scanners::Raydebug@
-
-Copied from @Scanners::Debug@, highlights the token dump instead of importing it. It also reacts to the @.raydebug@ file name suffix now.
-
-h3. @Scanners::Ruby@
-
-* *ADDED* more predefined keywords (see http://murfy.de/ruby-constants).
-* *IMPROVED* support for singleton method definitions.
- See "#147":http://redmine.rubychan.de/issues/147.
-* *FIXED*: Don't highlight methods with a capital letter as constants
- (eg. GL.PushMatrix).
-* *NEW*: Highlight buggy floats (like .5) as @:error@.
-* *CLEANUP* of documentation, names of constants and variables, state handling.
-
- Moved @StringState@ class from @patterns.rb@ into a separate file.
-* *NEW*: Complicated rule for recognition of @foo=@ style method names.
-* *NEW*: Handles @:keep_state@ option (a bit; experimental).
-
- Actually, Ruby checks if there is @[~>=]@, but not @=>@ following the name.
-
-* *REMOVED* @EncodingError@
-
-h3. *REMOVED* @Scanners::Scheme@
-
-* It is too buggy, and nobody was using it. To be added again when it's fixed.
- See "#59":http://redmine.rubychan.de/issues/59.
-
-h3. @Scanners::SQL@
-
-* *IMPROVED*: Extended list of keywords and functions (thanks to
- Joshua Galvez, Etienne Massip, and others).
-
- See "#221":http://redmine.rubychan.de/issues/221.
-* *FIXED*: Closes open string groups.
-* *FIXED*: Words after @.@ are always recognized as @:ident@.
-
-h3. @Scanners::YAML@
-
-* *FIXED*: Allow spaces before colon in mappings.
-
- See "#231":http://redmine.rubychan.de/issues/231.
-
-h3. *NEW*: @Styles::Alpha@
-
-A style that uses transparent HSLA colors as defined in CSS 3.
-See "#199":http://redmine.rubychan.de/issues/199.
-
-It also uses the CSS 3 property @user-select: none@ to keep the user from selecting the line numbers. This is especially nice for @:inline@ line numbers.
-See "#226":http://redmine.rubychan.de/issues/226.
-
-h3. @WordList@
-
-Stripped down to 19 LOC.
-
-* *REMOVED* caching option because it creates memory leaks.
-* *REMOVED* block option.
-
-h3. @FileType@
-
-* *REMOVED* @FileType#shebang@ is a protected method now.
-* *NEW*: Recognizes @.gemspec@, @.rjs@, @.rpdf@ extensions, @Gemfile@, and @Capfile@ as Ruby.
-
- Thanks to the authors of the TextMate Ruby bundle!
-
-h3. @Plugin@
-
-* *IMPROVED*: @register_for@ sets the @plugin_id@; it can now be a @Symbol@.
-* *ADDED* @PluginHost#const_missing@ method: Plugins are loaded automatically.
- Using @Scanners::JavaScript@ in your code loads @scanners/java_script.rb@.
-* *ADDED* @#all_plugins@ method to simplify getting
- information about all available plugins (suggested by bnhymn).
-
-h3. @GZip@
-
-* *MOVED* into @CodeRay@ namespace.
-* *MOVED* file from @gzip_simple.rb@ to @gzip.rb@.
-* *REMOVED* @String@ extensions.
-
-h3. Internal API changes
-
-* *FIXED* @Encoders::HTML#token@'s second parameter is no longer optional.
-* *CHANGED* @Encoders::HTML::Output@'s API.
-* *REMOVED* lots of unused methods.
-
-
h2. Changes in 0.9.8 "banister" [2011-05-01]
Fixes for JRuby's 1.9 mode and minor issues.
diff --git a/Gemfile b/Gemfile
index bac8282..d5ae377 100644
--- a/Gemfile
+++ b/Gemfile
@@ -8,6 +8,7 @@ gemspec
group :development do
gem "rake", "~> 0.9.2"
gem "shoulda-context", "= 1.0.0.beta1"
+ gem "RedCloth"
gem "json" unless RUBY_VERSION >= '1.9.1'
gem "bundler", "~> 1.0.0"
end
diff --git a/bin/coderay b/bin/coderay
index fa87fa9..acb05ed 100755
--- a/bin/coderay
+++ b/bin/coderay
@@ -105,23 +105,38 @@ when 'highlight', nil
if output_file
output_filetype ||= CodeRay::FileType[output_file]
else
- output_filetype ||= tty? ? :term : :page
+ output_filetype ||= :term
end
+ output_filetype = :page if output_filetype.to_s == 'html'
+
if input_file
input = File.read input_file
else
input = $stdin.read
end
- output = CodeRay.scan(input, input_filetype).encode(output_filetype)
-
- if output_file
- File.open output_file, 'w' do |file|
- file.puts output
+ begin
+ file =
+ if output_file
+ File.open output_file, 'w'
+ else
+ $stdout.sync = true
+ $stdout
+ end
+ CodeRay.encode(input, input_filetype, output_filetype, :out => file)
+ file.puts
+ rescue CodeRay::PluginHost::PluginNotFound => boom
+ if boom.message[/CodeRay::(\w+)s could not load plugin :?(.*?): /]
+ puts "I don't know the #$1 \"#$2\"."
+ else
+ puts boom.message
end
- else
- puts output
+ # puts "I don't know this plugin: #{boom.message[/Could not load plugin (.*?): /, 1]}."
+ rescue CodeRay::Scanners::Scanner::ScanError # FIXME: rescue Errno::EPIPE
+ # ignore
+ ensure
+ file.close
end
end
when 'list'
@@ -151,6 +166,13 @@ when 'commands'
when 'help'
help
else
- puts "Unknown command: #{subcommand}"
+ $stdout = $stderr
help
+ puts
+ if subcommand[/\A\w+\z/]
+ puts "Unknown command: #{subcommand}"
+ else
+ puts "File not found: #{subcommand}"
+ end
+ exit 1
end
diff --git a/etc/CodeRay.tmproj b/etc/CodeRay.tmproj
index cbf492b..4ca8548 100644
--- a/etc/CodeRay.tmproj
+++ b/etc/CodeRay.tmproj
@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
+ <key>currentDocument</key>
+ <string>../test/functional/for_redcloth.rb</string>
<key>documents</key>
<array>
<dict>
@@ -15,8 +17,6 @@
<string>../lib</string>
</dict>
<dict>
- <key>expanded</key>
- <true/>
<key>name</key>
<string>bin</string>
<key>regexFolderFilter</key>
@@ -26,21 +26,9 @@
</dict>
<dict>
<key>filename</key>
- <string>../diff</string>
- <key>lastUsed</key>
- <date>2011-05-21T05:54:27Z</date>
- </dict>
- <dict>
- <key>filename</key>
- <string>../test/scanners/diff.diff</string>
- <key>lastUsed</key>
- <date>2010-06-27T00:11:50Z</date>
- </dict>
- <dict>
- <key>filename</key>
<string>../Changes.textile</string>
<key>lastUsed</key>
- <date>2011-05-21T04:50:20Z</date>
+ <date>2011-08-16T18:06:14Z</date>
</dict>
<dict>
<key>filename</key>
@@ -55,8 +43,6 @@
<date>2010-06-27T05:41:28Z</date>
</dict>
<dict>
- <key>expanded</key>
- <true/>
<key>name</key>
<string>etc</string>
<key>regexFolderFilter</key>
@@ -65,14 +51,6 @@
<string></string>
</dict>
<dict>
- <key>name</key>
- <string>gem_server</string>
- <key>regexFolderFilter</key>
- <string>!.*/(\.[^/]*|CVS|_darcs|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
- <key>sourceDirectory</key>
- <string>../gem_server</string>
- </dict>
- <dict>
<key>filename</key>
<string>../IDEA</string>
<key>lastUsed</key>
@@ -93,8 +71,6 @@
<string>../rake_helpers</string>
</dict>
<dict>
- <key>expanded</key>
- <true/>
<key>name</key>
<string>rake_tasks</string>
<key>regexFolderFilter</key>
@@ -106,7 +82,7 @@
<key>filename</key>
<string>../Rakefile</string>
<key>lastUsed</key>
- <date>2011-07-08T15:25:14Z</date>
+ <date>2011-08-01T01:19:43Z</date>
</dict>
<dict>
<key>expanded</key>
@@ -119,6 +95,8 @@
<string>../test/executable</string>
</dict>
<dict>
+ <key>expanded</key>
+ <true/>
<key>name</key>
<string>functional</string>
<key>regexFolderFilter</key>
@@ -133,7 +111,7 @@
<key>filename</key>
<string>../test/scanners/coderay_suite.rb</string>
<key>lastUsed</key>
- <date>2011-06-26T01:18:25Z</date>
+ <date>2011-08-01T01:19:42Z</date>
</dict>
<dict>
<key>filename</key>
@@ -146,6 +124,8 @@
<string>scanners</string>
</dict>
<dict>
+ <key>expanded</key>
+ <true/>
<key>name</key>
<string>unit</string>
<key>regexFolderFilter</key>
@@ -157,28 +137,32 @@
<key>filename</key>
<string>../bench/bench.rb</string>
<key>lastUsed</key>
- <date>2011-06-26T15:21:10Z</date>
+ <date>2011-07-11T22:05:29Z</date>
</dict>
</array>
<key>fileHierarchyDrawerWidth</key>
<integer>204</integer>
<key>metaData</key>
<dict>
- <key>../diff</key>
+ <key>../test/functional/for_redcloth.rb</key>
<dict>
<key>caret</key>
<dict>
<key>column</key>
<integer>0</integer>
<key>line</key>
- <integer>0</integer>
+ <integer>2</integer>
</dict>
<key>firstVisibleColumn</key>
<integer>0</integer>
<key>firstVisibleLine</key>
- <integer>3</integer>
+ <integer>36</integer>
</dict>
</dict>
+ <key>openDocuments</key>
+ <array>
+ <string>../test/functional/for_redcloth.rb</string>
+ </array>
<key>showFileHierarchyDrawer</key>
<true/>
<key>windowFrame</key>
diff --git a/etc/language_report.textile b/etc/language_report.textile
index d79ed10..613f480 100644
--- a/etc/language_report.textile
+++ b/etc/language_report.textile
@@ -13,6 +13,12 @@ h3. Supported by both
* RHTML
* Ruby
* XML
+* JavaScript
+* CSS
+* PHP
+* Diff
+* SQL
+
h3. Only in CodeRay
@@ -21,12 +27,6 @@ h3. Only in CodeRay
h3. Only in Pygments, but soon in CodeRay
-* JavaScript
-* CSS
-* PHP
-* Diff
-* SQL
-
h3. Soon only in CodeRay
* IO
diff --git a/lib/coderay.rb b/lib/coderay.rb
index d2d7331..9779ff5 100644
--- a/lib/coderay.rb
+++ b/lib/coderay.rb
@@ -158,6 +158,7 @@ module CodeRay
#
# See also demo/demo_simple.
def scan code, lang, options = {}, &block
+ # FIXME: return a proxy for direct-stream encoding
scanner = Scanners[lang].new code, options, &block
scanner.tokenize
end
@@ -187,7 +188,7 @@ module CodeRay
# encodes it with the Encoder for +format+.
# +options+ will be passed to the Encoder.
#
- # See CodeRay::Encoder.encode
+ # See CodeRay::Encoder.encode.
def encode code, lang, format, options = {}
encoder(format, options).encode code, lang, options
end
diff --git a/lib/coderay/encoder.rb b/lib/coderay/encoder.rb
index 85a2456..cc331d1 100644
--- a/lib/coderay/encoder.rb
+++ b/lib/coderay/encoder.rb
@@ -153,7 +153,17 @@ module CodeRay
#
# See the HTML Encoder for an example of option caching.
def setup options
- @out = ''
+ @out = get_output(options)
+ end
+
+ def get_output options
+ options[:out] || ''
+ end
+
+ # Append data.to_s to the output. Returns the argument.
+ def output data
+ @out << data.to_s
+ data
end
# Called with merged options after encoding starts.
diff --git a/lib/coderay/encoders/_map.rb b/lib/coderay/encoders/_map.rb
index 24ada0a..e5cbdeb 100644
--- a/lib/coderay/encoders/_map.rb
+++ b/lib/coderay/encoders/_map.rb
@@ -3,7 +3,6 @@ module Encoders
map \
:loc => :lines_of_code,
- :html => :page,
:plain => :text,
:plaintext => :text,
:remove_comments => :comment_filter,
diff --git a/lib/coderay/encoders/count.rb b/lib/coderay/encoders/count.rb
index 52d4bdd..98a427e 100644
--- a/lib/coderay/encoders/count.rb
+++ b/lib/coderay/encoders/count.rb
@@ -11,17 +11,23 @@ module Encoders
protected
def setup options
- @out = 0
+ super
+
+ @count = 0
+ end
+
+ def finish options
+ output @count
end
public
def text_token text, kind
- @out += 1
+ @count += 1
end
def begin_group kind
- @out += 1
+ @count += 1
end
alias end_group begin_group
alias begin_line begin_group
diff --git a/lib/coderay/encoders/debug.rb b/lib/coderay/encoders/debug.rb
index 08f1309..95d6138 100644
--- a/lib/coderay/encoders/debug.rb
+++ b/lib/coderay/encoders/debug.rb
@@ -27,7 +27,7 @@ module Encoders
if kind == :space
@out << text
else
- # FIXME: Escape (
+ # TODO: Escape (
text = text.gsub(/[)\\]/, '\\\\\0') # escape ) and \
@out << kind.to_s << '(' << text << ')'
end
diff --git a/lib/coderay/encoders/filter.rb b/lib/coderay/encoders/filter.rb
index a71e43e..e7f34d6 100644
--- a/lib/coderay/encoders/filter.rb
+++ b/lib/coderay/encoders/filter.rb
@@ -21,29 +21,35 @@ module Encoders
protected
def setup options
- @out = options[:tokens] || Tokens.new
+ super
+
+ @tokens = options[:tokens] || Tokens.new
+ end
+
+ def finish options
+ output @tokens
end
public
def text_token text, kind # :nodoc:
- @out.text_token text, kind
+ @tokens.text_token text, kind
end
def begin_group kind # :nodoc:
- @out.begin_group kind
+ @tokens.begin_group kind
end
def begin_line kind # :nodoc:
- @out.begin_line kind
+ @tokens.begin_line kind
end
def end_group kind # :nodoc:
- @out.end_group kind
+ @tokens.end_group kind
end
def end_line kind # :nodoc:
- @out.end_line kind
+ @tokens.end_line kind
end
end
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index ec73d2c..abbafad 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -164,6 +164,11 @@ module Encoders
def setup options
super
+ if options[:wrap] || options[:line_numbers]
+ @real_out = @out
+ @out = ''
+ end
+
@HTML_ESCAPE = HTML_ESCAPE.dup
@HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
@@ -214,7 +219,7 @@ module Encoders
def finish options
unless @opened.empty?
- warn '%d tokens still open: %p' % [@opened.size, @opened]
+ warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG
@out << '</span>' while @opened.pop
@last_opened = nil
end
@@ -227,6 +232,11 @@ module Encoders
@out.wrap! options[:wrap]
@out.apply_title! options[:title]
+ if defined?(@real_out) && @real_out
+ @real_out << @out
+ @out = @real_out
+ end
+
super
end
diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb
index c459222..6de4b46 100644
--- a/lib/coderay/encoders/html/css.rb
+++ b/lib/coderay/encoders/html/css.rb
@@ -44,7 +44,7 @@ module Encoders
( [^\}]+ )? # $2 = style
\s* \} \s*
|
- ( . ) # $3 = error
+ ( [^\n]+ ) # $3 = error
/mx
def parse stylesheet
stylesheet.scan CSS_CLASS_PATTERN do |selectors, style, error|
diff --git a/lib/coderay/encoders/html/output.rb b/lib/coderay/encoders/html/output.rb
index 004351b..4f65878 100644
--- a/lib/coderay/encoders/html/output.rb
+++ b/lib/coderay/encoders/html/output.rb
@@ -55,7 +55,6 @@ module Encoders
end
def apply_title! title
- # FIXME: This may change the output!
self.sub!(/(<title>)(<\/title>)/) { $1 + title + $2 }
self
end
diff --git a/lib/coderay/encoders/json.rb b/lib/coderay/encoders/json.rb
index ccad554..0a95397 100644
--- a/lib/coderay/encoders/json.rb
+++ b/lib/coderay/encoders/json.rb
@@ -36,32 +36,45 @@ module Encoders
protected
def setup options
- @out = []
+ super
+
+ @first = true
+ @out << '['
end
def finish options
- @out.to_json
+ @out << ']'
+ end
+
+ def append data
+ if @first
+ @first = false
+ else
+ @out << ','
+ end
+
+ @out << data.to_json
end
public
def text_token text, kind
- @out << { :type => 'text', :text => text, :kind => kind }
+ append :type => 'text', :text => text, :kind => kind
end
def begin_group kind
- @out << { :type => 'block', :action => 'open', :kind => kind }
+ append :type => 'block', :action => 'open', :kind => kind
end
def end_group kind
- @out << { :type => 'block', :action => 'close', :kind => kind }
+ append :type => 'block', :action => 'close', :kind => kind
end
def begin_line kind
- @out << { :type => 'block', :action => 'begin_line', :kind => kind }
+ append :type => 'block', :action => 'begin_line', :kind => kind
end
def end_line kind
- @out << { :type => 'block', :action => 'end_line', :kind => kind }
+ append :type => 'block', :action => 'end_line', :kind => kind
end
end
diff --git a/lib/coderay/encoders/lines_of_code.rb b/lib/coderay/encoders/lines_of_code.rb
index c49cade..5f8422f 100644
--- a/lib/coderay/encoders/lines_of_code.rb
+++ b/lib/coderay/encoders/lines_of_code.rb
@@ -31,11 +31,12 @@ module Encoders
end
options[:exclude] = kinds_not_loc
+
super options
end
def finish options
- @out.to_s.scan(NON_EMPTY_LINE).size
+ output @tokens.text.scan(NON_EMPTY_LINE).size
end
end
diff --git a/lib/coderay/encoders/statistic.rb b/lib/coderay/encoders/statistic.rb
index e52f28f..2315d9e 100644
--- a/lib/coderay/encoders/statistic.rb
+++ b/lib/coderay/encoders/statistic.rb
@@ -15,15 +15,12 @@ module Encoders
protected
def setup options
+ super
+
@type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
@real_token_count = 0
end
- def generate tokens, options
- @tokens = tokens
- super
- end
-
STATS = <<-STATS # :nodoc:
Code Statistics
@@ -51,11 +48,13 @@ Token Types (%d):
types_stats = @type_stats.sort_by { |k, v| [-v.count, k.to_s] }.map do |k, v|
TOKEN_TYPES_ROW % [k, v.count, 100.0 * v.count / all_count, v.size]
end.join
- STATS % [
+ @out << STATS % [
all_count, @real_token_count, all_size,
@type_stats.delete_if { |k, v| k.is_a? String }.size,
types_stats
]
+
+ super
end
public
diff --git a/lib/coderay/encoders/text.rb b/lib/coderay/encoders/text.rb
index 84e2215..15c66f9 100644
--- a/lib/coderay/encoders/text.rb
+++ b/lib/coderay/encoders/text.rb
@@ -24,19 +24,22 @@ module Encoders
def text_token text, kind
super
- @out << @sep if @sep
+
+ if @first
+ @first = false
+ else
+ @out << @sep
+ end if @sep
end
protected
def setup options
super
+
+ @first = true
@sep = options[:separator]
end
- def finish options
- super.chomp @sep
- end
-
end
end
diff --git a/lib/coderay/encoders/token_kind_filter.rb b/lib/coderay/encoders/token_kind_filter.rb
index 1ecf6ae..4773ea3 100644
--- a/lib/coderay/encoders/token_kind_filter.rb
+++ b/lib/coderay/encoders/token_kind_filter.rb
@@ -34,6 +34,7 @@ module Encoders
protected
def setup options
super
+
@group_excluded = false
@exclude = options[:exclude]
@exclude = Array(@exclude) unless @exclude == :all
diff --git a/lib/coderay/encoders/xml.rb b/lib/coderay/encoders/xml.rb
index f2e7c02..3d306a6 100644
--- a/lib/coderay/encoders/xml.rb
+++ b/lib/coderay/encoders/xml.rb
@@ -10,7 +10,7 @@ module Encoders
FILE_EXTENSION = 'xml'
- require 'rexml/document'
+ autoload :REXML, 'rexml/document'
DEFAULT_OPTIONS = {
:tab_width => 8,
@@ -20,6 +20,8 @@ module Encoders
protected
def setup options
+ super
+
@doc = REXML::Document.new
@doc << REXML::XMLDecl.new
@tab_width = options[:tab_width]
@@ -27,9 +29,9 @@ module Encoders
end
def finish options
- @out = ''
@doc.write @out, options[:pretty], options[:transitive], true
- @out
+
+ super
end
public
diff --git a/lib/coderay/encoders/yaml.rb b/lib/coderay/encoders/yaml.rb
index c12f8d0..1eb2523 100644
--- a/lib/coderay/encoders/yaml.rb
+++ b/lib/coderay/encoders/yaml.rb
@@ -6,39 +6,44 @@ module Encoders
# Slow.
class YAML < Encoder
+ autoload :YAML, 'yaml'
+
register_for :yaml
FILE_EXTENSION = 'yaml'
protected
def setup options
- require 'yaml'
- @out = []
+ super
+
+ @data = []
end
def finish options
- @out.to_a.to_yaml
+ YAML.dump @data, @out
+
+ super
end
public
def text_token text, kind
- @out << [text, kind]
+ @data << [text, kind]
end
def begin_group kind
- @out << [:begin_group, kind]
+ @data << [:begin_group, kind]
end
def end_group kind
- @out << [:end_group, kind]
+ @data << [:end_group, kind]
end
def begin_line kind
- @out << [:begin_line, kind]
+ @data << [:begin_line, kind]
end
def end_line kind
- @out << [:end_line, kind]
+ @data << [:end_line, kind]
end
end
diff --git a/lib/coderay/helpers/file_type.rb b/lib/coderay/helpers/file_type.rb
index ea7125f..c752a03 100644
--- a/lib/coderay/helpers/file_type.rb
+++ b/lib/coderay/helpers/file_type.rb
@@ -90,7 +90,7 @@ module CodeRay
'h' => :c,
'htm' => :page,
'html' => :page,
- 'html.erb' => :rhtml,
+ 'html.erb' => :erb,
'java' => :java,
'js' => :java_script,
'json' => :json,
@@ -109,7 +109,7 @@ module CodeRay
'raydebug' => :raydebug,
'rb' => :ruby,
'rbw' => :ruby,
- 'rhtml' => :rhtml,
+ 'rhtml' => :erb,
'rjs' => :ruby,
'rpdf' => :ruby,
'ru' => :ruby,
diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb
index 8a8e149..06c1233 100644
--- a/lib/coderay/helpers/plugin.rb
+++ b/lib/coderay/helpers/plugin.rb
@@ -114,9 +114,10 @@ module CodeRay
def default id = nil
if id
id = validate_id id
- plugin_hash[nil] = id
+ raise "The default plugin can't be named \"default\"." if id == :default
+ plugin_hash[:default] = id
else
- load nil
+ load :default
end
end
@@ -179,10 +180,11 @@ module CodeRay
require path
rescue LoadError => boom
if @plugin_map_loaded
- if h.has_key?(nil) # default plugin
- h[nil]
+ if h.has_key?(:default)
+ warn '%p could not load plugin %p; falling back to %p' % [self, id, h[:default]]
+ h[:default]
else
- raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
+ raise PluginNotFound, '%p could not load plugin %p: %s' % [self, id, boom]
end
else
load_plugin_map
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb
index 0e0723c..e638c2c 100644
--- a/lib/coderay/scanner.rb
+++ b/lib/coderay/scanner.rb
@@ -74,7 +74,7 @@ module CodeRay
if code.respond_to? :encoding
code = encode_with_encoding code, self.encoding
else
- code = to_unix code if code.index ?\r
+ code = to_unix code
end
# code = code.dup if code.eql? original
code
@@ -100,7 +100,7 @@ module CodeRay
def encode_with_encoding code, target_encoding
if code.encoding == target_encoding
if code.valid_encoding?
- return to_unix(code)
+ return to_unix code
else
source_encoding = guess_encoding code
end
@@ -112,7 +112,7 @@ module CodeRay
end
def to_unix code
- code.gsub(/\r\n?/, "\n")
+ code.index(?\r) ? code.gsub(/\r\n?/, "\n") : code
end
def guess_encoding s
@@ -221,27 +221,39 @@ module CodeRay
end
include Enumerable
- # The current line position of the scanner. See also #column.
+ # The current line position of the scanner, starting with 1.
+ # See also: #column.
#
# Beware, this is implemented inefficiently. It should be used
# for debugging only.
- def line
- string[0..pos].count("\n") + 1
+ def line pos = self.pos
+ return 1 if pos <= 0
+ binary_string[0...pos].count("\n") + 1
end
- # The current column position of the scanner. See also #line.
+ # The current column position of the scanner, starting with 1.
+ # See also: #line.
#
# Beware, this is implemented inefficiently. It should be used
# for debugging only.
def column pos = self.pos
- return 0 if pos <= 0
- string = self.string
- if string.respond_to?(:bytesize) && string.bytesize != string.size
- #:nocov:
- string = string.dup.force_encoding('binary')
- #:nocov:
- end
- pos - (string.rindex(?\n, pos) || 0)
+ return 1 if pos <= 0
+ pos - (binary_string.rindex(?\n, pos - 1) || -1)
+ end
+
+ # The string in binary encoding.
+ #
+ # To be used with #pos, which is the index of the byte the scanner
+ # will scan next.
+ def binary_string
+ @binary_string ||=
+ if string.respond_to?(:bytesize) && string.bytesize != string.size
+ #:nocov:
+ string.dup.force_encoding('binary')
+ #:nocov:
+ else
+ string
+ end
end
protected
@@ -267,7 +279,7 @@ module CodeRay
def reset_instance
@tokens.clear if @tokens.respond_to?(:clear) && !@options[:keep_tokens]
@cached_tokens = nil
- @bin_string = nil if defined? @bin_string
+ @binary_string = nil if defined? @binary_string
end
# Scanner error with additional status information
@@ -297,8 +309,8 @@ surrounding code:
tokens.respond_to?(:last) ? tokens.last(10).map { |t| t.inspect }.join("\n") : '',
line, column, pos,
matched, state, bol?, eos?,
- string[pos - ambit, ambit],
- string[pos, ambit],
+ binary_string[pos - ambit, ambit],
+ binary_string[pos, ambit],
], backtrace
end
diff --git a/lib/coderay/scanners/_map.rb b/lib/coderay/scanners/_map.rb
index b94d452..a240298 100644
--- a/lib/coderay/scanners/_map.rb
+++ b/lib/coderay/scanners/_map.rb
@@ -6,11 +6,11 @@ module Scanners
:cplusplus => :cpp,
:ecmascript => :java_script,
:ecma_script => :java_script,
- :erb => :rhtml,
+ :rhtml => :erb,
+ :eruby => :erb,
:irb => :ruby,
:javascript => :java_script,
:js => :java_script,
- :nitro => :nitro_xhtml,
:pascal => :delphi,
:patch => :diff,
:plain => :text,
diff --git a/lib/coderay/scanners/clojure.rb b/lib/coderay/scanners/clojure.rb
index 89713de..f8fbf65 100644
--- a/lib/coderay/scanners/clojure.rb
+++ b/lib/coderay/scanners/clojure.rb
@@ -156,7 +156,7 @@ module CodeRay
elsif match = scan(/['`\(\[\)\]\{\}]|\#[({]|~@?|[@\^]/)
encoder.text_token match, :operator
elsif match = scan(/;.*/)
- encoder.text_token match, :comment # FIXME: recognize (comment ...) too
+ encoder.text_token match, :comment # TODO: recognize (comment ...) too
elsif match = scan(/\#?\\(?:newline|space|.?)/)
encoder.text_token match, :char
elsif match = scan(/\#[ft]/)
diff --git a/lib/coderay/scanners/css.rb b/lib/coderay/scanners/css.rb
index 6413f8f..e5f03f5 100644
--- a/lib/coderay/scanners/css.rb
+++ b/lib/coderay/scanners/css.rb
@@ -2,9 +2,9 @@ module CodeRay
module Scanners
class CSS < Scanner
-
+
register_for :css
-
+
KINDS_NOT_LOC = [
:comment,
:class, :pseudo_class, :type,
@@ -20,28 +20,28 @@ module Scanners
NMChar = /[-_a-zA-Z0-9]|#{Escape}/
NMStart = /[_a-zA-Z]|#{Escape}/
NL = /\r\n|\r|\n|\f/
- String1 = /"(?:[^\n\r\f\\"]|\\#{NL}|#{Escape})*"?/ # FIXME: buggy regexp
- String2 = /'(?:[^\n\r\f\\']|\\#{NL}|#{Escape})*'?/ # FIXME: buggy regexp
+ String1 = /"(?:[^\n\r\f\\"]|\\#{NL}|#{Escape})*"?/ # TODO: buggy regexp
+ String2 = /'(?:[^\n\r\f\\']|\\#{NL}|#{Escape})*'?/ # TODO: buggy regexp
String = /#{String1}|#{String2}/
-
+
HexColor = /#(?:#{Hex}{6}|#{Hex}{3})/
Color = /#{HexColor}/
-
+
Num = /-?(?:[0-9]+|[0-9]*\.[0-9]+)/
Name = /#{NMChar}+/
Ident = /-?#{NMStart}#{NMChar}*/
AtKeyword = /@#{Ident}/
Percentage = /#{Num}%/
-
+
reldimensions = %w[em ex px]
absdimensions = %w[in cm mm pt pc]
Unit = Regexp.union(*(reldimensions + absdimensions))
-
+
Dimension = /#{Num}#{Unit}/
-
+
Comment = %r! /\* (?: .*? \*/ | .* ) !mx
Function = /(?:url|alpha|attr|counters?)\((?:[^)\n\r\f]|\\\))*\)?/
-
+
Id = /##{Name}/
Class = /\.#{Name}/
PseudoClass = /:#{Name}/
@@ -64,20 +64,26 @@ module Scanners
when :initial, :media
if match = scan(/(?>#{RE::Ident})(?!\()|\*/ox)
encoder.text_token match, :type
+ next
elsif match = scan(RE::Class)
encoder.text_token match, :class
+ next
elsif match = scan(RE::Id)
encoder.text_token match, :constant
+ next
elsif match = scan(RE::PseudoClass)
encoder.text_token match, :pseudo_class
+ next
elsif match = scan(RE::AttributeSelector)
# TODO: Improve highlighting inside of attribute selectors.
encoder.text_token match[0,1], :operator
encoder.text_token match[1..-2], :attribute_name if match.size > 2
encoder.text_token match[-1,1], :operator if match[-1] == ?]
+ next
elsif match = scan(/@media/)
encoder.text_token match, :directive
states.push :media_before_name
+ next
end
when :block
@@ -87,18 +93,21 @@ module Scanners
else
encoder.text_token match, :key
end
+ next
end
-
+
when :media_before_name
if match = scan(RE::Ident)
encoder.text_token match, :type
states[-1] = :media_after_name
+ next
end
when :media_after_name
if match = scan(/\{/)
encoder.text_token match, :operator
states[-1] = :media
+ next
end
else
@@ -110,12 +119,12 @@ module Scanners
elsif match = scan(/\/\*(?:.*?\*\/|\z)/m)
encoder.text_token match, :comment
-
+
elsif match = scan(/\{/)
value_expected = false
encoder.text_token match, :operator
states.push :block
-
+
elsif match = scan(/\}/)
value_expected = false
if states.last == :block || states.last == :media
@@ -124,14 +133,14 @@ module Scanners
else
encoder.text_token match, :error
end
-
+
elsif match = scan(/#{RE::String}/o)
encoder.begin_group :string
encoder.text_token match[0, 1], :delimiter
encoder.text_token match[1..-2], :content if match.size > 2
encoder.text_token match[-1, 1], :delimiter if match.size >= 2
encoder.end_group :string
-
+
elsif match = scan(/#{RE::Function}/o)
encoder.begin_group :string
start = match[/^\w+\(/]
@@ -143,22 +152,22 @@ module Scanners
encoder.text_token match[start.size..-1], :content
end
encoder.end_group :string
-
+
elsif match = scan(/(?: #{RE::Dimension} | #{RE::Percentage} | #{RE::Num} )/ox)
encoder.text_token match, :float
-
+
elsif match = scan(/#{RE::Color}/o)
encoder.text_token match, :color
-
+
elsif match = scan(/! *important/)
encoder.text_token match, :important
-
+
elsif match = scan(/(?:rgb|hsl)a?\([^()\n]*\)?/)
encoder.text_token match, :color
-
+
elsif match = scan(RE::AtKeyword)
encoder.text_token match, :directive
-
+
elsif match = scan(/ [+>:;,.=()\/] /x)
if match == ':'
value_expected = true
@@ -166,18 +175,18 @@ module Scanners
value_expected = false
end
encoder.text_token match, :operator
-
+
else
encoder.text_token getch, :error
-
+
end
-
+
end
-
+
encoder
end
-
+
end
-
+
end
end
diff --git a/lib/coderay/scanners/rhtml.rb b/lib/coderay/scanners/erb.rb
index 9bfab5c..eaf3bba 100644
--- a/lib/coderay/scanners/rhtml.rb
+++ b/lib/coderay/scanners/erb.rb
@@ -5,9 +5,9 @@ module Scanners
load :ruby
# Scanner for HTML ERB templates.
- class RHTML < Scanner
+ class ERB < Scanner
- register_for :rhtml
+ register_for :erb
title 'HTML ERB Template'
KINDS_NOT_LOC = HTML::KINDS_NOT_LOC
@@ -56,7 +56,7 @@ module Scanners
if start_tag[/\A<%#/]
encoder.text_token code, :comment
else
- @ruby_scanner.tokenize code
+ @ruby_scanner.tokenize code, :tokens => encoder
end unless code.empty?
encoder.text_token end_tag, :inline_delimiter unless end_tag.empty?
encoder.end_group :inline
diff --git a/lib/coderay/scanners/groovy.rb b/lib/coderay/scanners/groovy.rb
index 2334bc6..3501aaf 100644
--- a/lib/coderay/scanners/groovy.rb
+++ b/lib/coderay/scanners/groovy.rb
@@ -220,7 +220,7 @@ module Scanners
encoder.text_token match, :content
elsif match = scan(/ \\. /mx)
- encoder.text_token match, :content # FIXME: Shouldn't this be :error?
+ encoder.text_token match, :content # TODO: Shouldn't this be :error?
elsif match = scan(/ \\ | \n /x)
encoder.end_group state
diff --git a/lib/coderay/scanners/java_script.rb b/lib/coderay/scanners/java_script.rb
index 9ebda6f..9063c5e 100644
--- a/lib/coderay/scanners/java_script.rb
+++ b/lib/coderay/scanners/java_script.rb
@@ -89,7 +89,7 @@ module Scanners
end
elsif value_expected && match = scan(/<([[:alpha:]]\w*) (?: [^\/>]*\/> | .*?<\/\1>)/xim)
- # FIXME: scan over nested tags
+ # TODO: scan over nested tags
xml_scanner.tokenize match, :tokens => encoder
value_expected = false
next
diff --git a/lib/coderay/scanners/nitro_xhtml.rb b/lib/coderay/scanners/nitro_xhtml.rb
deleted file mode 100644
index b67b60c..0000000
--- a/lib/coderay/scanners/nitro_xhtml.rb
+++ /dev/null
@@ -1,136 +0,0 @@
-module CodeRay
-module Scanners
-
- load :html
- load :ruby
-
- # Nitro XHTML Scanner
- #
- # Alias: +nitro+
- class NitroXHTML < Scanner
-
- register_for :nitro_xhtml
- file_extension :xhtml
- title 'Nitro XHTML'
-
- KINDS_NOT_LOC = HTML::KINDS_NOT_LOC
-
- NITRO_RUBY_BLOCK = /
- <\?r
- (?>
- [^\?]*
- (?> \?(?!>) [^\?]* )*
- )
- (?: \?> )?
- |
- <ruby>
- (?>
- [^<]*
- (?> <(?!\/ruby>) [^<]* )*
- )
- (?: <\/ruby> )?
- |
- <%
- (?>
- [^%]*
- (?> %(?!>) [^%]* )*
- )
- (?: %> )?
- /mx # :nodoc:
-
- NITRO_VALUE_BLOCK = /
- \#
- (?:
- \{
- [^{}]*
- (?>
- \{ [^}]* \}
- (?> [^{}]* )
- )*
- \}?
- | \| [^|]* \|?
- | \( [^)]* \)?
- | \[ [^\]]* \]?
- | \\ [^\\]* \\?
- )
- /x # :nodoc:
-
- NITRO_ENTITY = /
- % (?: \#\d+ | \w+ ) ;
- / # :nodoc:
-
- START_OF_RUBY = /
- (?=[<\#%])
- < (?: \?r | % | ruby> )
- | \# [{(|]
- | % (?: \#\d+ | \w+ ) ;
- /x # :nodoc:
-
- CLOSING_PAREN = Hash.new { |h, p| h[p] = p } # :nodoc:
- CLOSING_PAREN.update( {
- '(' => ')',
- '[' => ']',
- '{' => '}',
- } )
-
- protected
-
- def setup
- @ruby_scanner = CodeRay.scanner :ruby, :tokens => @tokens, :keep_tokens => true
- @html_scanner = CodeRay.scanner :html, :tokens => @tokens, :keep_tokens => true, :keep_state => true
- end
-
- def reset_instance
- super
- @html_scanner.reset
- end
-
- def scan_tokens encoder, options
-
- until eos?
-
- if (match = scan_until(/(?=#{START_OF_RUBY})/o) || scan_rest) and not match.empty?
- @html_scanner.tokenize match
-
- elsif match = scan(/#{NITRO_VALUE_BLOCK}/o)
- start_tag = match[0,2]
- delimiter = CLOSING_PAREN[start_tag[1,1]]
- end_tag = match[-1,1] == delimiter ? delimiter : ''
- encoder.begin_group :inline
- encoder.text_token start_tag, :inline_delimiter
- code = match[start_tag.size .. -1 - end_tag.size]
- @ruby_scanner.tokenize code, :tokens => encoder
- encoder.text_token end_tag, :inline_delimiter unless end_tag.empty?
- encoder.end_group :inline
-
- elsif match = scan(/#{NITRO_RUBY_BLOCK}/o)
- start_tag = '<?r'
- end_tag = match[-2,2] == '?>' ? '?>' : ''
- encoder.begin_group :inline
- encoder.text_token start_tag, :inline_delimiter
- code = match[start_tag.size .. -(end_tag.size)-1]
- @ruby_scanner.tokenize code, :tokens => encoder
- encoder.text_token end_tag, :inline_delimiter unless end_tag.empty?
- encoder.end_group :inline
-
- elsif entity = scan(/#{NITRO_ENTITY}/o)
- encoder.text_token entity, :entity
-
- elsif scan(/%/)
- encoder.text_token matched, :error
-
- else
- raise_inspect 'else-case reached!', encoder
-
- end
-
- end
-
- encoder
-
- end
-
- end
-
-end
-end
diff --git a/lib/coderay/scanners/php.rb b/lib/coderay/scanners/php.rb
index c290dab..b2632a2 100644
--- a/lib/coderay/scanners/php.rb
+++ b/lib/coderay/scanners/php.rb
@@ -234,8 +234,8 @@ module Scanners
def scan_tokens encoder, options
if check(RE::PHP_START) || # starts with <?
- (match?(/\s*<\S/) && check(/.{1,100}#{RE::PHP_START}/om)) || # starts with tag and contains <?
- check(/.{1,100}#{RE::HTML_INDICATOR}/om) ||
+ (match?(/\s*<\S/) && check(/.{1,1000}#{RE::PHP_START}/om)) || # starts with tag and contains <?
+ check(/.{0,1000}#{RE::HTML_INDICATOR}/om) ||
check(/.{1,100}#{RE::PHP_START}/om) # PHP start after max 100 chars
# is HTML with embedded PHP, so start with HTML
states = [:initial]
diff --git a/lib/coderay/scanners/ruby.rb b/lib/coderay/scanners/ruby.rb
index e1395ca..25da208 100644
--- a/lib/coderay/scanners/ruby.rb
+++ b/lib/coderay/scanners/ruby.rb
@@ -26,7 +26,7 @@ module Scanners
state, heredocs = @state
heredocs = heredocs.dup if heredocs.is_a?(Array)
- if state && state.instance_of?(self.class::StringState)
+ if state && state.instance_of?(StringState)
encoder.begin_group state.type
end
@@ -426,13 +426,18 @@ module Scanners
end
# cleaning up
- if options[:keep_state]
- heredocs = nil if heredocs && heredocs.empty?
- @state = state, heredocs
+ if state.is_a? StringState
+ encoder.end_group state.type
end
- if state.is_a? self.class::StringState
- encoder.end_group state.type
+ if options[:keep_state]
+ if state.is_a?(StringState) && state.heredoc
+ (heredocs ||= []).unshift state
+ state = :initial
+ elsif heredocs && heredocs.empty?
+ heredocs = nil
+ end
+ @state = state, heredocs
end
if inline_block_stack
diff --git a/lib/coderay/tokens.rb b/lib/coderay/tokens.rb
index f6f8845..1566276 100644
--- a/lib/coderay/tokens.rb
+++ b/lib/coderay/tokens.rb
@@ -73,9 +73,7 @@ module CodeRay
encoder.encode_tokens self, options
end
- # Turn into a string using Encoders::Text.
- #
- # +options+ are passed to the encoder if given.
+ # Turn tokens into a string by concatenating them.
def to_s
encode CodeRay::Encoders::Encoder.new
end
diff --git a/rake_tasks/diff.rake b/rake_tasks/diff.rake
index 13e2662..f0af55a 100644
--- a/rake_tasks/diff.rake
+++ b/rake_tasks/diff.rake
@@ -1,7 +1,7 @@
# A simple differ using svn. Handles externals.
class Differ < Hash
- include Rake::DSL
+ include Rake::DSL if defined? Rake::DSL
def initialize path
@path = path
diff --git a/test/executable/source_with_comments.rb b/test/executable/source_with_comments.rb
new file mode 100644
index 0000000..ec79358
--- /dev/null
+++ b/test/executable/source_with_comments.rb
@@ -0,0 +1,3 @@
+# a class
+class ClassName
+end
diff --git a/test/executable/suite.rb b/test/executable/suite.rb
index ec696ec..b3f8025 100644
--- a/test/executable/suite.rb
+++ b/test/executable/suite.rb
@@ -3,31 +3,44 @@ require 'rubygems' unless defined? Gem
require 'shoulda-context'
require 'pathname'
-$:.unshift 'lib'
+require 'json'
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
require 'coderay'
puts "Running CodeRay #{CodeRay::VERSION} executable tests..."
class TestCodeRayExecutable < Test::Unit::TestCase
- ruby = 'ruby'
-
ROOT_DIR = Pathname.new(File.dirname(__FILE__)) + '..' + '..'
EXECUTABLE = ROOT_DIR + 'bin' + 'coderay'
- EXE_COMMAND = '%s -wI%s %s'% [
- ruby, # calling Ruby process command
- ROOT_DIR + 'lib', # library dir
- EXECUTABLE # coderay
- ]
+ EXE_COMMAND =
+ if RUBY_PLATFORM === 'java' && `ruby --ng -e ''` && $?.success?
+ # use Nailgun
+ 'ruby --ng -wI%s %s'
+ else
+ 'ruby -wI%s %s'
+ end % [ROOT_DIR + 'lib', EXECUTABLE]
- def coderay args, fake_tty = false
- if fake_tty
+ def coderay args, options = {}
+ if options[:fake_tty]
command = "#{EXE_COMMAND} #{args} --tty"
else
command = "#{EXE_COMMAND} #{args}"
end
+
puts command if $DEBUG
- output = `#{command} 2>&1`
+
+ if options[:input]
+ output = IO.popen "#{command} 2>&1", "r+" do |io|
+ io.write options[:input]
+ io.close_write
+ io.read
+ end
+ else
+ output = `#{command} 2>&1`
+ end
+
if output[EXECUTABLE.to_s]
raise output
else
@@ -74,30 +87,30 @@ class TestCodeRayExecutable < Test::Unit::TestCase
end
context 'highlighting a file to the terminal' do
- source_file = 'test/executable/source.py'
+ source_file = ROOT_DIR + 'test/executable/source.py'
source = File.read source_file
ansi_seq = /\e\[[0-9;]+m/
should 'not throw an error' do
- assert_nothing_raised { coderay(source_file, :tty) }
+ assert_nothing_raised { coderay(source_file, :fake_tty => true) }
end
should 'output its contents to stdout' do
- target = coderay(source_file, :tty)
+ target = coderay(source_file, :fake_tty => true)
assert_equal source, target.chomp.gsub(ansi_seq, '')
end
should 'output ANSI-colored text' do
- target = coderay(source_file, :tty)
+ target = coderay(source_file, :fake_tty => true)
assert_not_equal source, target.chomp
assert_equal 6, target.scan(ansi_seq).size
end
end
- context 'highlighting a file into a pipe (source.rb > source.rb.html)' do
- source_file = 'test/executable/source.rb'
+ context 'highlighting a file into a pipe (source.rb -html > source.rb.html)' do
+ source_file = ROOT_DIR + 'test/executable/source.rb'
target_file = "#{source_file}.html"
- command = "#{source_file} > #{target_file}"
+ command = "#{source_file} -html > #{target_file}"
source = File.read source_file
@@ -126,7 +139,7 @@ class TestCodeRayExecutable < Test::Unit::TestCase
end
context 'highlighting a file into another file (source.rb source.rb.json)' do
- source_file = 'test/executable/source.rb'
+ source_file = ROOT_DIR + 'test/executable/source.rb'
target_file = "#{source_file}.json"
command = "#{source_file} #{target_file}"
@@ -151,8 +164,8 @@ class TestCodeRayExecutable < Test::Unit::TestCase
end
context 'highlighting a file without explicit input type (source.py)' do
- source_file = 'test/executable/source.py'
- command = source_file
+ source_file = ROOT_DIR + 'test/executable/source.py'
+ command = "#{source_file} -html"
source = File.read source_file
@@ -166,8 +179,8 @@ class TestCodeRayExecutable < Test::Unit::TestCase
end
context 'highlighting a file with explicit input type (-ruby source.py)' do
- source_file = 'test/executable/source.py'
- command = "-ruby #{source_file}"
+ source_file = ROOT_DIR + 'test/executable/source.py'
+ command = "-ruby #{source_file} -html"
source = File.read source_file
@@ -181,7 +194,7 @@ class TestCodeRayExecutable < Test::Unit::TestCase
end
context 'highlighting a file with explicit input and output type (-ruby source.py -span)' do
- source_file = 'test/executable/source.py'
+ source_file = ROOT_DIR + 'test/executable/source.py'
command = "-ruby #{source_file} -span"
source = File.read source_file
@@ -194,4 +207,19 @@ class TestCodeRayExecutable < Test::Unit::TestCase
end
end
+ context 'the LOC counter' do
+ source_file = ROOT_DIR + 'test/executable/source_with_comments.rb'
+ command = "-ruby -loc"
+
+ should 'work' do
+ output = coderay(command, :input => <<-CODE)
+# test
+=begin
+=end
+test
+ CODE
+ assert_equal "1\n", output
+ end
+ end
+
end
diff --git a/test/functional/basic.rb b/test/functional/basic.rb
index 94e1dd7..8200ae4 100755
--- a/test/functional/basic.rb
+++ b/test/functional/basic.rb
@@ -1,21 +1,12 @@
# encoding: utf-8
require 'test/unit'
+require File.expand_path('../../lib/assert_warning', __FILE__)
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
require 'coderay'
class BasicTest < Test::Unit::TestCase
- def assert_warning expected_warning
- require 'stringio'
- oldstderr = $stderr
- $stderr = StringIO.new
- yield
- $stderr.rewind
- given_warning = $stderr.read.chomp
- assert_equal expected_warning, given_warning
- ensure
- $stderr = oldstderr
- end
-
def test_version
assert_nothing_raised do
assert_match(/\A\d\.\d\.\d?\z/, CodeRay::VERSION)
@@ -135,7 +126,7 @@ more code # and another comment, in-line.
assert_equal 0, CodeRay.scan(rHTML, :html).lines_of_code
assert_equal 0, CodeRay.scan(rHTML, :php).lines_of_code
assert_equal 0, CodeRay.scan(rHTML, :yaml).lines_of_code
- assert_equal 4, CodeRay.scan(rHTML, :rhtml).lines_of_code
+ assert_equal 4, CodeRay.scan(rHTML, :erb).lines_of_code
end
def test_rubygems_not_loaded
@@ -243,8 +234,28 @@ more code # and another comment, in-line.
scanner = CodeRay::Scanners::Plain.new "foo\nbär+quux"
assert_equal 0, scanner.pos
assert_equal 1, scanner.line
- assert_equal 0, scanner.column
- scanner.scan(/foo\nbär/)
+ assert_equal 1, scanner.column
+ scanner.scan(/foo/)
+ assert_equal 3, scanner.pos
+ assert_equal 1, scanner.line
+ assert_equal 4, scanner.column
+ scanner.scan(/\n/)
+ assert_equal 4, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 1, scanner.column
+ scanner.scan(/b/)
+ assert_equal 5, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 2, scanner.column
+ scanner.scan(/a/)
+ assert_equal 5, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 2, scanner.column
+ scanner.scan(/ä/)
+ assert_equal 7, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 4, scanner.column
+ scanner.scan(/r/)
assert_equal 8, scanner.pos
assert_equal 2, scanner.line
assert_equal 5, scanner.column
diff --git a/test/functional/examples.rb b/test/functional/examples.rb
index f80c90c..3de8be5 100755
--- a/test/functional/examples.rb
+++ b/test/functional/examples.rb
@@ -1,4 +1,6 @@
require 'test/unit'
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
require 'coderay'
class ExamplesTest < Test::Unit::TestCase
diff --git a/test/functional/for_redcloth.rb b/test/functional/for_redcloth.rb
index 8c6491d..3c45eec 100644
--- a/test/functional/for_redcloth.rb
+++ b/test/functional/for_redcloth.rb
@@ -1,5 +1,7 @@
require 'test/unit'
-$:.unshift 'lib'
+require File.expand_path('../../lib/assert_warning', __FILE__)
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
require 'coderay'
begin
@@ -64,15 +66,19 @@ class BasicTest < Test::Unit::TestCase
# See http://jgarber.lighthouseapp.com/projects/13054/tickets/124-code-markup-does-not-allow-brackets.
def test_for_redcloth_false_positive
require 'coderay/for_redcloth'
- assert_equal '<p><code>[project]_dff.skjd</code></p>',
- RedCloth.new('@[project]_dff.skjd@').to_html
+ assert_warning 'CodeRay::Scanners could not load plugin :project; falling back to :text' do
+ assert_equal '<p><code>[project]_dff.skjd</code></p>',
+ RedCloth.new('@[project]_dff.skjd@').to_html
+ end
# false positive, but expected behavior / known issue
assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">_dff.skjd</span></p>",
RedCloth.new('@[ruby]_dff.skjd@').to_html
- assert_equal <<-BLOCKCODE.chomp,
+ assert_warning 'CodeRay::Scanners could not load plugin :project; falling back to :text' do
+ assert_equal <<-BLOCKCODE.chomp,
<pre><code>[project]_dff.skjd</code></pre>
- BLOCKCODE
- RedCloth.new('bc. [project]_dff.skjd').to_html
+ BLOCKCODE
+ RedCloth.new('bc. [project]_dff.skjd').to_html
+ end
end
end if defined? RedCloth \ No newline at end of file
diff --git a/test/functional/suite.rb b/test/functional/suite.rb
index 6d795c0..5490f98 100755
--- a/test/functional/suite.rb
+++ b/test/functional/suite.rb
@@ -1,5 +1,6 @@
require 'test/unit'
-$:.unshift 'lib'
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
require 'coderay'
mydir = File.dirname(__FILE__)
diff --git a/test/lib/README b/test/lib/README
index 7d7a0a0..7c41648 100644
--- a/test/lib/README
+++ b/test/lib/README
@@ -1,3 +1,2 @@
Contents:
- test/unit: We need the old Test::Unit for the scanner test suite to work with Ruby 1.9.
-- term/ansicolor: Used for colorful output of the scanner tests. \ No newline at end of file
diff --git a/test/lib/assert_warning.rb b/test/lib/assert_warning.rb
new file mode 100644
index 0000000..828b464
--- /dev/null
+++ b/test/lib/assert_warning.rb
@@ -0,0 +1,15 @@
+class Test::Unit::TestCase
+
+ def assert_warning expected_warning
+ require 'stringio'
+ oldstderr = $stderr
+ $stderr = StringIO.new
+ yield
+ $stderr.rewind
+ given_warning = $stderr.read.chomp
+ assert_equal expected_warning, given_warning
+ ensure
+ $stderr = oldstderr
+ end
+
+end
diff --git a/test/unit/file_type.rb b/test/unit/file_type.rb
index d62a006..1dc1ba0 100644
--- a/test/unit/file_type.rb
+++ b/test/unit/file_type.rb
@@ -73,8 +73,8 @@ class FileTypeTests < Test::Unit::TestCase
assert_equal :page, FileType['test.htm']
assert_equal :page, FileType['test.xhtml']
assert_equal :page, FileType['test.html.xhtml']
- assert_equal :rhtml, FileType['_form.rhtml']
- assert_equal :rhtml, FileType['_form.html.erb']
+ assert_equal :erb, FileType['_form.rhtml']
+ assert_equal :erb, FileType['_form.html.erb']
end
def test_yaml
diff --git a/test/unit/plugin.rb b/test/unit/plugin.rb
index 678b883..2231c75 100755
--- a/test/unit/plugin.rb
+++ b/test/unit/plugin.rb
@@ -1,6 +1,8 @@
require 'test/unit'
+require File.expand_path('../../lib/assert_warning', __FILE__)
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
require 'coderay'
-require 'pathname'
class PluginScannerTest < Test::Unit::TestCase
@@ -20,7 +22,7 @@ class PluginScannerTest < Test::Unit::TestCase
extend CodeRay::Plugin
plugin_host PluginsWithDefault
end
- default :default
+ default :default_plugin
end
def test_load
@@ -36,7 +38,9 @@ class PluginScannerTest < Test::Unit::TestCase
def test_default
assert_nothing_raised do
- assert_operator PluginsWithDefault[:gargamel], :<, PluginsWithDefault::Plugin
+ assert_warning 'PluginScannerTest::PluginsWithDefault could not load plugin :gargamel; falling back to :default_plugin' do
+ assert_operator PluginsWithDefault[:gargamel], :<, PluginsWithDefault::Plugin
+ end
end
assert_equal PluginsWithDefault::Default, PluginsWithDefault.default
end
@@ -64,16 +68,4 @@ class PluginScannerTest < Test::Unit::TestCase
assert_equal 'The Example', Plugins::Example.title
end
- def assert_warning expected_warning
- require 'stringio'
- oldstderr = $stderr
- $stderr = StringIO.new
- yield
- $stderr.rewind
- given_warning = $stderr.read.chomp
- assert_equal expected_warning, given_warning
- ensure
- $stderr = oldstderr
- end
-
end
diff --git a/test/unit/plugins/example.rb b/test/unit/plugins/example.rb
index e28dc7c..af1aeba 100644
--- a/test/unit/plugins/example.rb
+++ b/test/unit/plugins/example.rb
@@ -3,4 +3,4 @@ class Example < PluginScannerTest::Plugins::Plugin
register_for :example
title 'The Example'
-end \ No newline at end of file
+end
diff --git a/test/unit/plugins/user_defined/user_plugin.rb b/test/unit/plugins/user_defined/user_plugin.rb
index 61e5372..f47c934 100644
--- a/test/unit/plugins/user_defined/user_plugin.rb
+++ b/test/unit/plugins/user_defined/user_plugin.rb
@@ -2,4 +2,4 @@ class UserPlugin < PluginScannerTest::Plugins::Plugin
register_for :user_plugin
-end \ No newline at end of file
+end
diff --git a/test/unit/plugins_with_default/default.rb b/test/unit/plugins_with_default/default.rb
deleted file mode 100644
index e67c9f1..0000000
--- a/test/unit/plugins_with_default/default.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class Default < PluginScannerTest::PluginsWithDefault::Plugin
-
- register_for :default
-
-end \ No newline at end of file
diff --git a/test/unit/plugins_with_default/default_plugin.rb b/test/unit/plugins_with_default/default_plugin.rb
new file mode 100644
index 0000000..ae9e4c5
--- /dev/null
+++ b/test/unit/plugins_with_default/default_plugin.rb
@@ -0,0 +1,5 @@
+class DefaultPlugin < PluginScannerTest::PluginsWithDefault::Plugin
+
+ register_for :default_plugin
+
+end
diff --git a/test/unit/plugins_with_default/example_without_register_for.rb b/test/unit/plugins_with_default/example_without_register_for.rb
index d9f40be..083baf6 100644
--- a/test/unit/plugins_with_default/example_without_register_for.rb
+++ b/test/unit/plugins_with_default/example_without_register_for.rb
@@ -1,5 +1,5 @@
-class ExampleWithoutRegisterFor < PluginScannerTest::Plugins::Plugin
+class ExampleWithoutRegisterFor < PluginScannerTest::PluginsWithDefault::Plugin
register_for :wrong_id
-end \ No newline at end of file
+end