improve documentation

This commit is contained in:
Ryan Tomayko 2010-03-09 09:15:50 -08:00
parent 606087986e
commit 580d0ef7fc

View File

@ -43,11 +43,12 @@ rescue LoadError => boom
end end
# We use [{{ mustache }}](http://defunkt.github.com/mustache/) for # We use [{{ mustache }}](http://defunkt.github.com/mustache/) for
# templating. # HTML templating.
require 'mustache' require 'mustache'
# Code is run through [Pygments](http://pygments.org/) for syntax # Code is run through [Pygments](http://pygments.org/) for syntax
# highlighting. Fail fast if we can't find the `pygmentize` program. # highlighting. Fail fast right here if we can't find the `pygmentize`
# program on PATH.
if ! ENV['PATH'].split(':').any? { |dir| File.exist?("#{dir}/pygmentize") } if ! ENV['PATH'].split(':').any? { |dir| File.exist?("#{dir}/pygmentize") }
fail "Pygments is required for syntax highlighting" fail "Pygments is required for syntax highlighting"
end end
@ -69,8 +70,7 @@ class Rocco
else else
File.read(filename) File.read(filename)
end end
# Parsing and highlighting @sections = highlight(split(parse(@data)))
@sections = highlight(parse(@data))
end end
# The filename as given to `Rocco.new`. # The filename as given to `Rocco.new`.
@ -90,7 +90,9 @@ class Rocco
#### Internal Parsing and Highlighting #### Internal Parsing and Highlighting
# Parse the raw file data into a list of two-tuples. # Parse the raw file data into a list of two-tuples. Each tuple has the
# form `[docs, code]` where both elements are arrays containing the
# raw lines parsed from the input file.
def parse(data) def parse(data)
sections = [] sections = []
docs, code = [], [] docs, code = [], []
@ -110,26 +112,34 @@ class Rocco
sections sections
end end
# Take the raw section data and apply markdown formatting and syntax # Take the list of paired *sections* two-tuples and split into two
# highlighting. # separate lists: one holding the comments with leaders removed and
def highlight(sections) # one with the code blocks.
# Start by splitting the docs and codes blocks into two separate lists. def split(sections)
docs_blocks, code_blocks = [], [] docs_blocks, code_blocks = [], []
sections.each do |docs,code| sections.each do |docs,code|
docs_blocks << docs.map { |line| line.sub(/^\s*#\s?/, '') }.join("\n") docs_blocks << docs.map { |line| line.sub(/^\s*#\s?/, '') }.join("\n")
code_blocks << code.join("\n") code_blocks << code.join("\n")
end end
[docs_blocks, code_blocks]
end
# Combine all docs blocks into a single big markdown document and run # Take the result of `split` and apply Markdown formatting to comments and
# through RDiscount. Then split it back out into separate sections. # syntax highlighting to source code.
def highlight(blocks)
docs_blocks, code_blocks = 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 = Markdown.new(markdown, :smart). docs_html = Markdown.new(markdown, :smart).
to_html. to_html.
split(/\n*<h5>DIVIDER<\/h5>\n*/m) split(/\n*<h5>DIVIDER<\/h5>\n*/m)
# Combine all code blocks into a single big stream and run through # Combine all code blocks into a single big stream and run through
# pygments. We `popen` a pygmentize process and then fork off a # Pygments. We `popen` a read/write pygmentize process in the parent and
# writer process. # then fork off a child process to write the input.
code_html = nil code_html = nil
open("|pygmentize -l ruby -f html", 'r+') do |fd| open("|pygmentize -l ruby -f html", 'r+') do |fd|
fork { fork {
@ -138,22 +148,19 @@ class Rocco
fd.close_write fd.close_write
exit! exit!
} }
fd.close_write fd.close_write
code_html = fd.read code_html = fd.read
fd.close_read fd.close_read
end end
# Do some post-processing on the pygments output to remove # Do some post-processing on the pygments output to split things back
# partial `<pre>` blocks. We'll add these back when we build to main # into sections and remove partial `<pre>` blocks.
# document.
code_html = code_html. code_html = code_html.
split(/\n*<span class="c1"># DIVIDER<\/span>\n*/m). split(/\n*<span class="c1"># DIVIDER<\/span>\n*/m).
map { |code| code.sub(/\n?<div class="highlight"><pre>/m, '') }. map { |code| code.sub(/\n?<div class="highlight"><pre>/m, '') }.
map { |code| code.sub(/\n?<\/pre><\/div>\n/m, '') } map { |code| code.sub(/\n?<\/pre><\/div>\n/m, '') }
# Combine the docs and code lists into the same sections style list we # Lastly, combine the docs and code lists back into a list of two-tuples.
# started with.
docs_html.zip(code_html) docs_html.zip(code_html)
end end
end end