Skip to content

Instantly share code, notes, and snippets.

@ebot
Created September 24, 2009 13:17
Show Gist options
  • Save ebot/192720 to your computer and use it in GitHub Desktop.
Save ebot/192720 to your computer and use it in GitHub Desktop.
Converts the Backpack deinstall xml file to individual textile files per backpack page and. generates an html preview page too.
#!/usr/bin/env ruby -wKU
require 'rexml/document'
require 'rubygems'
require 'RedCloth'
include REXML
doc = Document.new( File.new( 'bpack.xml', 'r' ) )
XPath.match( doc, "//pages/page" ).each do |page|
page_title = page.attributes['title']
unless page_title.index('z') == 0
puts "Assembling notes for #{page_title}"
page_file = File.new "notes/#{page_title}.textile", 'w'
page_file << "h1. #{page_title}\n\n"
page_file << "h2. Tags\n\n<hr/>\n\n"
tags = ""
page.elements.each("tags/tag" ) do |tag|
puts " Adding tag #{tag.attributes['name']}"
tags << ', ' unless tags == ''
tags << tag.attributes['name']
end
page_file << "#{tags}\n\n"
page_file << "h2. Lists\n\n<hr/>\n\n"
page.elements.each("lists/list" ) do |list|
puts " Adding list #{list.attributes['name']}"
page_file << "h3. #{list.attributes['name']}\n\n"
list.elements.each("items/item[@completed = 'false']" ) do |item|
page_file << "* #{item.text}\n"
end
page_file << "\n"
end
page_file << "h2. Notes\n\n<hr/>\n\n"
page.elements.each("notes/note" ) do |note|
puts " Adding note #{note.attributes['title']}"
page_file << "h3. #{note.attributes['title']} - #{note.attributes['created_at']}\n\n"
page_file << "#{note.text}\n\n"
end
# Preview the page and save it
puts " Building preview file #{page_title}.html"
page_file.close
page_file = File.new "notes/#{page_title}.textile", 'r'
rc = RedCloth.new page_file.read
page_preview = File.new "notes/#{page_title}.html", 'w'
page_preview << rc.to_html
page_preview.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment