be serious about 80c line lengths in lib/rocco.rb

This commit is contained in:
Ryan Tomayko 2011-03-05 04:32:34 -08:00
parent a841026e52
commit 3224735671

View File

@ -89,6 +89,7 @@ class Rocco
else else
File.read(filename) File.read(filename)
end end
defaults = { defaults = {
:language => 'ruby', :language => 'ruby',
:comment_chars => '#', :comment_chars => '#',
@ -112,16 +113,21 @@ class Rocco
# into the comment_char syntax (we'll discuss that syntax in detail when # into the comment_char syntax (we'll discuss that syntax in detail when
# we get to `generate_comment_chars()` in a moment. # we get to `generate_comment_chars()` in a moment.
else else
@options[:comment_chars] = { :single => @options[:comment_chars], :multi => nil } @options[:comment_chars] = {
:single => @options[:comment_chars],
:multi => nil
}
end end
# Turn `:comment_chars` into a regex matching a series of spaces, the # Turn `:comment_chars` into a regex matching a series of spaces, the
# `:comment_chars` string, and the an optional space. We'll use that # `:comment_chars` string, and the an optional space. We'll use that
# to detect single-line comments. # to detect single-line comments.
@comment_pattern = Regexp.new("^\\s*#{@options[:comment_chars][:single]}\s?") @comment_pattern =
Regexp.new("^\\s*#{@options[:comment_chars][:single]}\s?")
# `parse()` the file contents stored in `@data`. Run the result through `split()` # `parse()` the file contents stored in `@data`. Run the result through
# and that result through `highlight()` to generate the final section list. # `split()` and that result through `highlight()` to generate the final
# section list.
@sections = highlight(split(parse(@data))) @sections = highlight(split(parse(@data)))
end end
@ -152,11 +158,8 @@ class Rocco
# Returns `true` if `pygmentize` is available locally, `false` otherwise. # Returns `true` if `pygmentize` is available locally, `false` otherwise.
def pygmentize? def pygmentize?
# Memoize the result @_pygmentize ||= ENV['PATH'].split(':').
if @_pygmentize.nil? any? { |dir| executable?("#{dir}/pygmentize") }
@_pygmentize = ENV['PATH'].split(':').any? { |dir| executable?("#{dir}/pygmentize") }
end
@_pygmentize
end end
# If `pygmentize` is available, we can use it to autodetect a file's # If `pygmentize` is available, we can use it to autodetect a file's
@ -166,14 +169,13 @@ class Rocco
# #
# We'll memoize the result, as we'll call this a few times. # We'll memoize the result, as we'll call this a few times.
def detect_language def detect_language
@_language ||= begin @_language ||=
if pygmentize? if pygmentize?
lang = %x[pygmentize -N #{@file}].strip! %x[pygmentize -N #{@file}].strip!
else else
"text" "text"
end end
end end
end
# Given a file's language, we should be able to autopopulate the # Given a file's language, we should be able to autopopulate the
# `comment_chars` variables for single-line comments. If we don't # `comment_chars` variables for single-line comments. If we don't
@ -183,7 +185,10 @@ class Rocco
# #
# Comment characters are listed as: # Comment characters are listed as:
# #
# { :single => "//", :multi_start => "/**", :multi_middle => "*", :multi_end => "*/" } # { :single => "//",
# :multi_start => "/**",
# :multi_middle => "*",
# :multi_end => "*/" }
# #
# `:single` denotes the leading character of a single-line comment. # `:single` denotes the leading character of a single-line comment.
# `:multi_start` denotes the string that should appear alone on a # `:multi_start` denotes the string that should appear alone on a
@ -203,30 +208,55 @@ class Rocco
# #
# At the moment, we're only returning `:single`. Consider this # At the moment, we're only returning `:single`. Consider this
# groundwork for block comment parsing. # groundwork for block comment parsing.
def generate_comment_chars COMMENT_STYLES = {
@_commentchar ||= begin
language = @options[:language]
comment_styles = {
"bash" => { :single => "#", :multi => nil }, "bash" => { :single => "#", :multi => nil },
"c" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, "c" => {
"coffee-script" => { :single => "#", :multi => { :start => "###", :middle => nil, :end => "###" } }, :single => "//",
"cpp" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, :multi => { :start => "/**", :middle => "*", :end => "*/" }
"css" => { :single => nil, :multi => { :start => "/**", :middle => "*", :end => "*/" } }, },
"java" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, "coffee-script" => {
"js" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, :single => "#",
"lua" => { :single => "--", :multi => nil }, :multi => { :start => "###", :middle => nil, :end => "###" }
"python" => { :single => "#", :multi => { :start => '"""', :middle => nil, :end => '"""' } }, },
"rb" => { :single => "#", :multi => { :start => '=begin', :middle => nil, :end => '=end' } }, "cpp" => {
:single => "//",
:multi => { :start => "/**", :middle => "*", :end => "*/" }
},
"css" => {
:single => nil,
:multi => { :start => "/**", :middle => "*", :end => "*/" }
},
"java" => {
:single => "//",
:multi => { :start => "/**", :middle => "*", :end => "*/" }
},
"js" => {
:single => "//",
:multi => { :start => "/**", :middle => "*", :end => "*/" }
},
"lua" => {
:single => "--",
:multi => nil
},
"python" => {
:single => "#",
:multi => { :start => '"""', :middle => nil, :end => '"""' }
},
"rb" => {
:single => "#",
:multi => { :start => '=begin', :middle => nil, :end => '=end' }
},
"scheme" => { :single => ";;", :multi => nil }, "scheme" => { :single => ";;", :multi => nil },
} }
if comment_styles[language] def generate_comment_chars
comment_styles[language] @_commentchar ||=
if COMMENT_STYLES[@options[:language]]
COMMENT_STYLES[@options[:language]]
else else
{ :single => @options[:comment_chars], :multi => nil } { :single => @options[:comment_chars], :multi => nil }
end end
end end
end
# Internal Parsing and Highlighting # Internal Parsing and Highlighting
# --------------------------------- # ---------------------------------
@ -243,14 +273,16 @@ class Rocco
# PEP 263 encoding information in python sourcefiles, and the similar ruby # PEP 263 encoding information in python sourcefiles, and the similar ruby
# 1.9 syntax. # 1.9 syntax.
lines.shift if lines[0] =~ /^\#\!/ lines.shift if lines[0] =~ /^\#\!/
lines.shift if lines[0] =~ /coding[:=]\s*[-\w.]+/ and [ "python", "rb" ].include? @options[:language] lines.shift if lines[0] =~ /coding[:=]\s*[-\w.]+/ &&
[ "python", "rb" ].include?(@options[:language])
# To detect both block comments and single-line comments, we'll set # To detect both block comments and single-line comments, we'll set
# up a tiny state machine, and loop through each line of the file. # up a tiny state machine, and loop through each line of the file.
# This requires an `in_comment_block` boolean, and a few regular # This requires an `in_comment_block` boolean, and a few regular
# expressions for line tests. # expressions for line tests.
in_comment_block = false in_comment_block = false
single_line_comment, block_comment_start, block_comment_mid, block_comment_end = nil, nil, nil, nil single_line_comment, block_comment_start, block_comment_mid, block_comment_end =
nil, nil, nil, nil
if not @options[:comment_chars][:single].nil? if not @options[:comment_chars][:single].nil?
single_line_comment = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:single])}\\s?") single_line_comment = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:single])}\\s?")
end end
@ -313,7 +345,8 @@ class Rocco
if section.any? && section[0].any? if section.any? && section[0].any?
leading_space = section[0][0].match( "^\s+" ) leading_space = section[0][0].match( "^\s+" )
if leading_space if leading_space
section[0] = section[0].map{ |line| line.sub( /^#{leading_space.to_s}/, '' ) } section[0] =
section[0].map{ |line| line.sub( /^#{leading_space.to_s}/, '' ) }
end end
end end
section section
@ -350,20 +383,42 @@ class Rocco
# Combine all code blocks into a single big stream with section dividers and # Combine all code blocks into a single big stream with section dividers and
# run through either `pygmentize(1)` or <http://pygments.appspot.com> # run through either `pygmentize(1)` or <http://pygments.appspot.com>
if not @options[:comment_chars][:single].nil? span, espan = '<span class="c.?">', '</span>'
divider_input = "\n\n#{@options[:comment_chars][:single]} DIVIDER\n\n" if @options[:comment_chars][:single]
divider_output = /\n*<span class="c.?">#{Regexp.escape(@options[:comment_chars][:single])} DIVIDER<\/span>\n*/m front = @options[:comment_chars][:single]
divider_input = "\n\n#{front} DIVIDER\n\n"
divider_output = Regexp.new(
[ "\\n*",
span,
Regexp.escape(front),
' DIVIDER',
espan,
"\\n*"
].join, Regexp::MULTILINE
)
else else
divider_input = "\n\n#{@options[:comment_chars][:multi][:start]}\nDIVIDER\n#{@options[:comment_chars][:multi][:end]}\n\n" front = @options[:comment_chars][:multi][:start]
divider_output = /\n*<span class="c.?">#{Regexp.escape(@options[:comment_chars][:multi][:start])}<\/span>\n<span class="c.?">DIVIDER<\/span>\n<span class="c.?">#{Regexp.escape(@options[:comment_chars][:multi][:end])}<\/span>\n*/m back = @options[:comment_chars][:multi][:end]
divider_input = "\n\n#{front}\nDIVIDER\n#{back}\n\n"
divider_output = Regexp.new(
[ "\\n*",
span, Regexp.escape(front), espan,
"\\n",
span, "DIVIDER", espan,
"\\n",
span, Regexp.escape(back), espan,
"\\n*"
].join, Regexp::MULTILINE
)
end end
code_stream = code_blocks.join( divider_input ) code_stream = code_blocks.join( divider_input )
code_html =
if pygmentize? if pygmentize?
code_html = highlight_pygmentize(code_stream) highlight_pygmentize(code_stream)
else else
code_html = highlight_webservice(code_stream) highlight_webservice(code_stream)
end end
# Do some post-processing on the pygments output to split things back # Do some post-processing on the pygments output to split things back