Skip to content

Commit

Permalink
cleanups: Page encoder is default for HTML, benchmark KB/s instead of…
Browse files Browse the repository at this point in the history
… tokens/s
  • Loading branch information
korny committed Jul 4, 2011
1 parent 498df2b commit 6a52b21
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Changes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Stripped down to 19 LOC.
h3. @FileType@

* *REMOVED* @FileType#shebang@ is a protected method now.
* *NEW*: Recognizes @.gemspec@, @.rjs@, @.rpdf@ extensions and @Capfile@ as Ruby.
* *NEW*: Recognizes @.gemspec@, @.rjs@, @.rpdf@ extensions, @Gemfile@, and @Capfile@ as Ruby.

Thanks to the authors of the TextMate Ruby bundle!

Expand Down
61 changes: 2 additions & 59 deletions bench/bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
require 'fileutils'

if format == 'comp'
format = 'html'
compare = true
format = 'page'
begin
require 'syntax'
require 'syntax/convertors/html.rb'
Expand Down Expand Up @@ -80,8 +79,6 @@ def here fn = nil

options = {
:tab_width => 2,
:wrap => :page,
:line_numbers => :table,
:css => $style ? :style : :class,
}
$hl = CodeRay.encoder(format, options) unless $dump_output
Expand All @@ -93,15 +90,12 @@ def here fn = nil
raise 'Can\'t dump stream.'
end
$o = $hl.encode(data, lang, options)
@token_count = 253528 #$hl.token_stream.count rescue 1
else
if $dump_input
tokens = CodeRay::Tokens.load data
else
tokens = CodeRay.scan(data, lang)
end
@token_count = tokens.count
p @token_count
tokens.optimize! if $optimize
if $dump_output
$o = tokens.optimize.dump
Expand All @@ -121,60 +115,9 @@ def here fn = nil

time_real = time.real

puts "\t%7.2f KB/s (%d.%d KB)\t%0.2f KTok/s" % [((@size / 1000.0) / time_real), @size / 1000, @size % 1000, ((@token_count / 1000.0) / time_real)]
puts "\t%7.2f KB/s (%d.%d KB)" % [((@size / 1000.0) / time_real), @size / 1000, @size % 1000]
puts $o if ARGV.include? '-o'

if compare
if defined? Syntax
time = bm.report('Syntax') do
c = Syntax::Convertors::HTML.for_syntax lang
puts 'No Syntax syntax found!' if c.tokenizer.is_a? Syntax::Default
begin
v = $VERBOSE
$VERBOSE = nil
N.times do
output = c.convert(data)
end
$VERBOSE = v
rescue => boom
output = boom.inspect
end
Dir.chdir(here) do
File.open('test.syntax.' + format, 'wb') do |f|
f.write '<html><head><style>%s</style></head><body><div class="ruby">%s</div></body></html>' % [DATA.read, output]
end
end
$file_created << ", test.syntax.#{format}"
end
puts "\t%7.2f KB/s" % ((@size / 1024.0) / time.real)
end

=begin
time = bm.report('SilverCity') do
Dir.chdir(here) do
File.open('input-data', 'w') { |f| f.write data }
N.times do
`c:/Python/Scripts/source2html.pyo --generator=#{lang} input-data > test.silvercity.html`
end
end
$file_created << ", test.silvercity.#{format}"
end
puts "\t%7.2f KB/s" % ((@size / 1024.0) / time.real)
=end
time = bm.report('Pygments') do
Dir.chdir(here) do
Dir.chdir File.expand_path('~/Python/pygments') do
File.open('input-data', 'wb') { |f| f.write data }
N.times do
`pygmentize -O encoding=utf-8 -l#{lang} -fhtml -Ofull input-data > /dev/null`
end
end
end
#$file_created << ", test.silvercity.#{format}"
end
puts "\t%7.2f KB/s" % ((@size / 1024.0) / time.real)
end

