squi.rb |
#!/usr/bin/env ruby This is a very rudimentary tool to convert Code Browser files to HTML. You can download the Ruby source file at squi.rb, and two auxiliary files template.rhtml and default2.css. Uses RedCloth for plain-text markup (it’s a Textile implementation in Ruby) and ERb. To use, download squi.rb and then enter For some example output, you can run Squi is named after my friend Squirrel. Squirrel is not her real name, of course, but we sometimes call her that. require 'rubygems'
require 'redcloth'
require 'erb'
def node_tree(filename, name, identifier = nil, parent = nil)
state = :comment, new_state = nil
current_node = Node.new name, identifier, parent
comment_identifier = $extensions[filename[/\..*?$/][1..-1]]
File.open filename, "r" do |f|
f.each_line do |li|
if li =~ /^#{comment_identifier}\[(c|of|cf|l)\]/
new_state = :comment
else
new_state = :code
end
if state != new_state
if new_state == :comment
current_node.append "<" + "/pre>\n"
elsif new_state == :code
current_node.append "<" + "pre>"
end
state = new_state
end
case li
when /^#{comment_identifier}\[c\](.*)/
new_state = :comment
current_node.append $1 + "\n"
when /^#{comment_identifier}\[of\](.*?):(.*)/
new_state = :comment
child_node = Node.new($2, $1, current_node)
current_node.append "\n%(folder)\"#{child_node.name}\":#{child_node.filename}.html%\n\n"
current_node.append_child child_node
current_node = child_node
when /^#{comment_identifier}\[cf\](.*)/
new_state = :comment
current_node = current_node.parent
when /^#{comment_identifier}\[l\](.*?):(.*?):(.*)/
state = :comment
child_node = node_tree $3, $2, $1, current_node
current_node.append_child child_node
current_node.append "\n%(link)\"#{child_node.name}\":#{child_node.filename}.html%\n\n"
else
new_state = :code
current_node.append ' ' + li
end
end
end
if state == :code
current_node.append "<" + "/pre>\n"
end
current_node
end
root = node_tree ARGV[0], ARGV[0]
# Write the stuff to files
def write_node(node, root_node, template)
File.open node.filename + '.html', 'w' do |f|
root_node = root_node
current_node = node
f.puts template.result(binding)
end
node.children.each{|n| write_node(n, root_node, template)}
end
template = ""
File.open "template.rhtml", "r" do |f| template = f.read end
write_node root, root, ERB.new(template)
|