Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions lib/rocco.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#### Prerequisites

# We'll need a Markdown library. Try to load one if not already established.
unless defined?(Markdown)
unless defined? Markdown
markdown_libraries = %w[redcarpet rdiscount bluecloth]
begin
require markdown_libraries.shift
Expand Down Expand Up @@ -102,14 +102,12 @@ def initialize(filename, sources=[], options={})

# 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]
@options[:comment_chars] = generate_comment_chars
elsif @options[:language] then @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 }
else @options[:comment_chars] = { :single => @options[:comment_chars], :multi => nil }
end

# Turn `:comment_chars` into a regex matching a series of spaces, the
Expand All @@ -120,7 +118,7 @@ def initialize(filename, sources=[], options={})
# `parse()` the file contents stored in `@data`. Run the result through
# `split()` and that result through `highlight()` to generate the final
# section list.
@sections = highlight(split(parse(@data)))
@sections = highlight split parse @data
end

# The filename as given to `Rocco.new`.
Expand Down Expand Up @@ -219,8 +217,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 = [], [], []
lines = data.split("\n")
sections, docs, code, lines = [], [], [], data.split("\n")

# The first line is ignored if it is a shebang line. We also ignore the
# PEP 263 encoding information in python sourcefiles, and the similar ruby
Expand Down Expand Up @@ -350,8 +347,8 @@ def split(sections)
sections.each do |docs,code|
docs_blocks << docs.join("\n")
code_blocks << code.map do |line|
tabs = line.match(/^(\t+)/)
tabs ? line.sub(/^\t+/, ' ' * tabs.captures[0].length) : line
tabs = l.match /^(\t+)/
end.join("\n")
end
[docs_blocks, code_blocks]
Expand All @@ -378,7 +375,7 @@ def highlight(blocks)
# 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")
markdown = docs_blocks.join "\n\n##### DIVIDER\n\n"
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
Expand All @@ -390,7 +387,7 @@ def highlight(blocks)
divider_output = Regexp.new(
[ "\\n*",
span,
Regexp.escape(CGI.escapeHTML(front)),
Regexp.escape(CGI.escapeHTML front),
' DIVIDER',
espan,
"\\n*"
Expand All @@ -402,11 +399,11 @@ def highlight(blocks)
divider_input = "\n\n#{front}\nDIVIDER\n#{back}\n\n"
divider_output = Regexp.new(
[ "\\n*",
span, Regexp.escape(CGI.escapeHTML(front)), espan,
span, Regexp.escape(CGI.escapeHTML front), espan,
"\\n",
span, "DIVIDER", espan,
"\\n",
span, Regexp.escape(CGI.escapeHTML(back)), espan,
span, Regexp.escape(CGI.escapeHTML back), espan,
"\\n*"
].join, Regexp::MULTILINE
)
Expand Down