end
end
puts "Files created: #$file_created"
Expand Down
2 changes: 1 addition & 1 deletion bin/coderay
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ usage:
defaults:
language detect from input file name or shebang; fall back to plain text
input STDIN
format detect from output file name or use terminal; fall back to HTML page
format detect from output file name or use terminal; fall back to HTML
output STDOUT
common:
Expand Down
4 changes: 2 additions & 2 deletions etc/CodeRay.tmproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<key>filename</key>
<string>../test/scanners/coderay_suite.rb</string>
<key>lastUsed</key>
<date>2011-05-08T16:19:54Z</date>
<date>2011-06-26T01:18:25Z</date>
</dict>
<dict>
<key>filename</key>
Expand All @@ -157,7 +157,7 @@
<key>filename</key>
<string>../bench/bench.rb</string>
<key>lastUsed</key>
<date>2011-02-20T05:36:12Z</date>
<date>2011-06-26T15:21:10Z</date>
</dict>
</array>
<key>fileHierarchyDrawerWidth</key>
Expand Down
2 changes: 1 addition & 1 deletion lib/coderay/duo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Duo
# Create a new Duo, holding a lang and a format to highlight code.
#
# simple:
# CodeRay::Duo[:ruby, :page].highlight 'bla 42'
# CodeRay::Duo[:ruby, :html].highlight 'bla 42'
#
# with options:
# CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'
Expand Down
15 changes: 8 additions & 7 deletions lib/coderay/encoders/_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ module CodeRay
module Encoders

map \
:loc => :lines_of_code,
:term => :terminal,
:tty => :terminal,
:plain => :text,
:plaintext => :text,
:loc => :lines_of_code,
:html => :page,
:plain => :text,
:plaintext => :text,
:remove_comments => :comment_filter,
:stats => :statistic
:stats => :statistic,
:term => :terminal,
:tty => :terminal

# No default because Tokens#nonsense would not raise NoMethodError.
# No default because Tokens#nonsense should raise NoMethodError.

end
end
17 changes: 9 additions & 8 deletions lib/coderay/encoders/div.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
module CodeRay
module Encoders

load :html

# Wraps HTML output into a DIV element, using inline styles by default.
#
# See Encoders::HTML for available options.
class Div < HTML

FILE_EXTENSION = 'div.html'

register_for :div

DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
:css => :style,
:wrap => :div

:css => :style,
:wrap => :div,
:line_numbers => false

end

end
end
2 changes: 1 addition & 1 deletion lib/coderay/encoders/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class HTML < Encoder

register_for :html

FILE_EXTENSION = 'html'
FILE_EXTENSION = 'snippet.html'

DEFAULT_OPTIONS = {
:tab_width => 8,
Expand Down
2 changes: 1 addition & 1 deletion lib/coderay/encoders/html/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def apply target, replacement
TABLE = Template.new <<-TABLE
<table class="CodeRay"><tr>
<td class="line_numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
<td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
<td class="code"><pre><%CONTENT%></pre></td>
</tr></table>
TABLE

Expand Down
4 changes: 2 additions & 2 deletions lib/coderay/encoders/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Page < HTML
register_for :page

DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
:css => :class,
:wrap => :page,
:css => :class,
:wrap => :page,
:line_numbers => :table

end
Expand Down
19 changes: 10 additions & 9 deletions lib/coderay/encoders/span.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
module CodeRay
module Encoders

load :html

# Wraps HTML output into a SPAN element, using inline styles by default.
#
# See Encoders::HTML for available options.
class Span < HTML

FILE_EXTENSION = 'span.html'

register_for :span

DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
:css => :style,
:wrap => :span

:css => :style,
:wrap => :span,
:line_numbers => false

end

end
end
11 changes: 7 additions & 4 deletions lib/coderay/helpers/file_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def shebang filename
'groovy' => :groovy,
'gvy' => :groovy,
'h' => :c,
'htm' => :html,
'html' => :html,
'htm' => :page,
'html' => :page,
'html.erb' => :rhtml,
'java' => :java,
'js' => :java_script,
Expand All @@ -101,6 +101,7 @@ def shebang filename
'php3' => :php,
'php4' => :php,
'php5' => :php,
'prawn' => :ruby,
'py' => :python,
'py3' => :python,
'pyw' => :python,
Expand All @@ -111,11 +112,12 @@ def shebang filename
'rhtml' => :rhtml,
'rjs' => :ruby,
'rpdf' => :ruby,
'ru' => :ruby,
'rxml' => :ruby,
'sch' => :scheme,
'sql' => :sql,
'ss' => :scheme,
'xhtml' => :xhtml,
'xhtml' => :page,
'xml' => :xml,
'yaml' => :yaml,
'yml' => :yaml,
Expand All @@ -127,9 +129,10 @@ def shebang filename
TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/

TypeFromName = {
'Capfile' => :ruby,
'Capfile' => :ruby,
'Rakefile' => :ruby,
'Rantfile' => :ruby,
'Gemfile' => :ruby,
}

end
Expand Down
2 changes: 1 addition & 1 deletion rake_tasks/benchmark.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
desc 'Do a benchmark'
task :benchmark do
ruby "-v"
ruby "-wIlib bench/bench.rb ruby div 1000 N5"
ruby "-wIlib bench/bench.rb ruby div 3000 N5"
end

task :bench => :benchmark
7 changes: 4 additions & 3 deletions test/executable/suite.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test/unit'
require 'rubygems' unless defined? Gem
require 'shoulda-context'

require 'pathname'
Expand Down Expand Up @@ -100,7 +101,7 @@ def coderay args, fake_tty = false

source = File.read source_file

pre = %r{<pre [^>]*>(.*?)</pre>}m
pre = %r{<td class="code"><pre>(.*?)</pre>}m
tag = /<[^>]*>/

should 'not throw an error' do
Expand Down Expand Up @@ -155,7 +156,7 @@ def coderay args, fake_tty = false

source = File.read source_file

pre = %r{<pre [^>]*>(.*?)</pre>}m
pre = %r{<td class="code"><pre>(.*?)</pre>}m
tag_class = /<span class="([^>"]*)"?[^>]*>/

should 'respect the file extension and highlight the input as Python' do
Expand All @@ -170,7 +171,7 @@ def coderay args, fake_tty = false

source = File.read source_file

pre = %r{<pre [^>]*>(.*?)</pre>}m
pre = %r{<td class="code"><pre>(.*?)</pre>}m
tag_class = /<span class="([^>"]*)"?[^>]*>/

should 'ignore the file extension and highlight the input as Ruby' do
Expand Down
4 changes: 2 additions & 2 deletions test/functional/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_encode_file
end

def test_highlight
assert_match '<div class="code"><pre>test</pre></div>', CodeRay.highlight('test', :python)
assert_match '<pre>test</pre>', CodeRay.highlight('test', :python)
end

def test_highlight_file
Expand Down Expand Up @@ -173,7 +173,7 @@ class HoneyBee < CodeRay::Encoders::Encoder

def test_encoder_file_extension
assert_nothing_raised do
assert_equal 'html', CodeRay::Encoders::HTML::FILE_EXTENSION
assert_equal 'html', CodeRay::Encoders::Page::FILE_EXTENSION
assert_equal 'cocoa', Milk::FILE_EXTENSION
assert_equal 'cocoa', Milk.new.file_extension
assert_equal 'honeybee', HoneyBee::FILE_EXTENSION
Expand Down
4 changes: 2 additions & 2 deletions test/functional/examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_examples
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
</pre></td>
<td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span style="color:#00D">5</span>.times <span style="color:#080;font-weight:bold">do</span>
<td class="code"><pre><span style="color:#00D">5</span>.times <span style="color:#080;font-weight:bold">do</span>
puts <span style="background-color:hsla(0,100%,50%,0.08)"><span style="color:#710">'</span><span style="color:#D20">Hello, world!</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span></pre></td>
</tr></table>
Expand All @@ -38,7 +38,7 @@ def test_examples
<table class="CodeRay"><tr>
<td class="line_numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>
</pre></td>
<td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">puts <span class="s"><span class="dl">&quot;</span><span class="k">Hello, world!</span><span class="dl">&quot;</span></span></pre></td>
<td class="code"><pre>puts <span class="s"><span class="dl">&quot;</span><span class="k">Hello, world!</span><span class="dl">&quot;</span></span></pre></td>
</tr></table>
</body>
Expand Down
6 changes: 3 additions & 3 deletions test/unit/file_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def test_cpp
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 :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']
end
Expand Down

0 comments on commit 6a52b21

Please sign in to comment.