# This script converts all the manuscript textile files # into html pages to suit the ruportbook website # to do: # automatically collect other assets in manuscript directories ie image files require 'rubygems' require 'redcloth' require 'hpricot' # pointing to the stuff we're using @template = "site_template.html" @source_dir = "../manuscript" @output_dir = "output" # finds all the files def crunch_files(dir) Dir.foreach(dir) {|name| target = "#{dir}/#{name}" if File.directory?(target) and name.index(".") != 0 crunch_files(target) elsif File.file? target and name.index(".rhtml") != nil puts "converting #{name}" convert_file(target,name) end } end # writes the finished file def convert_file(target, name) html = merge_textile(target) f = File.open("#{@output_dir}/#{name.chomp(".rhtml")}.html",'w+') f.write html f.close end # this is orchestrating all the html crunching def merge_textile(filename) # get the textile converted to hpricot main_content = Hpricot(parse_textile(filename)) # create the nav and anchors side_nav = build_sidebar_nav(main_content) # get the template and insert the new content into it page = Hpricot(get_template) (page/"#mainContent").inner_html = tidy_footnotes(main_content.to_html) (page/"#secondaryContent").inner_html = side_nav page end # makes the sidebar and anchors def build_sidebar_nav(content) nav = "\n" content.search("/h3").each do |heading| heading_string = heading.to_plain_text nav << "
\n" heading.inner_html("#{heading.innerHTML}") end nav end # makes the footnote markup a little more attractive def tidy_footnotes(content) content.gsub!(/\(footnote\??: ?/, ' (') content end # returns the html file used as a template def get_template File.read @template end # given a file ref and returns some redcloth def parse_textile filename f = File.read filename r = RedCloth.new f r.to_html end # removes nast chars from text to use in anchors def clean_for_url(dirty) cleaner = dirty.gsub(/[ ]/, '_') clean = cleaner.gsub(/[()#'’:;"!@$%^&*+=\[\]?><\/]/, '') clean.downcase end # preflight check def preflight Dir.mkdir(@output_dir) unless File.exists?(@output_dir) unless File.exists?(@source_dir) puts "source_dir not found" exit end unless File.exists?(@template) puts "html template not found" exit end end if $0 == __FILE__ preflight crunch_files(@source_dir) end