Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Cleaning up the main library file #76

Merged
merged 15 commits into from

2 participants

@krainboltgreene
  • Switching from if !condition to the more accepted idiom unless condition, which happens to be ever so slightly faster
  • Making the variable names a little more specific
  • Removing unneeded variable assignment, or switching to mass assignment
  • Changing a few multi-liners to single line expressions (hopefully to have those expressions turned into functions)
  • Adding a version specification to the redcarpet dependency, because otherwise it breaks everything.
@rtomayko
Owner

Digging all of this.

@rtomayko rtomayko merged commit 727c510 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 9, 2012
  1. Changing from if ! to expressive unless

    Kurtis Rainbolt-Greene authored
  2. Changing from if ! to expressive unless

    Kurtis Rainbolt-Greene authored
  3. Using a more expressive variable name

    Kurtis Rainbolt-Greene authored
  4. Since yield is used, block isn't needed

    Kurtis Rainbolt-Greene authored
  5. Removing two accidental whitespaces

    Kurtis Rainbolt-Greene authored
  6. Moving if block to a single line

    Kurtis Rainbolt-Greene authored
  7. Removing unrequired ()'s for methods

    Kurtis Rainbolt-Greene authored
  8. Refactorting to single line

    Kurtis Rainbolt-Greene authored
  9. Refactoring comment_pattern to single line

    Kurtis Rainbolt-Greene authored
  10. Refactoring variabl assignment to use mass assignment

    Kurtis Rainbolt-Greene authored
  11. Refactoring docs_blocks if block to single line

    Kurtis Rainbolt-Greene authored
  12. Refactoring divider parser to single line

    Kurtis Rainbolt-Greene authored
  13. Refactoring to read simplier

    Kurtis Rainbolt-Greene authored
  14. Specifying the latest 1.* release due to API changes

    Kurtis Rainbolt-Greene authored
  15. Switching out this defaults variable and directly merging options

    Kurtis Rainbolt-Greene authored
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 40 deletions.
  1. +1 −1  bin/rocco
  2. +23 −38 lib/rocco.rb
  3. +1 −1  rocco.gemspec
View
2  bin/rocco
@@ -48,7 +48,7 @@ ARGV.options { |o|
} or abort_with_note
# Use http://pygments.appspot.com in case `pygmentize(1)` isn't available.
-if ! ENV['PATH'].split(':').any? { |dir| File.exist?("#{dir}/pygmentize") }
+unless ENV['PATH'].split(':').any? { |dir| File.exist?("#{dir}/pygmentize") }
unless options[:webservice]
$stderr.puts "pygmentize not in PATH; using pygments.appspot.com instead"
options[:webservice] = true
View
61 lib/rocco.rb
@@ -32,12 +32,12 @@
#### Prerequisites
# We'll need a Markdown library. Try to load one if not already established.
-if !defined?(Markdown)
- libs = %w[redcarpet rdiscount bluecloth]
+unless defined?(Markdown)
+ markdown_libraries = %w[redcarpet rdiscount bluecloth]
begin
- require libs.shift
+ require markdown_libraries.shift
rescue LoadError => boom
- retry if libs.any?
+ retry if markdown_libraries.any?
raise
end
end
@@ -51,7 +51,7 @@
# Code is run through [Pygments](http://pygments.org/) for syntax
# highlighting. If it's not installed, locally, use a webservice.
-if !ENV['PATH'].split(':').any? { |dir| File.executable?("#{dir}/pygmentize") }
+unless ENV['PATH'].split(':').any? { |dir| File.executable?("#{dir}/pygmentize") }
warn "WARNING: Pygments not found. Using webservice."
end
@@ -77,55 +77,45 @@
class Rocco
VERSION = '0.8.2'
- def initialize(filename, sources=[], options={}, &block)
+ def initialize(filename, sources=[], options={})
@file = filename
@sources = sources
# When `block` is given, it must read the contents of the file using
# whatever means necessary and return it as a string. With no `block`,
# the file is read to retrieve data.
- @data =
- if block_given?
- yield
- else
- File.read(filename)
- end
+ @data = if block_given? then yield else File.read(filename) end
- defaults = {
+ @options = {
:language => 'ruby',
:comment_chars => '#',
:template_file => nil,
- :stylesheet => 'http://jashkenas.github.com/docco/resources/docco.css'
- }
- @options = defaults.merge(options)
+ :stylesheet => 'http://jashkenas.github.com/docco/resources/docco.css'
+ }.merge(options)
# If we detect a language
- if detect_language() != "text"
+ if "text" != detect_language
# then assign the detected language to `:language`, and look for
# comment characters based on that language
- @options[:language] = detect_language()
- @options[:comment_chars] = generate_comment_chars()
+ @options[:language] = detect_language
+ @options[:comment_chars] = generate_comment_chars
# If we didn't detect a language, but the user provided one, use it
# to look around for comment characters to override the default.
- elsif @options[:language] != defaults[:language]
- @options[:comment_chars] = generate_comment_chars()
+ elsif @options[:language]
+ @options[:comment_chars] = generate_comment_chars
# If neither is true, then convert the default comment character string
# into the comment_char syntax (we'll discuss that syntax in detail when
# we get to `generate_comment_chars()` in a moment.
else
- @options[:comment_chars] = {
- :single => @options[:comment_chars],
- :multi => nil
- }
+ @options[:comment_chars] = { :single => @options[:comment_chars], :multi => nil }
end
# Turn `:comment_chars` into a regex matching a series of spaces, the
# `:comment_chars` string, and the an optional space. We'll use that
# 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()` and that result through `highlight()` to generate the final
@@ -229,8 +219,7 @@ def generate_comment_chars
# form `[docs, code]` where both elements are arrays containing the
# raw lines parsed from the input file, comment characters stripped.
def parse(data)
- sections = []
- docs, code = [], []
+ sections, docs, code = [], [], []
lines = data.split("\n")
# The first line is ignored if it is a shebang line. We also ignore the
@@ -384,16 +373,13 @@ def highlight(blocks)
docs_blocks, code_blocks = blocks
# Pre-process Docblock @annotations.
- if @options[:docblocks]
- docs_blocks = docblock(docs_blocks)
- end
+ docs_blocks = docblock(docs_blocks) if @options[:docblocks]
# Combine all docs blocks into a single big markdown document with section
# dividers and run through the Markdown processor. Then split it back out
# into separate sections.
markdown = docs_blocks.join("\n\n##### DIVIDER\n\n")
- docs_html = process_markdown(markdown).
- split(/\n*<h5>DIVIDER<\/h5>\n*/m)
+ docs_html = process_markdown(markdown).split(/\n*<h5>DIVIDER<\/h5>\n*/m)
# Combine all code blocks into a single big stream with section dividers and
# run through either `pygmentize(1)` or <http://pygments.appspot.com>
@@ -475,10 +461,9 @@ def highlight_pygmentize(code)
# Pygments is not one of those things that's trivial for a ruby user to install,
# so we'll fall back on a webservice to highlight the code if it isn't available.
def highlight_webservice(code)
- Net::HTTP.post_form(
- URI.parse('http://pygments.appspot.com/'),
- {'lang' => @options[:language], 'code' => code}
- ).body
+ url = URI.parse 'http://pygments.appspot.com/'
+ options = { 'lang' => @options[:language], 'code' => code}
+ Net::HTTP.post_form(url, options).body
end
end
View
2  rocco.gemspec
@@ -52,7 +52,7 @@ Gem::Specification.new do |s|
s.executables = ["rocco"]
s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/}
- s.add_dependency 'redcarpet'
+ s.add_dependency 'redcarpet', '~> 1.17'
s.add_dependency 'mustache'
s.has_rdoc = false
Something went wrong with that request. Please try again.