Skip to content

Instantly share code, notes, and snippets.

@akm
Created November 14, 2010 09:55
Show Gist options
  • Select an option

  • Save akm/676050 to your computer and use it in GitHub Desktop.

Select an option

Save akm/676050 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require "optparse"
DEFAULT_OPTIONS = {
:layout => File.expand_path("./textile2html_layout.html.erb", File.dirname(__FILE__)),
:src_dir => File.expand_path("."),
:dest_dir => File.expand_path("."),
:noop => false,
:verbose => false,
}
options = DEFAULT_OPTIONS.dup
OptionParser.new do |opt|
opt.on("-l", "--layout=#{options[:layout]}"){|v| options[:layout] = v}
opt.on("-s", "--src-dir=#{options[:src_dir]}"){|v| options[:src_dir] = v}
opt.on("-d", "--dest-dir=#{options[:dest_dir]}"){|v| options[:dest_dir] = v}
opt.on("-n", "--noop"){ options[:noop] = true}
opt.on("-v", "--verbose"){ options[:verbose] = true}
opt.parse!(ARGV)
end
require 'fileutils'
require 'erb'
require 'rubygems'
require 'RedCloth'
def binding_for_yield
binding
end
def pickup_headers(body)
links = []
result = body.gsub(/(<h[1-6]>)(.*?)<\/h[1-6]>/) do |*args|
# puts "args: #{args.inspect}"
tag, desc = $1, $2
depth = tag.gsub(/\D/, '').to_i
links << [depth, desc]
"<h#{depth}><a name=\"#{desc}\">#{desc}</a></h#{depth}>"
end
return result, links
end
def build_page_outlines(links)
src = links.map{|(depth, desc)| "#" * depth << ' "' << desc << '":#' << desc }.join("\n")
RedCloth.new(src).to_html
end
src_dir = File.expand_path(options[:src_dir])
src_files = Dir["#{src_dir}/**/*.textile"]
src_files.each do |src_file|
src = File.read(src_file)
html_body = RedCloth.new(src).to_html
html_body, links = pickup_headers(html_body)
b = binding_for_yield do|*args|
arg = args.first
case arg
when nil then html_body
when :page_outline then build_page_outlines(links)
else
nil
end
end
erb = ERB.new(File.read(options[:layout]))
html = erb.result(b)
rel_path = src_file.sub("#{Regexp.escape(src_dir)}/", '')
dest_rel_path = rel_path.sub(/\.textile$/, '.html')
dest_path = File.expand_path(dest_rel_path, options[:dest_dir])
dest_dir = File.dirname(dest_path)
FileUtils.mkdir_p(dest_dir)
File.open(dest_path, "w"){|f| f.puts(html)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment