summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/terminal.rb
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2010-03-30 22:17:44 +0000
committermurphy <murphy@rubychan.de>2010-03-30 22:17:44 +0000
commit262e15cea7a1c32e40c6cb42012a1d8dbca2c88b (patch)
tree5f196200853d1302bad8a7c2ec0ab1d0b70f5f5f /lib/coderay/encoders/terminal.rb
parent79c0f560899260044501f04585701533f0a2694b (diff)
downloadcoderay-262e15cea7a1c32e40c6cb42012a1d8dbca2c88b.tar.gz
Improved Terminal encoder (documentation, handling of line tokens).
Diffstat (limited to 'lib/coderay/encoders/terminal.rb')
-rw-r--r--lib/coderay/encoders/terminal.rb138
1 files changed, 81 insertions, 57 deletions
diff --git a/lib/coderay/encoders/terminal.rb b/lib/coderay/encoders/terminal.rb
index 09f81f5..782147a 100644
--- a/lib/coderay/encoders/terminal.rb
+++ b/lib/coderay/encoders/terminal.rb
@@ -1,23 +1,30 @@
-# By Rob Aldred (http://robaldred.co.uk)
-# Based on idea by Nathan Weizenbaum (http://nex-3.com)
-# MIT License (http://www.opensource.org/licenses/mit-license.php)
-#
-# A CodeRay encoder that outputs code highlighted for a color terminal.
-# Check out http://robaldred.co.uk
-
module CodeRay
module Encoders
+
+ # Outputs code highlighted for a color terminal.
+ #
+ # Note: This encoder is in beta. It currently doesn't use the Styles.
+ #
+ # Alias: +term+
+ #
+ # == Authors & License
+ #
+ # By Rob Aldred (http://robaldred.co.uk)
+ #
+ # Based on idea by Nathan Weizenbaum (http://nex-3.com)
+ #
+ # MIT License (http://www.opensource.org/licenses/mit-license.php)
class Terminal < Encoder
register_for :terminal
-
+
TOKEN_COLORS = {
:attribute_name => '33',
- :attribute_name_fat => '33',
:attribute_value => '31',
- :attribute_value_fat => '31',
:bin => '1;35',
- :char => {:self => '36', :delimiter => '34'},
+ :char => {
+ :self => '36', :delimiter => '34'
+ },
:class => '1;35',
:class_variable => '36',
:color => '32',
@@ -37,7 +44,7 @@ module CodeRay
:include => '33',
:integer => '1;34',
:interpreted => '1;35',
- :label => '1;4',
+ :label => '1;15',
:local_variable => '33',
:oct => '1;35',
:operator_name => '1;29',
@@ -46,6 +53,7 @@ module CodeRay
:predefined => ['4', '1;34'],
:preprocessor => '36',
:regexp => {
+ :self => '31',
:content => '31',
:delimiter => '1;29',
:modifier => '35',
@@ -56,82 +64,98 @@ module CodeRay
:string => '32',
:symbol => '1;32',
:tag => '34',
- :tag_fat => '1;34',
:tag_special => ['34', '4'],
:type => '1;34',
- :variable => '34'
+ :variable => '34',
+
+ :insert => '42',
+ :delete => '41',
+ :change => '44',
+ :head => '41'
}
TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
TOKEN_COLORS[:open] = TOKEN_COLORS[:close] = TOKEN_COLORS[:nesting_delimiter] = TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
- protected
+ protected
def setup(options)
super
- @opened = [nil]
- @subcolors = nil
+ @opened = []
end
def finish(options)
super
end
- def token text, type
- case text
-
- when nil
- # raise 'Token with nil as text was given: %p' % [[text, type]]
-
- when String
-
- if color = (@subcolors || TOKEN_COLORS)[type]
- color = color[:self] || return if Hash === color
+ def text_token text, type
+ if color = (@subcolors || TOKEN_COLORS)[type]
+ if Hash === color
+ if color[:self]
+ color = color[:self]
+ else
+ return text
+ end
+ end
- @out << col(color) + text.gsub("\n", col(0) + "\n" + col(color)) + col(0)
- @out << col(@subcolors[:self]) if @subcolors && @subcolors[:self]
+ out = ansi_colorize(color)
+ out << text.gsub("\n", ansi_clear + "\n" + ansi_colorize(color))
+ out << ansi_clear
+ out << ansi_colorize(@subcolors[:self]) if @subcolors && @subcolors[:self]
+ out
+ else
+ text
+ end
+ end
+
+ def open_token type
+ if color = TOKEN_COLORS[type]
+ if Hash === color
+ @subcolors = color
+ ansi_colorize(color[:self]) if color[:self]
else
- @out << text
+ @subcolors = {}
+ ansi_colorize(color)
end
+ else
+ @subcolors = nil
+ ''
+ end
+ end
- # token groups, eg. strings
- when :open
- @opened[0] = type
- if color = TOKEN_COLORS[type]
- if Hash === color
- @subcolors = color
- @out << col(color[:self]) if color[:self]
- else
- @subcolors = {}
- @out << col(color)
- end
- end
+ def block_token action, type
+ case action
+
+ when :open, :begin_line
@opened << type
- when :close
+ open_token type
+ when :close, :end_line
if @opened.empty?
# nothing to close
else
- if (@subcolors || {})[:self]
- @out << col(0)
- end
- @subcolors = nil
@opened.pop
+ if action == :end_line
+ # whole lines to be highlighted,
+ # eg. added/modified/deleted lines in a diff
+ "\t" * 100 + ansi_clear
+ else
+ ansi_clear
+ end +
+ open_token(@opened.last)
end
-
- # whole lines to be highlighted, eg. a added/modified/deleted lines in a diff
- when :begin_line
-
- when :end_line
-
+
else
raise 'unknown token kind: %p' % [text]
end
end
-
- private
-
- def col(color)
+
+ private
+
+ def ansi_colorize(color)
Array(color).map { |c| "\e[#{c}m" }.join
end
+ def ansi_clear
+ ansi_colorize(0)
+ end
end
end
end \ No newline at end of file